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 Reserving delete records of collection |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <memory> |
24 | #include <ailego/container/bitmap.h> |
25 | #include "common/macro_define.h" |
26 | #include "common/types.h" |
27 | #include "concurrent_bitmap.h" |
28 | #include "delta_store.h" |
29 | #include "snapshot.h" |
30 | #include "typedef.h" |
31 | |
32 | namespace proxima { |
33 | namespace be { |
34 | namespace index { |
35 | |
36 | class DeleteStore; |
37 | using DeleteStorePtr = std::shared_ptr<DeleteStore>; |
38 | |
39 | /* |
40 | * DeleteStore is responsible for storage of delete docs. |
41 | * It will store in memory and disk at the same time, and |
42 | * also provides quick search whether one doc exists ability. |
43 | */ |
44 | class DeleteStore { |
45 | public: |
46 | PROXIMA_DISALLOW_COPY_AND_ASSIGN(DeleteStore); |
47 | |
48 | //! Constructor |
49 | DeleteStore(const std::string &coll_name, const std::string &coll_path) |
50 | : collection_name_(coll_name), collection_path_(coll_path) {} |
51 | |
52 | //! Destructor |
53 | ~DeleteStore(); |
54 | |
55 | //! Create an instance |
56 | static DeleteStorePtr Create(const std::string &collection_name, |
57 | const std::string &collection_path); |
58 | |
59 | //! Create an instance and open |
60 | static int CreateAndOpen(const std::string &collection_name, |
61 | const std::string &collection_path, |
62 | const ReadOptions &options, |
63 | DeleteStorePtr *delete_store); |
64 | |
65 | public: |
66 | //! Open persist storage and initialize |
67 | int open(const ReadOptions &options); |
68 | |
69 | //! Flush memory to persist storage |
70 | int flush(); |
71 | |
72 | //! Close persist storage and cleanup |
73 | int close(); |
74 | |
75 | public: |
76 | //! Insert a doc id |
77 | int insert(idx_t doc_id); |
78 | |
79 | //! Check if exist a doc id |
80 | bool has(idx_t doc_id) const; |
81 | |
82 | public: |
83 | //! Return belonged collection name |
84 | const std::string &collection_name() const { |
85 | return collection_name_; |
86 | } |
87 | |
88 | //! Return persist storage file path |
89 | const std::string &file_path() const { |
90 | return snapshot_->file_path(); |
91 | } |
92 | |
93 | //! Return delete count |
94 | size_t count() const { |
95 | return delta_store_.count(); |
96 | } |
97 | |
98 | private: |
99 | std::string collection_name_{}; |
100 | std::string collection_path_{}; |
101 | |
102 | SnapshotPtr snapshot_{}; |
103 | DeltaStore<idx_t> delta_store_{}; |
104 | ConcurrentBitmap bitmap_{}; |
105 | |
106 | bool opened_{false}; |
107 | }; |
108 | |
109 | |
110 | } // end namespace index |
111 | } // namespace be |
112 | } // end namespace proxima |
113 | |