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 SimpleForwardReader can open a persist forward index, and |
19 | * provides search ability. |
20 | */ |
21 | #pragma once |
22 | |
23 | #include <memory> |
24 | #include "common/macro_define.h" |
25 | #include "common/types.h" |
26 | #include "forward_reader.h" |
27 | #include "../collection_dataset.h" |
28 | #include "../typedef.h" |
29 | |
30 | namespace proxima { |
31 | namespace be { |
32 | namespace index { |
33 | |
34 | class SimpleForwardReader; |
35 | using SimpleForwardReaderPtr = std::shared_ptr<SimpleForwardReader>; |
36 | |
37 | /* |
38 | * SimpleForwardReader can open persist forward index, load into memory, |
39 | * and provides search ability. |
40 | */ |
41 | class SimpleForwardReader : public ForwardReader { |
42 | public: |
43 | PROXIMA_DISALLOW_COPY_AND_ASSIGN(SimpleForwardReader); |
44 | |
45 | //! Constructor |
46 | SimpleForwardReader(const std::string &coll_name, |
47 | const std::string &coll_path, SegmentID seg_id) { |
48 | this->set_collection_name(coll_name); |
49 | this->set_collection_path(coll_path); |
50 | this->set_segment_id(seg_id); |
51 | } |
52 | |
53 | //! Destructor |
54 | ~SimpleForwardReader(); |
55 | |
56 | public: |
57 | //! Open persist storage |
58 | int open(const ReadOptions &read_options) override; |
59 | |
60 | //! Close persist storage |
61 | int close() override; |
62 | |
63 | //! Get forward by doc id |
64 | int seek(idx_t doc_id, ForwardData *forward) override; |
65 | |
66 | public: |
67 | //! Return index path |
68 | std::string index_file_path() const override { |
69 | return index_file_path_; |
70 | } |
71 | |
72 | //! Return doc count |
73 | size_t doc_count() const override { |
74 | if (forward_searcher_) { |
75 | return forward_searcher_->count(); |
76 | } else { |
77 | return 0U; |
78 | } |
79 | } |
80 | |
81 | private: |
82 | int open_proxima_container(const ReadOptions &read_options); |
83 | |
84 | int open_forward_searcher(); |
85 | |
86 | private: |
87 | IndexContainerPtr container_{}; |
88 | IndexImmutableClosetPtr forward_searcher_{}; |
89 | |
90 | std::string index_file_path_{}; |
91 | bool opened_{false}; |
92 | }; |
93 | |
94 | |
95 | } // end namespace index |
96 | } // namespace be |
97 | } // end namespace proxima |
98 | |