Use FlatHashMap in KeyValue.
This commit is contained in:
parent
b51440c246
commit
7644caaf60
@ -14,7 +14,7 @@
|
|||||||
#include "td/utils/algorithm.h"
|
#include "td/utils/algorithm.h"
|
||||||
#include "td/utils/buffer.h"
|
#include "td/utils/buffer.h"
|
||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
#include "td/utils/HashTableUtils.h"
|
#include "td/utils/FlatHashMap.h"
|
||||||
#include "td/utils/logging.h"
|
#include "td/utils/logging.h"
|
||||||
#include "td/utils/misc.h"
|
#include "td/utils/misc.h"
|
||||||
#include "td/utils/port/RwMutex.h"
|
#include "td/utils/port/RwMutex.h"
|
||||||
@ -26,7 +26,6 @@
|
|||||||
#include "td/utils/tl_storers.h"
|
#include "td/utils/tl_storers.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
@ -83,6 +82,10 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
|
|||||||
[&](const BinlogEvent &binlog_event) {
|
[&](const BinlogEvent &binlog_event) {
|
||||||
Event event;
|
Event event;
|
||||||
event.parse(TlParser(binlog_event.get_data()));
|
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_));
|
map_.emplace(event.key.str(), std::make_pair(event.value.str(), binlog_event.id_));
|
||||||
},
|
},
|
||||||
std::move(db_key), DbKey::empty(), scheduler_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) {
|
void external_init_handle(const BinlogEvent &binlog_event) {
|
||||||
Event event;
|
Event event;
|
||||||
event.parse(TlParser(binlog_event.get_data()));
|
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_));
|
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 {
|
SeqNo set(string key, string value) final {
|
||||||
auto lock = rw_mutex_.lock_write().move_as_ok();
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
||||||
uint64 old_event_id = 0;
|
uint64 old_event_id = 0;
|
||||||
|
CHECK(!key.empty());
|
||||||
auto it_ok = map_.emplace(key, std::make_pair(value, 0));
|
auto it_ok = map_.emplace(key, std::make_pair(value, 0));
|
||||||
if (!it_ok.second) {
|
if (!it_ok.second) {
|
||||||
if (it_ok.first->second.first == value) {
|
if (it_ok.first->second.first == value) {
|
||||||
@ -210,9 +218,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
|
|||||||
binlog_->lazy_sync(std::move(promise));
|
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();
|
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_) {
|
for (const auto &kv : map_) {
|
||||||
if (begins_with(kv.first, prefix)) {
|
if (begins_with(kv.first, prefix)) {
|
||||||
res.emplace(kv.first.substr(prefix.size()), kv.second.first);
|
res.emplace(kv.first.substr(prefix.size()), kv.second.first);
|
||||||
@ -221,9 +229,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
|
|||||||
return res;
|
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();
|
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_) {
|
for (const auto &kv : map_) {
|
||||||
res.emplace(kv.first, kv.second.first);
|
res.emplace(kv.first, kv.second.first);
|
||||||
}
|
}
|
||||||
@ -248,6 +256,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
|
|||||||
seq_no++;
|
seq_no++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
friend class BinlogKeyValue;
|
friend class BinlogKeyValue;
|
||||||
|
|
||||||
@ -256,7 +265,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<string, std::pair<string, uint64>, Hash<string>> map_;
|
FlatHashMap<string, std::pair<string, uint64>> map_;
|
||||||
std::shared_ptr<BinlogT> binlog_;
|
std::shared_ptr<BinlogT> binlog_;
|
||||||
RwMutex rw_mutex_;
|
RwMutex rw_mutex_;
|
||||||
int32 magic_ = MAGIC;
|
int32 magic_ = MAGIC;
|
||||||
|
@ -7,12 +7,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
#include "td/utils/HashTableUtils.h"
|
#include "td/utils/FlatHashMap.h"
|
||||||
#include "td/utils/Promise.h"
|
#include "td/utils/Promise.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
class KeyValueSyncInterface {
|
class KeyValueSyncInterface {
|
||||||
@ -34,9 +32,9 @@ class KeyValueSyncInterface {
|
|||||||
|
|
||||||
virtual string get(const string &key) = 0;
|
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;
|
virtual SeqNo erase(const string &key) = 0;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user