Debian Codegraph @

lib/dat/trie.hpp

raw symbols SCIP
1 /*
2 Copyright(C) 2011-2016 Brazil
3 Copyright(C) 2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #pragma once
21
22 #include "array.hpp"
23 #include "header.hpp"
24 #include "node.hpp"
25 #include "block.hpp"
26 #include "entry.hpp"
27 #include "key.hpp"
28 #include "file.hpp"
29
30 namespace grn {
31 namespace dat {
32
33 class GRN_DAT_API Trie {
34 public:
35 Trie();
36 ~Trie();
37
38 void create(const char *file_name = NULL,
39 UInt64 file_size = 0,
40 UInt32 max_num_keys = 0,
41 double num_nodes_per_key = 0.0,
42 double average_key_length = 0.0);
43
44 void create(const Trie &trie,
45 const char *file_name = NULL,
46 UInt64 file_size = 0,
47 UInt32 max_num_keys = 0,
48 double num_nodes_per_key = 0.0,
49 double average_key_length = 0.0);
50
51 void repair(const Trie &trie, const char *file_name = NULL);
52
53 void open(const char *file_name);
54 void close();
55
56 void swap(Trie *trie);
57
58 // Users can access a key by its position or ID.
59 const Key &get_key(UInt32 key_pos) const {
60 GRN_DAT_DEBUG_THROW_IF(key_pos >= next_key_pos());
61 return *reinterpret_cast<const Key *>(key_buf_.ptr() + key_pos);
62 }
63 // If a specified ID is invalid, e.g. the key is already deleted, this
64 // function returns a reference to an invalid key object whose id() returns
65 // INVALID_KEY_ID.
66 const Key &ith_key(UInt32 key_id) const {
67 if ((key_id >= min_key_id()) && (key_id <= max_key_id()) &&
68 ith_entry(key_id).is_valid()) {
69 return get_key(ith_entry(key_id).key_pos());
70 }
71 return Key::invalid_key();
72 }
73
74 bool search(const void *ptr, UInt32 length, UInt32 *key_pos = NULL) const {
75 return search_key(static_cast<const UInt8 *>(ptr), length, key_pos);
76 }
77 // Longest prefix match search.
78 bool lcp_search(const void *ptr, UInt32 length,
79 UInt32 *key_pos = NULL) const {
80 return lcp_search_key(static_cast<const UInt8 *>(ptr), length, key_pos);
81 }
82
83 bool remove(UInt32 key_id) {
84 const Key &key = ith_key(key_id);
85 if (key.is_valid()) {
86 return remove(key.ptr(), key.length());
87 }
88 return false;
89 }
90 bool remove(const void *ptr, UInt32 length) {
91 return remove_key(static_cast<const UInt8 *>(ptr), length);
92 }
93
94 bool insert(const void *ptr, UInt32 length, UInt32 *key_pos = NULL) {
95 return insert_key(static_cast<const UInt8 *>(ptr), length, key_pos);
96 }
97
98 bool update(UInt32 key_id, const void *ptr, UInt32 length,
99 UInt32 *key_pos = NULL) {
100 return update_key(ith_key(key_id), static_cast<const UInt8 *>(ptr),
101 length, key_pos);
102 }
103 bool update(const void *src_ptr, UInt32 src_length,
104 const void *dest_ptr, UInt32 dest_length,
105 UInt32 *key_pos = NULL) {
106 UInt32 src_key_pos;
107 if (!search(src_ptr, src_length, &src_key_pos)) {
108 return false;
109 }
110 const Key &src_key = get_key(src_key_pos);
111 return update_key(src_key, static_cast<const UInt8 *>(dest_ptr),
112 dest_length, key_pos);
113 }
114
115 const Node &ith_node(UInt32 i) const {
116 GRN_DAT_DEBUG_THROW_IF(i >= num_nodes());
117 return nodes_[i];
118 }
119 const Block &ith_block(UInt32 i) const {
120 GRN_DAT_DEBUG_THROW_IF(i >= num_blocks());
121 return blocks_[i];
122 }
123 const Entry &ith_entry(UInt32 i) const {
124 GRN_DAT_DEBUG_THROW_IF(i < min_key_id());
125 GRN_DAT_DEBUG_THROW_IF(i > max_key_id());
126 return entries_[i];
127 }
128
129 const Header &header() const {
130 return *header_;
131 }
132
133 UInt64 file_size() const {
134 return header_->file_size();
135 }
136 UInt64 virtual_size() const {
137 return sizeof(Header)
138 + (sizeof(Entry) * num_keys())
139 + (sizeof(Block) * num_blocks())
140 + (sizeof(Node) * num_nodes())
141 + total_key_length();
142 }
143 UInt32 total_key_length() const {
144 return header_->total_key_length();
145 }
146 UInt32 num_keys() const {
147 return header_->num_keys();
148 }
149 UInt32 min_key_id() const {
150 return header_->min_key_id();
151 }
152 UInt32 next_key_id() const {
153 return header_->next_key_id();
154 }
155 UInt32 max_key_id() const {
156 return header_->max_key_id();
157 }
158 UInt32 max_num_keys() const {
159 return header_->max_num_keys();
160 }
161 UInt32 num_nodes() const {
162 return header_->num_nodes();
163 }
164 UInt32 num_phantoms() const {
165 return header_->num_phantoms();
166 }
167 UInt32 num_zombies() const {
168 return header_->num_zombies();
169 }
170 UInt32 max_num_nodes() const {
171 return header_->max_num_nodes();
172 }
173 UInt32 num_blocks() const {
174 return header_->num_blocks();
175 }
176 UInt32 max_num_blocks() const {
177 return header_->max_num_blocks();
178 }
179 UInt32 next_key_pos() const {
180 return header_->next_key_pos();
181 }
182 UInt32 key_buf_size() const {
183 return header_->key_buf_size();
184 }
185 UInt32 status_flags() const {
186 return header_->status_flags();
187 }
188
189 bool is_changing() {
190 return (status_flags() & CHANGING_MASK) == 0;
191 }
192 void clear_status_flags() {
193 header_->set_status_flags(status_flags() & ~CHANGING_MASK);
194 }
195
196 void flush();
197
198 private:
199 File file_;
200 Header *header_;
201 Array<Node> nodes_;
202 Array<Block> blocks_;
203 Array<Entry> entries_;
204 Array<UInt32> key_buf_;
205
206 void create_file(const char *file_name,
207 UInt64 file_size,
208 UInt32 max_num_keys,
209 double num_nodes_per_key,
210 double average_key_length);
211 void create_file(const char *file_name,
212 UInt64 file_size,
213 UInt32 max_num_keys,
214 UInt32 max_num_blocks,
215 UInt32 key_buf_size);
216
217 void open_file(const char *file_name);
218
219 void map_address(void *address);
220
221 void build_from_trie(const Trie &trie);
222 void build_from_trie(const Trie &trie, UInt32 src, UInt32 dest);
223
224 void repair_trie(const Trie &trie);
225 void build_from_keys(const UInt32 *begin, const UInt32 *end,
226 UInt32 depth, UInt32 node_id);
227
228 void mkq_sort(UInt32 *l, UInt32 *r, UInt32 depth);
229 void insertion_sort(UInt32 *l, UInt32 *r, UInt32 depth);
230
231 inline int get_label(UInt32 key_id, UInt32 depth) const;
232 inline int get_median(UInt32 a, UInt32 b, UInt32 c, UInt32 depth) const;
233 inline bool less_than(UInt32 lhs, UInt32 rhs, UInt32 depth) const;
234 inline static void swap_ids(UInt32 *lhs, UInt32 *rhs);
235
236 bool search_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos) const;
237 bool search_linker(const UInt8 *ptr, UInt32 length,
238 UInt32 &node_id, UInt32 &query_pos) const;
239
240 bool lcp_search_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos) const;
241
242 bool remove_key(const UInt8 *ptr, UInt32 length);
243
244 bool insert_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos);
245 bool insert_linker(const UInt8 *ptr, UInt32 length,
246 UInt32 &node_id, UInt32 query_pos);
247
248 bool update_key(const Key &key, const UInt8 *ptr, UInt32 length,
249 UInt32 *key_pos);
250
251 UInt32 insert_node(UInt32 node_id, UInt16 label);
252 UInt32 append_key(const UInt8 *ptr, UInt32 length, UInt32 key_id);
253
254 UInt32 separate(const UInt8 *ptr, UInt32 length,
255 UInt32 node_id, UInt32 i);
256 void resolve(UInt32 node_id, UInt16 label);
257 void migrate_nodes(UInt32 node_id, UInt32 dest_offset,
258 const UInt16 *labels, UInt32 num_labels);
259
260 UInt32 find_offset(const UInt16 *labels, UInt32 num_labels);
261
262 void reserve_node(UInt32 node_id);
263 void reserve_block(UInt32 block_id);
264
265 void update_block_level(UInt32 block_id, UInt32 level);
266 void set_block_level(UInt32 block_id, UInt32 level);
267 void unset_block_level(UInt32 block_id);
268
269 Node &ith_node(UInt32 i) {
270 GRN_DAT_DEBUG_THROW_IF(i >= num_nodes());
271 return nodes_[i];
272 }
273 Block &ith_block(UInt32 i) {
274 GRN_DAT_DEBUG_THROW_IF(i >= num_blocks());
275 return blocks_[i];
276 }
277 Entry &ith_entry(UInt32 i) {
278 GRN_DAT_DEBUG_THROW_IF(i < min_key_id());
279 GRN_DAT_DEBUG_THROW_IF(i > (max_key_id() + 1));
280 return entries_[i];
281 }
282
283 // Disallows copy and assignment.
284 Trie(const Trie &);
285 Trie &operator=(const Trie &);
286 };
287
288 } // namespace dat
289 } // namespace grn