-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtkrzw_dbm_util.cc
2033 lines (2007 loc) · 86.9 KB
/
tkrzw_dbm_util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************************************
* Command line interface of DBM utilities
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#include "tkrzw_cmd_util.h"
namespace tkrzw {
// Prints the usage to the standard error and die.
static void PrintUsageAndDie() {
auto P = EPrintF;
const char* progname = "tkrzw_dbm_util";
P("%s: DBM utilities of Tkrzw\n", progname);
P("\n");
P("Usage:\n");
P(" %s create [common_options] [tuning_options] [options] file\n", progname);
P(" : Creates a database file.\n");
P(" %s inspect [common_options] file [attr]\n", progname);
P(" : Prints inspection of a database file.\n");
P(" %s get [common_options] file key\n", progname);
P(" : Gets a record and prints it.\n");
P(" %s set [common_options] [options] file key\n", progname);
P(" : Sets a record.\n");
P(" %s remove [common_options] file key\n", progname);
P(" : Removes a record.\n");
P(" %s rekey [common_options] file old_key new_key\n", progname);
P(" : Changes the key of a record.\n");
P(" %s list [common_options] file\n", progname);
P(" : Lists up records and prints them.\n");
P(" %s rebuild [common_options] [tuning_options] file\n", progname);
P(" : Rebuilds a database file for optimization.\n");
P(" %s restore [common_options] old_file [new_file]\n", progname);
P(" : Restores a broken database file.\n");
P(" %s merge [common_options] dest_file src_files...\n", progname);
P(" : Merges database files.\n");
P(" %s export [common_options] [options] dbm_file rec_file\n", progname);
P(" : Exports records to a flat record file.\n");
P(" %s import [common_options] [options] dbm_file rec_file\n", progname);
P(" : Imports records from a flat record file.\n");
P("\n");
P("Common options:\n");
P(" --dbm impl : The name of a DBM implementation:"
" auto, hash, tree, skip, tiny, baby, cache, stdhash, stdtree, poly, shard."
" (default: auto)\n");
P(" --file impl : The name of a file implementation:"
" mmap-para, mmap-atom, pos-para, pos-atom. (default: mmap-para)\n");
P(" --no_wait : Fails if the file is locked by another process.\n");
P(" --no_lock : Omits file locking.\n");
P(" --sync_hard : Synchronizes the file physically when closing.\n");
P(" --alloc_init num : The initial allocation size. (default: %lld)\n",
File::DEFAULT_ALLOC_INIT_SIZE);
P(" --alloc_inc num : The allocation increment factor. (default: %.1f)\n",
File::DEFAULT_ALLOC_INC_FACTOR);
P(" --block_size num : The block size of the positional access file. (default: 1)\n");
P(" --direct_io : Enables the direct I/O option of the positional access file.\n");
P(" --sync_io : Enables the synchronous I/O option of the positional access file.\n");
P(" --padding : Enables padding at the end of the file.\n");
P(" --pagecache : Enables the mini page cache in the process.\n");
P(" --multi : Calls xxxMulti methods for get, set, and remove subcommands.\n");
P("\n");
P("Options for the create subcommand:\n");
P(" --truncate : Truncates an existing database file.\n");
P("\n");
P("Options for the inspect subcommand:\n");
P(" --validate : Validates records.\n");
P("\n");
P("Options for the set subcommand:\n");
P(" --no_overwrite : Fails if there's an existing record with the same key.\n");
P(" --append str : Appends the value at the end after the given delimiter.\n");
P(" --incr num : Increments the value with the given initial value.\n");
P(" --reducer func : Sets the reducer for the skip database:"
" none, first, second, last, concat, concatnull, concattab, concatline, total."
" (default: none)\n");
P("\n");
P("Options for the rekey subcommand:\n");
P(" --no_overwrite : Fails if there's an existing record with the same key.\n");
P("\n");
P("Options for the list subcommand:\n");
P(" --move type : Type of movement:"
" first, jump, jumplower, jumplowerinc, jumpupper, jumpupperinc. (default: first)\n");
P(" --jump_key str : Specifies the jump key. (default: empty string)\n");
P(" --items num : The number of items to print. (default: 10)\n");
P(" --escape : C-style escape is applied to the TSV data.\n");
P(" --keys : Prints keys only.\n");
P("\n");
P("Options for the rebuild subcommand:\n");
P(" --restore : Skips broken records to restore a broken database.\n");
P("\n");
P("Options for the restore subcommand:\n");
P(" --auto str : The restore mode automatically done:"
" none, default, default-ns, sync, sync-ns. (default: none)\n");
P(" --end_offset : The exclusive end offset of records to read. (default: -1)\n");
P(" --class : The class name given to PolyDBM or ShardDBM.\n");
P("\n");
P("Options for the merge subcommand:\n");
P(" --reducer func : Sets the reducer for the skip database:"
" none, first, second, last, concat, concatnull, concattab, concatline, total."
" (default: none)\n");
P("\n");
P("Options for the export and import subcommands:\n");
P(" --tsv : The record file is in TSV format instead of flat record.\n");
P(" --escape : C-style escape/unescape is applied to the TSV data.\n");
P(" --keys : Exports keys only.\n");
P(" --ulog num : Uses update logs based on the timestamp.\n");
P(" --ulog_ids num num : Sets the server ID and the DBM index of update logs.\n");
P("\n");
P("Tuning options for HashDBM:\n");
P(" --in_place : Uses in-place rather than pre-defined ones.\n");
P(" --append : Uses appending rather than pre-defined ones.\n");
P(" --record_crc num : The record CRC mode: -1, 0, 8, 16, 32. (default: 0 or -1)\n");
P(" --record_comp str : The record compression mode:"
" default, none, zlib, zstd, lz4, lzma, rc4, aes. (default: none or default)\n");
P(" --offset_width num : The width to represent the offset of records. (default: %d or -1)\n",
HashDBM::DEFAULT_OFFSET_WIDTH);
P(" --align_pow num : Sets the power to align records. (default: %d or -1)\n",
HashDBM::DEFAULT_ALIGN_POW);
P(" --buckets num : Sets the number of buckets for hashing. (default: %lld or -1)\n",
HashDBM::DEFAULT_NUM_BUCKETS);
P(" --cipher_key str : Sets the encryption key for cipher compressors. (default: empty)\n");
P("\n");
P("Tuning options for TreeDBM:\n");
P(" --in_place : Uses in-place rather than pre-defined ones.\n");
P(" --append : Uses appending rather than pre-defined ones.\n");
P(" --record_crc num : The record CRC mode: -1, 0, 8, 16, 32. (default: 0 or -1)\n");
P(" --record_comp str : The record compression mode:"
" default, none, zlib, zstd, lz4, lzma, rc4, aes. (default: none or default)\n");
P(" --offset_width num : The width to represent the offset of records. (default: %d or -1)\n",
TreeDBM::DEFAULT_OFFSET_WIDTH);
P(" --align_pow num : Sets the power to align records. (default: %d or -1)\n",
TreeDBM::DEFAULT_ALIGN_POW);
P(" --buckets num : Sets the number of buckets for hashing. (default: %lld or -1)\n",
TreeDBM::DEFAULT_NUM_BUCKETS);
P(" --cipher_key str : Sets the encryption key for cipher compressors. (default: empty)\n");
P(" --max_page_size num : Sets the maximum size of a page. (default: %d or -1)\n",
TreeDBM::DEFAULT_MAX_PAGE_SIZE);
P(" --max_branches num : Sets the maximum number of branches of inner nodes."
" (default: %d or -1)\n", TreeDBM::DEFAULT_MAX_BRANCHES);
P(" --comparator func : Sets the key comparator:"
" lex, lexcase, dec, hex, real, float. (default: lex)\n");
P("\n");
P("Tuning options for SkipDBM:\n");
P(" --offset_width num : The width to represent the offset of records. (default: %d)\n",
SkipDBM::DEFAULT_OFFSET_WIDTH);
P(" --step_unit num : Sets the step unit of the skip list. (default: %d)\n",
SkipDBM::DEFAULT_STEP_UNIT);
P(" --max_level num : Sets the maximum level of the skip list. (default: %d)\n",
SkipDBM::DEFAULT_MAX_LEVEL);
P(" --sort_mem_size num : Sets the memory size used for sorting. (default: %lld)\n",
SkipDBM::DEFAULT_SORT_MEM_SIZE);
P(" --insert_in_order : Inserts records in ascending order of the key.\n");
P("\n");
P("Options for PolyDBM and ShardDBM:\n");
P(" --params str : Sets the parameters in \"key=value,key=value\" format.\n");
P("\n");
std::exit(1);
}
// Gets a DBM implemenation name.
std::string GetDBMImplName(const std::string& dbm_impl, const std::string& file_path) {
if (dbm_impl == "auto") {
const std::string ext = StrLowerCase(PathToExtension(file_path));
if (ext == "tkh") {
return "hash";
} else if (ext == "tkt") {
return "tree";
} else if (ext == "tks") {
return "skip";
} else if (ext == "tkmt" || ext == "flat") {
return "tiny";
} else if (ext == "tkmb") {
return "baby";
} else if (ext == "tkmc") {
return "cache";
} else if (ext == "tksh") {
return "stdhash";
} else if (ext == "tkst") {
return "stdtree";
} else if (!ext.empty()) {
return ext;
}
}
return dbm_impl;
}
// Makes a DBM object or die.
std::unique_ptr<DBM> MakeDBMOrDie(
const std::string& dbm_impl, const std::string& file_impl,
const std::string& file_path, int32_t alloc_init_size, double alloc_increment,
int64_t block_size, bool is_direct_io, bool is_sync_io, bool is_padding, bool is_pagecache) {
if (file_path.empty()) {
Die("The file path must be specified");
}
auto file = MakeFileOrDie(file_impl, alloc_init_size, alloc_increment);
SetAccessStrategyOrDie(
file.get(), block_size, is_direct_io, is_sync_io, is_padding, is_pagecache);
const std::string dbm_impl_mod = GetDBMImplName(dbm_impl, file_path);
std::unique_ptr<DBM> dbm;
if (dbm_impl_mod == "hash") {
dbm = std::make_unique<HashDBM>(std::move(file));
} else if (dbm_impl_mod == "tree") {
dbm = std::make_unique<TreeDBM>(std::move(file));
} else if (dbm_impl_mod == "skip") {
dbm = std::make_unique<SkipDBM>(std::move(file));
} else if (dbm_impl_mod == "tiny") {
dbm = std::make_unique<TinyDBM>(std::move(file));
} else if (dbm_impl_mod == "baby") {
dbm = std::make_unique<BabyDBM>(std::move(file));
} else if (dbm_impl_mod == "cache") {
dbm = std::make_unique<CacheDBM>(std::move(file));
} else if (dbm_impl_mod == "stdhash") {
dbm = std::make_unique<StdHashDBM>(std::move(file));
} else if (dbm_impl_mod == "stdtree") {
dbm = std::make_unique<StdTreeDBM>(std::move(file));
} else if (dbm_impl_mod == "poly") {
dbm = std::make_unique<PolyDBM>();
} else if (dbm_impl_mod == "shard") {
dbm = std::make_unique<ShardDBM>();
} else {
Die("Unknown DBM implementation: ", dbm_impl_mod);
}
return dbm;
}
// Gets a key comparator or die.
KeyComparator GetKeyComparatorOrDie(const std::string& cmp_name) {
KeyComparator comp = nullptr;
if (cmp_name == "lex") {
comp = LexicalKeyComparator;
} else if (cmp_name == "lexcase") {
comp = LexicalCaseKeyComparator;
} else if (cmp_name == "dec") {
comp = DecimalKeyComparator;
} else if (cmp_name == "hex") {
comp = HexadecimalKeyComparator;
} else if (cmp_name == "real") {
comp = RealNumberKeyComparator;
} else if (cmp_name == "float") {
comp = FloatBigEndianKeyComparator;
} else if (cmp_name == "pairlex") {
comp = PairLexicalKeyComparator;
} else if (cmp_name == "pairlexcase") {
comp = PairLexicalCaseKeyComparator;
} else if (cmp_name == "pairdec") {
comp = PairDecimalKeyComparator;
} else if (cmp_name == "pairhex") {
comp = PairHexadecimalKeyComparator;
} else if (cmp_name == "pairreal") {
comp = PairRealNumberKeyComparator;
} else if (cmp_name == "pairfloat") {
comp = PairFloatBigEndianKeyComparator;
} else {
Die("Unknown KeyComparator implementation: ", cmp_name);
}
return comp;
}
// Gets a reducer or die.
SkipDBM::ReducerType GetReducerOrDie(const std::string& reducer_name) {
SkipDBM::ReducerType reducer = nullptr;
if (reducer_name == "none") {
reducer = nullptr;
} else if (reducer_name == "first") {
reducer = SkipDBM::ReduceToFirst;
} else if (reducer_name == "second") {
reducer = SkipDBM::ReduceToSecond;
} else if (reducer_name == "last") {
reducer = SkipDBM::ReduceToLast;
} else if (reducer_name == "concat") {
reducer = SkipDBM::ReduceConcat;
} else if (reducer_name == "concatnull") {
reducer = SkipDBM::ReduceConcatWithNull;
} else if (reducer_name == "concattab") {
reducer = SkipDBM::ReduceConcatWithTab;
} else if (reducer_name == "concatline") {
reducer = SkipDBM::ReduceConcatWithLine;
} else if (reducer_name == "total") {
reducer = SkipDBM::ReduceToTotal;
} else {
Die("Unknown ReducerType implementation: ", reducer_name);
}
return reducer;
}
// Opens a database file.
bool OpenDBM(DBM* dbm, const std::string& path, bool writable, bool create, bool truncate,
bool with_no_wait, bool with_no_lock, bool with_sync_hard,
bool is_in_place, bool is_append, int32_t record_crc, const std::string& record_comp,
int32_t offset_width, int32_t align_pow, int64_t num_buckets,
const std::string cipher_key,
int32_t max_page_size, int32_t max_branches, const std::string& cmp_name,
int32_t step_unit, int32_t max_level, int64_t sort_mem_size, bool insert_in_order,
const std::string& poly_params) {
bool has_error= false;
int32_t open_options = File::OPEN_DEFAULT;
if (!create) {
open_options |= File::OPEN_NO_CREATE;
}
if (truncate) {
open_options |= File::OPEN_TRUNCATE;
}
if (with_no_wait) {
open_options |= File::OPEN_NO_WAIT;
}
if (with_no_lock) {
open_options |= File::OPEN_NO_LOCK;
}
const auto& dbm_type = dbm->GetType();
if (dbm_type == typeid(HashDBM)) {
HashDBM* hash_dbm = dynamic_cast<HashDBM*>(dbm);
tkrzw::HashDBM::TuningParameters tuning_params;
if (is_in_place) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_IN_PLACE;
} else if (is_append) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_APPENDING;
}
if (record_crc == 0) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_NONE;
} else if (record_crc == 8) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_8;
} else if (record_crc == 16) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_16;
} else if (record_crc == 32) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_32;
}
if (record_comp == "none") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_NONE;
} else if (record_comp == "zlib") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZLIB;
} else if (record_comp == "zstd") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZSTD;
} else if (record_comp == "lz4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZ4;
} else if (record_comp == "lzma") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZMA;
} else if (record_comp == "rc4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_RC4;
} else if (record_comp == "aes") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_AES;
}
tuning_params.offset_width = offset_width;
tuning_params.align_pow = align_pow;
tuning_params.num_buckets = num_buckets;
tuning_params.restore_mode = tkrzw::HashDBM::RESTORE_READ_ONLY;
tuning_params.cache_buckets = -1;
tuning_params.cipher_key = cipher_key;
const Status status = hash_dbm->OpenAdvanced(path, writable, open_options, tuning_params);
if (status != Status::SUCCESS) {
EPrintL("OpenAdvanced failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(TreeDBM)) {
TreeDBM* tree_dbm = dynamic_cast<TreeDBM*>(dbm);
tkrzw::TreeDBM::TuningParameters tuning_params;
if (is_in_place) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_IN_PLACE;
} else if (is_append) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_APPENDING;
}
if (record_crc == 0) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_NONE;
} else if (record_crc == 8) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_8;
} else if (record_crc == 16) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_16;
} else if (record_crc == 32) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_32;
}
if (record_comp == "none") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_NONE;
} else if (record_comp == "zlib") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZLIB;
} else if (record_comp == "zstd") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZSTD;
} else if (record_comp == "lz4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZ4;
} else if (record_comp == "lzma") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZMA;
} else if (record_comp == "rc4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_RC4;
} else if (record_comp == "aes") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_AES;
}
tuning_params.offset_width = offset_width;
tuning_params.align_pow = align_pow;
tuning_params.num_buckets = num_buckets;
tuning_params.restore_mode = tkrzw::HashDBM::RESTORE_READ_ONLY;
tuning_params.cache_buckets = -1;
tuning_params.cipher_key = cipher_key;
tuning_params.max_page_size = max_page_size;
tuning_params.max_branches = max_branches;
if (!cmp_name.empty()) {
tuning_params.key_comparator = GetKeyComparatorOrDie(cmp_name);
}
const Status status = tree_dbm->OpenAdvanced(path, writable, open_options, tuning_params);
if (status != Status::SUCCESS) {
EPrintL("OpenAdvanced failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(SkipDBM)) {
SkipDBM* skip_dbm = dynamic_cast<SkipDBM*>(dbm);
tkrzw::SkipDBM::TuningParameters tuning_params;
tuning_params.offset_width = offset_width;
tuning_params.step_unit = step_unit;
tuning_params.max_level = max_level;
tuning_params.restore_mode = tkrzw::SkipDBM::RESTORE_READ_ONLY;
tuning_params.sort_mem_size = sort_mem_size;
tuning_params.insert_in_order = insert_in_order;
const Status status = skip_dbm->OpenAdvanced(path, writable, open_options, tuning_params);
if (status != Status::SUCCESS) {
EPrintL("OpenAdvanced failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(TinyDBM) || dbm_type == typeid(BabyDBM) ||
dbm_type == typeid(CacheDBM) ||
dbm_type == typeid(StdHashDBM) || dbm_type == typeid(StdTreeDBM)) {
const Status status = dbm->Open(path, writable, open_options);
if (status != Status::SUCCESS) {
EPrintL("Open failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(PolyDBM) || dbm_type == typeid(ShardDBM)) {
ParamDBM* param_dbm = dynamic_cast<ParamDBM*>(dbm);
const std::map<std::string, std::string> tuning_params =
tkrzw::StrSplitIntoMap(poly_params, ",", "=");
const Status status =
param_dbm->OpenAdvanced(path, writable, open_options, tuning_params);
if (status != Status::SUCCESS) {
EPrintL("OpenAdvanced failed: ", status);
has_error = true;
}
}
return !has_error;
}
// Closes a database file
bool CloseDBM(DBM* dbm) {
bool has_error = false;
const Status status = dbm->Close();
if (status != Status::SUCCESS) {
EPrintL("Close failed: ", status);
has_error = true;
}
return !has_error;
}
// Rebuilds a database file.
bool RebuildDBM(DBM* dbm, bool is_in_place, bool is_append,
int32_t record_crc, const std::string& record_comp,
int32_t offset_width, int32_t align_pow, int64_t num_buckets,
const std::string cipher_key,
int32_t max_page_size, int32_t max_branches,
int32_t step_unit, int32_t max_level,
const std::string& poly_params, bool restore) {
bool has_error= false;
const auto& dbm_type = dbm->GetType();
if (dbm_type == typeid(HashDBM)) {
HashDBM* hash_dbm = dynamic_cast<HashDBM*>(dbm);
tkrzw::HashDBM::TuningParameters tuning_params;
if (is_in_place) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_IN_PLACE;
} else if (is_append) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_APPENDING;
}
if (record_crc == 0) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_NONE;
} else if (record_crc == 8) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_8;
} else if (record_crc == 16) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_16;
} else if (record_crc == 32) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_32;
}
if (record_comp == "none") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_NONE;
} else if (record_comp == "zlib") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZLIB;
} else if (record_comp == "zstd") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZSTD;
} else if (record_comp == "lz4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZ4;
} else if (record_comp == "lzma") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZMA;
} else if (record_comp == "rc4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_RC4;
} else if (record_comp == "aes") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_AES;
}
tuning_params.offset_width = offset_width;
tuning_params.align_pow = align_pow;
tuning_params.num_buckets = num_buckets;
tuning_params.cache_buckets = -1;
tuning_params.cipher_key = cipher_key;
const Status status = hash_dbm->RebuildAdvanced(tuning_params, restore, true);
if (status != Status::SUCCESS) {
EPrintL("RebuildAdvanced failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(TreeDBM)) {
TreeDBM* tree_dbm = dynamic_cast<TreeDBM*>(dbm);
tkrzw::TreeDBM::TuningParameters tuning_params;
if (is_in_place) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_IN_PLACE;
} else if (is_append) {
tuning_params.update_mode = tkrzw::HashDBM::UPDATE_APPENDING;
}
if (record_crc == 0) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_NONE;
} else if (record_crc == 8) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_8;
} else if (record_crc == 16) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_16;
} else if (record_crc == 32) {
tuning_params.record_crc_mode = tkrzw::HashDBM::RECORD_CRC_32;
}
if (record_comp == "none") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_NONE;
} else if (record_comp == "zlib") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZLIB;
} else if (record_comp == "zstd") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_ZSTD;
} else if (record_comp == "lz4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZ4;
} else if (record_comp == "lzma") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_LZMA;
} else if (record_comp == "rc4") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_RC4;
} else if (record_comp == "aes") {
tuning_params.record_comp_mode = tkrzw::HashDBM::RECORD_COMP_AES;
}
tuning_params.offset_width = offset_width;
tuning_params.align_pow = align_pow;
tuning_params.num_buckets = num_buckets;
tuning_params.cache_buckets = -1;
tuning_params.cipher_key = cipher_key;
tuning_params.max_page_size = max_page_size;
tuning_params.max_branches = max_branches;
const Status status = tree_dbm->RebuildAdvanced(tuning_params, restore, true);
if (status != Status::SUCCESS) {
EPrintL("RebuildAdvanced failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(SkipDBM)) {
SkipDBM* skip_dbm = dynamic_cast<SkipDBM*>(dbm);
tkrzw::SkipDBM::TuningParameters tuning_params;
tuning_params.offset_width = offset_width;
tuning_params.step_unit = step_unit;
tuning_params.max_level = max_level;
const Status status = skip_dbm->RebuildAdvanced(tuning_params, restore, true);
if (status != Status::SUCCESS) {
EPrintL("RebuildAdvanced failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(PolyDBM) || dbm_type == typeid(ShardDBM)) {
ParamDBM* param_dbm = dynamic_cast<ParamDBM*>(dbm);
const std::map<std::string, std::string> tuning_params =
tkrzw::StrSplitIntoMap(poly_params, ",", "=");
const Status status = param_dbm->RebuildAdvanced(tuning_params);
if (status != Status::SUCCESS) {
EPrintL("RebuildAdvanced failed: ", status);
has_error = true;
}
}
return !has_error;
}
// Prints metadata of a database file.
void PrintDBMMetadata(const char* label, DBM* dbm) {
PrintF("%s Number of Records: %lld\n", label, dbm->CountSimple());
PrintF("%s File Size: %lld\n", label, dbm->GetFileSizeSimple());
const auto& dbm_type = dbm->GetType();
if (dbm_type == typeid(HashDBM)) {
HashDBM* hash_dbm = dynamic_cast<HashDBM*>(dbm);
PrintF("%s Effective Data Size: %lld\n", label, hash_dbm->GetEffectiveDataSize());
PrintF("%s Number of Buckets: %lld\n", label, hash_dbm->CountBuckets());
}
if (dbm_type == typeid(TreeDBM)) {
TreeDBM* tree_dbm = dynamic_cast<TreeDBM*>(dbm);
PrintF("%s Effective Data Size: %lld\n", label, tree_dbm->GetEffectiveDataSize());
}
if (dbm_type == typeid(SkipDBM)) {
SkipDBM* skip_dbm = dynamic_cast<SkipDBM*>(dbm);
PrintF("%s Effective Data Size: %lld\n", label, skip_dbm->GetEffectiveDataSize());
}
}
// Processes the create subcommand.
static int32_t ProcessCreate(int32_t argc, const char** args) {
const std::map<std::string, int32_t>& cmd_configs = {
{"", 1}, {"--dbm", 1}, {"--file", 1}, {"--no_wait", 0}, {"--no_lock", 0}, {"--sync_hard", 0},
{"--alloc_init", 1}, {"--alloc_inc", 1},
{"--block_size", 1}, {"--direct_io", 0},
{"--sync_io", 0}, {"--padding", 0}, {"--pagecache", 0},
{"--in_place", 0}, {"--append", 0}, {"--record_crc", 1}, {"--record_comp", 1},
{"--offset_width", 1}, {"--align_pow", 1}, {"--buckets", 1}, {"--cipher_key", 1},
{"--max_page_size", 1}, {"--max_branches", 1}, {"--comparator", 1},
{"--step_unit", 1}, {"--max_level", 1},
{"--params", 1}, {"--truncate", 0},
};
std::map<std::string, std::vector<std::string>> cmd_args;
std::string cmd_error;
if (!ParseCommandArguments(argc, args, cmd_configs, &cmd_args, &cmd_error)) {
EPrint("Invalid command: ", cmd_error, "\n\n");
PrintUsageAndDie();
}
const std::string file_path = GetStringArgument(cmd_args, "", 0, "");
const std::string dbm_impl = GetStringArgument(cmd_args, "--dbm", 0, "auto");
const std::string file_impl = GetStringArgument(cmd_args, "--file", 0, "mmap-para");
const bool with_no_wait = CheckMap(cmd_args, "--no_wait");
const bool with_no_lock = CheckMap(cmd_args, "--no_lock");
const bool with_sync_hard = CheckMap(cmd_args, "--sync_hard");
const int32_t alloc_init_size = GetIntegerArgument(cmd_args, "--alloc_init", 0, -1);
const double alloc_increment = GetDoubleArgument(cmd_args, "--alloc_inc", 0, 0);
const int64_t block_size = GetIntegerArgument(cmd_args, "--block_size", 0, 1);
const bool is_direct_io = CheckMap(cmd_args, "--direct_io");
const bool is_sync_io = CheckMap(cmd_args, "--sync_io");
const bool is_padding = CheckMap(cmd_args, "--padding");
const bool is_pagecache = CheckMap(cmd_args, "--pagecache");
const bool is_in_place = CheckMap(cmd_args, "--in_place");
const bool is_append = CheckMap(cmd_args, "--append");
const int32_t record_crc = GetIntegerArgument(cmd_args, "--record_crc", 0, 0);
const std::string record_comp = GetStringArgument(cmd_args, "--record_comp", 0, "");
const int32_t offset_width = GetIntegerArgument(cmd_args, "--offset_width", 0, -1);
const int32_t align_pow = GetIntegerArgument(cmd_args, "--align_pow", 0, -1);
const int64_t num_buckets = GetIntegerArgument(cmd_args, "--buckets", 0, -1);
const std::string cipher_key = GetStringArgument(cmd_args, "--cipher_key", 0, "");
const int32_t max_page_size = GetIntegerArgument(cmd_args, "--max_page_size", 0, -1);
const int32_t max_branches = GetIntegerArgument(cmd_args, "--max_branches", 0, -1);
const std::string cmp_name = GetStringArgument(cmd_args, "--comparator", 0, "lex");
const int32_t step_unit = GetIntegerArgument(cmd_args, "--step_unit", 0, -1);
const int32_t max_level = GetIntegerArgument(cmd_args, "--max_level", 0, -1);
const std::string poly_params = GetStringArgument(cmd_args, "--params", 0, "");
const bool with_truncate = CheckMap(cmd_args, "--truncate");
if (file_path.empty()) {
Die("The file path must be specified");
}
std::unique_ptr<DBM> dbm =
MakeDBMOrDie(dbm_impl, file_impl, file_path, alloc_init_size, alloc_increment,
block_size, is_direct_io, is_sync_io, is_padding, is_pagecache);
if (!OpenDBM(dbm.get(), file_path, true, true,
with_truncate, with_no_wait, with_no_lock, with_sync_hard,
is_in_place, is_append, record_crc, record_comp,
offset_width, align_pow, num_buckets, cipher_key,
max_page_size, max_branches, cmp_name,
step_unit, max_level, -1, false,
poly_params)) {
return 1;
}
if (!CloseDBM(dbm.get())) {
return 1;
}
return 0;
}
// Processes the inspect subcommand.
static int32_t ProcessInspect(int32_t argc, const char** args) {
const std::map<std::string, int32_t>& cmd_configs = {
{"--dbm", 1}, {"--file", 1}, {"--no_wait", 0}, {"--no_lock", 0}, {"--sync_hard", 0},
{"--alloc_init", 1}, {"--alloc_inc", 1},
{"--block_size", 1}, {"--direct_io", 0},
{"--sync_io", 0}, {"--padding", 0}, {"--pagecache", 0}, {"--cipher_key", 1},
{"--validate", 0}, {"--params", 1},
};
std::map<std::string, std::vector<std::string>> cmd_args;
std::string cmd_error;
if (!ParseCommandArguments(argc, args, cmd_configs, &cmd_args, &cmd_error)) {
EPrint("Invalid command: ", cmd_error, "\n\n");
PrintUsageAndDie();
}
const std::string file_path = GetStringArgument(cmd_args, "", 0, "");
const std::string attr_name = GetStringArgument(cmd_args, "", 1, "");
const std::string dbm_impl = GetStringArgument(cmd_args, "--dbm", 0, "auto");
const std::string file_impl = GetStringArgument(cmd_args, "--file", 0, "mmap-para");
const bool with_no_wait = CheckMap(cmd_args, "--no_wait");
const bool with_no_lock = CheckMap(cmd_args, "--no_lock");
const bool with_sync_hard = CheckMap(cmd_args, "--sync_hard");
const int32_t alloc_init_size = GetIntegerArgument(cmd_args, "--alloc_init", 0, -1);
const double alloc_increment = GetDoubleArgument(cmd_args, "--alloc_inc", 0, 0);
const int64_t block_size = GetIntegerArgument(cmd_args, "--block_size", 0, 1);
const bool is_direct_io = CheckMap(cmd_args, "--direct_io");
const bool is_sync_io = CheckMap(cmd_args, "--sync_io");
const bool is_padding = CheckMap(cmd_args, "--padding");
const bool is_pagecache = CheckMap(cmd_args, "--pagecache");
const std::string cipher_key = GetStringArgument(cmd_args, "--cipher_key", 0, "");
const bool with_validate = CheckMap(cmd_args, "--validate");
const std::string poly_params = GetStringArgument(cmd_args, "--params", 0, "");
if (file_path.empty()) {
Die("The file path must be specified");
}
std::unique_ptr<DBM> dbm = MakeDBMOrDie(
dbm_impl, file_impl, file_path, alloc_init_size, alloc_increment,
block_size, is_direct_io, is_sync_io, is_padding, is_pagecache);
if (!OpenDBM(dbm.get(), file_path, false, false, false,
with_no_wait, with_no_lock, with_sync_hard,
false, false, 0, "", -1, -1, -1, cipher_key,
-1, -1, "",
-1, -1, -1, false,
poly_params)) {
return 1;
}
if (attr_name.empty()) {
PrintF("Inspection:\n");
for (const auto& meta : dbm->Inspect()) {
PrintL(StrCat(" ", meta.first, "=", meta.second));
}
PrintF("Actual File Size: %lld\n", dbm->GetFileSizeSimple());
PrintF("Number of Records: %lld\n", dbm->CountSimple());
PrintF("Healthy: %s\n", dbm->IsHealthy() ? "true" : "false");
PrintF("Should be Rebuilt: %s\n", dbm->ShouldBeRebuiltSimple() ? "true" : "false");
} else {
for (const auto& meta : dbm->Inspect()) {
if (meta.first == attr_name) {
PrintL(meta.second);
}
}
}
bool has_error = false;
if (with_validate) {
const auto& dbm_type = dbm->GetType();
if (dbm_type == typeid(HashDBM)) {
HashDBM* hash_dbm = dynamic_cast<HashDBM*>(dbm.get());
Print("Validating hash buckets: ... ");
double start_time = GetWallTime();
Status status = hash_dbm->ValidateHashBuckets();
double end_time = GetWallTime();
if (status == Status::SUCCESS) {
PrintF("ok (elapsed=%.6f)\n", end_time - start_time);
} else {
PrintF("failed (elapsed=%.6f)\n", end_time - start_time);
EPrintL("ValidateRecords failed: ", status);
has_error = true;
}
Print("Validating records: ... ");
start_time = GetWallTime();
status = hash_dbm->ValidateRecords(-1, -1);
end_time = GetWallTime();
if (status == Status::SUCCESS) {
PrintF("ok (elapsed=%.6f)\n", end_time - start_time);
} else {
PrintF("failed (elapsed=%.6f)\n", end_time - start_time);
EPrintL("ValidateRecords failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(TreeDBM)) {
TreeDBM* tree_dbm = dynamic_cast<TreeDBM*>(dbm.get());
Print("Validating hash buckets: ... ");
double start_time = GetWallTime();
Status status = tree_dbm->ValidateHashBuckets();
double end_time = GetWallTime();
if (status == Status::SUCCESS) {
PrintF("ok (elapsed=%.6f)\n", end_time - start_time);
} else {
PrintF("failed (elapsed=%.6f)\n", end_time - start_time);
EPrintL("ValidateRecords failed: ", status);
has_error = true;
}
Print("Validating records: ... ");
start_time = GetWallTime();
status = tree_dbm->ValidateRecords(-1, -1);
end_time = GetWallTime();
if (status == Status::SUCCESS) {
PrintF("ok (elapsed=%.6f)\n", end_time - start_time);
} else {
PrintF("failed (elapsed=%.6f)\n", end_time - start_time);
EPrintL("ValidateRecords failed: ", status);
has_error = true;
}
}
if (dbm_type == typeid(SkipDBM)) {
SkipDBM* skip_dbm = dynamic_cast<SkipDBM*>(dbm.get());
Print("Validating records: ... ");
double start_time = GetWallTime();
const Status status = skip_dbm->ValidateRecords();
double end_time = GetWallTime();
if (status == Status::SUCCESS) {
PrintF("ok (elapsed=%.6f)\n", end_time - start_time);
} else {
PrintF("failed (elapsed=%.6f)\n", end_time - start_time);
EPrintL("ValidateRecords failed: ", status);
has_error = true;
}
}
}
if (!CloseDBM(dbm.get())) {
return 1;
}
return has_error ? 1 : 0;
}
// Processes the get subcommand.
static int32_t ProcessGet(int32_t argc, const char** args) {
const std::map<std::string, int32_t>& cmd_configs = {
{"--dbm", 1}, {"--file", 1}, {"--no_wait", 0}, {"--no_lock", 0}, {"--sync_hard", 0},
{"--alloc_init", 1}, {"--alloc_inc", 1},
{"--block_size", 1}, {"--direct_io", 0},
{"--sync_io", 0}, {"--padding", 0}, {"--pagecache", 0}, {"--cipher_key", 1},
{"--multi", 0}, {"--params", 1},
};
std::map<std::string, std::vector<std::string>> cmd_args;
std::string cmd_error;
if (!ParseCommandArguments(argc, args, cmd_configs, &cmd_args, &cmd_error)) {
EPrint("Invalid command: ", cmd_error, "\n\n");
PrintUsageAndDie();
}
const std::string file_path = GetStringArgument(cmd_args, "", 0, "");
const std::string key = GetStringArgument(cmd_args, "", 1, "");
const std::string dbm_impl = GetStringArgument(cmd_args, "--dbm", 0, "auto");
const std::string file_impl = GetStringArgument(cmd_args, "--file", 0, "mmap-para");
const bool with_no_wait = CheckMap(cmd_args, "--no_wait");
const bool with_no_lock = CheckMap(cmd_args, "--no_lock");
const bool with_sync_hard = CheckMap(cmd_args, "--sync_hard");
const int32_t alloc_init_size = GetIntegerArgument(cmd_args, "--alloc_init", 0, -1);
const double alloc_increment = GetDoubleArgument(cmd_args, "--alloc_inc", 0, 0);
const int64_t block_size = GetIntegerArgument(cmd_args, "--block_size", 0, 1);
const bool is_direct_io = CheckMap(cmd_args, "--direct_io");
const bool is_sync_io = CheckMap(cmd_args, "--sync_io");
const bool is_padding = CheckMap(cmd_args, "--padding");
const bool is_pagecache = CheckMap(cmd_args, "--pagecache");
const std::string cipher_key = GetStringArgument(cmd_args, "--cipher_key", 0, "");
const bool is_multi = CheckMap(cmd_args, "--multi");
const std::string poly_params = GetStringArgument(cmd_args, "--params", 0, "");
if (file_path.empty()) {
Die("The file path must be specified");
}
if (!is_multi && cmd_args[""].size() != 2) {
Die("The key must be specified");
}
std::unique_ptr<DBM> dbm =
MakeDBMOrDie(dbm_impl, file_impl, file_path, alloc_init_size, alloc_increment,
block_size, is_direct_io, is_sync_io, is_padding, is_pagecache);
if (!OpenDBM(dbm.get(), file_path, false, false, false,
with_no_wait, with_no_lock, with_sync_hard,
false, false, 0, "", -1, -1, -1, cipher_key,
-1, -1, "",
-1, -1, -1, false,
poly_params)) {
return 1;
}
bool ok = false;
if (is_multi) {
std::vector<std::string_view> keys;
const auto& rec_args = cmd_args[""];
for (int32_t i = 1; i < static_cast<int32_t>(rec_args.size()); i++) {
keys.emplace_back(rec_args[i]);
}
std::map<std::string, std::string> records;
const Status status = dbm->GetMulti(keys, &records);
if (status == Status::SUCCESS || status == Status::NOT_FOUND_ERROR) {
for (const auto& record : records) {
PrintL(record.first, "\t", record.second);
}
ok = true;
} else {
EPrintL("GetMulti failed: ", status);
}
} else {
std::string value;
const Status status = dbm->Get(key, &value);
if (status == Status::SUCCESS) {
PrintL(value);
ok = true;
} else {
EPrintL("Get failed: ", status);
}
}
if (!CloseDBM(dbm.get())) {
return 1;
}
return ok ? 0 : 1;
}
// Processes the set subcommand.
static int32_t ProcessSet(int32_t argc, const char** args) {
const std::map<std::string, int32_t>& cmd_configs = {
{"--dbm", 1}, {"--file", 1}, {"--no_wait", 0}, {"--no_lock", 0}, {"--sync_hard", 0},
{"--alloc_init", 1}, {"--alloc_inc", 1},
{"--block_size", 1}, {"--direct_io", 0},
{"--sync_io", 0}, {"--padding", 0}, {"--pagecache", 0}, {"--cipher_key", 1},
{"--multi", 0}, {"--no_overwrite", 0}, {"--append", 1}, {"--incr", 1}, {"--reducer", 1},
{"--params", 1},
};
std::map<std::string, std::vector<std::string>> cmd_args;
std::string cmd_error;
if (!ParseCommandArguments(argc, args, cmd_configs, &cmd_args, &cmd_error)) {
EPrint("Invalid command: ", cmd_error, "\n\n");
PrintUsageAndDie();
}
const std::string file_path = GetStringArgument(cmd_args, "", 0, "");
const std::string key = GetStringArgument(cmd_args, "", 1, "");
const std::string value = GetStringArgument(cmd_args, "", 2, "");
const std::string dbm_impl = GetStringArgument(cmd_args, "--dbm", 0, "auto");
const std::string file_impl = GetStringArgument(cmd_args, "--file", 0, "mmap-para");
const bool with_no_wait = CheckMap(cmd_args, "--no_wait");
const bool with_no_lock = CheckMap(cmd_args, "--no_lock");
const bool with_sync_hard = CheckMap(cmd_args, "--sync_hard");
const int32_t alloc_init_size = GetIntegerArgument(cmd_args, "--alloc_init", 0, -1);
const double alloc_increment = GetDoubleArgument(cmd_args, "--alloc_inc", 0, 0);
const int64_t block_size = GetIntegerArgument(cmd_args, "--block_size", 0, 1);
const bool is_direct_io = CheckMap(cmd_args, "--direct_io");
const bool is_sync_io = CheckMap(cmd_args, "--sync_io");
const bool is_padding = CheckMap(cmd_args, "--padding");
const bool is_pagecache = CheckMap(cmd_args, "--pagecache");
const std::string cipher_key = GetStringArgument(cmd_args, "--cipher_key", 0, "");
const bool is_multi = CheckMap(cmd_args, "--multi");
const bool with_no_overwrite = CheckMap(cmd_args, "--no_overwrite");
const std::string append_delim =
GetStringArgument(cmd_args, "--append", 0, SkipDBM::REMOVING_VALUE);
const int64_t incr_init = GetIntegerArgument(cmd_args, "--incr", 0, INT64MIN);
const std::string reducer_name = GetStringArgument(cmd_args, "--reducer", 0, "none");
const std::string poly_params = GetStringArgument(cmd_args, "--params", 0, "");
if (file_path.empty()) {
Die("The file path must be specified");
}
if (!is_multi && cmd_args[""].size() != 3) {
Die("The key and the value must be specified");
}
std::unique_ptr<DBM> dbm =
MakeDBMOrDie(dbm_impl, file_impl, file_path, alloc_init_size, alloc_increment,
block_size, is_direct_io, is_sync_io, is_padding, is_pagecache);
if (!OpenDBM(dbm.get(), file_path, true, false, false,
with_no_wait, with_no_lock, with_sync_hard,
false, false, 0, "", -1, -1, -1, cipher_key,
-1, -1, "",
-1, -1, -1, false,
poly_params)) {
return 1;
}
bool ok = false;
if (incr_init != INT64MIN) {
int64_t current = 0;
const Status status = dbm->Increment(key, StrToInt(value), ¤t, incr_init);
if (status == Status::SUCCESS) {
PrintL(current);
ok = true;
} else {
EPrintL("Increment failed: ", status);
}
} else if (append_delim != SkipDBM::REMOVING_VALUE) {
if (is_multi) {
std::map<std::string_view, std::string_view> records;
const auto& rec_args = cmd_args[""];
for (int32_t i = 1; i < static_cast<int32_t>(rec_args.size()) - 1; i += 2) {
records.emplace(rec_args[i], rec_args[i + 1]);
}
const Status status = dbm->AppendMulti(records, append_delim);
if (status == Status::SUCCESS) {
ok = true;
} else {
EPrintL("AppendMulti failed: ", status);
}
} else {
const Status status = dbm->Append(key, value, append_delim);
if (status == Status::SUCCESS) {
ok = true;
} else {
EPrintL("Append failed: ", status);
}
}
} else {
if (is_multi) {
std::map<std::string_view, std::string_view> records;
const auto& rec_args = cmd_args[""];
for (int32_t i = 1; i < static_cast<int32_t>(rec_args.size()) - 1; i += 2) {
records.emplace(rec_args[i], rec_args[i + 1]);
}
const Status status = dbm->SetMulti(records, !with_no_overwrite);
if (status == Status::SUCCESS) {
ok = true;
} else {
EPrintL("SetMulti failed: ", status);
}
} else {
const Status status = dbm->Set(key, value, !with_no_overwrite);
if (status == Status::SUCCESS) {
ok = true;
} else {
EPrintL("Set failed: ", status);
}
}
}
const auto& dbm_type = dbm->GetType();
if (dbm_type == typeid(SkipDBM)) {
SkipDBM* skip_dbm = dynamic_cast<SkipDBM*>(dbm.get());
const Status status = skip_dbm->SynchronizeAdvanced(