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 delete store |
19 | */ |
20 | |
21 | #include "delete_store.h" |
22 | #include <limits> |
23 | #include "common/error_code.h" |
24 | #include "file_helper.h" |
25 | #include "typedef.h" |
26 | |
27 | namespace proxima { |
28 | namespace be { |
29 | namespace index { |
30 | |
31 | DeleteStorePtr DeleteStore::Create(const std::string &collection_name, |
32 | const std::string &collection_path) { |
33 | return std::make_shared<DeleteStore>(collection_name, collection_path); |
34 | } |
35 | |
36 | int DeleteStore::CreateAndOpen(const std::string &collection_name, |
37 | const std::string &collection_path, |
38 | const ReadOptions &options, |
39 | DeleteStorePtr *delete_store) { |
40 | (*delete_store) = Create(collection_name, collection_path); |
41 | |
42 | return (*delete_store)->open(options); |
43 | } |
44 | |
45 | DeleteStore::~DeleteStore() { |
46 | if (opened_) { |
47 | this->close(); |
48 | } |
49 | } |
50 | |
51 | int DeleteStore::open(const ReadOptions &read_options) { |
52 | CHECK_STATUS(opened_, false); |
53 | |
54 | int ret = Snapshot::CreateAndOpen(collection_path_, FileID::DELETE_FILE, |
55 | read_options, &snapshot_); |
56 | CHECK_RETURN_WITH_CLOG(ret, 0, "Create and open snapshot failed." ); |
57 | |
58 | ret = delta_store_.mount(snapshot_->data()); |
59 | CHECK_RETURN_WITH_CLOG(ret, 0, "Mount snapshot failed." ); |
60 | |
61 | for (size_t i = 0; i < delta_store_.count(); i++) { |
62 | bitmap_.set(*(delta_store_.at(i))); |
63 | } |
64 | |
65 | // To avoid bitmap resize |
66 | bitmap_.set(std::numeric_limits<uint32_t>::max()); |
67 | |
68 | opened_ = true; |
69 | CLOG_DEBUG("Opened delete store." ); |
70 | return 0; |
71 | } |
72 | |
73 | int DeleteStore::flush() { |
74 | CHECK_STATUS(opened_, true); |
75 | |
76 | return snapshot_->flush(); |
77 | } |
78 | |
79 | int DeleteStore::close() { |
80 | CHECK_STATUS(opened_, true); |
81 | |
82 | delta_store_.unmount(); |
83 | bitmap_.clear(); |
84 | |
85 | int ret = snapshot_->close(); |
86 | if (ret != 0) { |
87 | LOG_WARN("Close snapshot failed." ); |
88 | } |
89 | |
90 | opened_ = false; |
91 | CLOG_DEBUG("Closed delete store." ); |
92 | return ret; |
93 | } |
94 | |
95 | int DeleteStore::insert(idx_t doc_id) { |
96 | CHECK_STATUS(opened_, true); |
97 | bitmap_.set(doc_id); |
98 | |
99 | return delta_store_.append(doc_id); |
100 | } |
101 | |
102 | bool DeleteStore::has(idx_t doc_id) const { |
103 | return bitmap_.test(doc_id); |
104 | } |
105 | |
106 | } // end namespace index |
107 | } // namespace be |
108 | } // end namespace proxima |
109 | |