forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_dbm.h
1421 lines (1291 loc) · 47.6 KB
/
tkrzw_dbm.h
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
/*************************************************************************************************
* Datatabase manager interface
*
* 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.
*************************************************************************************************/
#ifndef _TKRZW_DBM_H
#define _TKRZW_DBM_H
#include <functional>
#include <initializer_list>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <typeinfo>
#include <utility>
#include <vector>
#include <cinttypes>
#include "tkrzw_file.h"
#include "tkrzw_lib_common.h"
#include "tkrzw_str_util.h"
namespace tkrzw {
/**
* Interface of database manager.
*/
class DBM {
public:
/**
* Interface of processor for a record.
*/
class RecordProcessor {
public:
/**
* The special string indicating no operation.
* The uniqueness comes from the address of the data region. So, checking should be done
* like your_value.data() == NOOP.data().
*/
static const std::string_view NOOP;
/**
* The special string indicating removing operation.
* The uniqueness comes from the address of the data region. So, checking should be done
* like your_value.data() == REMOVE.data().
*/
static const std::string_view REMOVE;
/**
* Destructor.
*/
virtual ~RecordProcessor() = default;
/**
* Processes an existing record.
* @param key The key of the existing record.
* @param value The value of the existing record.
* @return A string reference to NOOP, REMOVE, or a string of a new value.
* @details The memory referred to by the return value must be alive until the end of
* the life-span of this object or until this function is called next time.
*/
virtual std::string_view ProcessFull(std::string_view key, std::string_view value) {
return NOOP;
}
/**
* Processes an empty record space.
* @param key The key specified by the caller.
* @return A string reference to NOOP, REMOVE, or the new value.
* @details The memory referred to by the return value must be alive until the end of
* the life-span of this object or until this function is called next time.
*/
virtual std::string_view ProcessEmpty(std::string_view key) {
return NOOP;
}
};
/**
* Lambda function type to process a record.
* @details The first parameter is the key of the record. The second parameter is the value
* of the existing record, or NOOP if it the record doesn't exist. The return value is a
* string reference to NOOP, REMOVE, or the new record value.
*/
typedef std::function<std::string_view(std::string_view, std::string_view)> RecordLambdaType;
/**
* Record processor to implement DBM::Process with a lambda function.
*/
class RecordProcessorLambda final : public RecordProcessor {
public:
/**
* Constructor.
* @param proc_lambda A lambda function to process a record.
*/
explicit RecordProcessorLambda(RecordLambdaType proc_lambda) : proc_lambda_(proc_lambda) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
return proc_lambda_(key, value);
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
return proc_lambda_(key, NOOP);
}
private:
// Lambda function to process a record.
RecordLambdaType proc_lambda_;
};
/**
* Record processor to implement DBM::Get.
*/
class RecordProcessorGet final : public RecordProcessor {
public:
/**
* Constructor.
* @param status The pointer to a status object to contain the result status.
* @param value The pointer to a string object to contain the result value.
*/
RecordProcessorGet(Status* status, std::string* value) : status_(status), value_(value) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (value_ != nullptr) {
*value_ = value;
}
return NOOP;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
status_->Set(Status::NOT_FOUND_ERROR);
return NOOP;
}
private:
/** Status to report. */
Status* status_;
/** Value to report. */
std::string* value_;
};
/**
* Record processor to implement DBM::Set.
*/
class RecordProcessorSet final : public RecordProcessor {
public:
/**
* Constructor.
* @param status The pointer to a status object to contain the result status.
* @param value A string of the value to set.
* @param overwrite Whether to overwrite the existing value.
* @param old_value The pointer to a string object to contain the existing value.
*/
RecordProcessorSet(Status* status, std::string_view value, bool overwrite,
std::string* old_value)
: status_(status), value_(value), overwrite_(overwrite), old_value_(old_value) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (old_value_ != nullptr) {
*old_value_ = value;
}
if (overwrite_) {
return value_;
}
status_->Set(Status::DUPLICATION_ERROR);
return NOOP;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
return value_;
}
private:
/** Status to report. */
Status* status_;
/** Value to store. */
std::string_view value_;
/** True to overwrite the existing value. */
bool overwrite_;
/** String to store the old value. */
std::string* old_value_;
};
/**
* Record processor to implement DBM::Remove.
*/
class RecordProcessorRemove final : public RecordProcessor {
public:
/**
* Constructor.
* @param status The pointer to a status object to contain the result status.
* @param old_value The pointer to a string object to contain the existing value.
*/
explicit RecordProcessorRemove(Status* status, std::string* old_value)
: status_(status), old_value_(old_value) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (old_value_ != nullptr) {
*old_value_ = value;
}
return REMOVE;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
status_->Set(Status::NOT_FOUND_ERROR);
return NOOP;
}
private:
/** Status to report. */
Status* status_;
/** String to store the old value. */
std::string* old_value_;
};
/**
* Record processor to implement DBM::Append.
*/
class RecordProcessorAppend final : public RecordProcessor {
public:
/**
* Constructor.
* @param value A string of the value to set.
* @param delim A string of the delimiter.
*/
RecordProcessorAppend(std::string_view value, std::string_view delim)
: value_(value), delim_(delim) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (delim_.empty()) {
new_value_.reserve(value.size() + value_.size());
new_value_.append(value);
new_value_.append(value_);
} else {
new_value_.reserve(value.size() + delim_.size() + value_.size());
new_value_.append(value);
new_value_.append(delim_);
new_value_.append(value_);
}
return new_value_;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
return value_;
}
private:
/** Value to store. */
std::string_view value_;
/** Delimiter after the existing value. */
std::string_view delim_;
/** The new value. */
std::string new_value_;
};
/**
* Record processor to implement DBM::CompareExchange.
*/
class RecordProcessorCompareExchange final : public DBM::RecordProcessor {
public:
/**
* Constructor.
* @param status The pointer to a status object to contain the result status.
* @param expected A string of the expected value.
* @param desired A string of the expected value.
* @param actual The pointer to a string object to contain the actual value. If it is
* nullptr, it is ignored.
*/
RecordProcessorCompareExchange(Status* status, std::string_view expected,
std::string_view desired, std::string* actual)
: status_(status), expected_(expected), desired_(desired), actual_(actual) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (actual_ != nullptr) {
*actual_ = value;
}
if (expected_.data() != nullptr && expected_ == value) {
return desired_.data() == nullptr ? REMOVE : desired_;
}
status_->Set(Status::INFEASIBLE_ERROR);
return NOOP;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
if (actual_ != nullptr) {
*actual_ = "";
}
if (expected_.data() == nullptr) {
return desired_.data() == nullptr ? NOOP : desired_;
}
status_->Set(Status::INFEASIBLE_ERROR);
return NOOP;
}
private:
/** Status to report. */
Status* status_;
/** The expected value. */
std::string_view expected_;
/** The desired value. */
std::string_view desired_;
/** Actual value to report. */
std::string* actual_;
};
/**
* Record checker to implement DBM::CompareExchangeMulti.
*/
class RecordCheckerCompareExchangeMulti final : public DBM::RecordProcessor {
public:
/**
* Constructor.
* @param noop Whether to do no operation.
* @param expected A string of the expected value.
*/
RecordCheckerCompareExchangeMulti(bool* noop, std::string_view expected)
: noop_(noop), expected_(expected) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (expected_.data() == nullptr || expected_ != value) {
*noop_ = true;
}
return NOOP;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
if (expected_.data() != nullptr) {
*noop_ = true;
}
return NOOP;
}
private:
/** Whether to do no operation. */
bool* noop_;
/** The expected value. */
std::string_view expected_;
};
/**
* Record setter to implement DBM::CompareExchangeMulti.
*/
class RecordSetterCompareExchangeMulti final : public DBM::RecordProcessor {
public:
/**
* Constructor.
* @param noop True to do no operation.
* @param desired A string of the expected value.
*/
RecordSetterCompareExchangeMulti(bool* noop, std::string_view desired)
: noop_(noop), desired_(desired) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (*noop_) {
return NOOP;
}
return desired_.data() == nullptr ? REMOVE : desired_;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
if (*noop_) {
return NOOP;
}
return desired_.data() == nullptr ? NOOP : desired_;
}
private:
/** Whether to do no operation. */
bool* noop_;
/** The desired value. */
std::string_view desired_;
};
/**
* Record processor to implement DBM::Increment.
*/
class RecordProcessorIncrement final : public DBM::RecordProcessor {
public:
/**
* Constructor.
* @param increment The incremental value.
* @param current The pointer to a string object to contain the current value.
* @param initial The initial value.
*/
RecordProcessorIncrement(int64_t increment, int64_t* current, int64_t initial)
: increment_(increment), current_(current), initial_(initial) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (increment_ == INT64MIN) {
if (current_ != nullptr) {
*current_ = StrToIntBigEndian(value);
}
return NOOP;
}
const int64_t num = StrToIntBigEndian(value) + increment_;
if (current_ != nullptr) {
*current_ = num;
}
value_ = IntToStrBigEndian(num);
return value_;
}
/**
* Processes an empty record space.
*/
std::string_view ProcessEmpty(std::string_view key) override {
if (increment_ == INT64MIN) {
if (current_ != nullptr) {
*current_ = initial_;
}
return NOOP;
}
const int64_t num = initial_ + increment_;
if (current_ != nullptr) {
*current_ = num;
}
value_ = IntToStrBigEndian(num);
return value_;
}
private:
/** The incrementing value. */
int64_t increment_;
/** The current value to report. */
int64_t* current_;
/** The initial value. */
int64_t initial_;
/** The new string value. */
std::string value_;
};
/**
* Record processor to implement DBM::Export.
*/
class RecordProcessorExport final : public RecordProcessor {
public:
/**
* Constructor.
*/
RecordProcessorExport(Status* status, DBM* dbm) : status_(status), dbm_(dbm) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
*status_ |= dbm_->Set(key, value);
return NOOP;
}
private:
/** Status to report. */
Status* status_;
/** Destination database. */
DBM* dbm_;
};
/**
* Record processor to implement DBM::Iterator::Get.
*/
class RecordProcessorIteratorGet final : public RecordProcessor {
public:
/**
* Constructor.
* @param key The pointer to a string object to contain the result key.
* @param value The pointer to a string object to contain the result value.
*/
RecordProcessorIteratorGet(std::string* key, std::string* value) : key_(key), value_(value) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
if (key_ != nullptr) {
*key_ = key;
}
if (value_ != nullptr) {
*value_ = value;
}
return NOOP;
}
private:
/** Key to report. */
std::string* key_;
/** Value to report. */
std::string* value_;
};
/**
* Record processor to implement DBM::Iterator::Set.
*/
class RecordProcessorIteratorSet final : public RecordProcessor {
public:
/**
* Constructor.
* @param value A string of the value to set.
*/
explicit RecordProcessorIteratorSet(std::string_view value) : value_(value) {}
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
return value_;
}
private:
/** Value to store. */
std::string_view value_;
};
/**
* Record processor to implement DBM::Iterator::Remove.
*/
class RecordProcessorIteratorRemove final : public RecordProcessor {
public:
/**
* Processes an existing record.
*/
std::string_view ProcessFull(std::string_view key, std::string_view value) override {
return REMOVE;
}
};
/**
* Interface of iterator for each record.
*/
class Iterator {
public:
/**
* Destructor.
*/
virtual ~Iterator() = default;
/**
* Initializes the iterator to indicate the first record.
* @return The result status.
* @details Even if there's no record, the operation doesn't fail.
*/
virtual Status First() = 0;
/**
* Initializes the iterator to indicate the last record.
* @return The result status.
* @details Even if there's no record, the operation doesn't fail. This method is suppoerted
* only by ordered databases.
*/
virtual Status Last() = 0;
/**
* Initializes the iterator to indicate a specific record.
* @param key The key of the record to look for.
* @return The result status.
* @details Ordered databases can support "lower bound" jump; If there's no record with the
* same key, the iterator refers to the first record whose key is greater than the given key.
* The operation fails with unordered databases if there's no record with the same key.
*/
virtual Status Jump(std::string_view key) = 0;
/**
* Initializes the iterator to indicate the last record whose key is lower than a given key.
* @param key The key to compare with.
* @param inclusive If true, the condition is inclusive: equal to or lower than the key.
* @return The result status.
* @details Even if there's no matching record, the operation doesn't fail. This method is
* suppoerted only by ordered databases.
*/
virtual Status JumpLower(std::string_view key, bool inclusive = false) = 0;
/**
* Initializes the iterator to indicate the first record whose key is upper than a given key.
* @param key The key to compare with.
* @param inclusive If true, the condition is inclusive: equal to or upper than the key.
* @return The result status.
* @details Even if there's no matching record, the operation doesn't fail. This method is
* suppoerted only by ordered databases.
*/
virtual Status JumpUpper(std::string_view key, bool inclusive = false) = 0;
/**
* Moves the iterator to the next record.
* @return The result status.
* @details If the current record is missing, the operation fails. Even if there's no next
* record, the operation doesn't fail.
*/
virtual Status Next() = 0;
/**
* Moves the iterator to the previous record.
* @return The result status.
* @details If the current record is missing, the operation fails. Even if there's no previous
* record, the operation doesn't fail. This method is suppoerted only by ordered databases.
*/
virtual Status Previous() = 0;
/**
* Processes the current record with a processor.
* @param proc The pointer to the processor object.
* @param writable True if the processor can edit the record.
* @return The result status.
* @details If the current record exists, the ProcessFull of the processor is called.
* Otherwise, this method fails and no method of the processor is called. If the current
* record is removed, the iterator is moved to the next record.
*/
virtual Status Process(RecordProcessor* proc, bool writable) = 0;
/**
* Processes the current record with a lambda function.
* @param rec_lambda The lambda function to process a record. The first parameter is the key
* of the record. The second parameter is the value of the existing record. The return
* value is a string reference to NOOP, REMOVE, or the new record value.
* @param writable True if the processor can edit the record.
* @return The result status.
*/
virtual Status Process(RecordLambdaType rec_lambda, bool writable) {
RecordProcessorLambda proc(rec_lambda);
return Process(&proc, writable);
}
/**
* Gets the key and the value of the current record of the iterator.
* @param key The pointer to a string object to contain the record key. If it is nullptr,
* the key data is ignored.
* @param value The pointer to a string object to contain the record value. If it is nullptr,
* the value data is ignored.
* @return The result status.
*/
virtual Status Get(std::string* key = nullptr, std::string* value = nullptr) {
RecordProcessorIteratorGet proc(key, value);
return Process(&proc, false);
}
/**
* Gets the key of the current record, in a simple way.
* @param default_value The value to be returned on failure.
* @return The key of the current record on success, or the default value on failure.
*/
virtual std::string GetKey(std::string_view default_value = "") {
std::string key;
return Get(&key, nullptr) == Status::SUCCESS ? key : std::string(default_value);
}
/**
* Gets the value of the current record, in a simple way.
* @param default_value The value to be returned on failure.
* @return The value of the current record on success, or the default value on failure.
*/
virtual std::string GetValue(std::string_view default_value = "") {
std::string value;
return Get(nullptr, &value) == Status::SUCCESS ? value : std::string(default_value);
}
/**
* Sets the value of the current record.
* @param value The value of the record.
* @return The result status.
*/
virtual Status Set(std::string_view value) {
RecordProcessorIteratorSet proc(value);
return Process(&proc, true);
}
/**
* Removes the current record.
* @return The result status.
* @details If possible, the iterator moves to the next record.
*/
virtual Status Remove() {
RecordProcessorIteratorRemove proc;
return Process(&proc, true);
}
};
/**
* Interface of processor for a record.
*/
class FileProcessor {
public:
/**
* Destructor.
*/
virtual ~FileProcessor() = default;
/**
* Process a file.
* @param path The path of the file.
*/
virtual void Process(const std::string& path) {}
};
/**
* File processor to implement DBM::CopyFileData.
*/
class FileProcessorCopyFileData : public FileProcessor {
public:
/**
* Constructor.
* @param status The pointer to a status object to contain the result status.
* @param dest_path The destination path for copying.
*/
FileProcessorCopyFileData(Status* status, const std::string dest_path);
/**
* Process a file.
* @param path The path of the file.
*/
void Process(const std::string& path) override;
private:
Status* status_;
std::string dest_path_;
};
/**
* Destructor.
*/
virtual ~DBM() = default;
/**
* Opens a database file.
* @param path A path of the file.
* @param writable If true, the file is writable. If false, it is read-only.
* @param options Bit-sum options of File::OpenOption enumss for opening the file.
* @return The result status.
*/
virtual Status Open(const std::string& path, bool writable,
int32_t options = File::OPEN_DEFAULT) = 0;
/**
* Closes the database file.
* @return The result status.
*/
virtual Status Close() = 0;
/**
* Processes a record with a processor.
* @param key The key of the record.
* @param proc The pointer to the processor object.
* @param writable True if the processor can edit the record.
* @return The result status.
* @details If the specified record exists, the ProcessFull of the processor is called.
* Otherwise, the ProcessEmpty of the processor is called.
*/
virtual Status Process(std::string_view key, RecordProcessor* proc, bool writable) = 0;
/**
* Processes a record with a lambda function.
* @param key The key of the record.
* @param rec_lambda The lambda function to process a record. The first parameter is the key
* of the record. The second parameter is the value of the existing record, or NOOP if it the
* record doesn't exist. The return value is a string reference to NOOP, REMOVE, or the new
* record value.
* @param writable True if the processor can edit the record.
* @return The result status.
*/
virtual Status Process(std::string_view key, RecordLambdaType rec_lambda, bool writable) {
RecordProcessorLambda proc(rec_lambda);
return Process(key, &proc, writable);
}
/**
* Gets the value of a record of a key.
* @param key The key of the record.
* @param value The pointer to a string object to contain the result value. If it is nullptr,
* the value data is ignored.
* @return The result status. If there's no matching record, NOT_FOUND_ERROR is returned.
*/
virtual Status Get(std::string_view key, std::string* value = nullptr) {
Status impl_status(Status::SUCCESS);
RecordProcessorGet proc(&impl_status, value);
const Status status = Process(key, &proc, false);
if (status != Status::SUCCESS) {
return status;
}
return impl_status;
}
/**
* Gets the value of a record of a key, in a simple way.
* @param key The key of the record.
* @param default_value The value to be returned on failure.
* @return The value of the matching record on success, or the default value on failure.
*/
virtual std::string GetSimple(std::string_view key, std::string_view default_value = "") {
std::string value;
return Get(key, &value) == Status::SUCCESS ? value : std::string(default_value);
}
/**
* Gets the values of multiple records of keys, with a string view vector.
* @param keys The keys of records to retrieve.
* @param records The pointer to a map to store retrieved records. Keys which don't match
* existing records are ignored.
* @return The result status. If all records of the given keys are found, SUCCESS is returned.
* If one or more records are missing, NOT_FOUND_ERROR is returned. Thus, even with an error
* code, the result map can have elements.
*/
virtual Status GetMulti(
const std::vector<std::string_view>& keys, std::map<std::string, std::string>* records) {
Status status(Status::SUCCESS);
for (const auto& key : keys) {
std::string value;
const Status tmp_status = Get(key, &value);
if (tmp_status == Status::SUCCESS) {
records->emplace(key, std::move(value));
} else {
status |= tmp_status;
}
}
return status;
}
/**
* Gets the values of multiple records of keys, with an initializer list.
* @param keys The keys of records to retrieve.
* @param records The pointer to a map to store retrieved records. Keys which don't match
* existing records are ignored.
* @return The result status. If all records of the given keys are found, SUCCESS is returned.
* If one or more records are missing, NOT_FOUND_ERROR is returned. Thus, even with an error
* code, the result map can have elements.
*/
virtual Status GetMulti(const std::initializer_list<std::string_view>& keys,
std::map<std::string, std::string>* records) {
std::vector<std::string_view> vector_keys(keys.begin(), keys.end());
return GetMulti(vector_keys, records);
}
/**
* Gets the values of multiple records of keys, with a string vector.
* @param keys The keys of records to retrieve.
* @param records The pointer to a map to store retrieved records. Keys which don't match
* existing records are ignored.
* @return The result status. If all records of the given keys are found, SUCCESS is returned.
* If one or more records are missing, NOT_FOUND_ERROR is returned. Thus, even with an error
* code, the result map can have elements.
*/
virtual Status GetMulti(
const std::vector<std::string>& keys, std::map<std::string, std::string>* records) {
return GetMulti(MakeStrViewVectorFromValues(keys), records);
}
/**
* Sets a record of a key and a value.
* @param key The key of the record.
* @param value The value of the record.
* @param overwrite Whether to overwrite the existing value if there's a record with the same
* key. If true, the existing value is overwritten by the new value. If false, the operation
* is given up and an error status is returned.
* @param old_value The pointer to a string object to contain the old value. Assignment is done
* even on the duplication error. If it is nullptr, it is ignored.
* @return The result status. If overwriting is abandoned, DUPLICATION_ERROR is returned.
*/
virtual Status Set(std::string_view key, std::string_view value, bool overwrite = true,
std::string* old_value = nullptr) {
Status impl_status(Status::SUCCESS);
RecordProcessorSet proc(&impl_status, value, overwrite, old_value);
const Status status = Process(key, &proc, true);
if (status != Status::SUCCESS) {
return status;
}
return impl_status;
}
/**
* Sets multiple records, with a map of string views.
* @param records The records to store.
* @param overwrite Whether to overwrite the existing value if there's a record with the same
* key. If true, the existing value is overwritten by the new value. If false, the operation
* is given up and an error status is returned.
* @return The result status. If there are records avoiding overwriting, DUPLICATION_ERROR
* is returned.
*/
virtual Status SetMulti(
const std::map<std::string_view, std::string_view>& records, bool overwrite = true) {
Status status(Status::SUCCESS);
for (const auto& record : records) {
status |= Set(record.first, record.second, overwrite);
if (status != Status::SUCCESS && status != Status::DUPLICATION_ERROR) {
break;
}
}
return status;
}
/**
* Sets multiple records, with an initializer list.
* @param records The records to store.
* @param overwrite Whether to overwrite the existing value if there's a record with the same
* key. If true, the existing value is overwritten by the new value. If false, the operation
* is given up and an error status is returned.
* @return The result status. If there are records avoiding overwriting, DUPLICATION_ERROR
* is returned.
*/
virtual Status SetMulti(
const std::initializer_list<std::pair<std::string_view, std::string_view>>& records,
bool overwrite = true) {
std::map<std::string_view, std::string_view> map_records;
for (const auto& record : records) {
map_records.emplace(std::pair(
std::string_view(record.first), std::string_view(record.second)));
}
return SetMulti(map_records, overwrite);
}
/**
* Sets multiple records, with a map of strings.
* @param records The records to store.
* @param overwrite Whether to overwrite the existing value if there's a record with the same
* key. If true, the existing value is overwritten by the new value. If false, the operation
* is given up and an error status is returned.
* @return The result status. If there are records avoiding overwriting, DUPLICATION_ERROR
* is returned.
*/
virtual Status SetMulti(
const std::map<std::string, std::string>& records, bool overwrite = true) {
return SetMulti(MakeStrViewMapFromRecords(records), overwrite);
}
/**
* Removes a record of a key.
* @param key The key of the record.
* @param old_value The pointer to a string object to contain the old value. If it is nullptr,
* it is ignored.
* @return The result status. If there's no matching record, NOT_FOUND_ERROR is returned.
*/
virtual Status Remove(std::string_view key, std::string* old_value = nullptr) {
Status impl_status(Status::SUCCESS);
RecordProcessorRemove proc(&impl_status, old_value);
const Status status = Process(key, &proc, true);
if (status != Status::SUCCESS) {
return status;
}
return impl_status;
}
/**
* Removes records of keys, with a string view vector.
* @param keys The keys of records to remove.
* @return The result status. If there are missing records, NOT_FOUND_ERROR is returned.
*/
virtual Status RemoveMulti(const std::vector<std::string_view>& keys) {