/
RocksDBStore.cc
773 lines (709 loc) · 21.6 KB
/
RocksDBStore.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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <set>
#include <map>
#include <string>
#include <memory>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "rocksdb/db.h"
#include "rocksdb/table.h"
#include "rocksdb/env.h"
#include "rocksdb/write_batch.h"
#include "rocksdb/slice.h"
#include "rocksdb/cache.h"
#include "rocksdb/filter_policy.h"
#include "rocksdb/utilities/convenience.h"
#include "rocksdb/merge_operator.h"
using std::string;
#include "common/perf_counters.h"
#include "common/debug.h"
#include "include/str_list.h"
#include "include/str_map.h"
#include "KeyValueDB.h"
#include "RocksDBStore.h"
#include "common/debug.h"
#define dout_subsys ceph_subsys_rocksdb
#undef dout_prefix
#define dout_prefix *_dout << "rocksdb: "
//
// One of these per rocksdb instance, implements the merge operator prefix stuff
//
class RocksDBStore::MergeOperatorRouter : public rocksdb::AssociativeMergeOperator {
RocksDBStore& store;
public:
const char *Name() const {
// Construct a name that rocksDB will validate against. We want to
// do this in a way that doesn't constrain the ordering of calls
// to set_merge_operator, so sort the merge operators and then
// construct a name from all of those parts.
store.assoc_name.clear();
map<std::string,std::string> names;
for (auto& p : store.merge_ops) names[p.first] = p.second->name();
for (auto& p : names) {
store.assoc_name += '.';
store.assoc_name += p.first;
store.assoc_name += ':';
store.assoc_name += p.second;
}
return store.assoc_name.c_str();
}
MergeOperatorRouter(RocksDBStore &_store) : store(_store) {}
virtual bool Merge(const rocksdb::Slice& key,
const rocksdb::Slice* existing_value,
const rocksdb::Slice& value,
std::string* new_value,
rocksdb::Logger* logger) const {
// Check each prefix
for (auto& p : store.merge_ops) {
if (p.first.compare(0, p.first.length(),
key.data(), p.first.length()) == 0 &&
key.data()[p.first.length()] == 0) {
if (existing_value) {
p.second->merge(existing_value->data(), existing_value->size(),
value.data(), value.size(),
new_value);
} else {
p.second->merge_nonexistent(value.data(), value.size(), new_value);
}
break;
}
}
return true; // OK :)
}
};
int RocksDBStore::set_merge_operator(
const string& prefix,
std::shared_ptr<KeyValueDB::MergeOperator> mop)
{
// If you fail here, it's because you can't do this on an open database
assert(db == nullptr);
merge_ops.push_back(std::make_pair(prefix,mop));
return 0;
}
class CephRocksdbLogger : public rocksdb::Logger {
CephContext *cct;
public:
explicit CephRocksdbLogger(CephContext *c) : cct(c) {
cct->get();
}
~CephRocksdbLogger() {
cct->put();
}
// Write an entry to the log file with the specified format.
void Logv(const char* format, va_list ap) {
Logv(rocksdb::INFO_LEVEL, format, ap);
}
// Write an entry to the log file with the specified log level
// and format. Any log with level under the internal log level
// of *this (see @SetInfoLogLevel and @GetInfoLogLevel) will not be
// printed.
void Logv(const rocksdb::InfoLogLevel log_level, const char* format,
va_list ap) {
int v = rocksdb::NUM_INFO_LOG_LEVELS - log_level - 1;
dout(v);
char buf[65536];
vsnprintf(buf, sizeof(buf), format, ap);
*_dout << buf << dendl;
}
};
rocksdb::Logger *create_rocksdb_ceph_logger()
{
return new CephRocksdbLogger(g_ceph_context);
}
int string2bool(string val, bool &b_val)
{
if (strcasecmp(val.c_str(), "false") == 0) {
b_val = false;
return 0;
} else if (strcasecmp(val.c_str(), "true") == 0) {
b_val = true;
return 0;
} else {
std::string err;
int b = strict_strtol(val.c_str(), 10, &err);
if (!err.empty())
return -EINVAL;
b_val = !!b;
return 0;
}
}
int RocksDBStore::tryInterpret(const string key, const string val, rocksdb::Options &opt)
{
if (key == "compaction_threads") {
std::string err;
int f = strict_sistrtoll(val.c_str(), &err);
if (!err.empty())
return -EINVAL;
//Low priority threadpool is used for compaction
opt.env->SetBackgroundThreads(f, rocksdb::Env::Priority::LOW);
} else if (key == "flusher_threads") {
std::string err;
int f = strict_sistrtoll(val.c_str(), &err);
if (!err.empty())
return -EINVAL;
//High priority threadpool is used for flusher
opt.env->SetBackgroundThreads(f, rocksdb::Env::Priority::HIGH);
} else if (key == "compact_on_mount") {
int ret = string2bool(val, compact_on_mount);
if (ret != 0)
return ret;
} else if (key == "disableWAL") {
int ret = string2bool(val, disableWAL);
if (ret != 0)
return ret;
} else {
//unrecognize config options.
return -EINVAL;
}
return 0;
}
int RocksDBStore::ParseOptionsFromString(const string opt_str, rocksdb::Options &opt)
{
map<string, string> str_map;
int r = get_str_map(opt_str, &str_map, ",\n;");
if (r < 0)
return r;
map<string, string>::iterator it;
for(it = str_map.begin(); it != str_map.end(); ++it) {
string this_opt = it->first + "=" + it->second;
rocksdb::Status status = rocksdb::GetOptionsFromString(opt, this_opt , &opt);
if (!status.ok()) {
//unrecognized by rocksdb, try to interpret by ourselves.
r = tryInterpret(it->first, it->second, opt);
if (r < 0) {
derr << status.ToString() << dendl;
return -EINVAL;
}
}
lgeneric_dout(cct, 0) << " set rocksdb option " << it->first
<< " = " << it->second << dendl;
}
return 0;
}
int RocksDBStore::init(string _options_str)
{
options_str = _options_str;
rocksdb::Options opt;
//try parse options
if (options_str.length()) {
int r = ParseOptionsFromString(options_str, opt);
if (r != 0) {
return -EINVAL;
}
}
return 0;
}
int RocksDBStore::create_and_open(ostream &out)
{
if (env) {
unique_ptr<rocksdb::Directory> dir;
env->NewDirectory(path, &dir);
} else {
int r = ::mkdir(path.c_str(), 0755);
if (r < 0)
r = -errno;
if (r < 0 && r != -EEXIST) {
derr << __func__ << " failed to create " << path << ": " << cpp_strerror(r)
<< dendl;
return r;
}
}
return do_open(out, true);
}
int RocksDBStore::do_open(ostream &out, bool create_if_missing)
{
rocksdb::Options opt;
rocksdb::Status status;
if (options_str.length()) {
int r = ParseOptionsFromString(options_str, opt);
if (r != 0) {
return -EINVAL;
}
}
opt.create_if_missing = create_if_missing;
if (g_conf->rocksdb_separate_wal_dir) {
opt.wal_dir = path + ".wal";
}
if (g_conf->rocksdb_db_paths.length()) {
list<string> paths;
get_str_list(g_conf->rocksdb_db_paths, "; \t", paths);
for (auto& p : paths) {
size_t pos = p.find(',');
if (pos == std::string::npos) {
derr << __func__ << " invalid db path item " << p << " in "
<< g_conf->rocksdb_db_paths << dendl;
return -EINVAL;
}
string path = p.substr(0, pos);
string size_str = p.substr(pos + 1);
uint64_t size = atoll(size_str.c_str());
if (!size) {
derr << __func__ << " invalid db path item " << p << " in "
<< g_conf->rocksdb_db_paths << dendl;
return -EINVAL;
}
opt.db_paths.push_back(rocksdb::DbPath(path, size));
dout(10) << __func__ << " db_path " << path << " size " << size << dendl;
}
}
if (g_conf->rocksdb_log_to_ceph_log) {
opt.info_log.reset(new CephRocksdbLogger(g_ceph_context));
}
if (priv) {
dout(10) << __func__ << " using custom Env " << priv << dendl;
opt.env = static_cast<rocksdb::Env*>(priv);
}
auto cache = rocksdb::NewLRUCache(g_conf->rocksdb_cache_size, g_conf->rocksdb_cache_shard_bits);
rocksdb::BlockBasedTableOptions bbt_opts;
bbt_opts.block_size = g_conf->rocksdb_block_size;
bbt_opts.block_cache = cache;
opt.table_factory.reset(rocksdb::NewBlockBasedTableFactory(bbt_opts));
dout(10) << __func__ << " set block size to " << g_conf->rocksdb_block_size
<< " cache size to " << g_conf->rocksdb_cache_size
<< " num of cache shards to " << (1 << g_conf->rocksdb_cache_shard_bits) << dendl;
opt.merge_operator.reset(new MergeOperatorRouter(*this));
status = rocksdb::DB::Open(opt, path, &db);
if (!status.ok()) {
derr << status.ToString() << dendl;
return -EINVAL;
}
PerfCountersBuilder plb(g_ceph_context, "rocksdb", l_rocksdb_first, l_rocksdb_last);
plb.add_u64_counter(l_rocksdb_gets, "rocksdb_get", "Gets");
plb.add_u64_counter(l_rocksdb_txns, "rocksdb_transaction", "Transactions");
plb.add_time_avg(l_rocksdb_get_latency, "rocksdb_get_latency", "Get latency");
plb.add_time_avg(l_rocksdb_submit_latency, "rocksdb_submit_latency", "Submit Latency");
plb.add_time_avg(l_rocksdb_submit_sync_latency, "rocksdb_submit_sync_latency", "Submit Sync Latency");
plb.add_u64_counter(l_rocksdb_compact, "rocksdb_compact", "Compactions");
plb.add_u64_counter(l_rocksdb_compact_range, "rocksdb_compact_range", "Compactions by range");
plb.add_u64_counter(l_rocksdb_compact_queue_merge, "rocksdb_compact_queue_merge", "Mergings of ranges in compaction queue");
plb.add_u64(l_rocksdb_compact_queue_len, "rocksdb_compact_queue_len", "Length of compaction queue");
logger = plb.create_perf_counters();
cct->get_perfcounters_collection()->add(logger);
if (compact_on_mount) {
derr << "Compacting rocksdb store..." << dendl;
compact();
derr << "Finished compacting rocksdb store" << dendl;
}
return 0;
}
int RocksDBStore::_test_init(const string& dir)
{
rocksdb::Options options;
options.create_if_missing = true;
rocksdb::DB *db;
rocksdb::Status status = rocksdb::DB::Open(options, dir, &db);
delete db;
db = nullptr;
return status.ok() ? 0 : -EIO;
}
RocksDBStore::~RocksDBStore()
{
close();
delete logger;
// Ensure db is destroyed before dependent db_cache and filterpolicy
delete db;
db = nullptr;
if (priv) {
delete static_cast<rocksdb::Env*>(priv);
}
}
void RocksDBStore::close()
{
// stop compaction thread
compact_queue_lock.Lock();
if (compact_thread.is_started()) {
compact_queue_stop = true;
compact_queue_cond.Signal();
compact_queue_lock.Unlock();
compact_thread.join();
} else {
compact_queue_lock.Unlock();
}
if (logger)
cct->get_perfcounters_collection()->remove(logger);
}
int RocksDBStore::submit_transaction(KeyValueDB::Transaction t)
{
utime_t start = ceph_clock_now(g_ceph_context);
RocksDBTransactionImpl * _t =
static_cast<RocksDBTransactionImpl *>(t.get());
rocksdb::WriteOptions woptions;
woptions.disableWAL = disableWAL;
rocksdb::Status s = db->Write(woptions, _t->bat);
utime_t lat = ceph_clock_now(g_ceph_context) - start;
logger->inc(l_rocksdb_txns);
logger->tinc(l_rocksdb_submit_latency, lat);
return s.ok() ? 0 : -1;
}
int RocksDBStore::submit_transaction_sync(KeyValueDB::Transaction t)
{
utime_t start = ceph_clock_now(g_ceph_context);
RocksDBTransactionImpl * _t =
static_cast<RocksDBTransactionImpl *>(t.get());
rocksdb::WriteOptions woptions;
woptions.sync = true;
woptions.disableWAL = disableWAL;
rocksdb::Status s = db->Write(woptions, _t->bat);
utime_t lat = ceph_clock_now(g_ceph_context) - start;
logger->inc(l_rocksdb_txns);
logger->tinc(l_rocksdb_submit_sync_latency, lat);
return s.ok() ? 0 : -1;
}
int RocksDBStore::get_info_log_level(string info_log_level)
{
if (info_log_level == "debug") {
return 0;
} else if (info_log_level == "info") {
return 1;
} else if (info_log_level == "warn") {
return 2;
} else if (info_log_level == "error") {
return 3;
} else if (info_log_level == "fatal") {
return 4;
} else {
return 1;
}
}
RocksDBStore::RocksDBTransactionImpl::RocksDBTransactionImpl(RocksDBStore *_db)
{
db = _db;
bat = new rocksdb::WriteBatch();
}
RocksDBStore::RocksDBTransactionImpl::~RocksDBTransactionImpl()
{
delete bat;
}
void RocksDBStore::RocksDBTransactionImpl::set(
const string &prefix,
const string &k,
const bufferlist &to_set_bl)
{
string key = combine_strings(prefix, k);
// bufferlist::c_str() is non-constant, so we can't call c_str()
if (to_set_bl.is_contiguous() && to_set_bl.length() > 0) {
bat->Put(rocksdb::Slice(key),
rocksdb::Slice(to_set_bl.buffers().front().c_str(),
to_set_bl.length()));
} else {
// make a copy
bufferlist val = to_set_bl;
bat->Put(rocksdb::Slice(key),
rocksdb::Slice(val.c_str(), val.length()));
}
}
void RocksDBStore::RocksDBTransactionImpl::rmkey(const string &prefix,
const string &k)
{
bat->Delete(combine_strings(prefix, k));
}
void RocksDBStore::RocksDBTransactionImpl::rm_single_key(const string &prefix,
const string &k)
{
bat->SingleDelete(combine_strings(prefix, k));
}
void RocksDBStore::RocksDBTransactionImpl::rmkeys_by_prefix(const string &prefix)
{
KeyValueDB::Iterator it = db->get_iterator(prefix);
for (it->seek_to_first();
it->valid();
it->next()) {
bat->Delete(combine_strings(prefix, it->key()));
}
}
void RocksDBStore::RocksDBTransactionImpl::merge(
const string &prefix,
const string &k,
const bufferlist &to_set_bl)
{
string key = combine_strings(prefix, k);
// bufferlist::c_str() is non-constant, so we can't call c_str()
if (to_set_bl.is_contiguous() && to_set_bl.length() > 0) {
bat->Merge(rocksdb::Slice(key),
rocksdb::Slice(to_set_bl.buffers().front().c_str(),
to_set_bl.length()));
} else {
// make a copy
bufferlist val = to_set_bl;
bat->Merge(rocksdb::Slice(key),
rocksdb::Slice(val.c_str(), val.length()));
}
}
int RocksDBStore::get(
const string &prefix,
const std::set<string> &keys,
std::map<string, bufferlist> *out)
{
utime_t start = ceph_clock_now(g_ceph_context);
KeyValueDB::Iterator it = get_iterator(prefix);
for (std::set<string>::const_iterator i = keys.begin();
i != keys.end();
++i) {
it->lower_bound(*i);
if (it->valid() && it->key() == *i) {
out->insert(make_pair(*i, it->value()));
} else if (!it->valid())
break;
}
utime_t lat = ceph_clock_now(g_ceph_context) - start;
logger->inc(l_rocksdb_gets);
logger->tinc(l_rocksdb_get_latency, lat);
return 0;
}
int RocksDBStore::get(
const string &prefix,
const string &key,
bufferlist *out)
{
assert(out && (out->length() == 0));
utime_t start = ceph_clock_now(g_ceph_context);
int r = 0;
KeyValueDB::Iterator it = get_iterator(prefix);
it->lower_bound(key);
if (it->valid() && it->key() == key) {
out->append(it->value_as_ptr());
} else {
r = -ENOENT;
}
utime_t lat = ceph_clock_now(g_ceph_context) - start;
logger->inc(l_rocksdb_gets);
logger->tinc(l_rocksdb_get_latency, lat);
return r;
}
string RocksDBStore::combine_strings(const string &prefix, const string &value)
{
string out = prefix;
out.push_back(0);
out.append(value);
return out;
}
bufferlist RocksDBStore::to_bufferlist(rocksdb::Slice in)
{
bufferlist bl;
bl.append(bufferptr(in.data(), in.size()));
return bl;
}
int RocksDBStore::split_key(rocksdb::Slice in, string *prefix, string *key)
{
size_t prefix_len = 0;
// Find separator inside Slice
char* separator = (char*) memchr(in.data(), 0, in.size());
if (separator == NULL)
return -EINVAL;
prefix_len = size_t(separator - in.data());
if (prefix_len >= in.size())
return -EINVAL;
// Fetch prefix and/or key directly from Slice
if (prefix)
*prefix = string(in.data(), prefix_len);
if (key)
*key = string(separator+1, in.size()-prefix_len-1);
return 0;
}
void RocksDBStore::compact()
{
logger->inc(l_rocksdb_compact);
rocksdb::CompactRangeOptions options;
db->CompactRange(options, nullptr, nullptr);
}
void RocksDBStore::compact_thread_entry()
{
compact_queue_lock.Lock();
while (!compact_queue_stop) {
while (!compact_queue.empty()) {
pair<string,string> range = compact_queue.front();
compact_queue.pop_front();
logger->set(l_rocksdb_compact_queue_len, compact_queue.size());
compact_queue_lock.Unlock();
logger->inc(l_rocksdb_compact_range);
compact_range(range.first, range.second);
compact_queue_lock.Lock();
continue;
}
compact_queue_cond.Wait(compact_queue_lock);
}
compact_queue_lock.Unlock();
}
void RocksDBStore::compact_range_async(const string& start, const string& end)
{
Mutex::Locker l(compact_queue_lock);
// try to merge adjacent ranges. this is O(n), but the queue should
// be short. note that we do not cover all overlap cases and merge
// opportunities here, but we capture the ones we currently need.
list< pair<string,string> >::iterator p = compact_queue.begin();
while (p != compact_queue.end()) {
if (p->first == start && p->second == end) {
// dup; no-op
return;
}
if (p->first <= end && p->first > start) {
// merge with existing range to the right
compact_queue.push_back(make_pair(start, p->second));
compact_queue.erase(p);
logger->inc(l_rocksdb_compact_queue_merge);
break;
}
if (p->second >= start && p->second < end) {
// merge with existing range to the left
compact_queue.push_back(make_pair(p->first, end));
compact_queue.erase(p);
logger->inc(l_rocksdb_compact_queue_merge);
break;
}
++p;
}
if (p == compact_queue.end()) {
// no merge, new entry.
compact_queue.push_back(make_pair(start, end));
logger->set(l_rocksdb_compact_queue_len, compact_queue.size());
}
compact_queue_cond.Signal();
if (!compact_thread.is_started()) {
compact_thread.create("rstore_commpact");
}
}
bool RocksDBStore::check_omap_dir(string &omap_dir)
{
rocksdb::Options options;
options.create_if_missing = true;
rocksdb::DB *db;
rocksdb::Status status = rocksdb::DB::Open(options, omap_dir, &db);
delete db;
db = nullptr;
return status.ok();
}
void RocksDBStore::compact_range(const string& start, const string& end)
{
rocksdb::CompactRangeOptions options;
rocksdb::Slice cstart(start);
rocksdb::Slice cend(end);
db->CompactRange(options, &cstart, &cend);
}
RocksDBStore::RocksDBWholeSpaceIteratorImpl::~RocksDBWholeSpaceIteratorImpl()
{
delete dbiter;
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::seek_to_first()
{
dbiter->SeekToFirst();
return dbiter->status().ok() ? 0 : -1;
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::seek_to_first(const string &prefix)
{
rocksdb::Slice slice_prefix(prefix);
dbiter->Seek(slice_prefix);
return dbiter->status().ok() ? 0 : -1;
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::seek_to_last()
{
dbiter->SeekToLast();
return dbiter->status().ok() ? 0 : -1;
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::seek_to_last(const string &prefix)
{
string limit = past_prefix(prefix);
rocksdb::Slice slice_limit(limit);
dbiter->Seek(slice_limit);
if (!dbiter->Valid()) {
dbiter->SeekToLast();
} else {
dbiter->Prev();
}
return dbiter->status().ok() ? 0 : -1;
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::upper_bound(const string &prefix, const string &after)
{
lower_bound(prefix, after);
if (valid()) {
pair<string,string> key = raw_key();
if (key.first == prefix && key.second == after)
next();
}
return dbiter->status().ok() ? 0 : -1;
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::lower_bound(const string &prefix, const string &to)
{
string bound = combine_strings(prefix, to);
rocksdb::Slice slice_bound(bound);
dbiter->Seek(slice_bound);
return dbiter->status().ok() ? 0 : -1;
}
bool RocksDBStore::RocksDBWholeSpaceIteratorImpl::valid()
{
return dbiter->Valid();
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::next()
{
if (valid()) {
dbiter->Next();
}
return dbiter->status().ok() ? 0 : -1;
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::prev()
{
if (valid()) {
dbiter->Prev();
}
return dbiter->status().ok() ? 0 : -1;
}
string RocksDBStore::RocksDBWholeSpaceIteratorImpl::key()
{
string out_key;
split_key(dbiter->key(), 0, &out_key);
return out_key;
}
pair<string,string> RocksDBStore::RocksDBWholeSpaceIteratorImpl::raw_key()
{
string prefix, key;
split_key(dbiter->key(), &prefix, &key);
return make_pair(prefix, key);
}
bool RocksDBStore::RocksDBWholeSpaceIteratorImpl::raw_key_is_prefixed(const string &prefix) {
// Look for "prefix\0" right in rocksb::Slice
rocksdb::Slice key = dbiter->key();
if ((key.size() > prefix.length()) && (key[prefix.length()] == '\0')) {
return memcmp(key.data(), prefix.c_str(), prefix.length()) == 0;
} else {
return false;
}
}
bufferlist RocksDBStore::RocksDBWholeSpaceIteratorImpl::value()
{
return to_bufferlist(dbiter->value());
}
bufferptr RocksDBStore::RocksDBWholeSpaceIteratorImpl::value_as_ptr()
{
rocksdb::Slice val = dbiter->value();
return bufferptr(val.data(), val.size());
}
int RocksDBStore::RocksDBWholeSpaceIteratorImpl::status()
{
return dbiter->status().ok() ? 0 : -1;
}
string RocksDBStore::past_prefix(const string &prefix)
{
string limit = prefix;
limit.push_back(1);
return limit;
}
RocksDBStore::WholeSpaceIterator RocksDBStore::_get_iterator()
{
return std::make_shared<RocksDBWholeSpaceIteratorImpl>(
db->NewIterator(rocksdb::ReadOptions()));
}
RocksDBStore::WholeSpaceIterator RocksDBStore::_get_snapshot_iterator()
{
const rocksdb::Snapshot *snapshot;
rocksdb::ReadOptions options;
snapshot = db->GetSnapshot();
options.snapshot = snapshot;
return std::make_shared<RocksDBSnapshotIteratorImpl>(
db, snapshot, db->NewIterator(options));
}
RocksDBStore::RocksDBSnapshotIteratorImpl::~RocksDBSnapshotIteratorImpl()
{
db->ReleaseSnapshot(snapshot);
}