2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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)
|
|
|
|
//
|
|
|
|
#include "td/db/SqliteKeyValueAsync.h"
|
|
|
|
|
2019-02-22 21:15:43 +01:00
|
|
|
#include "td/db/SqliteKeyValue.h"
|
|
|
|
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/optional.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-05-24 23:37:43 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SqliteKeyValueAsync final : public SqliteKeyValueAsyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit SqliteKeyValueAsync(std::shared_ptr<SqliteKeyValueSafe> kv_safe, int32 scheduler_id = -1) {
|
|
|
|
impl_ = create_actor_on_scheduler<Impl>("KV", scheduler_id, std::move(kv_safe));
|
|
|
|
}
|
2021-12-12 20:34:19 +01:00
|
|
|
void set(string key, string value, Promise<Unit> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::set, std::move(key), std::move(value), std::move(promise));
|
|
|
|
}
|
2021-12-12 20:34:19 +01:00
|
|
|
void set_all(std::unordered_map<string, string> key_values, Promise<Unit> promise) final {
|
|
|
|
send_closure_later(impl_, &Impl::set_all, std::move(key_values), std::move(promise));
|
|
|
|
}
|
|
|
|
void erase(string key, Promise<Unit> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::erase, std::move(key), std::move(promise));
|
|
|
|
}
|
2021-12-12 20:34:19 +01:00
|
|
|
void erase_by_prefix(string key_prefix, Promise<Unit> promise) final {
|
2020-04-18 00:55:54 +02:00
|
|
|
send_closure_later(impl_, &Impl::erase_by_prefix, std::move(key_prefix), std::move(promise));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void get(string key, Promise<string> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::get, std::move(key), std::move(promise));
|
|
|
|
}
|
2021-12-12 20:34:19 +01:00
|
|
|
void close(Promise<Unit> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(impl_, &Impl::close, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-04 04:58:54 +02:00
|
|
|
class Impl final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit Impl(std::shared_ptr<SqliteKeyValueSafe> kv_safe) : kv_safe_(std::move(kv_safe)) {
|
|
|
|
}
|
2021-10-18 18:26:14 +02:00
|
|
|
|
2021-12-12 20:34:19 +01:00
|
|
|
void set(string key, string value, Promise<Unit> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = buffer_.find(key);
|
|
|
|
if (it != buffer_.end()) {
|
|
|
|
it->second = std::move(value);
|
|
|
|
} else {
|
|
|
|
buffer_.emplace(std::move(key), std::move(value));
|
|
|
|
}
|
|
|
|
if (promise) {
|
|
|
|
buffer_promises_.push_back(std::move(promise));
|
|
|
|
}
|
|
|
|
cnt_++;
|
|
|
|
do_flush(false /*force*/);
|
|
|
|
}
|
2021-10-18 18:26:14 +02:00
|
|
|
|
2021-12-12 20:34:19 +01:00
|
|
|
void set_all(std::unordered_map<string, string> key_values, Promise<Unit> promise) {
|
|
|
|
do_flush(true /*force*/);
|
|
|
|
kv_->set_all(key_values);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void erase(string key, Promise<Unit> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = buffer_.find(key);
|
|
|
|
if (it != buffer_.end()) {
|
|
|
|
it->second = optional<string>();
|
|
|
|
} else {
|
|
|
|
buffer_.emplace(std::move(key), optional<string>());
|
|
|
|
}
|
|
|
|
if (promise) {
|
|
|
|
buffer_promises_.push_back(std::move(promise));
|
|
|
|
}
|
|
|
|
cnt_++;
|
|
|
|
do_flush(false /*force*/);
|
|
|
|
}
|
2021-10-18 18:26:14 +02:00
|
|
|
|
2021-12-12 20:34:19 +01:00
|
|
|
void erase_by_prefix(string key_prefix, Promise<Unit> promise) {
|
2020-04-18 00:55:54 +02:00
|
|
|
do_flush(true /*force*/);
|
|
|
|
kv_->erase_by_prefix(key_prefix);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void get(const string &key, Promise<string> promise) {
|
|
|
|
auto it = buffer_.find(key);
|
|
|
|
if (it != buffer_.end()) {
|
|
|
|
return promise.set_value(it->second ? it->second.value() : "");
|
|
|
|
}
|
|
|
|
promise.set_value(kv_->get(key));
|
|
|
|
}
|
2021-12-12 20:34:19 +01:00
|
|
|
|
|
|
|
void close(Promise<Unit> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
do_flush(true /*force*/);
|
|
|
|
kv_safe_.reset();
|
|
|
|
kv_ = nullptr;
|
|
|
|
stop();
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<SqliteKeyValueSafe> kv_safe_;
|
|
|
|
SqliteKeyValue *kv_ = nullptr;
|
|
|
|
|
2019-03-31 21:46:32 +02:00
|
|
|
static constexpr double MAX_PENDING_QUERIES_DELAY = 0.01;
|
2018-12-31 20:04:05 +01:00
|
|
|
static constexpr size_t MAX_PENDING_QUERIES_COUNT = 100;
|
|
|
|
std::unordered_map<string, optional<string>> buffer_;
|
2021-12-12 20:34:19 +01:00
|
|
|
std::vector<Promise<Unit>> buffer_promises_;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t cnt_ = 0;
|
|
|
|
|
|
|
|
double wakeup_at_ = 0;
|
|
|
|
void do_flush(bool force) {
|
|
|
|
if (buffer_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!force) {
|
|
|
|
auto now = Time::now_cached();
|
|
|
|
if (wakeup_at_ == 0) {
|
|
|
|
wakeup_at_ = now + MAX_PENDING_QUERIES_DELAY;
|
|
|
|
}
|
|
|
|
if (now < wakeup_at_ && cnt_ < MAX_PENDING_QUERIES_COUNT) {
|
|
|
|
set_timeout_at(wakeup_at_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wakeup_at_ = 0;
|
|
|
|
cnt_ = 0;
|
|
|
|
|
2021-10-07 12:18:00 +02:00
|
|
|
kv_->begin_write_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &it : buffer_) {
|
|
|
|
if (it.second) {
|
|
|
|
kv_->set(it.first, it.second.value());
|
|
|
|
} else {
|
|
|
|
kv_->erase(it.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kv_->commit_transaction().ensure();
|
|
|
|
buffer_.clear();
|
|
|
|
for (auto &promise : buffer_promises_) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
buffer_promises_.clear();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
do_flush(false /*force*/);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
kv_ = &kv_safe_->get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ActorOwn<Impl> impl_;
|
|
|
|
};
|
2018-05-24 23:37:43 +02:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<SqliteKeyValueAsyncInterface> create_sqlite_key_value_async(std::shared_ptr<SqliteKeyValueSafe> kv,
|
|
|
|
int32 scheduler_id) {
|
|
|
|
return td::make_unique<SqliteKeyValueAsync>(std::move(kv), scheduler_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|