2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/db/binlog/Binlog.h"
|
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
2019-10-22 19:01:15 +02:00
|
|
|
#include "td/db/DbKey.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/KeyValueSyncInterface.h"
|
|
|
|
|
2022-02-10 10:23:52 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
2023-10-12 19:58:38 +02:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/port/RwMutex.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
|
|
|
|
#include <memory>
|
2023-10-16 09:11:09 +02:00
|
|
|
#include <unordered_map>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class BinlogT>
|
2021-07-04 04:58:54 +02:00
|
|
|
class BinlogKeyValue final : public KeyValueSyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-06-16 04:10:16 +02:00
|
|
|
static constexpr int32 MAGIC = 0x2a280000;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
struct Event final : public Storer {
|
2018-12-31 20:04:05 +01:00
|
|
|
Event() = default;
|
|
|
|
Event(Slice key, Slice value) : key(key), value(value) {
|
|
|
|
}
|
|
|
|
Slice key;
|
|
|
|
Slice value;
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &&storer) const {
|
|
|
|
storer.store_string(key);
|
|
|
|
storer.store_string(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &&parser) {
|
|
|
|
key = parser.template fetch_string<Slice>();
|
|
|
|
value = parser.template fetch_string<Slice>();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t size() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
TlStorerCalcLength storer;
|
|
|
|
store(storer);
|
|
|
|
return storer.get_length();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t store(uint8 *ptr) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
TlStorerUnsafe storer(ptr);
|
|
|
|
store(storer);
|
2018-06-25 01:58:43 +02:00
|
|
|
return static_cast<size_t>(storer.get_buf() - ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int32 get_magic() const {
|
|
|
|
return magic_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status init(string name, DbKey db_key = DbKey::empty(), int scheduler_id = -1,
|
|
|
|
int32 override_magic = 0) TD_WARN_UNUSED_RESULT {
|
|
|
|
close();
|
|
|
|
if (override_magic) {
|
|
|
|
magic_ = override_magic;
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
binlog_ = std::make_shared<BinlogT>();
|
2019-09-28 04:14:21 +02:00
|
|
|
TRY_STATUS(binlog_->init(
|
|
|
|
name,
|
|
|
|
[&](const BinlogEvent &binlog_event) {
|
|
|
|
Event event;
|
2023-02-02 01:08:26 +01:00
|
|
|
event.parse(TlParser(binlog_event.get_data()));
|
2023-10-12 19:58:38 +02:00
|
|
|
if (event.key.empty()) {
|
|
|
|
LOG(ERROR) << "Have event with empty key";
|
|
|
|
return;
|
|
|
|
}
|
2019-09-28 04:14:21 +02:00
|
|
|
map_.emplace(event.key.str(), std::make_pair(event.value.str(), binlog_event.id_));
|
|
|
|
},
|
|
|
|
std::move(db_key), DbKey::empty(), scheduler_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void external_init_begin(int32 override_magic = 0) {
|
|
|
|
close();
|
|
|
|
if (override_magic) {
|
|
|
|
magic_ = override_magic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class OtherBinlogT>
|
|
|
|
void external_init_handle(BinlogKeyValue<OtherBinlogT> &&other) {
|
|
|
|
map_ = std::move(other.map_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void external_init_handle(const BinlogEvent &binlog_event) {
|
|
|
|
Event event;
|
2023-02-02 01:08:26 +01:00
|
|
|
event.parse(TlParser(binlog_event.get_data()));
|
2023-10-12 19:58:38 +02:00
|
|
|
if (event.key.empty()) {
|
|
|
|
LOG(ERROR) << "Have external event with empty key";
|
|
|
|
return;
|
|
|
|
}
|
2018-07-10 02:38:17 +02:00
|
|
|
map_.emplace(event.key.str(), std::make_pair(event.value.str(), binlog_event.id_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void external_init_finish(std::shared_ptr<BinlogT> binlog) {
|
|
|
|
binlog_ = std::move(binlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
*this = BinlogKeyValue();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void close(Promise<> promise) final {
|
2020-07-14 18:10:26 +02:00
|
|
|
binlog_->close(std::move(promise));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
SeqNo set(string key, string value) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 old_event_id = 0;
|
2023-10-12 19:58:38 +02:00
|
|
|
CHECK(!key.empty());
|
2022-02-07 20:41:07 +01:00
|
|
|
auto it_ok = map_.emplace(key, std::make_pair(value, 0));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!it_ok.second) {
|
|
|
|
if (it_ok.first->second.first == value) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-16 04:23:22 +02:00
|
|
|
VLOG(binlog) << "Change value of key " << key << " from " << hex_encode(it_ok.first->second.first) << " to "
|
|
|
|
<< hex_encode(value);
|
2023-01-16 10:47:37 +01:00
|
|
|
old_event_id = it_ok.first->second.second;
|
2018-12-31 20:04:05 +01:00
|
|
|
it_ok.first->second.first = value;
|
2021-06-16 04:23:22 +02:00
|
|
|
} else {
|
|
|
|
VLOG(binlog) << "Set value of key " << key << " to " << hex_encode(value);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
bool rewrite = false;
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 event_id;
|
|
|
|
auto seq_no = binlog_->next_event_id();
|
|
|
|
if (old_event_id != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
rewrite = true;
|
2023-01-16 10:47:37 +01:00
|
|
|
event_id = old_event_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2023-01-16 10:47:37 +01:00
|
|
|
event_id = seq_no;
|
|
|
|
it_ok.first->second.second = event_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lock.reset();
|
|
|
|
add_event(seq_no,
|
2023-01-16 10:47:37 +01:00
|
|
|
BinlogEvent::create_raw(event_id, magic_, rewrite ? BinlogEvent::Flags::Rewrite : 0, Event{key, value}));
|
2018-12-31 20:04:05 +01:00
|
|
|
return seq_no;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
SeqNo erase(const string &key) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
|
|
|
auto it = map_.find(key);
|
|
|
|
if (it == map_.end()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-16 04:23:22 +02:00
|
|
|
VLOG(binlog) << "Remove value of key " << key << ", which is " << hex_encode(it->second.first);
|
2023-01-16 10:47:37 +01:00
|
|
|
uint64 event_id = it->second.second;
|
2018-12-31 20:04:05 +01:00
|
|
|
map_.erase(it);
|
2023-01-16 10:47:37 +01:00
|
|
|
auto seq_no = binlog_->next_event_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
lock.reset();
|
2023-01-16 10:47:37 +01:00
|
|
|
add_event(seq_no, BinlogEvent::create_raw(event_id, BinlogEvent::ServiceTypes::Empty, BinlogEvent::Flags::Rewrite,
|
2018-12-31 20:04:05 +01:00
|
|
|
EmptyStorer()));
|
|
|
|
return seq_no;
|
|
|
|
}
|
|
|
|
|
2023-02-12 01:29:19 +01:00
|
|
|
SeqNo erase_batch(vector<string> keys) final {
|
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
|
|
|
vector<uint64> log_event_ids;
|
|
|
|
for (auto &key : keys) {
|
|
|
|
auto it = map_.find(key);
|
|
|
|
if (it != map_.end()) {
|
|
|
|
log_event_ids.push_back(it->second.second);
|
|
|
|
map_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (log_event_ids.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
VLOG(binlog) << "Remove value of keys " << keys;
|
|
|
|
return binlog_->erase_batch(std::move(log_event_ids));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void add_event(uint64 seq_no, BufferSlice &&event) {
|
2018-06-28 19:00:11 +02:00
|
|
|
binlog_->add_raw_event(BinlogDebugInfo{__FILE__, __LINE__}, seq_no, std::move(event));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
bool isset(const string &key) final {
|
2018-03-08 16:01:45 +01:00
|
|
|
auto lock = rw_mutex_.lock_read().move_as_ok();
|
|
|
|
return map_.count(key) > 0;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
string get(const string &key) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_read().move_as_ok();
|
|
|
|
auto it = map_.find(key);
|
|
|
|
if (it == map_.end()) {
|
|
|
|
return string();
|
|
|
|
}
|
2021-06-16 04:23:22 +02:00
|
|
|
VLOG(binlog) << "Get value of key " << key << ", which is " << hex_encode(it->second.first);
|
2018-12-31 20:04:05 +01:00
|
|
|
return it->second.first;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void force_sync(Promise<> &&promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
binlog_->force_sync(std::move(promise));
|
|
|
|
}
|
2019-01-06 22:06:52 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void lazy_sync(Promise<> &&promise) {
|
|
|
|
binlog_->lazy_sync(std::move(promise));
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:11:09 +02:00
|
|
|
std::unordered_map<string, string> prefix_get(Slice prefix) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
2023-10-16 09:11:09 +02:00
|
|
|
std::unordered_map<string, string> res;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (const auto &kv : map_) {
|
|
|
|
if (begins_with(kv.first, prefix)) {
|
2022-02-09 15:05:27 +01:00
|
|
|
res.emplace(kv.first.substr(prefix.size()), kv.second.first);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-10-12 19:58:38 +02:00
|
|
|
FlatHashMap<string, string> get_all() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
2023-10-12 19:58:38 +02:00
|
|
|
FlatHashMap<string, string> res;
|
2023-10-12 20:04:09 +02:00
|
|
|
res.reserve(map_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
for (const auto &kv : map_) {
|
2022-02-09 15:05:27 +01:00
|
|
|
res.emplace(kv.first, kv.second.first);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void erase_by_prefix(Slice prefix) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
2023-01-16 10:47:37 +01:00
|
|
|
vector<uint64> event_ids;
|
2022-02-10 10:23:52 +01:00
|
|
|
table_remove_if(map_, [&](const auto &it) {
|
|
|
|
if (begins_with(it.first, prefix)) {
|
2023-01-16 10:47:37 +01:00
|
|
|
event_ids.push_back(it.second.second);
|
2022-02-10 10:23:52 +01:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-02-10 10:23:52 +01:00
|
|
|
return false;
|
|
|
|
});
|
2023-01-16 10:47:37 +01:00
|
|
|
auto seq_no = binlog_->next_event_id(narrow_cast<int32>(event_ids.size()));
|
2018-12-31 20:04:05 +01:00
|
|
|
lock.reset();
|
2023-01-16 10:47:37 +01:00
|
|
|
for (auto event_id : event_ids) {
|
|
|
|
add_event(seq_no, BinlogEvent::create_raw(event_id, BinlogEvent::ServiceTypes::Empty, BinlogEvent::Flags::Rewrite,
|
2018-12-31 20:04:05 +01:00
|
|
|
EmptyStorer()));
|
|
|
|
seq_no++;
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 19:58:38 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
friend class BinlogKeyValue;
|
|
|
|
|
|
|
|
static Status destroy(Slice name) {
|
|
|
|
return Binlog::destroy(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-10-12 19:58:38 +02:00
|
|
|
FlatHashMap<string, std::pair<string, uint64>> map_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<BinlogT> binlog_;
|
|
|
|
RwMutex rw_mutex_;
|
2020-06-16 04:10:16 +02:00
|
|
|
int32 magic_ = MAGIC;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <>
|
|
|
|
inline void BinlogKeyValue<Binlog>::add_event(uint64 seq_no, BufferSlice &&event) {
|
2018-06-28 19:00:11 +02:00
|
|
|
binlog_->add_raw_event(std::move(event), BinlogDebugInfo{__FILE__, __LINE__});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <>
|
|
|
|
inline void BinlogKeyValue<Binlog>::force_sync(Promise<> &&promise) {
|
|
|
|
binlog_->sync();
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <>
|
|
|
|
inline void BinlogKeyValue<Binlog>::lazy_sync(Promise<> &&promise) {
|
|
|
|
force_sync(std::move(promise));
|
|
|
|
}
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|