Debian Codegraph @

lib/dat/trie.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2011-2015 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 #include "trie.hpp"
21
22 #include <algorithm>
23 #include <cstring>
24
25 #include "vector.hpp"
26
27 namespace grn {
28 namespace dat {
29 namespace {
30
31 class StatusFlagManager {
32 public:
33 StatusFlagManager(Header *header, UInt32 status_flag)
34 : header_(header), status_flag_(status_flag) {
35 header_->set_status_flags(header_->status_flags() | status_flag_);
36 }
37 ~StatusFlagManager() {
38 header_->set_status_flags(header_->status_flags() & ~status_flag_);
39 }
40
41 private:
42 Header *header_;
43 UInt32 status_flag_;
44
45 // Disallows copy and assignment.
46 StatusFlagManager(const StatusFlagManager &);
47 StatusFlagManager &operator=(const StatusFlagManager &);
48 };
49
50 } // namespace
51
52 Trie::Trie()
53 : file_(),
54 header_(NULL),
55 nodes_(),
56 blocks_(),
57 entries_(),
58 key_buf_() {}
59
60 Trie::~Trie() {}
61
62 void Trie::create(const char *file_name,
63 UInt64 file_size,
64 UInt32 max_num_keys,
65 double num_nodes_per_key,
66 double average_key_length) {
67 GRN_DAT_THROW_IF(PARAM_ERROR, (file_size != 0) && (max_num_keys != 0));
68
69 if (num_nodes_per_key < 1.0) {
70 num_nodes_per_key = DEFAULT_NUM_NODES_PER_KEY;
71 }
72 if (num_nodes_per_key > MAX_NUM_NODES_PER_KEY) {
73 num_nodes_per_key = MAX_NUM_NODES_PER_KEY;
74 }
75 GRN_DAT_THROW_IF(PARAM_ERROR, num_nodes_per_key < 1.0);
76 GRN_DAT_THROW_IF(PARAM_ERROR, num_nodes_per_key > MAX_NUM_NODES_PER_KEY);
77
78 if (average_key_length < 1.0) {
79 average_key_length = DEFAULT_AVERAGE_KEY_LENGTH;
80 }
81 GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length < 1.0);
82 GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length > MAX_KEY_LENGTH);
83
84 if (max_num_keys == 0) {
85 if (file_size == 0) {
86 file_size = DEFAULT_FILE_SIZE;
87 } else {
88 GRN_DAT_THROW_IF(PARAM_ERROR, file_size < MIN_FILE_SIZE);
89 GRN_DAT_THROW_IF(PARAM_ERROR, file_size > MAX_FILE_SIZE);
90 }
91 } else {
92 GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys > MAX_NUM_KEYS);
93 }
94
95 Trie new_trie;
96 new_trie.create_file(file_name, file_size, max_num_keys,
97 num_nodes_per_key, average_key_length);
98 new_trie.swap(this);
99 }
100
101 void Trie::create(const Trie &trie,
102 const char *file_name,
103 UInt64 file_size,
104 UInt32 max_num_keys,
105 double num_nodes_per_key,
106 double average_key_length) {
107 GRN_DAT_THROW_IF(PARAM_ERROR, (file_size != 0) && (max_num_keys != 0));
108
109 if (num_nodes_per_key < 1.0) {
110 if (trie.num_keys() == 0) {
111 num_nodes_per_key = DEFAULT_NUM_NODES_PER_KEY;
112 } else {
113 num_nodes_per_key = 1.0 * trie.num_nodes() / trie.num_keys();
114 if (num_nodes_per_key > MAX_NUM_NODES_PER_KEY) {
115 num_nodes_per_key = MAX_NUM_NODES_PER_KEY;
116 }
117 }
118 }
119 GRN_DAT_THROW_IF(PARAM_ERROR, num_nodes_per_key < 1.0);
120 GRN_DAT_THROW_IF(PARAM_ERROR, num_nodes_per_key > MAX_NUM_NODES_PER_KEY);
121
122 if (average_key_length < 1.0) {
123 if (trie.num_keys() == 0) {
124 average_key_length = DEFAULT_AVERAGE_KEY_LENGTH;
125 } else {
126 average_key_length = 1.0 * trie.total_key_length() / trie.num_keys();
127 }
128 }
129 GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length < 1.0);
130 GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length > MAX_KEY_LENGTH);
131
132 if (max_num_keys == 0) {
133 if (file_size == 0) {
134 file_size = trie.file_size();
135 }
136 GRN_DAT_THROW_IF(PARAM_ERROR, file_size < MIN_FILE_SIZE);
137 GRN_DAT_THROW_IF(PARAM_ERROR, file_size > MAX_FILE_SIZE);
138 GRN_DAT_THROW_IF(PARAM_ERROR, file_size < trie.virtual_size());
139 } else {
140 GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys < trie.num_keys());
141 GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys < trie.max_key_id());
142 GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys > MAX_NUM_KEYS);
143 }
144
145 Trie new_trie;
146 new_trie.create_file(file_name, file_size, max_num_keys,
147 num_nodes_per_key, average_key_length);
148 new_trie.build_from_trie(trie);
149 new_trie.swap(this);
150 }
151
152 void Trie::repair(const Trie &trie, const char *file_name) {
153 Trie new_trie;
154 new_trie.create_file(file_name, trie.file_size(), trie.max_num_keys(),
155 trie.max_num_blocks(), trie.key_buf_size());
156 new_trie.repair_trie(trie);
157 new_trie.swap(this);
158 }
159
160 void Trie::open(const char *file_name) {
161 GRN_DAT_THROW_IF(PARAM_ERROR, file_name == NULL);
162
163 Trie new_trie;
164 new_trie.open_file(file_name);
165 new_trie.swap(this);
166 }
167
168 void Trie::close() {
169 Trie().swap(this);
170 }
171
172 void Trie::swap(Trie *trie) {
173 file_.swap(&trie->file_);
174 std::swap(header_, trie->header_);
175 nodes_.swap(&trie->nodes_);
176 blocks_.swap(&trie->blocks_);
177 entries_.swap(&trie->entries_);
178 key_buf_.swap(&trie->key_buf_);
179 }
180
181 void Trie::flush() {
182 file_.flush();
183 }
184
185 void Trie::create_file(const char *file_name,
186 UInt64 file_size,
187 UInt32 max_num_keys,
188 double num_nodes_per_key,
189 double average_key_length) {
190 GRN_DAT_THROW_IF(PARAM_ERROR, (file_size == 0) && (max_num_keys == 0));
191 GRN_DAT_THROW_IF(PARAM_ERROR, (file_size != 0) && (max_num_keys != 0));
192 if (max_num_keys == 0) {
193 const UInt64 avail = file_size - sizeof(Header);
194 const double num_bytes_per_key = (sizeof(Node) * num_nodes_per_key)
195 + (1.0 * sizeof(Block) / BLOCK_SIZE * num_nodes_per_key)
196 + sizeof(Entry)
197 + sizeof(UInt32) + sizeof(UInt8) + average_key_length + 1.5;
198 const UInt32 available_num_keys =
199 static_cast<UInt32>(static_cast<double>(avail) / num_bytes_per_key);
200 if (available_num_keys > MAX_NUM_KEYS) {
201 max_num_keys = MAX_NUM_KEYS;
202 } else {
203 max_num_keys = available_num_keys;
204 }
205 GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys == 0);
206 }
207
208 UInt32 max_num_blocks;
209 {
210 const double max_num_nodes = num_nodes_per_key * max_num_keys;
211 GRN_DAT_THROW_IF(PARAM_ERROR, (max_num_nodes - 1.0) >= MAX_NUM_NODES);
212 max_num_blocks = ((UInt32)max_num_nodes + BLOCK_SIZE - 1) / BLOCK_SIZE;
213 GRN_DAT_THROW_IF(PARAM_ERROR, max_num_blocks == 0);
214 GRN_DAT_THROW_IF(PARAM_ERROR, max_num_blocks > MAX_NUM_BLOCKS);
215 }
216
217 UInt32 key_buf_size;
218 if (file_size == 0) {
219 const double total_key_length = average_key_length * max_num_keys;
220 GRN_DAT_THROW_IF(PARAM_ERROR,
221 (total_key_length - 1.0) >= MAX_TOTAL_KEY_LENGTH);
222
223 // The last term is the estimated number of bytes that will be used for
224 // 32-bit alignment.
225 const UInt64 total_num_bytes = (UInt64)(total_key_length)
226 + (UInt64)(sizeof(UInt32) + sizeof(UInt8)) * max_num_keys
227 + (UInt32)(max_num_keys * 1.5);
228 GRN_DAT_THROW_IF(PARAM_ERROR,
229 (total_num_bytes / sizeof(UInt32)) >= MAX_KEY_BUF_SIZE);
230 key_buf_size = (UInt32)(total_num_bytes / sizeof(UInt32));
231
232 file_size = sizeof(Header)
233 + (sizeof(Block) * max_num_blocks)
234 + (sizeof(Node) * BLOCK_SIZE * max_num_blocks)
235 + (sizeof(Entry) * max_num_keys)
236 + (sizeof(UInt32) * key_buf_size);
237 } else {
238 const UInt64 avail = file_size - sizeof(Header)
239 - (sizeof(Block) * max_num_blocks)
240 - (sizeof(Node) * BLOCK_SIZE * max_num_blocks)
241 - (sizeof(Entry) * max_num_keys);
242 GRN_DAT_THROW_IF(PARAM_ERROR, (avail / sizeof(UInt32)) > MAX_KEY_BUF_SIZE);
243 key_buf_size = (UInt32)(avail / sizeof(UInt32));
244 }
245
246 create_file(file_name, file_size, max_num_keys,
247 max_num_blocks, key_buf_size);
248 }
249
250 void Trie::create_file(const char *file_name,
251 UInt64 file_size,
252 UInt32 max_num_keys,
253 UInt32 max_num_blocks,
254 UInt32 key_buf_size) {
255 GRN_DAT_THROW_IF(PARAM_ERROR, file_size < (sizeof(Header)
256 + (sizeof(Block) * max_num_blocks)
257 + (sizeof(Node) * BLOCK_SIZE * max_num_blocks)
258 + (sizeof(Entry) * max_num_keys)
259 + (sizeof(UInt32) * key_buf_size)));
260
261 file_.create(file_name, file_size);
262
263 Header * const header = static_cast<Header *>(file_.ptr());
264 *header = Header();
265 header->set_file_size(file_size);
266 header->set_max_num_keys(max_num_keys);
267 header->set_max_num_blocks(max_num_blocks);
268 header->set_key_buf_size(key_buf_size);
269
270 map_address(file_.ptr());
271
272 reserve_node(ROOT_NODE_ID);
273 ith_node(INVALID_OFFSET).set_is_offset(true);
274 }
275
276 void Trie::open_file(const char *file_name) {
277 GRN_DAT_THROW_IF(PARAM_ERROR, file_name == NULL);
278
279 file_.open(file_name);
280 map_address(file_.ptr());
281 GRN_DAT_THROW_IF(FORMAT_ERROR, file_size() != file_.size());
282 }
283
284 void Trie::map_address(void *address) {
285 GRN_DAT_THROW_IF(PARAM_ERROR, address == NULL);
286
287 header_ = static_cast<Header *>(address);
288 nodes_.assign(header_ + 1, max_num_nodes());
289 blocks_.assign(nodes_.end(), max_num_blocks());
290 entries_.assign(reinterpret_cast<Entry *>(blocks_.end()) - 1,
291 max_num_keys() + 1);
292 key_buf_.assign(entries_.end(), key_buf_size());
293
294 GRN_DAT_THROW_IF(UNEXPECTED_ERROR, static_cast<void *>(key_buf_.end())
295 > static_cast<void *>(static_cast<char *>(address) + file_size()));
296 }
297
298 void Trie::build_from_trie(const Trie &trie) {
299 GRN_DAT_THROW_IF(SIZE_ERROR, max_num_keys() < trie.num_keys());
300 GRN_DAT_THROW_IF(SIZE_ERROR, max_num_keys() < trie.max_key_id());
301
302 header_->set_total_key_length(trie.total_key_length());
303 header_->set_num_keys(trie.num_keys());
304 header_->set_max_key_id(trie.max_key_id());
305 header_->set_next_key_id(trie.next_key_id());
306 for (UInt32 i = min_key_id(); i <= max_key_id(); ++i) {
307 ith_entry(i) = trie.ith_entry(i);
308 }
309 build_from_trie(trie, ROOT_NODE_ID, ROOT_NODE_ID);
310 }
311
312 void Trie::build_from_trie(const Trie &trie, UInt32 src, UInt32 dest) {
313 // Keys are sorted in lexicographic order.
314 if (trie.ith_node(src).is_linker()) {
315 const Key &key = trie.get_key(trie.ith_node(src).key_pos());
316 Key::create(key_buf_.ptr() + next_key_pos(),
317 key.id(), key.ptr(), key.length());
318 ith_node(dest).set_key_pos(next_key_pos());
319 ith_entry(key.id()).set_key_pos(next_key_pos());
320 header_->set_next_key_pos(
321 next_key_pos() + Key::estimate_size(key.length()));
322 return;
323 }
324
325 const UInt32 src_offset = trie.ith_node(src).offset();
326 UInt32 dest_offset;
327 {
328 UInt16 labels[MAX_LABEL + 1];
329 UInt32 num_labels = 0;
330
331 UInt32 label = trie.ith_node(src).child();
332 while (label != INVALID_LABEL) {
333 GRN_DAT_DEBUG_THROW_IF(label > MAX_LABEL);
334 const UInt32 child = src_offset ^ label;
335 if (trie.ith_node(child).is_linker() ||
336 (trie.ith_node(child).child() != INVALID_LABEL)) {
337 labels[num_labels++] = static_cast<UInt16>(label);
338 }
339 label = trie.ith_node(child).sibling();
340 }
341 if (num_labels == 0) {
342 return;
343 }
344
345 dest_offset = find_offset(labels, num_labels);
346 for (UInt32 i = 0; i < num_labels; ++i) {
347 const UInt32 child = dest_offset ^ labels[i];
348 reserve_node(child);
349 ith_node(child).set_label(labels[i]);
350 if ((i + 1) < num_labels) {
351 ith_node(child).set_sibling(labels[i + 1]);
352 }
353 }
354
355 GRN_DAT_DEBUG_THROW_IF(ith_node(dest_offset).is_offset());
356 ith_node(dest_offset).set_is_offset(true);
357 ith_node(dest).set_offset(dest_offset);
358 ith_node(dest).set_child(labels[0]);
359 }
360
361 UInt32 label = ith_node(dest).child();
362 while (label != INVALID_LABEL) {
363 build_from_trie(trie, src_offset ^ label, dest_offset ^ label);
364 label = ith_node(dest_offset ^ label).sibling();
365 }
366 }
367
368 void Trie::repair_trie(const Trie &trie) {
369 Vector<UInt32> valid_ids;
370 header_->set_max_key_id(trie.max_key_id());
371 header_->set_next_key_id(trie.max_key_id() + 1);
372 UInt32 prev_invalid_key_id = INVALID_KEY_ID;
373 for (UInt32 i = min_key_id(); i <= max_key_id(); ++i) {
374 const Entry &entry = trie.ith_entry(i);
375 if (entry.is_valid()) {
376 valid_ids.push_back(i);
377 ith_entry(i) = entry;
378 const Key &key = trie.get_key(entry.key_pos());
379 Key::create(key_buf_.ptr() + next_key_pos(),
380 key.id(), key.ptr(), key.length());
381 ith_entry(i).set_key_pos(next_key_pos());
382 header_->set_next_key_pos(
383 next_key_pos() + Key::estimate_size(key.length()));
384 header_->set_total_key_length(
385 total_key_length() + key.length());
386 header_->set_num_keys(num_keys() + 1);
387 } else {
388 if (prev_invalid_key_id == INVALID_KEY_ID) {
389 header_->set_next_key_id(i);
390 } else {
391 ith_entry(prev_invalid_key_id).set_next(i);
392 }
393 prev_invalid_key_id = i;
394 }
395 }
396 if (prev_invalid_key_id != INVALID_KEY_ID) {
397 ith_entry(prev_invalid_key_id).set_next(max_key_id() + 1);
398 }
399 mkq_sort(valid_ids.begin(), valid_ids.end(), 0);
400 build_from_keys(valid_ids.begin(), valid_ids.end(), 0, ROOT_NODE_ID);
401 }
402
403 void Trie::build_from_keys(const UInt32 *begin, const UInt32 *end,
404 UInt32 depth, UInt32 node_id) {
405 if ((end - begin) == 1) {
406 ith_node(node_id).set_key_pos(ith_entry(*begin).key_pos());
407 return;
408 }
409
410 UInt32 offset;
411 {
412 UInt16 labels[MAX_LABEL + 2];
413 UInt32 num_labels = 0;
414
415 const UInt32 *it = begin;
416 if (ith_key(*it).length() == depth) {
417 labels[num_labels++] = TERMINAL_LABEL;
418 ++it;
419 }
420
421 labels[num_labels++] = (UInt8)ith_key(*it)[depth];
422 for (++it; it < end; ++it) {
423 const Key &key = ith_key(*it);
424 if ((UInt8)key[depth] != labels[num_labels - 1]) {
425 labels[num_labels++] = (UInt8)key[depth];
426 }
427 }
428 labels[num_labels] = INVALID_LABEL;
429
430 offset = find_offset(labels, num_labels);
431 ith_node(node_id).set_child(labels[0]);
432 for (UInt32 i = 0; i < num_labels; ++i) {
433 const UInt32 next = offset ^ labels[i];
434 reserve_node(next);
435 ith_node(next).set_label(labels[i]);
436 ith_node(next).set_sibling(labels[i + 1]);
437 }
438
439 if (offset >= num_nodes()) {
440 reserve_block(num_blocks());
441 }
442 ith_node(offset).set_is_offset(true);
443 ith_node(node_id).set_offset(offset);
444 }
445
446 if (ith_key(*begin).length() == depth) {
447 build_from_keys(begin, begin + 1, depth + 1, offset ^ TERMINAL_LABEL);
448 ++begin;
449 }
450
451 UInt16 label = ith_key(*begin)[depth];
452 for (const UInt32 *it = begin + 1; it < end; ++it) {
453 const Key &key = ith_key(*it);
454 if ((UInt8)key[depth] != label) {
455 build_from_keys(begin, it, depth + 1, offset ^ label);
456 label = (UInt8)key[depth];
457 begin = it;
458 }
459 }
460 build_from_keys(begin, end, depth + 1, offset ^ label);
461 }
462
463 void Trie::mkq_sort(UInt32 *l, UInt32 *r, UInt32 depth) {
464 while ((r - l) >= MKQ_SORT_THRESHOLD) {
465 UInt32 *pl = l;
466 UInt32 *pr = r;
467 UInt32 *pivot_l = l;
468 UInt32 *pivot_r = r;
469
470 const int pivot = get_median(*l, *(l + (r - l) / 2), *(r - 1), depth);
471 for ( ; ; ) {
472 while (pl < pr) {
473 const int label = get_label(*pl, depth);
474 if (label > pivot) {
475 break;
476 } else if (label == pivot) {
477 swap_ids(pl, pivot_l);
478 ++pivot_l;
479 }
480 ++pl;
481 }
482 while (pl < pr) {
483 const int label = get_label(*--pr, depth);
484 if (label < pivot) {
485 break;
486 } else if (label == pivot) {
487 swap_ids(pr, --pivot_r);
488 }
489 }
490 if (pl >= pr) {
491 break;
492 }
493 swap_ids(pl, pr);
494 ++pl;
495 }
496 while (pivot_l > l) {
497 swap_ids(--pivot_l, --pl);
498 }
499 while (pivot_r < r) {
500 swap_ids(pivot_r, pr);
501 ++pivot_r;
502 ++pr;
503 }
504
505 if (((pl - l) > (pr - pl)) || ((r - pr) > (pr - pl))) {
506 if ((pr - pl) > 1) {
507 mkq_sort(pl, pr, depth + 1);
508 }
509
510 if ((pl - l) < (r - pr)) {
511 if ((pl - l) > 1) {
512 mkq_sort(l, pl, depth);
513 }
514 l = pr;
515 } else {
516 if ((r - pr) > 1) {
517 mkq_sort(pr, r, depth);
518 }
519 r = pl;
520 }
521 } else {
522 if ((pl - l) > 1) {
523 mkq_sort(l, pl, depth);
524 }
525
526 if ((r - pr) > 1) {
527 mkq_sort(pr, r, depth);
528 }
529
530 l = pl, r = pr;
531 if ((pr - pl) > 1) {
532 ++depth;
533 }
534 }
535 }
536
537 if ((r - l) > 1) {
538 insertion_sort(l, r, depth);
539 }
540 }
541
542 void Trie::insertion_sort(UInt32 *l, UInt32 *r, UInt32 depth) {
543 for (UInt32 *i = l + 1; i < r; ++i) {
544 for (UInt32 *j = i; j > l; --j) {
545 if (less_than(*(j - 1), *j, depth)) {
546 break;
547 }
548 swap_ids(j - 1, j);
549 }
550 }
551 }
552
553 int Trie::get_median(UInt32 a, UInt32 b, UInt32 c, UInt32 depth) const {
554 const int x = get_label(a, depth);
555 const int y = get_label(b, depth);
556 const int z = get_label(c, depth);
557 if (x < y) {
558 if (y < z) {
559 return y;
560 } else if (x < z) {
561 return z;
562 }
563 return x;
564 } else if (x < z) {
565 return x;
566 } else if (y < z) {
567 return z;
568 }
569 return y;
570 }
571
572 int Trie::get_label(UInt32 key_id, UInt32 depth) const {
573 const Key &key = ith_key(key_id);
574 if (depth == key.length()) {
575 return -1;
576 } else {
577 return (UInt8)key[depth];
578 }
579 }
580
581 bool Trie::less_than(UInt32 lhs, UInt32 rhs, UInt32 depth) const {
582 const Key &lhs_key = ith_key(lhs);
583 const Key &rhs_key = ith_key(rhs);
584 const UInt32 length = (lhs_key.length() < rhs_key.length()) ?
585 lhs_key.length() : rhs_key.length();
586 for (UInt32 i = depth; i < length; ++i) {
587 if (lhs_key[i] != rhs_key[i]) {
588 return (UInt8)lhs_key[i] < (UInt8)rhs_key[i];
589 }
590 }
591 return lhs_key.length() < rhs_key.length();
592 }
593
594 void Trie::swap_ids(UInt32 *lhs, UInt32 *rhs) {
595 UInt32 temp = *lhs;
596 *lhs = *rhs;
597 *rhs = temp;
598 }
599
600 bool Trie::search_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos) const {
601 GRN_DAT_DEBUG_THROW_IF((ptr == NULL) && (length != 0));
602
603 UInt32 node_id = ROOT_NODE_ID;
604 UInt32 query_pos = 0;
605 if (!search_linker(ptr, length, node_id, query_pos)) {
606 return false;
607 }
608
609 const Base base = ith_node(node_id).base();
610 if (!base.is_linker()) {
611 return false;
612 }
613
614 if (get_key(base.key_pos()).equals_to(ptr, length, query_pos)) {
615 if (key_pos != NULL) {
616 *key_pos = base.key_pos();
617 }
618 return true;
619 }
620 return false;
621 }
622
623 bool Trie::search_linker(const UInt8 *ptr, UInt32 length,
624 UInt32 &node_id, UInt32 &query_pos) const {
625 GRN_DAT_DEBUG_THROW_IF((ptr == NULL) && (length != 0));
626
627 for ( ; query_pos < length; ++query_pos) {
628 const Base base = ith_node(node_id).base();
629 if (base.is_linker()) {
630 return true;
631 }
632
633 const UInt32 next = base.offset() ^ ptr[query_pos];
634 if (ith_node(next).label() != ptr[query_pos]) {
635 return false;
636 }
637 node_id = next;
638 }
639
640 const Base base = ith_node(node_id).base();
641 if (base.is_linker()) {
642 return true;
643 }
644
645 const UInt32 next = base.offset() ^ TERMINAL_LABEL;
646 if (ith_node(next).label() != TERMINAL_LABEL) {
647 return false;
648 }
649 node_id = next;
650 return ith_node(next).is_linker();
651 }
652
653 bool Trie::lcp_search_key(const UInt8 *ptr, UInt32 length,
654 UInt32 *key_pos) const {
655 GRN_DAT_DEBUG_THROW_IF((ptr == NULL) && (length != 0));
656
657 bool found = false;
658 UInt32 node_id = ROOT_NODE_ID;
659 UInt32 query_pos = 0;
660
661 for ( ; query_pos < length; ++query_pos) {
662 const Base base = ith_node(node_id).base();
663 if (base.is_linker()) {
664 const Key &key = get_key(base.key_pos());
665 if ((key.length() <= length) &&
666 key.equals_to(ptr, key.length(), query_pos)) {
667 if (key_pos != NULL) {
668 *key_pos = base.key_pos();
669 }
670 found = true;
671 }
672 return found;
673 }
674
675 if (ith_node(node_id).child() == TERMINAL_LABEL) {
676 const Base linker_base =
677 ith_node(base.offset() ^ TERMINAL_LABEL).base();
678 if (linker_base.is_linker()) {
679 if (key_pos != NULL) {
680 *key_pos = linker_base.key_pos();
681 }
682 found = true;
683 }
684 }
685
686 node_id = base.offset() ^ ptr[query_pos];
687 if (ith_node(node_id).label() != ptr[query_pos]) {
688 return found;
689 }
690 }
691
692 const Base base = ith_node(node_id).base();
693 if (base.is_linker()) {
694 const Key &key = get_key(base.key_pos());
695 if (key.length() <= length) {
696 if (key_pos != NULL) {
697 *key_pos = base.key_pos();
698 }
699 found = true;
700 }
701 } else if (ith_node(node_id).child() == TERMINAL_LABEL) {
702 const Base linker_base = ith_node(base.offset() ^ TERMINAL_LABEL).base();
703 if (linker_base.is_linker()) {
704 if (key_pos != NULL) {
705 *key_pos = linker_base.key_pos();
706 }
707 found = true;
708 }
709 }
710 return found;
711 }
712
713 bool Trie::remove_key(const UInt8 *ptr, UInt32 length) {
714 GRN_DAT_THROW_IF(STATUS_ERROR, (status_flags() & CHANGING_MASK) != 0);
715 StatusFlagManager status_flag_manager(header_, REMOVING_FLAG);
716
717 GRN_DAT_DEBUG_THROW_IF((ptr == NULL) && (length != 0));
718
719 UInt32 node_id = ROOT_NODE_ID;
720 UInt32 query_pos = 0;
721 if (!search_linker(ptr, length, node_id, query_pos)) {
722 return false;
723 }
724
725 const UInt32 key_pos = ith_node(node_id).key_pos();
726 if (!get_key(key_pos).equals_to(ptr, length, query_pos)) {
727 return false;
728 }
729
730 const UInt32 key_id = get_key(key_pos).id();
731 ith_node(node_id).set_offset(INVALID_OFFSET);
732 ith_entry(key_id).set_next(next_key_id());
733
734 header_->set_next_key_id(key_id);
735 header_->set_total_key_length(total_key_length() - length);
736 header_->set_num_keys(num_keys() - 1);
737 return true;
738 }
739
740 bool Trie::insert_key(const UInt8 *ptr, UInt32 length, UInt32 *key_pos) {
741 GRN_DAT_THROW_IF(STATUS_ERROR, (status_flags() & CHANGING_MASK) != 0);
742 StatusFlagManager status_flag_manager(header_, INSERTING_FLAG);
743
744 GRN_DAT_DEBUG_THROW_IF((ptr == NULL) && (length != 0));
745
746 UInt32 node_id = ROOT_NODE_ID;
747 UInt32 query_pos = 0;
748
749 search_linker(ptr, length, node_id, query_pos);
750 if (!insert_linker(ptr, length, node_id, query_pos)) {
751 if (key_pos != NULL) {
752 *key_pos = ith_node(node_id).key_pos();
753 }
754 return false;
755 }
756
757 const UInt32 new_key_id = next_key_id();
758 const UInt32 new_key_pos = append_key(ptr, length, new_key_id);
759
760 header_->set_total_key_length(total_key_length() + length);
761 header_->set_num_keys(num_keys() + 1);
762 if (new_key_id > max_key_id()) {
763 header_->set_max_key_id(new_key_id);
764 header_->set_next_key_id(new_key_id + 1);
765 } else {
766 header_->set_next_key_id(ith_entry(new_key_id).next());
767 }
768
769 ith_entry(new_key_id).set_key_pos(new_key_pos);
770 ith_node(node_id).set_key_pos(new_key_pos);
771 if (key_pos != NULL) {
772 *key_pos = new_key_pos;
773 }
774 return true;
775 }
776
777 bool Trie::insert_linker(const UInt8 *ptr, UInt32 length,
778 UInt32 &node_id, UInt32 query_pos) {
779 if (ith_node(node_id).is_linker()) {
780 const Key &key = get_key(ith_node(node_id).key_pos());
781 UInt32 i = query_pos;
782 while ((i < length) && (i < key.length())) {
783 if (ptr[i] != key[i]) {
784 break;
785 }
786 ++i;
787 }
788 if ((i == length) && (i == key.length())) {
789 return false;
790 }
791 GRN_DAT_THROW_IF(SIZE_ERROR, num_keys() >= max_num_keys());
792 GRN_DAT_DEBUG_THROW_IF(next_key_id() > max_num_keys());
793
794 for (UInt32 j = query_pos; j < i; ++j) {
795 node_id = insert_node(node_id, ptr[j]);
796 }
797 node_id = separate(ptr, length, node_id, i);
798 return true;
799 } else if (ith_node(node_id).label() == TERMINAL_LABEL) {
800 return true;
801 } else {
802 GRN_DAT_THROW_IF(SIZE_ERROR, num_keys() >= max_num_keys());
803 const UInt16 label = (query_pos < length) ?
804 (UInt16)ptr[query_pos] : (UInt16)TERMINAL_LABEL;
805 const Base base = ith_node(node_id).base();
806 if ((base.offset() == INVALID_OFFSET) ||
807 !ith_node(base.offset() ^ label).is_phantom()) {
808 resolve(node_id, label);
809 }
810 node_id = insert_node(node_id, label);
811 return true;
812 }
813 }
814
815 bool Trie::update_key(const Key &key, const UInt8 *ptr, UInt32 length,
816 UInt32 *key_pos) {
817 GRN_DAT_THROW_IF(STATUS_ERROR, (status_flags() & CHANGING_MASK) != 0);
818 StatusFlagManager status_flag_manager(header_, UPDATING_FLAG);
819
820 GRN_DAT_DEBUG_THROW_IF((ptr == NULL) && (length != 0));
821
822 if (!key.is_valid()) {
823 return false;
824 }
825
826 UInt32 node_id = ROOT_NODE_ID;
827 UInt32 query_pos = 0;
828
829 search_linker(ptr, length, node_id, query_pos);
830 if (!insert_linker(ptr, length, node_id, query_pos)) {
831 if (key_pos != NULL) {
832 *key_pos = ith_node(node_id).key_pos();
833 }
834 return false;
835 }
836
837 const UInt32 new_key_pos = append_key(ptr, length, key.id());
838 header_->set_total_key_length(total_key_length() + length - key.length());
839 ith_entry(key.id()).set_key_pos(new_key_pos);
840 ith_node(node_id).set_key_pos(new_key_pos);
841 if (key_pos != NULL) {
842 *key_pos = new_key_pos;
843 }
844
845 node_id = ROOT_NODE_ID;
846 query_pos = 0;
847 GRN_DAT_THROW_IF(UNEXPECTED_ERROR,
848 !search_linker(static_cast<const UInt8 *>(key.ptr()), key.length(),
849 node_id, query_pos));
850 ith_node(node_id).set_offset(INVALID_OFFSET);
851 return true;
852 }
853
854 UInt32 Trie::insert_node(UInt32 node_id, UInt16 label) {
855 GRN_DAT_DEBUG_THROW_IF(node_id >= num_nodes());
856 GRN_DAT_DEBUG_THROW_IF(label > MAX_LABEL);
857
858 const Base base = ith_node(node_id).base();
859 UInt32 offset;
860 if (base.is_linker() || (base.offset() == INVALID_OFFSET)) {
861 offset = find_offset(&label, 1);
862 } else {
863 offset = base.offset();
864 }
865
866 const UInt32 next = offset ^ label;
867 reserve_node(next);
868
869 ith_node(next).set_label(label);
870 if (base.is_linker()) {
871 GRN_DAT_DEBUG_THROW_IF(ith_node(offset).is_offset());
872 ith_node(offset).set_is_offset(true);
873 ith_node(next).set_key_pos(base.key_pos());
874 } else if (base.offset() == INVALID_OFFSET) {
875 GRN_DAT_DEBUG_THROW_IF(ith_node(offset).is_offset());
876 ith_node(offset).set_is_offset(true);
877 } else {
878 GRN_DAT_DEBUG_THROW_IF(!ith_node(offset).is_offset());
879 }
880 ith_node(node_id).set_offset(offset);
881
882 const UInt32 child_label = ith_node(node_id).child();
883 GRN_DAT_DEBUG_THROW_IF(child_label == label);
884 if (child_label == INVALID_LABEL) {
885 ith_node(node_id).set_child(label);
886 } else if ((label == TERMINAL_LABEL) ||
887 ((child_label != TERMINAL_LABEL) && (label < child_label))) {
888 GRN_DAT_DEBUG_THROW_IF(ith_node(offset ^ child_label).is_phantom());
889 GRN_DAT_DEBUG_THROW_IF(ith_node(offset ^ child_label).label() != child_label);
890 ith_node(next).set_sibling(child_label);
891 ith_node(node_id).set_child(label);
892 } else {
893 UInt32 prev = offset ^ child_label;
894 GRN_DAT_DEBUG_THROW_IF(ith_node(prev).label() != child_label);
895 UInt32 sibling_label = ith_node(prev).sibling();
896 while (label > sibling_label) {
897 prev = offset ^ sibling_label;
898 GRN_DAT_DEBUG_THROW_IF(ith_node(prev).label() != sibling_label);
899 sibling_label = ith_node(prev).sibling();
900 }
901 GRN_DAT_DEBUG_THROW_IF(label == sibling_label);
902 ith_node(next).set_sibling(ith_node(prev).sibling());
903 ith_node(prev).set_sibling(label);
904 }
905 return next;
906 }
907
908 UInt32 Trie::append_key(const UInt8 *ptr, UInt32 length, UInt32 key_id) {
909 GRN_DAT_THROW_IF(SIZE_ERROR, key_id > max_num_keys());
910
911 const UInt32 key_pos = next_key_pos();
912 const UInt32 key_size = Key::estimate_size(length);
913
914 GRN_DAT_THROW_IF(SIZE_ERROR, key_size > (key_buf_size() - key_pos));
915 Key::create(key_buf_.ptr() + key_pos, key_id, ptr, length);
916
917 header_->set_next_key_pos(key_pos + key_size);
918 return key_pos;
919 }
920
921 UInt32 Trie::separate(const UInt8 *ptr, UInt32 length,
922 UInt32 node_id, UInt32 i) {
923 GRN_DAT_DEBUG_THROW_IF(node_id >= num_nodes());
924 GRN_DAT_DEBUG_THROW_IF(!ith_node(node_id).is_linker());
925 GRN_DAT_DEBUG_THROW_IF(i > length);
926
927 const UInt32 key_pos = ith_node(node_id).key_pos();
928 const Key &key = get_key(key_pos);
929
930 UInt16 labels[2];
931 labels[0] = (i < key.length()) ? (UInt16)key[i] : (UInt16)TERMINAL_LABEL;
932 labels[1] = (i < length) ? (UInt16)ptr[i] : (UInt16)TERMINAL_LABEL;
933 GRN_DAT_DEBUG_THROW_IF(labels[0] == labels[1]);
934
935 const UInt32 offset = find_offset(labels, 2);
936
937 UInt32 next = offset ^ labels[0];
938 reserve_node(next);
939 GRN_DAT_DEBUG_THROW_IF(ith_node(offset).is_offset());
940
941 ith_node(next).set_label(labels[0]);
942 ith_node(next).set_key_pos(key_pos);
943
944 next = offset ^ labels[1];
945 reserve_node(next);
946
947 ith_node(next).set_label(labels[1]);
948
949 ith_node(offset).set_is_offset(true);
950 ith_node(node_id).set_offset(offset);
951
952 if ((labels[0] == TERMINAL_LABEL) ||
953 ((labels[1] != TERMINAL_LABEL) && (labels[0] < labels[1]))) {
954 ith_node(node_id).set_child(labels[0]);
955 ith_node(offset ^ labels[0]).set_sibling(labels[1]);
956 } else {
957 ith_node(node_id).set_child(labels[1]);
958 ith_node(offset ^ labels[1]).set_sibling(labels[0]);
959 }
960 return next;
961 }
962
963 void Trie::resolve(UInt32 node_id, UInt16 label) {
964 GRN_DAT_DEBUG_THROW_IF(node_id >= num_nodes());
965 GRN_DAT_DEBUG_THROW_IF(ith_node(node_id).is_linker());
966 GRN_DAT_DEBUG_THROW_IF(label > MAX_LABEL);
967
968 UInt32 offset = ith_node(node_id).offset();
969 if (offset != INVALID_OFFSET) {
970 UInt16 labels[MAX_LABEL + 1];
971 UInt32 num_labels = 0;
972
973 UInt32 next_label = ith_node(node_id).child();
974 GRN_DAT_DEBUG_THROW_IF(next_label == INVALID_LABEL);
975 while (next_label != INVALID_LABEL) {
976 GRN_DAT_DEBUG_THROW_IF(next_label > MAX_LABEL);
977 labels[num_labels++] = static_cast<UInt16>(next_label);
978 next_label = ith_node(offset ^ next_label).sibling();
979 }
980 GRN_DAT_DEBUG_THROW_IF(num_labels == 0);
981
982 labels[num_labels] = label;
983 offset = find_offset(labels, num_labels + 1);
984 migrate_nodes(node_id, offset, labels, num_labels);
985 } else {
986 offset = find_offset(&label, 1);
987 if (offset >= num_nodes()) {
988 GRN_DAT_DEBUG_THROW_IF((offset / BLOCK_SIZE) != num_blocks());
989 reserve_block(num_blocks());
990 }
991 ith_node(offset).set_is_offset(true);
992 ith_node(node_id).set_offset(offset);
993 }
994 }
995
996 void Trie::migrate_nodes(UInt32 node_id, UInt32 dest_offset,
997 const UInt16 *labels, UInt32 num_labels) {
998 GRN_DAT_DEBUG_THROW_IF(node_id >= num_nodes());
999 GRN_DAT_DEBUG_THROW_IF(ith_node(node_id).is_linker());
1000 GRN_DAT_DEBUG_THROW_IF(labels == NULL);
1001 GRN_DAT_DEBUG_THROW_IF(num_labels == 0);
1002 GRN_DAT_DEBUG_THROW_IF(num_labels > (MAX_LABEL + 1));
1003
1004 const UInt32 src_offset = ith_node(node_id).offset();
1005 GRN_DAT_DEBUG_THROW_IF(src_offset == INVALID_OFFSET);
1006 GRN_DAT_DEBUG_THROW_IF(!ith_node(src_offset).is_offset());
1007
1008 for (UInt32 i = 0; i < num_labels; ++i) {
1009 const UInt32 src_node_id = src_offset ^ labels[i];
1010 const UInt32 dest_node_id = dest_offset ^ labels[i];
1011 GRN_DAT_DEBUG_THROW_IF(ith_node(src_node_id).is_phantom());
1012 GRN_DAT_DEBUG_THROW_IF(ith_node(src_node_id).label() != labels[i]);
1013
1014 reserve_node(dest_node_id);
1015 ith_node(dest_node_id).set_except_is_offset(
1016 ith_node(src_node_id).except_is_offset());
1017 ith_node(dest_node_id).set_base(ith_node(src_node_id).base());
1018 }
1019 header_->set_num_zombies(num_zombies() + num_labels);
1020
1021 GRN_DAT_DEBUG_THROW_IF(ith_node(dest_offset).is_offset());
1022 ith_node(dest_offset).set_is_offset(true);
1023 ith_node(node_id).set_offset(dest_offset);
1024 }
1025
1026 UInt32 Trie::find_offset(const UInt16 *labels, UInt32 num_labels) {
1027 GRN_DAT_DEBUG_THROW_IF(labels == NULL);
1028 GRN_DAT_DEBUG_THROW_IF(num_labels == 0);
1029 GRN_DAT_DEBUG_THROW_IF(num_labels > (MAX_LABEL + 1));
1030
1031 // Blocks are tested in descending order of level. Basically, a lower level
1032 // block contains more phantom nodes.
1033 UInt32 level = 1;
1034 while (num_labels >= (1U << level)) {
1035 ++level;
1036 }
1037 level = (level < MAX_BLOCK_LEVEL) ? (MAX_BLOCK_LEVEL - level) : 0;
1038
1039 UInt32 block_count = 0;
1040 do {
1041 UInt32 leader = header_->ith_leader(level);
1042 if (leader == INVALID_LEADER) {
1043 // This level has no blocks and it is thus skipped.
1044 continue;
1045 }
1046
1047 UInt32 block_id = leader;
1048 do {
1049 const Block &block = ith_block(block_id);
1050 GRN_DAT_DEBUG_THROW_IF(block.level() != level);
1051
1052 const UInt32 first = (block_id * BLOCK_SIZE) | block.first_phantom();
1053 UInt32 node_id = first;
1054 do {
1055 GRN_DAT_DEBUG_THROW_IF(!ith_node(node_id).is_phantom());
1056 const UInt32 offset = node_id ^ labels[0];
1057 if (!ith_node(offset).is_offset()) {
1058 UInt32 i = 1;
1059 for ( ; i < num_labels; ++i) {
1060 if (!ith_node(offset ^ labels[i]).is_phantom()) {
1061 break;
1062 }
1063 }
1064 if (i >= num_labels) {
1065 return offset;
1066 }
1067 }
1068 node_id = (block_id * BLOCK_SIZE) | ith_node(node_id).next();
1069 } while (node_id != first);
1070
1071 const UInt32 prev = block_id;
1072 const UInt32 next = block.next();
1073 block_id = next;
1074 ith_block(prev).set_failure_count(ith_block(prev).failure_count() + 1);
1075
1076 // The level of a block is updated when this function fails many times,
1077 // actually MAX_FAILURE_COUNT times, in that block.
1078 if (ith_block(prev).failure_count() == MAX_FAILURE_COUNT) {
1079 update_block_level(prev, level + 1);
1080 if (next == leader) {
1081 break;
1082 } else {
1083 // Note that the leader might be updated in the level update.
1084 leader = header_->ith_leader(level);
1085 continue;
1086 }
1087 }
1088 } while ((++block_count < MAX_BLOCK_COUNT) && (block_id != leader));
1089 } while ((block_count < MAX_BLOCK_COUNT) && (level-- != 0));
1090
1091 return num_nodes() ^ labels[0];
1092 }
1093
1094 void Trie::reserve_node(UInt32 node_id) {
1095 GRN_DAT_DEBUG_THROW_IF(node_id > num_nodes());
1096 if (node_id >= num_nodes()) {
1097 reserve_block(node_id / BLOCK_SIZE);
1098 }
1099
1100 Node &node = ith_node(node_id);
1101 GRN_DAT_DEBUG_THROW_IF(!node.is_phantom());
1102
1103 const UInt32 block_id = node_id / BLOCK_SIZE;
1104 Block &block = ith_block(block_id);
1105 GRN_DAT_DEBUG_THROW_IF(block.num_phantoms() == 0);
1106
1107 const UInt32 next = (block_id * BLOCK_SIZE) | node.next();
1108 const UInt32 prev = (block_id * BLOCK_SIZE) | node.prev();
1109 GRN_DAT_DEBUG_THROW_IF(next >= num_nodes());
1110 GRN_DAT_DEBUG_THROW_IF(prev >= num_nodes());
1111
1112 if ((node_id & BLOCK_MASK) == block.first_phantom()) {
1113 // The first phantom node is removed from the block and the second phantom
1114 // node comes first.
1115 block.set_first_phantom(next & BLOCK_MASK);
1116 }
1117
1118 ith_node(next).set_prev(prev & BLOCK_MASK);
1119 ith_node(prev).set_next(next & BLOCK_MASK);
1120
1121 if (block.level() != MAX_BLOCK_LEVEL) {
1122 const UInt32 threshold = 1U << ((MAX_BLOCK_LEVEL - block.level() - 1) * 2);
1123 if (block.num_phantoms() == threshold) {
1124 update_block_level(block_id, block.level() + 1);
1125 }
1126 }
1127 block.set_num_phantoms(block.num_phantoms() - 1);
1128
1129 node.set_is_phantom(false);
1130
1131 GRN_DAT_DEBUG_THROW_IF(node.offset() != INVALID_OFFSET);
1132 GRN_DAT_DEBUG_THROW_IF(node.label() != INVALID_LABEL);
1133
1134 header_->set_num_phantoms(num_phantoms() - 1);
1135 }
1136
1137 void Trie::reserve_block(UInt32 block_id) {
1138 GRN_DAT_DEBUG_THROW_IF(block_id != num_blocks());
1139 GRN_DAT_THROW_IF(SIZE_ERROR, block_id >= max_num_blocks());
1140
1141 header_->set_num_blocks(block_id + 1);
1142 ith_block(block_id).set_failure_count(0);
1143 ith_block(block_id).set_first_phantom(0);
1144 ith_block(block_id).set_num_phantoms(BLOCK_SIZE);
1145
1146 const UInt32 begin = block_id * BLOCK_SIZE;
1147 const UInt32 end = begin + BLOCK_SIZE;
1148 GRN_DAT_DEBUG_THROW_IF(end != num_nodes());
1149
1150 Base base;
1151 base.set_offset(INVALID_OFFSET);
1152
1153 Check check;
1154 check.set_is_phantom(true);
1155
1156 for (UInt32 i = begin; i < end; ++i) {
1157 check.set_prev((i - 1) & BLOCK_MASK);
1158 check.set_next((i + 1) & BLOCK_MASK);
1159 ith_node(i).set_base(base);
1160 ith_node(i).set_check(check);
1161 }
1162
1163 // The level of the new block is 0.
1164 set_block_level(block_id, 0);
1165 header_->set_num_phantoms(num_phantoms() + BLOCK_SIZE);
1166 }
1167
1168 void Trie::update_block_level(UInt32 block_id, UInt32 level) {
1169 GRN_DAT_DEBUG_THROW_IF(block_id >= num_blocks());
1170 GRN_DAT_DEBUG_THROW_IF(level > MAX_BLOCK_LEVEL);
1171
1172 unset_block_level(block_id);
1173 set_block_level(block_id, level);
1174 }
1175
1176 void Trie::set_block_level(UInt32 block_id, UInt32 level) {
1177 GRN_DAT_DEBUG_THROW_IF(block_id >= num_blocks());
1178 GRN_DAT_DEBUG_THROW_IF(level > MAX_BLOCK_LEVEL);
1179
1180 const UInt32 leader = header_->ith_leader(level);
1181 if (leader == INVALID_LEADER) {
1182 // The new block becomes the only one block of the linked list.
1183 ith_block(block_id).set_next(block_id);
1184 ith_block(block_id).set_prev(block_id);
1185 header_->set_ith_leader(level, block_id);
1186 } else {
1187 // The new block is added to the end of the list.
1188 const UInt32 next = leader;
1189 const UInt32 prev = ith_block(leader).prev();
1190 GRN_DAT_DEBUG_THROW_IF(next >= num_blocks());
1191 GRN_DAT_DEBUG_THROW_IF(prev >= num_blocks());
1192 ith_block(block_id).set_next(next);
1193 ith_block(block_id).set_prev(prev);
1194 ith_block(next).set_prev(block_id);
1195 ith_block(prev).set_next(block_id);
1196 }
1197 ith_block(block_id).set_level(level);
1198 ith_block(block_id).set_failure_count(0);
1199 }
1200
1201 void Trie::unset_block_level(UInt32 block_id) {
1202 GRN_DAT_DEBUG_THROW_IF(block_id >= num_blocks());
1203
1204 const UInt32 level = ith_block(block_id).level();
1205 GRN_DAT_DEBUG_THROW_IF(level > MAX_BLOCK_LEVEL);
1206
1207 const UInt32 leader = header_->ith_leader(level);
1208 GRN_DAT_DEBUG_THROW_IF(leader == INVALID_LEADER);
1209
1210 const UInt32 next = ith_block(block_id).next();
1211 const UInt32 prev = ith_block(block_id).prev();
1212 GRN_DAT_DEBUG_THROW_IF(next >= num_blocks());
1213 GRN_DAT_DEBUG_THROW_IF(prev >= num_blocks());
1214
1215 if (next == block_id) {
1216 // The linked list becomes empty.
1217 header_->set_ith_leader(level, INVALID_LEADER);
1218 } else {
1219 ith_block(next).set_prev(prev);
1220 ith_block(prev).set_next(next);
1221 if (block_id == leader) {
1222 // The second block becomes the leader of the linked list.
1223 header_->set_ith_leader(level, next);
1224 }
1225 }
1226 }
1227
1228 } // namespace dat
1229 } // namespace grn