rocksdb批量删除指定前缀的key或者查找指定前缀开始的key

wiki:DeleteRange · facebook/rocksdb Wiki

批量删除指定前缀的key原先的做法:

Slice start, end;
// set start and end
auto it = db->NewIterator(ReadOptions());

for (it->Seek(start); cmp->Compare(it->key(), end) < 0; it->Next()) {
  db->Delete(WriteOptions(), it->key());
}

现在使用DeleteRange后的做法:

string start, end;
start = "test";
end = "tesu"; 
// 0是 DefaultColumnFamily()的意思
dbtype::Status s = db->DeleteRange(dbtype::WriteOptions(),0, start, end);

查找指定前缀开始的key:

string strStartKey = "test";
static ReadOptions tROpt;
std::unique_ptr<Iterator> iter(db->NewIterator(tROpt));
for (iter->Seek(strStartKey); iter->Valid(); iter->Next()) {
      cout << iter->key().ToString()<<"    " << iter->value().ToString() << endl;
}

你可能感兴趣的:(c++,rocksdb,数据库)