diff --git a/tddb/td/db/BinlogKeyValue.h b/tddb/td/db/BinlogKeyValue.h index 6b6e68422..629795cc9 100644 --- a/tddb/td/db/BinlogKeyValue.h +++ b/tddb/td/db/BinlogKeyValue.h @@ -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 -#include #include 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> prefix_get(Slice prefix) final { + FlatHashMap prefix_get(Slice prefix) final { auto lock = rw_mutex_.lock_write().move_as_ok(); - std::unordered_map> res; + FlatHashMap 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> get_all() final { + FlatHashMap get_all() final { auto lock = rw_mutex_.lock_write().move_as_ok(); - std::unordered_map> res; + FlatHashMap 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 friend class BinlogKeyValue; @@ -256,7 +265,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface { } private: - std::unordered_map, Hash> map_; + FlatHashMap> map_; std::shared_ptr binlog_; RwMutex rw_mutex_; int32 magic_ = MAGIC; diff --git a/tddb/td/db/KeyValueSyncInterface.h b/tddb/td/db/KeyValueSyncInterface.h index 111882fb2..09515974f 100644 --- a/tddb/td/db/KeyValueSyncInterface.h +++ b/tddb/td/db/KeyValueSyncInterface.h @@ -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 - namespace td { class KeyValueSyncInterface { @@ -34,9 +32,9 @@ class KeyValueSyncInterface { virtual string get(const string &key) = 0; - virtual std::unordered_map> prefix_get(Slice prefix) = 0; + virtual FlatHashMap prefix_get(Slice prefix) = 0; - virtual std::unordered_map> get_all() = 0; + virtual FlatHashMap get_all() = 0; virtual SeqNo erase(const string &key) = 0;