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 Storage of key->doc_id mappings |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <ailego/parallel/lock.h> |
24 | #include "common/macro_define.h" |
25 | #include "common/types.h" |
26 | #include "persist_hash_map.h" |
27 | #include "snapshot.h" |
28 | |
29 | namespace proxima { |
30 | namespace be { |
31 | namespace index { |
32 | |
33 | class IDMap; |
34 | using IDMapPtr = std::shared_ptr<IDMap>; |
35 | |
36 | /* |
37 | * IDMap is responsible for recording pk->doc_id pair in the collection. |
38 | */ |
39 | class IDMap { |
40 | public: |
41 | PROXIMA_DISALLOW_COPY_AND_ASSIGN(IDMap); |
42 | |
43 | //! Constructor |
44 | IDMap(const std::string &coll_name, const std::string &coll_path) |
45 | : collection_name_(coll_name), collection_path_(coll_path) {} |
46 | |
47 | //! Destructor |
48 | ~IDMap(); |
49 | |
50 | //! Create an instance |
51 | static IDMapPtr Create(const std::string &collection_name, |
52 | const std::string &collection_path); |
53 | |
54 | //! Create an instance and initialize |
55 | static int CreateAndOpen(const std::string &collection_name, |
56 | const std::string &collection_path, |
57 | const ReadOptions &read_options, IDMapPtr *id_map); |
58 | |
59 | public: |
60 | //! Open persist storage and initialize |
61 | int open(const ReadOptions &read_options); |
62 | |
63 | //! Flush memory to persist storage |
64 | int flush(); |
65 | |
66 | //! Close persist storage |
67 | int close(); |
68 | |
69 | public: |
70 | //! Insert <key, doc_id> pair |
71 | int insert(uint64_t key, idx_t doc_id); |
72 | |
73 | //! Remove <key, doc_id> pair |
74 | void remove(uint64_t key); |
75 | |
76 | //! Check doc primary key exist |
77 | bool has(uint64_t key) const; |
78 | |
79 | //! Get doc id by primary key |
80 | idx_t get_mapping_id(uint64_t key) 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 key-id mapping pair count |
94 | size_t count() const { |
95 | return key_map_.size(); |
96 | } |
97 | |
98 | private: |
99 | std::string collection_name_{}; |
100 | std::string collection_path_{}; |
101 | |
102 | SnapshotPtr snapshot_{}; |
103 | PersistHashMap<uint64_t, idx_t> key_map_{}; |
104 | |
105 | bool opened_{false}; |
106 | }; |
107 | |
108 | |
109 | } // end namespace index |
110 | } // namespace be |
111 | } // end namespace proxima |
112 | |