1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

celldb: version 2

- thread safe cache
- parallel commit
- multiple optimizations
- support of key-value merge operations
- improved tests and benchmarks
- in-memory version won't read from key value after start - uses vector in-memory table now
- use rocksdb::WALRecoveryMode::kTolerateCorruptedTailRecords - do not silently ignore errors during recovery
This commit is contained in:
birydrad 2024-12-11 14:48:48 +03:00
parent 1b70e48327
commit c863c42ed1
32 changed files with 3276 additions and 318 deletions

View file

@ -22,57 +22,99 @@
namespace td {
Result<MemoryKeyValue::GetStatus> MemoryKeyValue::get(Slice key, std::string &value) {
auto it = map_.find(key);
if (it == map_.end()) {
auto bucket = lock(key);
auto &map = bucket->map;
usage_stats_.get_count++;
auto it = map.find(key);
if (it == map.end()) {
usage_stats_.get_not_found_count++;
return GetStatus::NotFound;
}
value = it->second;
usage_stats_.get_found_count++;
return GetStatus::Ok;
}
std::unique_ptr<MemoryKeyValue::Bucket, MemoryKeyValue::Unlock> MemoryKeyValue::lock(td::Slice key) {
auto bucket_id = std::hash<std::string_view>()(std::string_view(key.data(), key.size())) % buckets_.size();
return lock(buckets_[bucket_id]);
}
Status MemoryKeyValue::for_each(std::function<Status(Slice, Slice)> f) {
for (auto &it : map_) {
TRY_STATUS(f(it.first, it.second));
for (auto &unlocked_bucket : buckets_) {
auto bucket = lock(unlocked_bucket);
for (auto &it : bucket->map) {
TRY_STATUS(f(it.first, it.second));
}
}
return Status::OK();
}
Status MemoryKeyValue::for_each_in_range(Slice begin, Slice end, std::function<Status(Slice, Slice)> f) {
for (auto it = map_.lower_bound(begin); it != map_.end(); it++) {
if (it->first < end) {
TRY_STATUS(f(it->first, it->second));
} else {
break;
for (auto &unlocked_bucket : buckets_) {
auto bucket = lock(unlocked_bucket);
auto &map = bucket->map;
for (auto it = map.lower_bound(begin); it != map.end(); it++) {
if (it->first < end) {
TRY_STATUS(f(it->first, it->second));
} else {
break;
}
}
}
return Status::OK();
}
Status MemoryKeyValue::set(Slice key, Slice value) {
map_[key.str()] = value.str();
auto bucket = lock(key);
auto &map = bucket->map;
usage_stats_.set_count++;
map[key.str()] = value.str();
return Status::OK();
}
Status MemoryKeyValue::merge(Slice key, Slice update) {
CHECK(merger_);
auto bucket = lock(key);
auto &map = bucket->map;
auto &value = map[key.str()];
merger_->merge_value_and_update(value, update);
if (value.empty()) {
map.erase(key.str());
}
return td::Status::OK();
}
Status MemoryKeyValue::erase(Slice key) {
auto it = map_.find(key);
if (it != map_.end()) {
map_.erase(it);
auto bucket = lock(key);
auto &map = bucket->map;
auto it = map.find(key);
if (it != map.end()) {
map.erase(it);
}
return Status::OK();
}
Result<size_t> MemoryKeyValue::count(Slice prefix) {
size_t res = 0;
for (auto it = map_.lower_bound(prefix); it != map_.end(); it++) {
if (Slice(it->first).truncate(prefix.size()) != prefix) {
break;
for (auto &unlocked_bucket : buckets_) {
auto bucket = lock(unlocked_bucket);
auto &map = bucket->map;
for (auto it = map.lower_bound(prefix); it != map.end(); it++) {
if (Slice(it->first).truncate(prefix.size()) != prefix) {
break;
}
res++;
}
res++;
}
return res;
}
std::unique_ptr<KeyValueReader> MemoryKeyValue::snapshot() {
auto res = std::make_unique<MemoryKeyValue>();
res->map_ = map_;
for (size_t i = 0; i < buckets_.size(); i++) {
auto bucket = lock(buckets_[i]);
res->buckets_[i].map = bucket->map;
}
return std::move(res);
}
@ -80,10 +122,10 @@ std::string MemoryKeyValue::stats() const {
return PSTRING() << "MemoryKeyValueStats{" << tag("get_count", get_count_) << "}";
}
Status MemoryKeyValue::begin_write_batch() {
UNREACHABLE();
return Status::OK();
}
Status MemoryKeyValue::commit_write_batch() {
UNREACHABLE();
return Status::OK();
}
Status MemoryKeyValue::abort_write_batch() {
UNREACHABLE();