1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Haichao.chc
17 * \date Oct 2020
18 * \brief Implementation of Snapshot
19 */
20
21#include "snapshot.h"
22#include <aitheta2/index_factory.h>
23
24namespace proxima {
25namespace be {
26namespace index {
27
28Snapshot::~Snapshot() {
29 if (opened_) {
30 this->close();
31 }
32}
33
34SnapshotPtr Snapshot::Create(const std::string &dir_path, FileID file_id,
35 uint32_t suffix_id,
36 const std::string &suffix_name) {
37 return std::make_shared<Snapshot>(dir_path, file_id, suffix_id, suffix_name);
38}
39
40int Snapshot::CreateAndOpen(const std::string &dir_path, FileID file_id,
41 const ReadOptions &options, SnapshotPtr *snapshot) {
42 *snapshot = Create(dir_path, file_id, INVALID_SEGMENT_ID, "");
43
44 return (*snapshot)->open(options);
45}
46
47int Snapshot::CreateAndOpen(const std::string &dir_path, FileID file_id,
48 uint32_t suffix_id, const ReadOptions &options,
49 SnapshotPtr *snapshot) {
50 *snapshot = Create(dir_path, file_id, suffix_id, "");
51
52 return (*snapshot)->open(options);
53}
54
55int Snapshot::CreateAndOpen(const std::string &dir_path, FileID file_id,
56 uint32_t suffix_id, const std::string &suffix_name,
57 const ReadOptions &options, SnapshotPtr *snapshot) {
58 *snapshot = Create(dir_path, file_id, suffix_id, suffix_name);
59
60 return (*snapshot)->open(options);
61}
62
63int Snapshot::open(const ReadOptions &read_options) {
64 CHECK_STATUS(opened_, false);
65
66 if (read_options.use_mmap) {
67 storage_ = IndexFactory::CreateStorage("MMapFileStorage");
68 } else {
69 storage_ = IndexFactory::CreateStorage("MemoryStorage");
70 }
71
72 if (!storage_) {
73 LOG_ERROR("Create storage failed.");
74 return ErrorCode_RuntimeError;
75 }
76
77 if (suffix_id_ != INVALID_SEGMENT_ID) {
78 if (suffix_name_.empty()) {
79 file_path_ = FileHelper::MakeFilePath(dir_path_, file_id_, suffix_id_);
80 } else {
81 file_path_ = FileHelper::MakeFilePath(dir_path_, file_id_, suffix_id_,
82 suffix_name_);
83 }
84 } else {
85 file_path_ = FileHelper::MakeFilePath(dir_path_, file_id_);
86 }
87
88 // Default open warmup flag
89 IndexParams stg_params;
90 stg_params.set("proxima.mmap_file.storage.memory_warmup", true);
91
92 storage_->init(stg_params);
93
94 int ret = storage_->open(file_path_, read_options.create_new);
95 CHECK_RETURN_WITH_LOG(ret, 0, "Open storage failed. file_path[%s] ret[%d]",
96 file_path_.c_str(), ret);
97
98 opened_ = true;
99 return 0;
100}
101
102int Snapshot::flush() {
103 CHECK_STATUS(opened_, true);
104
105 return storage_->flush();
106}
107
108int Snapshot::close() {
109 CHECK_STATUS(opened_, true);
110
111 int ret = storage_->close();
112 CHECK_RETURN(ret, 0);
113
114 opened_ = false;
115 return 0;
116}
117
118
119} // end namespace index
120} // namespace be
121} // end namespace proxima
122