forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_dbm_std.cc
933 lines (817 loc) · 25.7 KB
/
tkrzw_dbm_std.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
/*************************************************************************************************
* On-memory database manager implementations with the C++ standard containers
*
* 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_sys_config.h"
#include "tkrzw_dbm.h"
#include "tkrzw_dbm_std.h"
#include "tkrzw_file.h"
#include "tkrzw_file_mmap.h"
#include "tkrzw_file_pos.h"
#include "tkrzw_file_std.h"
#include "tkrzw_file_util.h"
#include "tkrzw_lib_common.h"
#include "tkrzw_str_util.h"
namespace tkrzw {
typedef std::unordered_map<std::string, std::string> StringHashMap;
typedef std::map<std::string, std::string> StringTreeMap;
template <class STRMAP> class StdDBMIteratorImpl;
template <class STRMAP>
class StdDBMImpl {
friend class StdDBMIteratorImpl<STRMAP>;
typedef std::list<StdDBMIteratorImpl<STRMAP>*> IteratorList;
public:
StdDBMImpl(std::unique_ptr<File> file, int64_t num_buckets);
~StdDBMImpl();
Status Open(const std::string& path, bool writable, int32_t options);
Status Close();
Status Process(std::string_view key, DBM::RecordProcessor* proc, bool writable);
Status ProcessMulti(
const std::vector<std::pair<std::string_view, DBM::RecordProcessor*>>& key_proc_pairs,
bool writable);
Status ProcessEach(DBM::RecordProcessor* proc, bool writable);
Status Count(int64_t* count);
Status GetFileSize(int64_t* size);
Status GetFilePath(std::string* path);
Status Clear();
Status Rebuild();
Status ShouldBeRebuilt(bool* tobe);
Status Synchronize(bool hard, DBM::FileProcessor* proc);
void Inspect(std::vector<std::pair<std::string, std::string>>* meta);
bool IsOpen();
bool IsWritable();
std::unique_ptr<DBM> MakeDBM();
private:
void CancelIterators();
Status ImportRecords();
Status ExportRecords();
STRMAP map_;
IteratorList iterators_;
std::unique_ptr<File> file_;
bool open_;
bool writable_;
std::string path_;
SpinSharedMutex mutex_;
};
template <class STRMAP>
class StdDBMIteratorImpl {
friend class StdDBMImpl<STRMAP>;
public:
explicit StdDBMIteratorImpl(StdDBMImpl<STRMAP>* dbm);
~StdDBMIteratorImpl();
Status First();
Status Last();
Status Jump(std::string_view key);
Status JumpLower(std::string_view key, bool inclusive);
Status JumpUpper(std::string_view key, bool inclusive);
Status Next();
Status Previous();
Status Process(DBM::RecordProcessor* proc, bool writable);
private:
StdDBMImpl<STRMAP>* dbm_;
typename STRMAP::const_iterator it_;
};
template <class STRMAP>
StdDBMImpl<STRMAP>::StdDBMImpl(std::unique_ptr<File> file, int64_t num_buckets)
: file_(std::move(file)), open_(false), writable_(false), path_() {
}
template <>
StdDBMImpl<StringHashMap>::StdDBMImpl(std::unique_ptr<File> file, int64_t num_buckets)
: file_(std::move(file)), open_(false), writable_(false), path_() {
map_.rehash(num_buckets);
map_.max_load_factor(FLOATMAX);
}
template <class STRMAP>
StdDBMImpl<STRMAP>::~StdDBMImpl() {
if (open_) {
Close();
}
for (auto* iterator : iterators_) {
iterator->dbm_ = nullptr;
}
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::Open(const std::string& path, bool writable, int32_t options) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (open_) {
return Status(Status::PRECONDITION_ERROR, "opened database");
}
const std::string norm_path = NormalizePath(path);
Status status = file_->Open(norm_path, writable, options);
if (status != Status::SUCCESS) {
return status;
}
status = ImportRecords();
if (status != Status::SUCCESS) {
file_->Close();
return status;
}
open_ = true;
writable_ = writable;
path_ = norm_path;
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::Close() {
std::lock_guard<SpinSharedMutex> lock(mutex_);
if (!open_) {
return Status(Status::PRECONDITION_ERROR, "not opened database");
}
Status status(Status::SUCCESS);
if (writable_) {
status |= ExportRecords();
}
status |= file_->Close();
CancelIterators();
map_.clear();
open_ = false;
writable_ = false;
path_.clear();
return status;
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::Process(
std::string_view key, DBM::RecordProcessor* proc, bool writable) {
const std::string key_str(key);
if (writable) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
auto it = map_.find(key_str);
if (it == map_.end()) {
const std::string_view new_value = proc->ProcessEmpty(key);
if (new_value.data() != DBM::RecordProcessor::NOOP.data() &&
new_value.data() != DBM::RecordProcessor::REMOVE.data()) {
map_.emplace(std::move(key_str), new_value);
}
} else {
const std::string_view new_value = proc->ProcessFull(key, it->second);
if (new_value.data() == DBM::RecordProcessor::NOOP.data()) {
} else if (new_value.data() == DBM::RecordProcessor::REMOVE.data()) {
for (auto* iterator : iterators_) {
if (iterator->it_ == it) {
++iterator->it_;
}
}
map_.erase(it);
} else {
it->second = new_value;
}
}
} else {
std::shared_lock<SpinSharedMutex> lock(mutex_);
const auto& const_map = map_;
const auto it = const_map.find(key_str);
if (it == const_map.end()) {
proc->ProcessEmpty(key_str);
} else {
proc->ProcessFull(key_str, it->second);
}
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::ProcessMulti(
const std::vector<std::pair<std::string_view, DBM::RecordProcessor*>>& key_proc_pairs,
bool writable) {
if (writable) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
for (const auto& key_proc : key_proc_pairs) {
const std::string_view key = key_proc.first;
DBM::RecordProcessor* proc = key_proc.second;
const std::string key_str(key);
auto it = map_.find(key_str);
if (it == map_.end()) {
const std::string_view new_value = proc->ProcessEmpty(key);
if (new_value.data() != DBM::RecordProcessor::NOOP.data() &&
new_value.data() != DBM::RecordProcessor::REMOVE.data()) {
map_.emplace(std::move(key_str), new_value);
}
} else {
const std::string_view new_value = proc->ProcessFull(key, it->second);
if (new_value.data() == DBM::RecordProcessor::NOOP.data()) {
} else if (new_value.data() == DBM::RecordProcessor::REMOVE.data()) {
for (auto* iterator : iterators_) {
if (iterator->it_ == it) {
++iterator->it_;
}
}
map_.erase(it);
} else {
it->second = new_value;
}
}
}
} else {
std::shared_lock<SpinSharedMutex> lock(mutex_);
const auto& const_map = map_;
for (const auto& key_proc : key_proc_pairs) {
const std::string_view key = key_proc.first;
DBM::RecordProcessor* proc = key_proc.second;
const std::string key_str(key);
const auto it = const_map.find(key_str);
if (it == const_map.end()) {
proc->ProcessEmpty(key_str);
} else {
proc->ProcessFull(key_str, it->second);
}
}
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::ProcessEach(DBM::RecordProcessor* proc, bool writable) {
if (writable) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
proc->ProcessEmpty(DBM::RecordProcessor::NOOP);
auto it = map_.begin();
while (it != map_.end()) {
const std::string_view new_value = proc->ProcessFull(it->first, it->second);
if (new_value.data() == DBM::RecordProcessor::NOOP.data()) {
++it;
} else if (new_value.data() == DBM::RecordProcessor::REMOVE.data()) {
for (auto* iterator : iterators_) {
if (iterator->it_ == it) {
++iterator->it_;
}
}
it = map_.erase(it);
} else {
it->second = new_value;
++it;
}
}
proc->ProcessEmpty(DBM::RecordProcessor::NOOP);
} else {
std::shared_lock<SpinSharedMutex> lock(mutex_);
proc->ProcessEmpty(DBM::RecordProcessor::NOOP);
const auto& const_map = map_;
auto it = const_map.begin();
while (it != const_map.end()) {
proc->ProcessFull(it->first, it->second);
++it;
}
proc->ProcessEmpty(DBM::RecordProcessor::NOOP);
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::Count(int64_t* count) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
*count = map_.size();
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::GetFileSize(int64_t* size) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
if (!open_) {
return Status(Status::PRECONDITION_ERROR, "not opened database");
}
*size = file_->GetSizeSimple();
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::GetFilePath(std::string* path) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
if (!open_) {
return Status(Status::PRECONDITION_ERROR, "not opened database");
}
*path = path_;
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::Clear() {
std::lock_guard<SpinSharedMutex> lock(mutex_);
map_.clear();
CancelIterators();
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::Rebuild() {
std::lock_guard<SpinSharedMutex> lock(mutex_);
CancelIterators();
return Status(Status::SUCCESS);
}
template <>
Status StdDBMImpl<StringHashMap>::Rebuild() {
std::lock_guard<SpinSharedMutex> lock(mutex_);
map_.rehash(map_.size() * 2 + 1);
CancelIterators();
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::ShouldBeRebuilt(bool* tobe) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
*tobe = false;
return Status(Status::SUCCESS);
}
template <>
Status StdDBMImpl<StringHashMap>::ShouldBeRebuilt(bool* tobe) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
*tobe = map_.load_factor() > 1.0;
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::Synchronize(bool hard, DBM::FileProcessor* proc) {
std::lock_guard<SpinSharedMutex> lock(mutex_);
Status status(Status::SUCCESS);
if (open_ && writable_) {
status |= ExportRecords();
status |= file_->Synchronize(hard);
if (proc != nullptr) {
proc->Process(path_);
}
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
void StdDBMImpl<STRMAP>::Inspect(std::vector<std::pair<std::string, std::string>>* meta) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
auto Add = [&](const std::string& name, const std::string& value) {
meta->emplace_back(std::make_pair(name, value));
};
if (open_) {
Add("path", path_);
}
Add("num_records", ToString(map_.size()));
}
template <>
void StdDBMImpl<StringHashMap>::Inspect(std::vector<std::pair<std::string, std::string>>* meta) {
std::shared_lock<SpinSharedMutex> lock(mutex_);
auto Add = [&](const std::string& name, const std::string& value) {
meta->emplace_back(std::make_pair(name, value));
};
if (open_) {
Add("path", path_);
}
Add("num_records", ToString(map_.size()));
Add("num_buckets", ToString(map_.bucket_count()));
}
template <class STRMAP>
bool StdDBMImpl<STRMAP>::IsOpen() {
std::shared_lock<SpinSharedMutex> lock(mutex_);
return open_;
}
template <class STRMAP>
bool StdDBMImpl<STRMAP>::IsWritable() {
std::shared_lock<SpinSharedMutex> lock(mutex_);
return open_ && writable_;
}
template <class STRMAP>
std::unique_ptr<DBM> StdDBMImpl<STRMAP>::MakeDBM() {
std::shared_lock<SpinSharedMutex> lock(mutex_);
return std::make_unique<StdTreeDBM>();
}
template <>
std::unique_ptr<DBM> StdDBMImpl<StringHashMap>::MakeDBM() {
std::shared_lock<SpinSharedMutex> lock(mutex_);
return std::make_unique<StdHashDBM>(map_.bucket_count());
}
template <class STRMAP>
void StdDBMImpl<STRMAP>::CancelIterators() {
for (auto* iterator : iterators_) {
iterator->it_ = map_.end();
}
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::ImportRecords() {
int64_t end_offset = 0;
Status status = file_->GetSize(&end_offset);
if (status != Status::SUCCESS) {
return status;
}
FlatRecordReader reader(file_.get());
std::string key_store;
while (true) {
std::string_view key;
Status status = reader.Read(&key);
if (status != Status::SUCCESS) {
if (status != Status::NOT_FOUND_ERROR) {
return status;
}
break;
}
key_store = key;
std::string_view value;
status = reader.Read(&value);
if (status != Status::SUCCESS) {
if (status != Status::NOT_FOUND_ERROR) {
return status;
}
return Status(Status::BROKEN_DATA_ERROR, "odd number of records");
}
map_.emplace(key_store, value);
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMImpl<STRMAP>::ExportRecords() {
Status status = file_->Truncate(0);
if (status != Status::SUCCESS) {
return status;
}
FlatRecord rec(file_.get());
auto it = map_.begin();
while (it != map_.end()) {
status = rec.Write(it->first);
if (status != Status::SUCCESS) {
return status;
}
status = rec.Write(it->second);
if (status != Status::SUCCESS) {
return status;
}
++it;
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
StdDBMIteratorImpl<STRMAP>::StdDBMIteratorImpl(StdDBMImpl<STRMAP>* dbm)
: dbm_(dbm) {
std::lock_guard<SpinSharedMutex> lock(dbm_->mutex_);
dbm_->iterators_.emplace_back(this);
it_ = dbm_->map_.end();
}
template <class STRMAP>
StdDBMIteratorImpl<STRMAP>::~StdDBMIteratorImpl() {
if (dbm_ != nullptr) {
std::lock_guard<SpinSharedMutex> lock(dbm_->mutex_);
dbm_->iterators_.remove(this);
}
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::First() {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
it_ = const_map.begin();
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::Last() {
return Status(Status::NOT_IMPLEMENTED_ERROR);
}
template <>
Status StdDBMIteratorImpl<StringTreeMap>::Last() {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
it_ = const_map.end();
if (it_ != const_map.begin()) {
it_--;
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::Jump(std::string_view key) {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
it_ = const_map.find(std::string(key));
if (it_ == dbm_->map_.end()) {
return Status(Status::NOT_FOUND_ERROR);
}
return Status(Status::SUCCESS);
}
template <>
Status StdDBMIteratorImpl<StringTreeMap>::Jump(std::string_view key) {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
it_ = const_map.lower_bound(std::string(key));
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::JumpLower(std::string_view key, bool inclusive) {
return Status(Status::NOT_IMPLEMENTED_ERROR);
}
template <>
Status StdDBMIteratorImpl<StringTreeMap>::JumpLower(std::string_view key, bool inclusive) {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
it_ = const_map.lower_bound(std::string(key));
if (it_ == const_map.end()) {
if (it_ == const_map.begin()) {
return Status(Status::SUCCESS);
}
assert(!const_map.empty());
it_--;
}
while (true) {
const bool ok = inclusive ? it_->first <= key : it_->first < key;
if (ok) {
return Status(Status::SUCCESS);
}
if (it_ == const_map.begin()) {
break;
}
it_--;
}
it_ = const_map.end();
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::JumpUpper(std::string_view key, bool inclusive) {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
return Status(Status::NOT_IMPLEMENTED_ERROR);
}
template <>
Status StdDBMIteratorImpl<StringTreeMap>::JumpUpper(std::string_view key, bool inclusive) {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
it_ = inclusive ? const_map.lower_bound(std::string(key)) :
const_map.upper_bound(std::string(key));
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::Next() {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
if (it_ == const_map.end()) {
return Status(Status::NOT_FOUND_ERROR);
}
if (it_ != const_map.end()) {
++it_;
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::Previous() {
return Status(Status::NOT_IMPLEMENTED_ERROR);
}
template <>
Status StdDBMIteratorImpl<StringTreeMap>::Previous() {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
const auto& const_map = dbm_->map_;
if (it_ == const_map.end()) {
return Status(Status::NOT_FOUND_ERROR);
}
if (it_ == const_map.begin()) {
it_ = const_map.end();
} else {
--it_;
}
return Status(Status::SUCCESS);
}
template <class STRMAP>
Status StdDBMIteratorImpl<STRMAP>::Process(DBM::RecordProcessor* proc, bool writable) {
if (writable) {
std::lock_guard<SpinSharedMutex> lock(dbm_->mutex_);
if (it_ == dbm_->map_.end()) {
return Status(Status::NOT_FOUND_ERROR);
}
const std::string_view new_value = proc->ProcessFull(it_->first, it_->second);
if (new_value.data() == DBM::RecordProcessor::NOOP.data()) {
} else if (new_value.data() == DBM::RecordProcessor::REMOVE.data()) {
for (auto* iterator : dbm_->iterators_) {
if (iterator != this && iterator->it_ == it_) {
++iterator->it_;
}
}
dbm_->map_.erase(it_++);
} else {
dbm_->map_.find(it_->first)->second = new_value;
}
} else {
std::shared_lock<SpinSharedMutex> lock(dbm_->mutex_);
if (it_ == dbm_->map_.end()) {
return Status(Status::NOT_FOUND_ERROR);
}
proc->ProcessFull(it_->first, it_->second);
}
return Status(Status::SUCCESS);
}
class StdHashDBMImpl final : public StdDBMImpl<StringHashMap> {
public:
explicit StdHashDBMImpl(std::unique_ptr<File> file, int64_t num_buckets)
: StdDBMImpl<StringHashMap>(std::move(file), num_buckets) {}
};
class StdHashDBMIteratorImpl final : public StdDBMIteratorImpl<StringHashMap> {
public:
StdHashDBMIteratorImpl(StdHashDBMImpl* dbm) : StdDBMIteratorImpl<StringHashMap>(dbm) {}
};
StdHashDBM::StdHashDBM(int64_t num_buckets) {
if (num_buckets < 1) {
num_buckets = DEFAULT_NUM_BUCKETS;
}
impl_ = new StdHashDBMImpl(std::make_unique<MemoryMapParallelFile>(), num_buckets);
}
StdHashDBM::StdHashDBM(std::unique_ptr<File> file, int64_t num_buckets) {
if (num_buckets < 1) {
num_buckets = DEFAULT_NUM_BUCKETS;
}
impl_ = new StdHashDBMImpl(std::move(file), num_buckets);
}
StdHashDBM::~StdHashDBM() {
delete impl_;
}
Status StdHashDBM::Open(const std::string& path, bool writable, int32_t options) {
return impl_->Open(path, writable, options);
}
Status StdHashDBM::Close() {
return impl_->Close();
}
Status StdHashDBM::Process(std::string_view key, RecordProcessor* proc, bool writable) {
assert(proc != nullptr);
return impl_->Process(key, proc, writable);
}
Status StdHashDBM::ProcessMulti(
const std::vector<std::pair<std::string_view, DBM::RecordProcessor*>>& key_proc_pairs,
bool writable) {
return impl_->ProcessMulti(key_proc_pairs, writable);
}
Status StdHashDBM::ProcessEach(RecordProcessor* proc, bool writable) {
assert(proc != nullptr);
return impl_->ProcessEach(proc, writable);
}
Status StdHashDBM::Count(int64_t* count) {
assert(count != nullptr);
return impl_->Count(count);
}
Status StdHashDBM::GetFileSize(int64_t* size) {
assert(size != nullptr);
return impl_->GetFileSize(size);
}
Status StdHashDBM::GetFilePath(std::string* path) {
assert(path != nullptr);
return impl_->GetFilePath(path);
}
Status StdHashDBM::Clear() {
return impl_->Clear();
}
Status StdHashDBM::Rebuild() {
return impl_->Rebuild();
}
Status StdHashDBM::ShouldBeRebuilt(bool* tobe) {
assert(tobe != nullptr);
return impl_->ShouldBeRebuilt(tobe);
}
Status StdHashDBM::Synchronize(bool hard, FileProcessor* proc) {
return impl_->Synchronize(hard, proc);
}
std::vector<std::pair<std::string, std::string>> StdHashDBM::Inspect() {
std::vector<std::pair<std::string, std::string>> meta;
meta.emplace_back(std::make_pair("class", "StdHashDBM"));
impl_->Inspect(&meta);
return meta;
}
bool StdHashDBM::IsOpen() const {
return impl_->IsOpen();
}
bool StdHashDBM::IsWritable() const {
return impl_->IsWritable();
}
std::unique_ptr<DBM::Iterator> StdHashDBM::MakeIterator() {
std::unique_ptr<StdHashDBM::Iterator> iter(new StdHashDBM::Iterator(impl_));
return iter;
}
std::unique_ptr<DBM> StdHashDBM::MakeDBM() const {
return impl_->MakeDBM();
}
StdHashDBM::Iterator::Iterator(StdHashDBMImpl* dbm_impl) {
impl_ = new StdHashDBMIteratorImpl(dbm_impl);
}
StdHashDBM::Iterator::~Iterator() {
delete impl_;
}
Status StdHashDBM::Iterator::First() {
return impl_->First();
}
Status StdHashDBM::Iterator::Last() {
return impl_->Last();
}
Status StdHashDBM::Iterator::Jump(std::string_view key) {
return impl_->Jump(key);
}
Status StdHashDBM::Iterator::JumpLower(std::string_view key, bool inclusive) {
return impl_->JumpLower(key, inclusive);
}
Status StdHashDBM::Iterator::JumpUpper(std::string_view key, bool inclusive) {
return impl_->JumpUpper(key, inclusive);
}
Status StdHashDBM::Iterator::Next() {
return impl_->Next();
}
Status StdHashDBM::Iterator::Previous() {
return impl_->Previous();
}
Status StdHashDBM::Iterator::Process(RecordProcessor* proc, bool writable) {
assert(proc != nullptr);
return impl_->Process(proc, writable);
}
class StdTreeDBMImpl final : public StdDBMImpl<StringTreeMap> {
public:
explicit StdTreeDBMImpl(std::unique_ptr<File> file)
: StdDBMImpl<StringTreeMap>(std::move(file), -1) {}
};
class StdTreeDBMIteratorImpl final : public StdDBMIteratorImpl<StringTreeMap> {
public:
StdTreeDBMIteratorImpl(StdTreeDBMImpl* dbm) : StdDBMIteratorImpl<StringTreeMap>(dbm) {}
};
StdTreeDBM::StdTreeDBM() {
impl_ = new StdTreeDBMImpl(std::make_unique<MemoryMapParallelFile>());
}
StdTreeDBM::StdTreeDBM(std::unique_ptr<File> file) {
impl_ = new StdTreeDBMImpl(std::move(file));
}
StdTreeDBM::~StdTreeDBM() {
delete impl_;
}
Status StdTreeDBM::Open(const std::string& path, bool writable, int32_t options) {
return impl_->Open(path, writable, options);
}
Status StdTreeDBM::Close() {
return impl_->Close();
}
Status StdTreeDBM::Process(std::string_view key, RecordProcessor* proc, bool writable) {
assert(proc != nullptr);
return impl_->Process(key, proc, writable);
}
Status StdTreeDBM::ProcessMulti(
const std::vector<std::pair<std::string_view, DBM::RecordProcessor*>>& key_proc_pairs,
bool writable) {
return impl_->ProcessMulti(key_proc_pairs, writable);
}
Status StdTreeDBM::ProcessEach(RecordProcessor* proc, bool writable) {
assert(proc != nullptr);
return impl_->ProcessEach(proc, writable);
}
Status StdTreeDBM::Count(int64_t* count) {
assert(count != nullptr);
return impl_->Count(count);
}
Status StdTreeDBM::GetFileSize(int64_t* size) {
assert(size != nullptr);
return impl_->GetFileSize(size);
}
Status StdTreeDBM::GetFilePath(std::string* path) {
assert(path != nullptr);
return impl_->GetFilePath(path);
}
Status StdTreeDBM::Clear() {
return impl_->Clear();
}
Status StdTreeDBM::Rebuild() {
return impl_->Rebuild();
}
Status StdTreeDBM::ShouldBeRebuilt(bool* tobe) {
assert(tobe != nullptr);
return impl_->ShouldBeRebuilt(tobe);
}
Status StdTreeDBM::Synchronize(bool hard, FileProcessor* proc) {
return impl_->Synchronize(hard, proc);
}
std::vector<std::pair<std::string, std::string>> StdTreeDBM::Inspect() {
std::vector<std::pair<std::string, std::string>> meta;
meta.emplace_back(std::make_pair("class", "StdTreeDBM"));
impl_->Inspect(&meta);
return meta;
}
bool StdTreeDBM::IsOpen() const {
return impl_->IsOpen();
}
bool StdTreeDBM::IsWritable() const {
return impl_->IsWritable();
}
std::unique_ptr<DBM::Iterator> StdTreeDBM::MakeIterator() {
std::unique_ptr<StdTreeDBM::Iterator> iter(new StdTreeDBM::Iterator(impl_));
return iter;
}
std::unique_ptr<DBM> StdTreeDBM::MakeDBM() const {
return impl_->MakeDBM();
}
StdTreeDBM::Iterator::Iterator(StdTreeDBMImpl* dbm_impl) {
impl_ = new StdTreeDBMIteratorImpl(dbm_impl);
}
StdTreeDBM::Iterator::~Iterator() {
delete impl_;
}
Status StdTreeDBM::Iterator::First() {
return impl_->First();
}
Status StdTreeDBM::Iterator::Last() {
return impl_->Last();
}
Status StdTreeDBM::Iterator::Jump(std::string_view key) {
return impl_->Jump(key);
}
Status StdTreeDBM::Iterator::JumpLower(std::string_view key, bool inclusive) {
return impl_->JumpLower(key, inclusive);
}
Status StdTreeDBM::Iterator::JumpUpper(std::string_view key, bool inclusive) {
return impl_->JumpUpper(key, inclusive);
}
Status StdTreeDBM::Iterator::Next() {
return impl_->Next();
}
Status StdTreeDBM::Iterator::Previous() {
return impl_->Previous();
}
Status StdTreeDBM::Iterator::Process(RecordProcessor* proc, bool writable) {
assert(proc != nullptr);
return impl_->Process(proc, writable);
}
} // namespace tkrzw
// END OF FILE