Debian Codegraph @

lib/dat.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2011-2018 Brazil
3 Copyright (C) 2018-2026 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 "grn.h"
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <cstring>
24 #include <sstream>
25 #include <new>
26 #include "grn_ctx_impl.h"
27 #include "grn_str.h"
28 #include "grn_io.h"
29 #include "grn_dat.h"
30 #include "grn_normalizer.h"
31 #include "grn_obj.h"
32 #include "grn_util.h"
33 #include "grn_wal.h"
34
35 #include "dat/trie.hpp"
36 #include "dat/cursor-factory.hpp"
37
38 namespace {
39
40 const uint32_t FILE_ID_LENGTH = 3;
41
42 class CriticalSection {
43 public:
44 CriticalSection() : lock_(NULL) {}
45 explicit CriticalSection(grn_critical_section *lock) : lock_(lock)
46 {
47 CRITICAL_SECTION_ENTER(*lock_);
48 }
49 ~CriticalSection() { leave(); }
50
51 void
52 enter(grn_critical_section *lock)
53 {
54 leave();
55 lock_ = lock;
56 }
57 void
58 leave()
59 {
60 if (lock_ != NULL) {
61 CRITICAL_SECTION_LEAVE(*lock_);
62 lock_ = NULL;
63 }
64 }
65
66 private:
67 grn_critical_section *lock_;
68
69 // Disallows copy and assignment.
70 CriticalSection(const CriticalSection &);
71 CriticalSection &
72 operator=(const CriticalSection &);
73 };
74
75 static inline int
76 grn_dat_name(grn_ctx *ctx, grn_dat *dat, char *buffer, int buffer_size)
77 {
78 int name_size;
79
80 if (DB_OBJ(dat)->id == GRN_ID_NIL) {
81 grn_strcpy(buffer, buffer_size, "(anonymous)");
82 name_size = strlen(buffer);
83 } else {
84 name_size = grn_obj_name(ctx, (grn_obj *)dat, buffer, buffer_size);
85 }
86
87 return name_size;
88 }
89
90 /*
91 grn_dat_remove_file() removes a file specified by `path' and then returns
92 true on success, false on failure. Note that grn_dat_remove_file() does not
93 change `ctx->rc'.
94 */
95 bool
96 grn_dat_remove_file(grn_ctx *ctx, const char *path)
97 {
98 struct stat stat;
99
100 if (::stat(path, &stat) == -1) {
101 return false;
102 }
103
104 if (grn_unlink(path) == -1) {
105 const char *system_message = grn_strerror(errno);
106 GRN_LOG(ctx,
107 GRN_LOG_WARNING,
108 "[dat][remove-file] failed to remove path: %s: <%s>",
109 system_message,
110 path);
111 return false;
112 }
113
114 GRN_LOG(ctx, GRN_LOG_INFO, "[dat][remove-file] removed: <%s>", path);
115 return true;
116 }
117
118 grn_rc
119 grn_dat_translate_error_code(grn::dat::ErrorCode error_code)
120 {
121 switch (error_code) {
122 case grn::dat::PARAM_ERROR:
123 {
124 return GRN_INVALID_ARGUMENT;
125 }
126 case grn::dat::IO_ERROR:
127 {
128 return GRN_INPUT_OUTPUT_ERROR;
129 }
130 case grn::dat::FORMAT_ERROR:
131 {
132 return GRN_INVALID_FORMAT;
133 }
134 case grn::dat::MEMORY_ERROR:
135 {
136 return GRN_NO_MEMORY_AVAILABLE;
137 }
138 case grn::dat::SIZE_ERROR:
139 case grn::dat::UNEXPECTED_ERROR:
140 {
141 return GRN_UNKNOWN_ERROR;
142 }
143 case grn::dat::STATUS_ERROR:
144 {
145 return GRN_FILE_CORRUPT;
146 }
147 default:
148 {
149 return GRN_UNKNOWN_ERROR;
150 }
151 }
152 }
153
154 void
155 grn_dat_init(grn_ctx *ctx, grn_dat *dat)
156 {
157 GRN_DB_OBJ_SET_TYPE(dat, GRN_TABLE_DAT_KEY);
158 dat->io = NULL;
159 dat->header = NULL;
160 dat->file_id = 0;
161 dat->encoding = GRN_ENC_DEFAULT;
162 dat->trie = NULL;
163 dat->old_trie = NULL;
164 grn_table_module_init(ctx, &(dat->tokenizer), GRN_ID_NIL);
165 grn_table_modules_init(ctx, &(dat->normalizers));
166 grn_table_modules_init(ctx, &(dat->token_filters));
167 GRN_PTR_INIT(&(dat->token_filter_procs), GRN_OBJ_VECTOR, GRN_ID_NIL);
168 grn_table_modules_init(ctx, &(dat->extractors));
169 CRITICAL_SECTION_INIT(dat->lock);
170 dat->is_dirty = false;
171 }
172
173 static void
174 grn_dat_close_token_filters(grn_ctx *ctx, grn_dat *dat)
175 {
176 grn_table_modules_fin(ctx, &(dat->token_filters));
177 GRN_OBJ_FIN(ctx, &(dat->token_filter_procs));
178 }
179
180 void
181 grn_dat_fin(grn_ctx *ctx, grn_dat *dat)
182 {
183 CRITICAL_SECTION_FIN(dat->lock);
184 delete static_cast<grn::dat::Trie *>(dat->old_trie);
185 delete static_cast<grn::dat::Trie *>(dat->trie);
186 dat->old_trie = NULL;
187 dat->trie = NULL;
188 if (dat->io) {
189 if (dat->is_dirty) {
190 uint32_t n_dirty_opens;
191 GRN_ATOMIC_ADD_EX(&(dat->header->n_dirty_opens), -1, n_dirty_opens);
192 }
193 grn_io_close(ctx, dat->io);
194 dat->io = NULL;
195 }
196 grn_table_module_fin(ctx, &(dat->tokenizer));
197 grn_table_modules_fin(ctx, &(dat->normalizers));
198 grn_dat_close_token_filters(ctx, dat);
199 grn_table_modules_fin(ctx, &(dat->extractors));
200 }
201
202 /*
203 grn_dat_generate_trie_path() generates the path from `base_path' and
204 `file_id'. The generated path is stored in `trie_path'.
205 */
206 void
207 grn_dat_generate_trie_path(const char *base_path,
208 char *trie_path,
209 uint32_t file_id)
210 {
211 if (!base_path || !base_path[0]) {
212 trie_path[0] = '\0';
213 return;
214 }
215 const size_t len = std::strlen(base_path);
216 grn_memcpy(trie_path, base_path, len);
217 trie_path[len] = '.';
218 grn_itoh(file_id % (1U << (4 * FILE_ID_LENGTH)),
219 trie_path + len + 1,
220 FILE_ID_LENGTH);
221 trie_path[len + 1 + FILE_ID_LENGTH] = '\0';
222 }
223
224 bool
225 grn_dat_open_trie_if_needed(grn_ctx *ctx, grn_dat *dat)
226 {
227 if (!dat) {
228 ERR(GRN_INVALID_ARGUMENT, "dat is null");
229 return false;
230 }
231
232 const uint32_t file_id = dat->header->file_id;
233 if (!file_id || (dat->trie && (file_id <= dat->file_id))) {
234 /*
235 There is no need to open file when no trie file is available or the
236 current trie file is the latest one.
237 */
238 return true;
239 }
240
241 CriticalSection critical_section(&dat->lock);
242
243 if (dat->trie && (file_id <= dat->file_id)) {
244 /*
245 There is no need to open file if the latest file has been opened by
246 another thread.
247 */
248 return true;
249 }
250
251 char trie_path[PATH_MAX];
252 grn_dat_generate_trie_path(grn_io_path(dat->io), trie_path, file_id);
253 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
254 grn::dat::Trie *const old_trie =
255 static_cast<grn::dat::Trie *>(dat->old_trie);
256 grn::dat::Trie *const new_trie = new (std::nothrow) grn::dat::Trie;
257 if (!new_trie) {
258 MERR("new grn::dat::Trie failed");
259 return false;
260 }
261
262 if (trie_path[0] == '\0') {
263 try {
264 new_trie->create(trie_path);
265 } catch (const grn::dat::Exception &ex) {
266 ERR(grn_dat_translate_error_code(ex.code()),
267 "grn::dat::Trie::create failed: %s",
268 ex.what());
269 delete new_trie;
270 return false;
271 }
272 } else {
273 try {
274 new_trie->open(trie_path);
275 } catch (const grn::dat::Exception &ex) {
276 ERR(grn_dat_translate_error_code(ex.code()),
277 "grn::dat::Trie::open failed: %s",
278 ex.what());
279 delete new_trie;
280 return false;
281 }
282 }
283
284 dat->old_trie = trie;
285 dat->trie = new_trie;
286 dat->file_id = file_id;
287
288 critical_section.leave();
289
290 delete old_trie;
291 if (file_id >= 3) {
292 grn_dat_generate_trie_path(grn_io_path(dat->io), trie_path, file_id - 2);
293 grn_dat_remove_file(ctx, trie_path);
294 }
295 return true;
296 }
297
298 bool
299 grn_dat_rebuild_trie(grn_ctx *ctx, grn_dat *dat)
300 {
301 const grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
302 grn::dat::Trie *const new_trie = new (std::nothrow) grn::dat::Trie;
303 if (!new_trie) {
304 MERR("new grn::dat::Trie failed");
305 return false;
306 }
307
308 const uint32_t file_id = dat->header->file_id;
309 char trie_path[PATH_MAX];
310 grn_dat_generate_trie_path(grn_io_path(dat->io), trie_path, file_id + 1);
311
312 for (uint64_t file_size = trie->file_size() * 2;; file_size *= 2) {
313 try {
314 new_trie->create(*trie, trie_path, file_size);
315 } catch (const grn::dat::SizeError &) {
316 continue;
317 } catch (const grn::dat::Exception &ex) {
318 ERR(grn_dat_translate_error_code(ex.code()),
319 "grn::dat::Trie::open failed: %s",
320 ex.what());
321 delete new_trie;
322 return false;
323 }
324 break;
325 }
326
327 grn::dat::Trie *const old_trie =
328 static_cast<grn::dat::Trie *>(dat->old_trie);
329 dat->old_trie = dat->trie;
330 dat->trie = new_trie;
331 dat->header->file_id = dat->file_id = file_id + 1;
332
333 delete old_trie;
334 if (file_id >= 2) {
335 char trie_path[PATH_MAX];
336 grn_dat_generate_trie_path(grn_io_path(dat->io), trie_path, file_id - 1);
337 grn_dat_remove_file(ctx, trie_path);
338 }
339 return true;
340 }
341
342 grn_id
343 grn_dat_add_internal(grn_ctx *ctx,
344 grn_dat *dat,
345 const void *key,
346 unsigned int key_size,
347 int *added)
348 {
349 if (!dat->trie) {
350 char trie_path[PATH_MAX];
351 grn_dat_generate_trie_path(grn_io_path(dat->io), trie_path, 1);
352 grn::dat::Trie *const new_trie = new (std::nothrow) grn::dat::Trie;
353 if (!new_trie) {
354 MERR("new grn::dat::Trie failed");
355 return GRN_ID_NIL;
356 }
357 try {
358 new_trie->create(trie_path);
359 } catch (const grn::dat::Exception &ex) {
360 ERR(grn_dat_translate_error_code(ex.code()),
361 "grn::dat::Trie::create failed: %s",
362 ex.what());
363 delete new_trie;
364 return GRN_ID_NIL;
365 }
366 dat->trie = new_trie;
367 dat->file_id = dat->header->file_id = 1;
368 }
369
370 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
371 try {
372 grn::dat::UInt32 key_pos;
373 const bool res = trie->insert(key, key_size, &key_pos);
374 if (added) {
375 *added = res ? 1 : 0;
376 }
377 return trie->get_key(key_pos).id();
378 } catch (const grn::dat::SizeError &) {
379 if (!grn_dat_rebuild_trie(ctx, dat)) {
380 return GRN_ID_NIL;
381 }
382 grn::dat::Trie *const new_trie = static_cast<grn::dat::Trie *>(dat->trie);
383 grn::dat::UInt32 key_pos;
384 const bool res = new_trie->insert(key, key_size, &key_pos);
385 if (added) {
386 *added = res ? 1 : 0;
387 }
388 return new_trie->get_key(key_pos).id();
389 } catch (const grn::dat::Exception &ex) {
390 ERR(grn_dat_translate_error_code(ex.code()),
391 "grn::dat::Trie::insert failed: %s",
392 ex.what());
393 return GRN_ID_NIL;
394 }
395 }
396
397 grn_rc
398 grn_dat_delete_by_id_internal(grn_ctx *ctx, grn_dat *dat, grn_id id)
399 {
400 try {
401 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
402 if (!trie->remove(id)) {
403 return GRN_INVALID_ARGUMENT;
404 }
405 } catch (const grn::dat::Exception &ex) {
406 ERR(grn_dat_translate_error_code(ex.code()),
407 "grn::dat::Trie::remove failed: %s",
408 ex.what());
409 return ctx->rc;
410 }
411 return GRN_SUCCESS;
412 }
413
414 grn_rc
415 grn_dat_delete_internal(grn_ctx *ctx,
416 grn_dat *dat,
417 const void *key,
418 unsigned int key_size)
419 {
420 try {
421 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
422 if (!trie->remove(key, key_size)) {
423 return GRN_INVALID_ARGUMENT;
424 }
425 } catch (const grn::dat::Exception &ex) {
426 ERR(grn_dat_translate_error_code(ex.code()),
427 "grn::dat::Trie::remove failed: %s",
428 ex.what());
429 return ctx->rc;
430 }
431 return GRN_SUCCESS;
432 }
433
434 grn_rc
435 grn_dat_update_by_id_internal(grn_ctx *ctx,
436 grn_dat *dat,
437 grn_id src_key_id,
438 const void *dest_key,
439 unsigned int dest_key_size)
440 {
441 try {
442 try {
443 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
444 if (!trie->update(src_key_id, dest_key, dest_key_size)) {
445 return GRN_INVALID_ARGUMENT;
446 }
447 } catch (const grn::dat::SizeError &) {
448 if (!grn_dat_rebuild_trie(ctx, dat)) {
449 return ctx->rc;
450 }
451 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
452 if (!trie->update(src_key_id, dest_key, dest_key_size)) {
453 return GRN_INVALID_ARGUMENT;
454 }
455 }
456 } catch (const grn::dat::Exception &ex) {
457 ERR(grn_dat_translate_error_code(ex.code()),
458 "grn::dat::Trie::update failed: %s",
459 ex.what());
460 return ctx->rc;
461 }
462 return GRN_SUCCESS;
463 }
464
465 grn_rc
466 grn_dat_update_internal(grn_ctx *ctx,
467 grn_dat *dat,
468 const void *src_key,
469 unsigned int src_key_size,
470 const void *dest_key,
471 unsigned int dest_key_size)
472 {
473 try {
474 try {
475 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
476 if (!trie->update(src_key, src_key_size, dest_key, dest_key_size)) {
477 return GRN_INVALID_ARGUMENT;
478 }
479 } catch (const grn::dat::SizeError &) {
480 if (!grn_dat_rebuild_trie(ctx, dat)) {
481 return ctx->rc;
482 }
483 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
484 if (!trie->update(src_key, src_key_size, dest_key, dest_key_size)) {
485 return GRN_INVALID_ARGUMENT;
486 }
487 }
488 } catch (const grn::dat::Exception &ex) {
489 ERR(grn_dat_translate_error_code(ex.code()),
490 "grn::dat::Trie::update failed: %s",
491 ex.what());
492 return ctx->rc;
493 }
494 return GRN_SUCCESS;
495 }
496
497 void
498 grn_dat_cursor_init(grn_ctx *, grn_dat_cursor *cursor)
499 {
500 GRN_DB_OBJ_SET_TYPE(cursor, GRN_CURSOR_TABLE_DAT_KEY);
501 cursor->dat = NULL;
502 cursor->cursor = NULL;
503 cursor->key = &grn::dat::Key::invalid_key();
504 cursor->curr_rec = GRN_ID_NIL;
505 }
506
507 void
508 grn_dat_cursor_fin(grn_ctx *, grn_dat_cursor *cursor)
509 {
510 delete static_cast<grn::dat::Cursor *>(cursor->cursor);
511 cursor->dat = NULL;
512 cursor->cursor = NULL;
513 cursor->key = &grn::dat::Key::invalid_key();
514 cursor->curr_rec = GRN_ID_NIL;
515 }
516
517 void
518 grn_dat_wal_set_wal_id(grn_ctx *, grn_dat *dat, uint64_t id)
519 {
520 dat->header->wal_id = id;
521 }
522
523 class WALRecorder {
524 private:
525 struct Used {
526 bool record_id;
527 bool key;
528 bool new_key;
529 };
530
531 public:
532 WALRecorder(grn_ctx *ctx, grn_dat *dat, const char *tag)
533 : ctx_(ctx),
534 dat_(dat),
535 tag_(tag),
536 wal_id_(0),
537 event(GRN_WAL_EVENT_NIL),
538 record_id(GRN_ID_NIL),
539 key(nullptr),
540 key_size(0)
541 {
542 }
543
544 grn_rc
545 record()
546 {
547 if (GRN_CTX_GET_WAL_ROLE(ctx_) == GRN_WAL_ROLE_NONE) {
548 return GRN_SUCCESS;
549 }
550 if (!dat_->io) {
551 return GRN_SUCCESS;
552 }
553 if (dat_->io->path[0] == '\0') {
554 return GRN_SUCCESS;
555 }
556
557 grn_rc rc = GRN_SUCCESS;
558 const char *usage = "";
559 Used used;
560
561 switch (event) {
562 case GRN_WAL_EVENT_ADD_ENTRY:
563 usage = "adding entry";
564 rc = record_add_entry(used);
565 break;
566 case GRN_WAL_EVENT_DELETE_ENTRY:
567 usage = "deleting entry";
568 rc = record_delete_entry(used);
569 break;
570 case GRN_WAL_EVENT_UPDATE_ENTRY:
571 usage = "updating entry";
572 rc = record_update_entry(used);
573 break;
574 default:
575 usage = "not implemented event";
576 rc = GRN_FUNCTION_NOT_IMPLEMENTED;
577 break;
578 }
579
580 if (rc != GRN_SUCCESS) {
581 auto details = format_details(used);
582 grn_obj_set_error(ctx_,
583 reinterpret_cast<grn_obj *>(dat_),
584 rc,
585 record_id,
586 tag_,
587 "failed to add WAL entry for %s: %s",
588 usage,
589 details.c_str());
590 }
591
592 return rc;
593 }
594
595 void
596 apply_wal_id()
597 {
598 grn_dat_wal_set_wal_id(ctx_, dat_, wal_id_);
599 }
600
601 private:
602 grn_ctx *ctx_;
603 grn_dat *dat_;
604 const char *tag_;
605 uint64_t wal_id_;
606
607 public:
608 grn_wal_event event;
609 grn_id record_id;
610 const void *key;
611 size_t key_size;
612 const void *new_key;
613 size_t new_key_size;
614
615 private:
616 // Disallows copy and assignment.
617 WALRecorder(const WALRecorder &);
618 WALRecorder &
619 operator=(const WALRecorder &);
620
621 grn_rc
622 record_add_entry(Used &used)
623 {
624 used.key = true;
625 return grn_wal_add_entry(ctx_,
626 reinterpret_cast<grn_obj *>(dat_),
627 false,
628 &wal_id_,
629 tag_,
630
631 GRN_WAL_KEY_EVENT,
632 GRN_WAL_VALUE_EVENT,
633 event,
634
635 GRN_WAL_KEY_KEY,
636 GRN_WAL_VALUE_BINARY,
637 key,
638 key_size,
639
640 GRN_WAL_KEY_END);
641 }
642
643 grn_rc
644 record_delete_entry(Used &used)
645 {
646 if (record_id == GRN_ID_NIL) {
647 used.key = true;
648 return grn_wal_add_entry(ctx_,
649 reinterpret_cast<grn_obj *>(dat_),
650 false,
651 &wal_id_,
652 tag_,
653
654 GRN_WAL_KEY_EVENT,
655 GRN_WAL_VALUE_EVENT,
656 event,
657
658 GRN_WAL_KEY_KEY,
659 GRN_WAL_VALUE_BINARY,
660 key,
661 key_size,
662
663 GRN_WAL_KEY_END);
664 } else {
665 used.record_id = true;
666 return grn_wal_add_entry(ctx_,
667 reinterpret_cast<grn_obj *>(dat_),
668 false,
669 &wal_id_,
670 tag_,
671
672 GRN_WAL_KEY_EVENT,
673 GRN_WAL_VALUE_EVENT,
674 event,
675
676 GRN_WAL_KEY_RECORD_ID,
677 GRN_WAL_VALUE_RECORD_ID,
678 record_id,
679
680 GRN_WAL_KEY_END);
681 }
682 }
683
684 grn_rc
685 record_update_entry(Used &used)
686 {
687 if (record_id == GRN_ID_NIL) {
688 used.key = true;
689 used.new_key = true;
690 return grn_wal_add_entry(ctx_,
691 reinterpret_cast<grn_obj *>(dat_),
692 false,
693 &wal_id_,
694 tag_,
695
696 GRN_WAL_KEY_EVENT,
697 GRN_WAL_VALUE_EVENT,
698 event,
699
700 GRN_WAL_KEY_KEY,
701 GRN_WAL_VALUE_BINARY,
702 key,
703 key_size,
704
705 GRN_WAL_KEY_NEW_KEY,
706 GRN_WAL_VALUE_BINARY,
707 new_key,
708 new_key_size,
709
710 GRN_WAL_KEY_END);
711 } else {
712 used.record_id = true;
713 used.new_key = true;
714 return grn_wal_add_entry(ctx_,
715 reinterpret_cast<grn_obj *>(dat_),
716 false,
717 &wal_id_,
718 tag_,
719
720 GRN_WAL_KEY_EVENT,
721 GRN_WAL_VALUE_EVENT,
722 event,
723
724 GRN_WAL_KEY_RECORD_ID,
725 GRN_WAL_VALUE_RECORD_ID,
726 record_id,
727
728 GRN_WAL_KEY_NEW_KEY,
729 GRN_WAL_VALUE_BINARY,
730 new_key,
731 new_key_size,
732
733 GRN_WAL_KEY_END);
734 }
735 }
736
737 std::string
738 format_details(const Used &used)
739 {
740 std::ostringstream details;
741 details << "event:" << event << "<" << grn_wal_event_to_string(event)
742 << "> ";
743 if (used.record_id) {
744 details << "record-id:" << record_id << " ";
745 }
746 if (used.key) {
747 details << "key-size:" << key_size << " ";
748 }
749 if (used.new_key) {
750 details << "new-key-size:" << new_key_size << " ";
751 }
752 return details.str();
753 }
754 };
755
756 class WALRecoverer {
757 public:
758 WALRecoverer(grn_ctx *ctx, grn_dat *dat)
759 : ctx_(ctx),
760 dat_(dat),
761 wal_error_tag_("[dat]"),
762 tag_("[dat][wal][recover]")
763 {
764 }
765
766 grn_rc
767 recover()
768 {
769 if (ctx_->rc != GRN_SUCCESS) {
770 return ctx_->rc;
771 }
772
773 auto reader =
774 grn_wal_reader_open(ctx_, reinterpret_cast<grn_obj *>(dat_), tag_);
775 if (ctx_->rc != GRN_SUCCESS) {
776 return ctx_->rc;
777 }
778
779 bool need_flush = false;
780 if (grn_io_is_locked(dat_->io)) {
781 grn_io_clear_lock(dat_->io);
782 need_flush = true;
783 }
784
785 if (grn_dat_is_changing(ctx_, dat_)) {
786 grn_dat_clear_status_flags(ctx_, dat_);
787 need_flush = true;
788 }
789
790 if (reader) {
791 while (true) {
792 grn_wal_reader_entry entry = {};
793 grn_rc rc = grn_wal_reader_read_entry(ctx_, reader, &entry);
794 if (rc != GRN_SUCCESS) {
795 break;
796 }
797 switch (entry.event) {
798 case GRN_WAL_EVENT_ADD_ENTRY:
799 grn_dat_add_internal(ctx_,
800 dat_,
801 entry.key.content.binary.data,
802 entry.key.content.binary.size,
803 nullptr);
804 break;
805 case GRN_WAL_EVENT_DELETE_ENTRY:
806 if (!grn_dat_open_trie_if_needed(ctx_, dat_)) {
807 break;
808 }
809 if (entry.record_id == GRN_ID_NIL) {
810 grn_dat_delete_internal(ctx_,
811 dat_,
812 entry.key.content.binary.data,
813 entry.key.content.binary.size);
814 } else {
815 grn_dat_delete_by_id_internal(ctx_, dat_, entry.record_id);
816 }
817 break;
818 case GRN_WAL_EVENT_UPDATE_ENTRY:
819 if (entry.record_id == GRN_ID_NIL) {
820 grn_dat_update_internal(ctx_,
821 dat_,
822 entry.key.content.binary.data,
823 entry.key.content.binary.size,
824 entry.new_key.content.binary.data,
825 entry.new_key.content.binary.size);
826 } else {
827 grn_dat_update_by_id_internal(ctx_,
828 dat_,
829 entry.record_id,
830 entry.new_key.content.binary.data,
831 entry.new_key.content.binary.size);
832 }
833 break;
834 default:
835 grn_wal_set_recover_error(ctx_,
836 GRN_FUNCTION_NOT_IMPLEMENTED,
837 reinterpret_cast<grn_obj *>(dat_),
838 &entry,
839 wal_error_tag_,
840 "not implemented yet");
841 break;
842 }
843 if (ctx_->rc != GRN_SUCCESS) {
844 break;
845 }
846 grn_dat_wal_set_wal_id(ctx_, dat_, entry.id);
847 need_flush = true;
848 }
849 grn_wal_reader_close(ctx_, reader);
850 }
851
852 if (ctx_->rc != GRN_SUCCESS) {
853 auto ctx = ctx_;
854 GRN_DEFINE_NAME(dat_);
855 GRN_LOG(ctx_,
856 GRN_LOG_NOTICE,
857 "%s repair broken double array trie: <%.*s>(%u)",
858 tag_,
859 name_size,
860 name,
861 DB_OBJ(dat_)->id);
862 grn_rc repair_rc = grn_dat_repair(ctx_, dat_);
863 if (repair_rc == GRN_SUCCESS) {
864 ERRCLR(ctx_);
865 GRN_LOG(ctx_,
866 GRN_LOG_NOTICE,
867 "%s succeeded to repair broken double array trie: <%.*s>(%u)",
868 tag_,
869 name_size,
870 name,
871 DB_OBJ(dat_)->id);
872 need_flush = true;
873 } else {
874 GRN_LOG(ctx_,
875 GRN_LOG_ERROR,
876 "%s failed to recover double array trie: <%.*s>(%u): %s",
877 tag_,
878 name_size,
879 name,
880 DB_OBJ(dat_)->id,
881 ctx->errbuf);
882 }
883 }
884
885 if (need_flush && ctx_->rc == GRN_SUCCESS) {
886 grn_obj_touch(ctx_, reinterpret_cast<grn_obj *>(dat_), NULL);
887 grn_obj_flush(ctx_, reinterpret_cast<grn_obj *>(dat_));
888 }
889
890 return ctx_->rc;
891 }
892
893 private:
894 grn_ctx *ctx_;
895 grn_dat *dat_;
896 const char *wal_error_tag_;
897 const char *tag_;
898
899 // Disallows copy and assignment.
900 WALRecoverer(const WALRecoverer &);
901 WALRecoverer &
902 operator=(const WALRecoverer &);
903 };
904
905 } // namespace
906
907 extern "C" {
908
909 grn_dat *
910 grn_dat_create(
911 grn_ctx *ctx, const char *path, uint32_t, uint32_t, uint32_t flags)
912 {
913 if (path) {
914 if (path[0] == '\0') {
915 path = NULL;
916 } else if (std::strlen(path) >= (PATH_MAX - (FILE_ID_LENGTH + 1))) {
917 ERR(GRN_FILENAME_TOO_LONG, "too long path");
918 return NULL;
919 }
920 }
921
922 grn_dat *const dat = static_cast<grn_dat *>(GRN_CALLOC(sizeof(grn_dat)));
923 if (!dat) {
924 return NULL;
925 }
926 grn_dat_init(ctx, dat);
927
928 dat->io = grn_io_create(ctx,
929 path,
930 sizeof(struct grn_dat_header),
931 4096,
932 0,
933 GRN_IO_AUTO,
934 GRN_IO_EXPIRE_SEGMENT);
935 if (!dat->io) {
936 GRN_FREE(dat);
937 return NULL;
938 }
939 grn_io_set_type(dat->io, GRN_TABLE_DAT_KEY);
940
941 dat->header = static_cast<struct grn_dat_header *>(grn_io_header(dat->io));
942 if (!dat->header) {
943 grn_dat_fin(ctx, dat);
944 grn_dat_remove_file(ctx, path);
945 GRN_FREE(dat);
946 return NULL;
947 }
948 const grn_encoding encoding =
949 (ctx->encoding != GRN_ENC_DEFAULT) ? ctx->encoding : grn_gctx.encoding;
950 dat->header->flags = flags;
951 dat->header->encoding = encoding;
952 dat->header->tokenizer = GRN_ID_NIL;
953 dat->header->file_id = 0;
954 if (dat->header->flags & GRN_OBJ_KEY_NORMALIZE) {
955 dat->header->flags &= ~GRN_OBJ_KEY_NORMALIZE;
956 dat->header->normalizer = GRN_ID_NIL;
957 grn_obj *normalizer = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1);
958 grn_table_modules_add(ctx, &(dat->normalizers), normalizer);
959 } else {
960 dat->header->normalizer = GRN_ID_NIL;
961 }
962 dat->header->n_dirty_opens = 0;
963 dat->header->wal_id = 0;
964 dat->encoding = encoding;
965
966 dat->obj.header.flags = dat->header->flags;
967
968 return dat;
969 }
970
971 grn_dat *
972 grn_dat_open(grn_ctx *ctx, const char *path)
973 {
974 if (path && (std::strlen(path) >= (PATH_MAX - (FILE_ID_LENGTH + 1)))) {
975 ERR(GRN_FILENAME_TOO_LONG, "too long path");
976 return NULL;
977 }
978
979 grn_dat *const dat = static_cast<grn_dat *>(GRN_CALLOC(sizeof(grn_dat)));
980 if (!dat) {
981 return NULL;
982 }
983
984 grn_dat_init(ctx, dat);
985 dat->io = grn_io_open(ctx, path, GRN_IO_AUTO);
986 if (!dat->io) {
987 // Don't reset rc by grn_dat_fin().
988 grn_rc rc = ctx->rc;
989 grn_dat_fin(ctx, dat);
990 ctx->rc = rc;
991 GRN_FREE(dat);
992 return NULL;
993 }
994
995 dat->header = (struct grn_dat_header *)grn_io_header(dat->io);
996 if (!dat->header) {
997 grn_dat_fin(ctx, dat);
998 GRN_FREE(dat);
999 return NULL;
1000 }
1001 dat->file_id = dat->header->file_id;
1002 dat->encoding = dat->header->encoding;
1003 if (dat->header->tokenizer != GRN_ID_NIL) {
1004 grn_table_module_set_proc(ctx,
1005 &(dat->tokenizer),
1006 grn_ctx_at(ctx, dat->header->tokenizer));
1007 }
1008 if (dat->header->flags & GRN_OBJ_KEY_NORMALIZE) {
1009 dat->header->flags &= ~GRN_OBJ_KEY_NORMALIZE;
1010 dat->header->normalizer = GRN_ID_NIL;
1011 grn_obj *normalizer = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1);
1012 grn_table_modules_add(ctx, &(dat->normalizers), normalizer);
1013 } else if (dat->header->normalizer != GRN_ID_NIL) {
1014 grn_obj *normalizer = grn_ctx_at(ctx, dat->header->normalizer);
1015 grn_table_modules_add(ctx, &(dat->normalizers), normalizer);
1016 }
1017 dat->obj.header.flags = dat->header->flags;
1018 return dat;
1019 }
1020
1021 grn_rc
1022 grn_dat_close(grn_ctx *ctx, grn_dat *dat)
1023 {
1024 if (dat) {
1025 if (dat->io->path[0] != '\0' &&
1026 GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_PRIMARY) {
1027 grn_obj_flush(ctx, reinterpret_cast<grn_obj *>(dat));
1028 }
1029 grn_dat_fin(ctx, dat);
1030 GRN_FREE(dat);
1031 }
1032 return GRN_SUCCESS;
1033 }
1034
1035 grn_rc
1036 grn_dat_remove(grn_ctx *ctx, const char *path)
1037 {
1038 if (!path) {
1039 ERR(GRN_INVALID_ARGUMENT, "path is null");
1040 return GRN_INVALID_ARGUMENT;
1041 }
1042
1043 grn_dat *const dat = grn_dat_open(ctx, path);
1044 if (!dat) {
1045 return ctx->rc;
1046 }
1047 const uint32_t file_id = dat->header->file_id;
1048 grn_dat_close(ctx, dat);
1049
1050 /*
1051 grn_dat_remove() tries to remove (file_id + 1)th trie file because
1052 grn::dat::Trie::create() might leave an incomplete file on failure.
1053 */
1054 char trie_path[PATH_MAX];
1055 grn_dat_generate_trie_path(path, trie_path, file_id + 1);
1056 grn_dat_remove_file(ctx, trie_path);
1057 for (uint32_t i = file_id; i > 0; --i) {
1058 grn_dat_generate_trie_path(path, trie_path, i);
1059 if (!grn_dat_remove_file(ctx, trie_path)) {
1060 break;
1061 }
1062 }
1063
1064 /*
1065 grn_io_remove() reports an error when it fails to remove `path'.
1066 */
1067 grn_rc wal_rc = grn_wal_remove(ctx, path, "[dat]");
1068 grn_rc io_rc = grn_io_remove(ctx, path);
1069 grn_rc rc = wal_rc;
1070 if (rc == GRN_SUCCESS) {
1071 rc = io_rc;
1072 }
1073 return rc;
1074 }
1075
1076 grn_id
1077 grn_dat_get(
1078 grn_ctx *ctx, grn_dat *dat, const void *key, unsigned int key_size, void **)
1079 {
1080 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1081 return GRN_ID_NIL;
1082 }
1083 const grn::dat::Trie *const trie =
1084 static_cast<const grn::dat::Trie *>(dat->trie);
1085 if (!trie) {
1086 return GRN_ID_NIL;
1087 }
1088 grn::dat::UInt32 key_pos;
1089 try {
1090 if (trie->search(key, key_size, &key_pos)) {
1091 return trie->get_key(key_pos).id();
1092 }
1093 } catch (const grn::dat::Exception &ex) {
1094 ERR(grn_dat_translate_error_code(ex.code()),
1095 "grn::dat::Trie::search failed: %s",
1096 ex.what());
1097 }
1098 return GRN_ID_NIL;
1099 }
1100
1101 grn_id
1102 grn_dat_add(grn_ctx *ctx,
1103 grn_dat *dat,
1104 const void *key,
1105 unsigned int key_size,
1106 void **,
1107 int *added)
1108 {
1109 if (!key_size) {
1110 char name[GRN_TABLE_MAX_KEY_SIZE];
1111 int name_size;
1112 name_size = grn_dat_name(ctx, dat, name, GRN_TABLE_MAX_KEY_SIZE);
1113 ERR(GRN_INVALID_ARGUMENT,
1114 "[dat] key size must not zero: <%.*s>",
1115 name_size,
1116 name);
1117 return GRN_ID_NIL;
1118 } else if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1119 return GRN_ID_NIL;
1120 }
1121
1122 WALRecorder recorder(ctx, dat, "[dat][add]");
1123 recorder.event = GRN_WAL_EVENT_ADD_ENTRY;
1124 recorder.key = key;
1125 recorder.key_size = key_size;
1126 if (recorder.record() != GRN_SUCCESS) {
1127 return GRN_ID_NIL;
1128 }
1129
1130 grn_id id = grn_dat_add_internal(ctx, dat, key, key_size, added);
1131 if (id != GRN_ID_NIL) {
1132 recorder.apply_wal_id();
1133 }
1134 return id;
1135 }
1136
1137 int
1138 grn_dat_get_key(
1139 grn_ctx *ctx, grn_dat *dat, grn_id id, void *keybuf, int bufsize)
1140 {
1141 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1142 return 0;
1143 }
1144 const grn::dat::Trie *const trie =
1145 static_cast<const grn::dat::Trie *>(dat->trie);
1146 if (!trie) {
1147 return 0;
1148 }
1149 const grn::dat::Key &key = trie->ith_key(id);
1150 if (!key.is_valid()) {
1151 return 0;
1152 }
1153 if (keybuf && (bufsize >= (int)key.length())) {
1154 grn_memcpy(keybuf, key.ptr(), key.length());
1155 }
1156 return (int)key.length();
1157 }
1158
1159 int
1160 grn_dat_get_key2(grn_ctx *ctx, grn_dat *dat, grn_id id, grn_obj *bulk)
1161 {
1162 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1163 return 0;
1164 }
1165 const grn::dat::Trie *const trie =
1166 static_cast<const grn::dat::Trie *>(dat->trie);
1167 if (!trie) {
1168 return 0;
1169 }
1170 const grn::dat::Key &key = trie->ith_key(id);
1171 if (!key.is_valid()) {
1172 return 0;
1173 }
1174 if (bulk->header.impl_flags & GRN_OBJ_REFER) {
1175 bulk->u.b.head = static_cast<char *>(const_cast<void *>(key.ptr()));
1176 bulk->u.b.curr = bulk->u.b.head + key.length();
1177 } else {
1178 grn_bulk_write(ctx,
1179 bulk,
1180 static_cast<const char *>(key.ptr()),
1181 key.length());
1182 }
1183 return (int)key.length();
1184 }
1185
1186 grn_rc
1187 grn_dat_delete_by_id(grn_ctx *ctx,
1188 grn_dat *dat,
1189 grn_id id,
1190 grn_table_delete_optarg *optarg)
1191 {
1192 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1193 return ctx->rc;
1194 } else if (!dat->trie || (id == GRN_ID_NIL)) {
1195 return GRN_INVALID_ARGUMENT;
1196 }
1197
1198 if (optarg && optarg->func) {
1199 const grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1200 if (!trie->ith_entry(id).is_valid()) {
1201 return GRN_INVALID_ARGUMENT;
1202 } else if (!optarg->func(ctx,
1203 reinterpret_cast<grn_obj *>(dat),
1204 id,
1205 optarg->func_arg)) {
1206 return GRN_SUCCESS;
1207 }
1208 }
1209
1210 WALRecorder recorder(ctx, dat, "[dat][delete][id]");
1211 recorder.event = GRN_WAL_EVENT_DELETE_ENTRY;
1212 recorder.record_id = id;
1213 auto rc = recorder.record();
1214 if (rc != GRN_SUCCESS) {
1215 return ctx->rc;
1216 }
1217 rc = grn_dat_delete_by_id_internal(ctx, dat, id);
1218 if (rc == GRN_SUCCESS) {
1219 recorder.apply_wal_id();
1220 }
1221 return rc;
1222 }
1223
1224 grn_rc
1225 grn_dat_delete(grn_ctx *ctx,
1226 grn_dat *dat,
1227 const void *key,
1228 unsigned int key_size,
1229 grn_table_delete_optarg *optarg)
1230 {
1231 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1232 return ctx->rc;
1233 } else if (!dat->trie || !key || !key_size) {
1234 return GRN_INVALID_ARGUMENT;
1235 }
1236
1237 if (optarg && optarg->func) {
1238 try {
1239 const grn::dat::Trie *const trie =
1240 static_cast<grn::dat::Trie *>(dat->trie);
1241 grn::dat::UInt32 key_pos;
1242 if (!trie->search(key, key_size, &key_pos)) {
1243 return GRN_INVALID_ARGUMENT;
1244 } else if (!optarg->func(ctx,
1245 reinterpret_cast<grn_obj *>(dat),
1246 trie->get_key(key_pos).id(),
1247 optarg->func_arg)) {
1248 return GRN_SUCCESS;
1249 }
1250 } catch (const grn::dat::Exception &ex) {
1251 ERR(grn_dat_translate_error_code(ex.code()),
1252 "grn::dat::Trie::search failed: %s",
1253 ex.what());
1254 return ctx->rc;
1255 }
1256 }
1257
1258 WALRecorder recorder(ctx, dat, "[dat][delete][key]");
1259 recorder.event = GRN_WAL_EVENT_DELETE_ENTRY;
1260 recorder.key = key;
1261 recorder.key_size = key_size;
1262 auto rc = recorder.record();
1263 if (rc != GRN_SUCCESS) {
1264 return ctx->rc;
1265 }
1266 rc = grn_dat_delete_internal(ctx, dat, key, key_size);
1267 if (rc == GRN_SUCCESS) {
1268 recorder.apply_wal_id();
1269 }
1270 return rc;
1271 }
1272
1273 grn_rc
1274 grn_dat_update_by_id(grn_ctx *ctx,
1275 grn_dat *dat,
1276 grn_id src_key_id,
1277 const void *dest_key,
1278 unsigned int dest_key_size)
1279 {
1280 if (!dest_key_size) {
1281 return GRN_INVALID_ARGUMENT;
1282 } else if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1283 return ctx->rc;
1284 } else if (!dat->trie) {
1285 return GRN_INVALID_ARGUMENT;
1286 }
1287
1288 WALRecorder recorder(ctx, dat, "[dat][update][id]");
1289 recorder.event = GRN_WAL_EVENT_UPDATE_ENTRY;
1290 recorder.record_id = src_key_id;
1291 recorder.new_key = dest_key;
1292 recorder.new_key_size = dest_key_size;
1293 auto rc = recorder.record();
1294 if (rc != GRN_SUCCESS) {
1295 return ctx->rc;
1296 }
1297 rc = grn_dat_update_by_id_internal(ctx,
1298 dat,
1299 src_key_id,
1300 dest_key,
1301 dest_key_size);
1302 if (rc == GRN_SUCCESS) {
1303 recorder.apply_wal_id();
1304 }
1305 return rc;
1306 }
1307
1308 grn_rc
1309 grn_dat_update(grn_ctx *ctx,
1310 grn_dat *dat,
1311 const void *src_key,
1312 unsigned int src_key_size,
1313 const void *dest_key,
1314 unsigned int dest_key_size)
1315 {
1316 if (!dest_key_size) {
1317 return GRN_INVALID_ARGUMENT;
1318 } else if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1319 return ctx->rc;
1320 } else if (!dat->trie) {
1321 return GRN_INVALID_ARGUMENT;
1322 }
1323
1324 WALRecorder recorder(ctx, dat, "[dat][update][key]");
1325 recorder.event = GRN_WAL_EVENT_UPDATE_ENTRY;
1326 recorder.key = src_key;
1327 recorder.key_size = src_key_size;
1328 recorder.new_key = dest_key;
1329 recorder.new_key_size = dest_key_size;
1330 auto rc = recorder.record();
1331 if (rc != GRN_SUCCESS) {
1332 return ctx->rc;
1333 }
1334 rc = grn_dat_update_internal(ctx,
1335 dat,
1336 src_key,
1337 src_key_size,
1338 dest_key,
1339 dest_key_size);
1340 if (rc == GRN_SUCCESS) {
1341 recorder.apply_wal_id();
1342 }
1343 return rc;
1344 }
1345
1346 int
1347 grn_dat_scan(grn_ctx *ctx,
1348 grn_dat *dat,
1349 const char *str,
1350 unsigned int str_size,
1351 grn_dat_scan_hit *scan_hits,
1352 unsigned int max_num_scan_hits,
1353 const char **str_rest)
1354 {
1355 if (!grn_dat_open_trie_if_needed(ctx, dat) || !str ||
1356 !(dat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) || !scan_hits) {
1357 if (str_rest) {
1358 *str_rest = str;
1359 }
1360 return -1;
1361 }
1362
1363 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1364 if (!trie) {
1365 if (str_rest) {
1366 *str_rest = str + str_size;
1367 }
1368 return 0;
1369 }
1370
1371 if (!max_num_scan_hits || !str_size) {
1372 if (str_rest) {
1373 *str_rest = str;
1374 }
1375 return 0;
1376 }
1377
1378 unsigned int num_scan_hits = 0;
1379 try {
1380 if (GRN_BULK_VSIZE(&(dat->normalizers)) > 0) {
1381 int flags = GRN_STRING_WITH_CHECKS;
1382 grn_obj *const normalized_string =
1383 grn_string_open(ctx,
1384 str,
1385 str_size,
1386 reinterpret_cast<grn_obj *>(dat),
1387 flags);
1388 if (!normalized_string) {
1389 if (str_rest) {
1390 *str_rest = str;
1391 }
1392 return -1;
1393 }
1394 grn_string_get_normalized(ctx, normalized_string, &str, &str_size, NULL);
1395 const short *checks = grn_string_get_checks(ctx, normalized_string);
1396 unsigned int offset = 0;
1397 while (str_size) {
1398 if (*checks) {
1399 grn::dat::UInt32 key_pos;
1400 if (trie->lcp_search(str, str_size, &key_pos)) {
1401 const grn::dat::Key &key = trie->get_key(key_pos);
1402 const grn::dat::UInt32 key_length = key.length();
1403 if ((key_length == str_size) || (checks[key_length])) {
1404 unsigned int length = 0;
1405 for (grn::dat::UInt32 i = 0; i < key_length; ++i) {
1406 if (checks[i] > 0) {
1407 length += checks[i];
1408 }
1409 }
1410 scan_hits[num_scan_hits].id = key.id();
1411 scan_hits[num_scan_hits].offset = offset;
1412 scan_hits[num_scan_hits].length = length;
1413 offset += length;
1414 str += key_length;
1415 str_size -= key_length;
1416 checks += key_length;
1417 if (++num_scan_hits >= max_num_scan_hits) {
1418 break;
1419 }
1420 continue;
1421 }
1422 }
1423 if (*checks > 0) {
1424 offset += *checks;
1425 }
1426 }
1427 ++str;
1428 --str_size;
1429 ++checks;
1430 }
1431 if (str_rest) {
1432 grn_string_get_original(ctx, normalized_string, str_rest, NULL);
1433 *str_rest += offset;
1434 }
1435 grn_obj_close(ctx, normalized_string);
1436 } else {
1437 const char *const begin = str;
1438 while (str_size) {
1439 grn::dat::UInt32 key_pos;
1440 if (trie->lcp_search(str, str_size, &key_pos)) {
1441 const grn::dat::Key &key = trie->get_key(key_pos);
1442 scan_hits[num_scan_hits].id = key.id();
1443 scan_hits[num_scan_hits].offset = str - begin;
1444 scan_hits[num_scan_hits].length = key.length();
1445 str += key.length();
1446 str_size -= key.length();
1447 if (++num_scan_hits >= max_num_scan_hits) {
1448 break;
1449 }
1450 } else {
1451 const int char_length = grn_charlen(ctx, str, str + str_size);
1452 if (char_length) {
1453 str += char_length;
1454 str_size -= char_length;
1455 } else {
1456 ++str;
1457 --str_size;
1458 }
1459 }
1460 }
1461 if (str_rest) {
1462 *str_rest = str;
1463 }
1464 }
1465 } catch (const grn::dat::Exception &ex) {
1466 ERR(grn_dat_translate_error_code(ex.code()),
1467 "grn::dat::lcp_search failed: %s",
1468 ex.what());
1469 if (str_rest) {
1470 *str_rest = str;
1471 }
1472 return -1;
1473 }
1474 return static_cast<int>(num_scan_hits);
1475 }
1476
1477 grn_id
1478 grn_dat_lcp_search(grn_ctx *ctx,
1479 grn_dat *dat,
1480 const void *key,
1481 unsigned int key_size)
1482 {
1483 if (!grn_dat_open_trie_if_needed(ctx, dat) || !key ||
1484 !(dat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE)) {
1485 return GRN_ID_NIL;
1486 }
1487
1488 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1489 if (!trie) {
1490 return GRN_ID_NIL;
1491 }
1492
1493 try {
1494 grn::dat::UInt32 key_pos;
1495 if (!trie->lcp_search(key, key_size, &key_pos)) {
1496 return GRN_ID_NIL;
1497 }
1498 return trie->get_key(key_pos).id();
1499 } catch (const grn::dat::Exception &ex) {
1500 ERR(grn_dat_translate_error_code(ex.code()),
1501 "grn::dat::PrefixCursor::open failed: %s",
1502 ex.what());
1503 return GRN_ID_NIL;
1504 }
1505 }
1506
1507 unsigned int
1508 grn_dat_size(grn_ctx *ctx, grn_dat *dat)
1509 {
1510 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1511 return 0;
1512 }
1513 const grn::dat::Trie *const trie =
1514 static_cast<const grn::dat::Trie *>(dat->trie);
1515 if (trie) {
1516 return trie->num_keys();
1517 }
1518 return 0;
1519 }
1520
1521 grn_dat_cursor *
1522 grn_dat_cursor_open(grn_ctx *ctx,
1523 grn_dat *dat,
1524 const void *min,
1525 unsigned int min_size,
1526 const void *max,
1527 unsigned int max_size,
1528 int offset,
1529 int limit,
1530 int flags)
1531 {
1532 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1533 return NULL;
1534 }
1535
1536 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1537 if (!trie) {
1538 grn_dat_cursor *const dc =
1539 static_cast<grn_dat_cursor *>(GRN_CALLOC(sizeof(grn_dat_cursor)));
1540 if (dc) {
1541 grn_dat_cursor_init(ctx, dc);
1542 }
1543 return dc;
1544 }
1545
1546 grn_dat_cursor *const dc =
1547 static_cast<grn_dat_cursor *>(GRN_CALLOC(sizeof(grn_dat_cursor)));
1548 if (!dc) {
1549 return NULL;
1550 }
1551 grn_dat_cursor_init(ctx, dc);
1552
1553 try {
1554 if ((flags & GRN_CURSOR_BY_ID) != 0) {
1555 dc->cursor = grn::dat::CursorFactory::open(
1556 *trie,
1557 min,
1558 min_size,
1559 max,
1560 max_size,
1561 offset,
1562 limit,
1563 grn::dat::ID_RANGE_CURSOR |
1564 ((flags & GRN_CURSOR_DESCENDING) ? grn::dat::DESCENDING_CURSOR : 0) |
1565 ((flags & GRN_CURSOR_GT) ? grn::dat::EXCEPT_LOWER_BOUND : 0) |
1566 ((flags & GRN_CURSOR_LT) ? grn::dat::EXCEPT_UPPER_BOUND : 0));
1567 } else if ((flags & GRN_CURSOR_PREFIX) != 0) {
1568 if (max && max_size) {
1569 if ((dat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) != 0) {
1570 dc->cursor = grn::dat::CursorFactory::open(
1571 *trie,
1572 NULL,
1573 min_size,
1574 max,
1575 max_size,
1576 offset,
1577 limit,
1578 grn::dat::PREFIX_CURSOR | grn::dat::DESCENDING_CURSOR);
1579 } else {
1580 // TODO: near
1581 }
1582 } else if (min && min_size) {
1583 if ((flags & GRN_CURSOR_RK) != 0) {
1584 // TODO: rk search
1585 } else {
1586 dc->cursor = grn::dat::CursorFactory::open(
1587 *trie,
1588 min,
1589 min_size,
1590 NULL,
1591 0,
1592 offset,
1593 limit,
1594 grn::dat::PREDICTIVE_CURSOR |
1595 ((flags & GRN_CURSOR_DESCENDING) ? grn::dat::DESCENDING_CURSOR
1596 : 0) |
1597 ((flags & GRN_CURSOR_GT) ? grn::dat::EXCEPT_EXACT_MATCH : 0));
1598 }
1599 }
1600 } else {
1601 dc->cursor = grn::dat::CursorFactory::open(
1602 *trie,
1603 min,
1604 min_size,
1605 max,
1606 max_size,
1607 offset,
1608 limit,
1609 grn::dat::KEY_RANGE_CURSOR |
1610 ((flags & GRN_CURSOR_DESCENDING) ? grn::dat::DESCENDING_CURSOR : 0) |
1611 ((flags & GRN_CURSOR_GT) ? grn::dat::EXCEPT_LOWER_BOUND : 0) |
1612 ((flags & GRN_CURSOR_LT) ? grn::dat::EXCEPT_UPPER_BOUND : 0));
1613 }
1614 } catch (const grn::dat::Exception &ex) {
1615 ERR(grn_dat_translate_error_code(ex.code()),
1616 "grn::dat::CursorFactory::open failed: %s",
1617 ex.what());
1618 GRN_FREE(dc);
1619 return NULL;
1620 }
1621 if (!dc->cursor) {
1622 ERR(GRN_INVALID_ARGUMENT, "unsupported query");
1623 GRN_FREE(dc);
1624 return NULL;
1625 }
1626 dc->dat = dat;
1627 return dc;
1628 }
1629
1630 grn_id
1631 grn_dat_cursor_next(grn_ctx *ctx, grn_dat_cursor *c)
1632 {
1633 if (!c || !c->cursor) {
1634 return GRN_ID_NIL;
1635 }
1636 try {
1637 grn::dat::Cursor *const cursor = static_cast<grn::dat::Cursor *>(c->cursor);
1638 const grn::dat::Key &key = cursor->next();
1639 c->key = &key;
1640 c->curr_rec = key.is_valid() ? key.id() : GRN_ID_NIL;
1641 } catch (const grn::dat::Exception &ex) {
1642 ERR(grn_dat_translate_error_code(ex.code()),
1643 "grn::dat::Cursor::next failed: %s",
1644 ex.what());
1645 return GRN_ID_NIL;
1646 }
1647 return c->curr_rec;
1648 }
1649
1650 void
1651 grn_dat_cursor_close(grn_ctx *ctx, grn_dat_cursor *c)
1652 {
1653 if (c) {
1654 grn_dat_cursor_fin(ctx, c);
1655 GRN_FREE(c);
1656 }
1657 }
1658
1659 int
1660 grn_dat_cursor_get_key(grn_ctx *ctx, grn_dat_cursor *c, const void **key)
1661 {
1662 if (c) {
1663 const grn::dat::Key &key_ref = *static_cast<const grn::dat::Key *>(c->key);
1664 if (key_ref.is_valid()) {
1665 *key = key_ref.ptr();
1666 return (int)key_ref.length();
1667 }
1668 }
1669 return 0;
1670 }
1671
1672 grn_rc
1673 grn_dat_cursor_delete(grn_ctx *ctx,
1674 grn_dat_cursor *c,
1675 grn_table_delete_optarg *optarg)
1676 {
1677 if (!c || !c->cursor) {
1678 return GRN_INVALID_ARGUMENT;
1679 }
1680 return grn_dat_delete_by_id(ctx, c->dat, c->curr_rec, optarg);
1681 }
1682
1683 size_t
1684 grn_dat_cursor_get_max_n_records(grn_ctx *ctx, grn_dat_cursor *c)
1685 {
1686 if (!c || !c->cursor) {
1687 return 0;
1688 }
1689 grn::dat::Cursor *const cursor = static_cast<grn::dat::Cursor *>(c->cursor);
1690 size_t max_n_records = cursor->limit();
1691 if (max_n_records == grn::dat::MAX_UINT32) {
1692 return grn_dat_size(ctx, c->dat);
1693 } else {
1694 return max_n_records;
1695 }
1696 }
1697
1698 grn_id
1699 grn_dat_curr_id(grn_ctx *ctx, grn_dat *dat)
1700 {
1701 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1702 return GRN_ID_NIL;
1703 }
1704 const grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1705 if (trie) {
1706 return trie->max_key_id();
1707 }
1708 return GRN_ID_NIL;
1709 }
1710
1711 grn_rc
1712 grn_dat_truncate(grn_ctx *ctx, grn_dat *dat)
1713 {
1714 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1715 return ctx->rc;
1716 }
1717 const grn::dat::Trie *const trie =
1718 static_cast<const grn::dat::Trie *>(dat->trie);
1719 if (!trie || !trie->max_key_id()) {
1720 return GRN_SUCCESS;
1721 }
1722
1723 std::string path(grn_io_path(dat->io));
1724 char trie_path[PATH_MAX];
1725 grn_dat_generate_trie_path(path.c_str(), trie_path, dat->header->file_id + 1);
1726 try {
1727 grn::dat::Trie().create(trie_path);
1728 } catch (const grn::dat::Exception &ex) {
1729 const grn_rc error_code = grn_dat_translate_error_code(ex.code());
1730 ERR(error_code, "grn::dat::Trie::create failed: %s", ex.what());
1731 return error_code;
1732 }
1733 ++dat->header->file_id;
1734 if (!path.empty()) {
1735 grn_wal_remove(ctx, path.c_str(), "[dat]");
1736 }
1737 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1738 return ctx->rc;
1739 }
1740 return GRN_SUCCESS;
1741 }
1742
1743 const char *
1744 _grn_dat_key(grn_ctx *ctx, grn_dat *dat, grn_id id, uint32_t *key_size)
1745 {
1746 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1747 *key_size = 0;
1748 return NULL;
1749 }
1750 const grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1751 if (!trie) {
1752 *key_size = 0;
1753 return NULL;
1754 }
1755 const grn::dat::Key &key = trie->ith_key(id);
1756 if (!key.is_valid()) {
1757 *key_size = 0;
1758 return NULL;
1759 }
1760 *key_size = key.length();
1761 return static_cast<const char *>(key.ptr());
1762 }
1763
1764 grn_id
1765 grn_dat_next(grn_ctx *ctx, grn_dat *dat, grn_id id)
1766 {
1767 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1768 return GRN_ID_NIL;
1769 }
1770 const grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1771 if (!trie) {
1772 return GRN_ID_NIL;
1773 }
1774 while (id < trie->max_key_id()) {
1775 if (trie->ith_key(++id).is_valid()) {
1776 return id;
1777 }
1778 }
1779 return GRN_ID_NIL;
1780 }
1781
1782 grn_id
1783 grn_dat_at(grn_ctx *ctx, grn_dat *dat, grn_id id)
1784 {
1785 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1786 return GRN_ID_NIL;
1787 }
1788 const grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1789 if (!trie) {
1790 return GRN_ID_NIL;
1791 }
1792 const grn::dat::Key &key = trie->ith_key(id);
1793 if (!key.is_valid()) {
1794 return GRN_ID_NIL;
1795 }
1796 return id;
1797 }
1798
1799 bool
1800 grn_dat_is_changing(grn_ctx *ctx, grn_dat *dat)
1801 {
1802 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1803 return false;
1804 }
1805 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1806 if (!trie) {
1807 return false;
1808 }
1809 return trie->is_changing();
1810 }
1811
1812 grn_rc
1813 grn_dat_clear_status_flags(grn_ctx *ctx, grn_dat *dat)
1814 {
1815 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1816 return ctx->rc;
1817 }
1818 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1819 if (!trie) {
1820 return GRN_INVALID_ARGUMENT;
1821 }
1822 trie->clear_status_flags();
1823 return GRN_SUCCESS;
1824 }
1825
1826 grn_rc
1827 grn_dat_repair(grn_ctx *ctx, grn_dat *dat)
1828 {
1829 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1830 return ctx->rc;
1831 }
1832 const grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1833 if (!trie) {
1834 return GRN_INVALID_ARGUMENT;
1835 }
1836
1837 char trie_path[PATH_MAX];
1838 grn_dat_generate_trie_path(grn_io_path(dat->io),
1839 trie_path,
1840 dat->header->file_id + 1);
1841 try {
1842 grn::dat::Trie().repair(*trie, trie_path);
1843 } catch (const grn::dat::Exception &ex) {
1844 const grn_rc error_code = grn_dat_translate_error_code(ex.code());
1845 ERR(error_code, "grn::dat::Trie::create failed: %s", ex.what());
1846 return error_code;
1847 }
1848 ++dat->header->file_id;
1849 if (!grn_dat_open_trie_if_needed(ctx, dat)) {
1850 return ctx->rc;
1851 }
1852 return GRN_SUCCESS;
1853 }
1854
1855 grn_rc
1856 grn_dat_flush(grn_ctx *ctx, grn_dat *dat)
1857 {
1858 if (!dat->io) {
1859 return GRN_SUCCESS;
1860 }
1861
1862 grn_rc rc = grn_io_flush(ctx, dat->io);
1863 if (rc != GRN_SUCCESS) {
1864 return rc;
1865 }
1866
1867 if (dat->trie) {
1868 grn::dat::Trie *const trie = static_cast<grn::dat::Trie *>(dat->trie);
1869 try {
1870 trie->flush();
1871 } catch (const grn::dat::Exception &ex) {
1872 const grn_rc error_code = grn_dat_translate_error_code(ex.code());
1873 if (error_code == GRN_INPUT_OUTPUT_ERROR) {
1874 SERR("grn::dat::Trie::flush failed: %s", ex.what());
1875 } else {
1876 ERR(error_code, "grn::dat::Trie::flush failed: %s", ex.what());
1877 }
1878 return error_code;
1879 }
1880 }
1881
1882 return GRN_SUCCESS;
1883 }
1884
1885 grn_rc
1886 grn_dat_dirty(grn_ctx *ctx, grn_dat *dat)
1887 {
1888 if (!dat->io) {
1889 return GRN_SUCCESS;
1890 }
1891
1892 grn_rc rc = GRN_SUCCESS;
1893
1894 {
1895 CriticalSection critical_section(&dat->lock);
1896 if (!dat->is_dirty) {
1897 uint32_t n_dirty_opens;
1898 dat->is_dirty = true;
1899 GRN_ATOMIC_ADD_EX(&(dat->header->n_dirty_opens), 1, n_dirty_opens);
1900 rc = grn_io_flush(ctx, dat->io);
1901 }
1902 }
1903
1904 return rc;
1905 }
1906
1907 bool
1908 grn_dat_is_dirty(grn_ctx *ctx, grn_dat *dat)
1909 {
1910 if (!dat->header) {
1911 return false;
1912 }
1913
1914 return dat->header->n_dirty_opens > 0;
1915 }
1916
1917 grn_rc
1918 grn_dat_clean(grn_ctx *ctx, grn_dat *dat)
1919 {
1920 grn_rc rc = GRN_SUCCESS;
1921
1922 if (!dat->io) {
1923 return rc;
1924 }
1925
1926 {
1927 CriticalSection critical_section(&dat->lock);
1928 if (dat->is_dirty) {
1929 uint32_t n_dirty_opens;
1930 dat->is_dirty = false;
1931 GRN_ATOMIC_ADD_EX(&(dat->header->n_dirty_opens), -1, n_dirty_opens);
1932 rc = grn_io_flush(ctx, dat->io);
1933 }
1934 }
1935
1936 return rc;
1937 }
1938
1939 grn_rc
1940 grn_dat_clear_dirty(grn_ctx *ctx, grn_dat *dat)
1941 {
1942 grn_rc rc = GRN_SUCCESS;
1943
1944 if (!dat->io) {
1945 return rc;
1946 }
1947
1948 {
1949 CriticalSection critical_section(&dat->lock);
1950 dat->is_dirty = false;
1951 dat->header->n_dirty_opens = 0;
1952 rc = grn_io_flush(ctx, dat->io);
1953 }
1954
1955 return rc;
1956 }
1957
1958 bool
1959 grn_dat_is_corrupt(grn_ctx *ctx, grn_dat *dat)
1960 {
1961 if (!dat->io) {
1962 return false;
1963 }
1964
1965 {
1966 CriticalSection critical_section(&dat->lock);
1967
1968 if (grn_io_is_corrupt(ctx, dat->io)) {
1969 return true;
1970 }
1971
1972 if (dat->header->file_id == 0) {
1973 return false;
1974 }
1975
1976 char trie_path[PATH_MAX];
1977 grn_dat_generate_trie_path(grn_io_path(dat->io),
1978 trie_path,
1979 dat->header->file_id);
1980 struct stat stat;
1981 if (::stat(trie_path, &stat) != 0) {
1982 SERR("[dat][corrupt] used path doesn't exist: <%s>", trie_path);
1983 return true;
1984 }
1985 }
1986
1987 return false;
1988 }
1989
1990 size_t
1991 grn_dat_get_disk_usage(grn_ctx *ctx, grn_dat *dat)
1992 {
1993 if (!dat->io) {
1994 return 0;
1995 }
1996
1997 {
1998 CriticalSection critical_section(&dat->lock);
1999 size_t usage;
2000
2001 usage = grn_io_get_disk_usage(ctx, dat->io);
2002
2003 if (dat->header->file_id == 0) {
2004 return usage;
2005 }
2006
2007 char trie_path[PATH_MAX];
2008 grn_dat_generate_trie_path(grn_io_path(dat->io),
2009 trie_path,
2010 dat->header->file_id);
2011 struct stat stat;
2012 if (::stat(trie_path, &stat) == 0) {
2013 usage += stat.st_size;
2014 }
2015
2016 return usage;
2017 }
2018 }
2019
2020 grn_rc
2021 grn_dat_wal_recover(grn_ctx *ctx, grn_dat *dat)
2022 {
2023 WALRecoverer recoverer(ctx, dat);
2024 return recoverer.recover();
2025 }
2026
2027 grn_rc
2028 grn_dat_warm(grn_ctx *ctx, grn_dat *dat)
2029 {
2030 if (!dat->io) {
2031 return ctx->rc;
2032 }
2033
2034 grn_rc rc = grn_io_warm(ctx, dat->io);
2035 if (rc != GRN_SUCCESS) {
2036 return rc;
2037 }
2038
2039 for (uint32_t i = 1; i <= dat->header->file_id; ++i) {
2040 char trie_path[PATH_MAX];
2041 grn_dat_generate_trie_path(grn_io_path(dat->io), trie_path, i);
2042 struct stat stat;
2043 if (::stat(trie_path, &stat) != 0) {
2044 continue;
2045 }
2046 if (!grn_io_warm_path(ctx, dat->io, trie_path)) {
2047 rc = ctx->rc;
2048 break;
2049 }
2050 }
2051
2052 return rc;
2053 }
2054
2055 } // extern "C"