Use FlatHashMap in KeyValue.

This commit is contained in:
levlam 2023-10-12 20:58:38 +03:00
parent b51440c246
commit 7644caaf60
2 changed files with 19 additions and 12 deletions

View File

@ -14,7 +14,7 @@
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/RwMutex.h"
@ -26,7 +26,6 @@
#include "td/utils/tl_storers.h"
#include <memory>
#include <unordered_map>
#include <utility>
namespace td {
@ -83,6 +82,10 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
[&](const BinlogEvent &binlog_event) {
Event event;
event.parse(TlParser(binlog_event.get_data()));
if (event.key.empty()) {
LOG(ERROR) << "Have event with empty key";
return;
}
map_.emplace(event.key.str(), std::make_pair(event.value.str(), binlog_event.id_));
},
std::move(db_key), DbKey::empty(), scheduler_id));
@ -104,6 +107,10 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
void external_init_handle(const BinlogEvent &binlog_event) {
Event event;
event.parse(TlParser(binlog_event.get_data()));
if (event.key.empty()) {
LOG(ERROR) << "Have external event with empty key";
return;
}
map_.emplace(event.key.str(), std::make_pair(event.value.str(), binlog_event.id_));
}
@ -121,6 +128,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
SeqNo set(string key, string value) final {
auto lock = rw_mutex_.lock_write().move_as_ok();
uint64 old_event_id = 0;
CHECK(!key.empty());
auto it_ok = map_.emplace(key, std::make_pair(value, 0));
if (!it_ok.second) {
if (it_ok.first->second.first == value) {
@ -210,9 +218,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
binlog_->lazy_sync(std::move(promise));
}
std::unordered_map<string, string, Hash<string>> prefix_get(Slice prefix) final {
FlatHashMap<string, string> prefix_get(Slice prefix) final {
auto lock = rw_mutex_.lock_write().move_as_ok();
std::unordered_map<string, string, Hash<string>> res;
FlatHashMap<string, string> res;
for (const auto &kv : map_) {
if (begins_with(kv.first, prefix)) {
res.emplace(kv.first.substr(prefix.size()), kv.second.first);
@ -221,9 +229,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
return res;
}
std::unordered_map<string, string, Hash<string>> get_all() final {
FlatHashMap<string, string> get_all() final {
auto lock = rw_mutex_.lock_write().move_as_ok();
std::unordered_map<string, string, Hash<string>> res;
FlatHashMap<string, string> res;
for (const auto &kv : map_) {
res.emplace(kv.first, kv.second.first);
}
@ -248,6 +256,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
seq_no++;
}
}
template <class T>
friend class BinlogKeyValue;
@ -256,7 +265,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
}
private:
std::unordered_map<string, std::pair<string, uint64>, Hash<string>> map_;
FlatHashMap<string, std::pair<string, uint64>> map_;
std::shared_ptr<BinlogT> binlog_;
RwMutex rw_mutex_;
int32 magic_ = MAGIC;

View File

@ -7,12 +7,10 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
#include <unordered_map>
namespace td {
class KeyValueSyncInterface {
@ -34,9 +32,9 @@ class KeyValueSyncInterface {
virtual string get(const string &key) = 0;
virtual std::unordered_map<string, string, Hash<string>> prefix_get(Slice prefix) = 0;
virtual FlatHashMap<string, string> prefix_get(Slice prefix) = 0;
virtual std::unordered_map<string, string, Hash<string>> get_all() = 0;
virtual FlatHashMap<string, string> get_all() = 0;
virtual SeqNo erase(const string &key) = 0;