2018-07-22 01:56:40 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-07-22 01:56:40 +02: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/SqliteKeyValue.h"
|
|
|
|
|
2021-09-25 20:25:51 +02:00
|
|
|
#include "td/utils/base64.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-07-22 01:56:40 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
Status SqliteKeyValue::init_with_connection(SqliteDb connection, string table_name) {
|
2020-02-26 16:15:19 +01:00
|
|
|
auto init_guard = ScopeExit() + [&] {
|
2019-09-28 04:14:21 +02:00
|
|
|
close();
|
|
|
|
};
|
2018-07-22 01:56:40 +02:00
|
|
|
db_ = std::move(connection);
|
|
|
|
table_name_ = std::move(table_name);
|
|
|
|
TRY_STATUS(init(db_, table_name_));
|
|
|
|
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(set_stmt_,
|
|
|
|
db_.get_statement(PSLICE() << "REPLACE INTO " << table_name_ << " (k, v) VALUES (?1, ?2)"));
|
|
|
|
TRY_RESULT_ASSIGN(get_stmt_, db_.get_statement(PSLICE() << "SELECT v FROM " << table_name_ << " WHERE k = ?1"));
|
|
|
|
TRY_RESULT_ASSIGN(erase_stmt_, db_.get_statement(PSLICE() << "DELETE FROM " << table_name_ << " WHERE k = ?1"));
|
|
|
|
TRY_RESULT_ASSIGN(get_all_stmt_, db_.get_statement(PSLICE() << "SELECT k, v FROM " << table_name_));
|
2018-07-22 01:56:40 +02:00
|
|
|
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(erase_by_prefix_stmt_,
|
|
|
|
db_.get_statement(PSLICE() << "DELETE FROM " << table_name_ << " WHERE ?1 <= k AND k < ?2"));
|
|
|
|
TRY_RESULT_ASSIGN(erase_by_prefix_rare_stmt_,
|
|
|
|
db_.get_statement(PSLICE() << "DELETE FROM " << table_name_ << " WHERE ?1 <= k"));
|
2018-07-22 01:56:40 +02:00
|
|
|
|
2019-09-02 20:11:56 +02:00
|
|
|
TRY_RESULT_ASSIGN(get_by_prefix_stmt_,
|
|
|
|
db_.get_statement(PSLICE() << "SELECT k, v FROM " << table_name_ << " WHERE ?1 <= k AND k < ?2"));
|
|
|
|
TRY_RESULT_ASSIGN(get_by_prefix_rare_stmt_,
|
|
|
|
db_.get_statement(PSLICE() << "SELECT k, v FROM " << table_name_ << " WHERE ?1 <= k"));
|
2018-07-22 01:56:40 +02:00
|
|
|
|
2018-07-22 02:00:45 +02:00
|
|
|
init_guard.dismiss();
|
2018-07-22 01:56:40 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-08-06 16:22:22 +02:00
|
|
|
Status SqliteKeyValue::drop() {
|
|
|
|
if (empty()) {
|
|
|
|
return Status::OK();
|
2018-07-22 01:56:40 +02:00
|
|
|
}
|
2018-08-06 16:22:22 +02:00
|
|
|
|
|
|
|
auto result = drop(db_, table_name_);
|
|
|
|
close();
|
|
|
|
return result;
|
2018-07-22 01:56:40 +02:00
|
|
|
}
|
|
|
|
|
2021-12-12 13:41:06 +01:00
|
|
|
void SqliteKeyValue::set(Slice key, Slice value) {
|
2018-07-22 01:56:40 +02:00
|
|
|
set_stmt_.bind_blob(1, key).ensure();
|
|
|
|
set_stmt_.bind_blob(2, value).ensure();
|
2020-05-16 23:23:33 +02:00
|
|
|
auto status = set_stmt_.step();
|
|
|
|
if (status.is_error()) {
|
2021-09-25 20:25:51 +02:00
|
|
|
LOG(FATAL) << "Failed to set \"" << base64_encode(key) << "\": " << status.error();
|
2020-05-16 23:23:33 +02:00
|
|
|
}
|
2018-07-22 01:56:40 +02:00
|
|
|
set_stmt_.reset();
|
|
|
|
}
|
|
|
|
|
2021-12-12 20:34:19 +01:00
|
|
|
void SqliteKeyValue::set_all(const std::unordered_map<string, string> &key_values) {
|
|
|
|
begin_write_transaction().ensure();
|
|
|
|
for (auto &key_value : key_values) {
|
|
|
|
set(key_value.first, key_value.second);
|
|
|
|
}
|
|
|
|
commit_transaction().ensure();
|
|
|
|
}
|
|
|
|
|
2018-07-22 01:56:40 +02:00
|
|
|
string SqliteKeyValue::get(Slice key) {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
get_stmt_.reset();
|
|
|
|
};
|
|
|
|
get_stmt_.bind_blob(1, key).ensure();
|
|
|
|
get_stmt_.step().ensure();
|
|
|
|
if (!get_stmt_.has_row()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
auto data = get_stmt_.view_blob(0).str();
|
|
|
|
get_stmt_.step().ignore();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:41:06 +01:00
|
|
|
void SqliteKeyValue::erase(Slice key) {
|
2018-07-22 01:56:40 +02:00
|
|
|
erase_stmt_.bind_blob(1, key).ensure();
|
|
|
|
erase_stmt_.step().ensure();
|
|
|
|
erase_stmt_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SqliteKeyValue::erase_by_prefix(Slice prefix) {
|
|
|
|
auto next = next_prefix(prefix);
|
|
|
|
if (next.empty()) {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
erase_by_prefix_rare_stmt_.reset();
|
|
|
|
};
|
|
|
|
erase_by_prefix_rare_stmt_.bind_blob(1, prefix).ensure();
|
|
|
|
erase_by_prefix_rare_stmt_.step().ensure();
|
|
|
|
} else {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
erase_by_prefix_stmt_.reset();
|
|
|
|
};
|
|
|
|
erase_by_prefix_stmt_.bind_blob(1, prefix).ensure();
|
|
|
|
erase_by_prefix_stmt_.bind_blob(2, next).ensure();
|
|
|
|
erase_by_prefix_stmt_.step().ensure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string SqliteKeyValue::next_prefix(Slice prefix) {
|
|
|
|
string next = prefix.str();
|
|
|
|
size_t pos = next.size();
|
|
|
|
while (pos) {
|
|
|
|
pos--;
|
|
|
|
auto value = static_cast<uint8>(next[pos]);
|
|
|
|
value++;
|
|
|
|
next[pos] = static_cast<char>(value);
|
|
|
|
if (value != 0) {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string{};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|