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 Segment represents piece of collection data. |
19 | * This class describes the data struct and meta info |
20 | */ |
21 | |
22 | #pragma once |
23 | |
24 | #include <memory> |
25 | #include "common/macro_define.h" |
26 | #include "common/types.h" |
27 | #include "meta/meta.h" |
28 | #include "../collection_dataset.h" |
29 | #include "../collection_query.h" |
30 | #include "../column/column_reader.h" |
31 | #include "../column/forward_reader.h" |
32 | #include "../constants.h" |
33 | #include "../typedef.h" |
34 | |
35 | namespace proxima { |
36 | namespace be { |
37 | namespace index { |
38 | |
39 | class Segment; |
40 | using SegmentPtr = std::shared_ptr<Segment>; |
41 | using SegmentPtrList = std::vector<SegmentPtr>; |
42 | |
43 | /* |
44 | * Segment state |
45 | */ |
46 | enum SegmentState : uint32_t { |
47 | CREATED = 0, |
48 | WRITING, |
49 | DUMPING, |
50 | COMPACTING, |
51 | PERSIST |
52 | }; |
53 | |
54 | /* |
55 | * Segment meta info, records basic stats of a segment. |
56 | */ |
57 | struct SegmentMeta { |
58 | uint32_t segment_id{0U}; |
59 | uint32_t state{0U}; |
60 | uint64_t doc_count{0U}; |
61 | uint64_t index_file_count{0U}; |
62 | uint64_t index_file_size{0U}; |
63 | uint64_t min_doc_id{0U}; |
64 | uint64_t max_doc_id{0U}; |
65 | uint64_t min_primary_key{0U}; |
66 | uint64_t max_primary_key{0U}; |
67 | uint64_t min_timestamp{0U}; |
68 | uint64_t max_timestamp{0U}; |
69 | uint64_t min_lsn{0U}; |
70 | uint64_t max_lsn{0U}; |
71 | uint32_t reserved_[8]; |
72 | |
73 | SegmentMeta() { |
74 | min_primary_key = INVALID_KEY; |
75 | min_timestamp = -1UL; |
76 | min_lsn = -1UL; |
77 | } |
78 | }; |
79 | |
80 | static_assert(sizeof(SegmentMeta) % 64 == 0, |
81 | "SegmentMeta must be aligned with 64 bytes" ); |
82 | |
83 | /* |
84 | * SegmentProvider provides interfaces which can get/set property |
85 | */ |
86 | class SegmentProvider { |
87 | public: |
88 | //! Destructor |
89 | virtual ~SegmentProvider() = default; |
90 | |
91 | public: |
92 | //! Return collection name |
93 | const std::string &collection_name() const { |
94 | return collection_name_; |
95 | } |
96 | |
97 | //! Return collection path |
98 | const std::string &collection_path() const { |
99 | return collection_path_; |
100 | } |
101 | |
102 | //! Return segment id |
103 | SegmentID segment_id() const { |
104 | return segment_meta_.segment_id; |
105 | } |
106 | |
107 | //! Return segment state |
108 | SegmentState state() const { |
109 | return (SegmentState)segment_meta_.state; |
110 | } |
111 | |
112 | //! Return segment min doc id |
113 | idx_t min_doc_id() const { |
114 | return segment_meta_.min_doc_id; |
115 | } |
116 | |
117 | //! Return segment meta |
118 | const SegmentMeta &segment_meta() const { |
119 | return segment_meta_; |
120 | } |
121 | |
122 | bool is_in_range(idx_t doc_id) const { |
123 | if (doc_id >= segment_meta_.min_doc_id && |
124 | doc_id <= segment_meta_.max_doc_id) { |
125 | return true; |
126 | } |
127 | |
128 | return false; |
129 | } |
130 | |
131 | //! Return document count |
132 | virtual size_t doc_count() const = 0; |
133 | |
134 | protected: |
135 | void set_collection_name(const std::string &val) { |
136 | collection_name_ = val; |
137 | } |
138 | |
139 | void set_collection_path(const std::string &val) { |
140 | collection_path_ = val; |
141 | } |
142 | |
143 | void set_segment_meta(const SegmentMeta &val) { |
144 | segment_meta_ = val; |
145 | } |
146 | |
147 | protected: |
148 | std::string collection_name_{}; |
149 | std::string collection_path_{}; |
150 | SegmentMeta segment_meta_{}; |
151 | }; |
152 | |
153 | /* |
154 | * Segment can search records in the segment, it also |
155 | * provides much information about the segment. |
156 | */ |
157 | class Segment : public SegmentProvider { |
158 | public: |
159 | //! Destructor |
160 | virtual ~Segment() = default; |
161 | |
162 | public: |
163 | //! Return forward reader |
164 | virtual ForwardReaderPtr get_forward_reader() const = 0; |
165 | |
166 | //! Return column reader |
167 | virtual ColumnReaderPtr get_column_reader( |
168 | const std::string &column_name) const = 0; |
169 | |
170 | public: |
171 | //! Knn search |
172 | virtual int knn_search(const std::string &column_name, |
173 | const std::string &query, |
174 | const QueryParams &query_params, |
175 | QueryResultList *results) = 0; |
176 | |
177 | //! Knn search with batch mode |
178 | virtual int knn_search(const std::string &column_name, |
179 | const std::string &query, |
180 | const QueryParams &query_params, uint32_t batch_count, |
181 | std::vector<QueryResultList> *results) = 0; |
182 | |
183 | //! Kv search some document |
184 | virtual int kv_search(uint64_t primary_key, QueryResult *result) = 0; |
185 | |
186 | public: |
187 | //! Add a column |
188 | virtual int add_column(const meta::ColumnMetaPtr &column_meta) = 0; |
189 | |
190 | //! Remove a column |
191 | virtual int remove_column(const std::string &column_name) = 0; |
192 | }; |
193 | |
194 | |
195 | } // end namespace index |
196 | } // namespace be |
197 | } // end namespace proxima |
198 | |