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 Abstract class for module which needs storage snapshot |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <memory> |
24 | #include "common/macro_define.h" |
25 | #include "constants.h" |
26 | #include "file_helper.h" |
27 | #include "typedef.h" |
28 | |
29 | namespace proxima { |
30 | namespace be { |
31 | namespace index { |
32 | |
33 | /* |
34 | * Snapshot read options: |
35 | * use_mmap: whether use mmap storage |
36 | * create_new: whether force create new file |
37 | */ |
38 | struct ReadOptions { |
39 | bool use_mmap{false}; |
40 | bool create_new{false}; |
41 | }; |
42 | |
43 | class Snapshot; |
44 | using SnapshotPtr = std::shared_ptr<Snapshot>; |
45 | |
46 | /* |
47 | * Snapshot represents a kind of storage which |
48 | * will sync data to disk automatically or routinely. |
49 | */ |
50 | class Snapshot { |
51 | public: |
52 | PROXIMA_DISALLOW_COPY_AND_ASSIGN(Snapshot); |
53 | |
54 | //! Constructor |
55 | Snapshot(const std::string &dir_path_val, FileID file_id_val, |
56 | uint32_t suffix_id_val, const std::string &suffix_name_val) |
57 | : dir_path_(dir_path_val), |
58 | file_id_(file_id_val), |
59 | suffix_id_(suffix_id_val), |
60 | suffix_name_(suffix_name_val) {} |
61 | |
62 | //! Destructor |
63 | ~Snapshot(); |
64 | |
65 | //! Constructor |
66 | static SnapshotPtr Create(const std::string &dir_path, FileID file_id, |
67 | uint32_t suffix_id, const std::string &suffix_name); |
68 | |
69 | //! Create and open an instance |
70 | static int CreateAndOpen(const std::string &dir_path, FileID file_id, |
71 | const ReadOptions &options, SnapshotPtr *snapshot); |
72 | |
73 | //! Create and open an instance |
74 | static int CreateAndOpen(const std::string &dir_path, FileID file_id, |
75 | uint32_t suffix_id, const ReadOptions &options, |
76 | SnapshotPtr *snapshot); |
77 | |
78 | //! Create and open an instance |
79 | static int CreateAndOpen(const std::string &dir_path, FileID file_id, |
80 | uint32_t suffix_id, const std::string &suffix_name, |
81 | const ReadOptions &options, SnapshotPtr *snapshot); |
82 | |
83 | public: |
84 | //! Open persist storage |
85 | int open(const ReadOptions &read_options); |
86 | |
87 | //! Flush memory data to persist storage |
88 | int flush(); |
89 | |
90 | //! Close persist storage |
91 | int close(); |
92 | |
93 | public: |
94 | //! Return if opened |
95 | bool is_open() const { |
96 | return opened_; |
97 | } |
98 | |
99 | //! Return directory path |
100 | const std::string &dir_path() const { |
101 | return dir_path_; |
102 | } |
103 | |
104 | //! Return file id |
105 | FileID file_id() const { |
106 | return file_id_; |
107 | } |
108 | |
109 | //! Return suffix id |
110 | uint32_t suffix_id() const { |
111 | return suffix_id_; |
112 | } |
113 | |
114 | //! Return suffix name |
115 | const std::string &suffix_name() const { |
116 | return suffix_name_; |
117 | } |
118 | |
119 | //! Return file path |
120 | const std::string &file_path() const { |
121 | return file_path_; |
122 | } |
123 | |
124 | //! Return storage data ptr |
125 | const IndexStoragePtr &data() const { |
126 | return storage_; |
127 | } |
128 | |
129 | private: |
130 | std::string dir_path_{}; |
131 | FileID file_id_{FileID::UNDEFINED}; |
132 | uint32_t suffix_id_{0U}; |
133 | std::string suffix_name_{}; |
134 | |
135 | std::string file_path_{}; |
136 | IndexStoragePtr storage_{}; |
137 | bool opened_{false}; |
138 | }; |
139 | |
140 | |
141 | } // end namespace index |
142 | } // namespace be |
143 | } // end namespace proxima |
144 | |