2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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/telegram/files/FileDb.h"
|
|
|
|
|
2019-02-05 20:43:45 +01:00
|
|
|
#include "td/telegram/files/FileData.h"
|
2019-02-06 01:06:29 +01:00
|
|
|
#include "td/telegram/files/FileData.hpp"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileLocation.h"
|
2019-02-12 15:04:12 +01:00
|
|
|
#include "td/telegram/files/FileLocation.hpp"
|
2021-09-03 22:21:44 +02:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2019-06-12 13:42:06 +02:00
|
|
|
#include "td/telegram/Version.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-07 01:17:11 +01:00
|
|
|
#include "td/db/SqliteConnectionSafe.h"
|
2019-04-26 00:47:25 +02:00
|
|
|
#include "td/db/SqliteDb.h"
|
2018-07-19 16:45:30 +02:00
|
|
|
#include "td/db/SqliteKeyValue.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/SqliteKeyValueSafe.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
Status drop_file_db(SqliteDb &db, int32 version) {
|
|
|
|
LOG(WARNING) << "Drop file_db " << tag("version", version) << tag("current_db_version", current_db_version());
|
|
|
|
TRY_STATUS(SqliteKeyValue::drop(db, "files"));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status init_file_db(SqliteDb &db, int32 version) {
|
2018-12-13 23:48:36 +01:00
|
|
|
LOG(INFO) << "Init file database " << tag("version", version);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Check if database exists
|
|
|
|
TRY_RESULT(has_table, db.has_table("files"));
|
|
|
|
|
|
|
|
if (!has_table) {
|
|
|
|
version = 0;
|
2021-04-16 16:43:51 +02:00
|
|
|
} else if (version < static_cast<int32>(DbVersion::FixFileRemoteLocationKeyBug)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(drop_file_db(db, version));
|
|
|
|
version = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version == 0) {
|
|
|
|
TRY_STATUS(SqliteKeyValue::init(db, "files"));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileDb final : public FileDbInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileDbActor final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2019-01-06 21:39:10 +01:00
|
|
|
FileDbActor(FileDbId current_pmc_id, std::shared_ptr<SqliteKeyValueSafe> file_kv_safe)
|
2018-12-31 20:04:05 +01:00
|
|
|
: current_pmc_id_(current_pmc_id), file_kv_safe_(std::move(file_kv_safe)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void close(Promise<> promise) {
|
|
|
|
file_kv_safe_.reset();
|
|
|
|
LOG(INFO) << "FileDb is closed";
|
|
|
|
promise.set_value(Unit());
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_file_data(const string &key, Promise<FileData> promise) {
|
2020-06-09 16:04:47 +02:00
|
|
|
promise.set_result(load_file_data_impl(actor_id(this), file_pmc(), key, current_pmc_id_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
void clear_file_data(FileDbId id, const string &remote_key, const string &local_key, const string &generate_key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &pmc = file_pmc();
|
2021-10-07 12:18:00 +02:00
|
|
|
pmc.begin_write_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (id > current_pmc_id_) {
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.set("file_id", to_string(id.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
current_pmc_id_ = id;
|
|
|
|
}
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.erase(PSTRING() << "file" << id.get());
|
|
|
|
LOG(DEBUG) << "ERASE " << format::as_hex_dump<4>(Slice(PSLICE() << "file" << id.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (!remote_key.empty()) {
|
|
|
|
pmc.erase(remote_key);
|
|
|
|
LOG(DEBUG) << "ERASE remote " << format::as_hex_dump<4>(Slice(remote_key));
|
|
|
|
}
|
|
|
|
if (!local_key.empty()) {
|
|
|
|
pmc.erase(local_key);
|
|
|
|
LOG(DEBUG) << "ERASE local " << format::as_hex_dump<4>(Slice(local_key));
|
|
|
|
}
|
|
|
|
if (!generate_key.empty()) {
|
|
|
|
pmc.erase(generate_key);
|
|
|
|
}
|
2021-10-07 12:18:00 +02:00
|
|
|
|
|
|
|
pmc.commit_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-06 21:39:10 +01:00
|
|
|
void store_file_data(FileDbId id, const string &file_data, const string &remote_key, const string &local_key,
|
2018-12-31 20:04:05 +01:00
|
|
|
const string &generate_key) {
|
|
|
|
auto &pmc = file_pmc();
|
2021-10-07 12:18:00 +02:00
|
|
|
pmc.begin_write_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (id > current_pmc_id_) {
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.set("file_id", to_string(id.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
current_pmc_id_ = id;
|
|
|
|
}
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.set(PSTRING() << "file" << id.get(), file_data);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (!remote_key.empty()) {
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.set(remote_key, to_string(id.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!local_key.empty()) {
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.set(local_key, to_string(id.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!generate_key.empty()) {
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.set(generate_key, to_string(id.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-07 12:18:00 +02:00
|
|
|
|
|
|
|
pmc.commit_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-06 21:39:10 +01:00
|
|
|
void store_file_data_ref(FileDbId id, FileDbId new_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &pmc = file_pmc();
|
2021-10-07 12:18:00 +02:00
|
|
|
pmc.begin_write_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (id > current_pmc_id_) {
|
2019-01-06 21:39:10 +01:00
|
|
|
pmc.set("file_id", to_string(id.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
current_pmc_id_ = id;
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:32:14 +02:00
|
|
|
do_store_file_data_ref(id, new_id);
|
2021-10-07 12:18:00 +02:00
|
|
|
|
|
|
|
pmc.commit_transaction().ensure();
|
2018-04-04 15:32:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
void optimize_refs(const std::vector<FileDbId> ids, FileDbId main_id) {
|
|
|
|
LOG(INFO) << "Optimize " << ids.size() << " ids in file database to " << main_id.get();
|
2018-04-04 15:32:14 +02:00
|
|
|
auto &pmc = file_pmc();
|
2021-10-07 12:18:00 +02:00
|
|
|
pmc.begin_write_transaction().ensure();
|
2018-04-04 15:32:14 +02:00
|
|
|
for (size_t i = 0; i + 1 < ids.size(); i++) {
|
|
|
|
do_store_file_data_ref(ids[i], main_id);
|
|
|
|
}
|
2021-10-07 12:18:00 +02:00
|
|
|
pmc.commit_transaction().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-06 21:39:10 +01:00
|
|
|
FileDbId current_pmc_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<SqliteKeyValueSafe> file_kv_safe_;
|
|
|
|
|
|
|
|
SqliteKeyValue &file_pmc() {
|
|
|
|
return file_kv_safe_->get();
|
|
|
|
}
|
2018-04-04 15:32:14 +02:00
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
void do_store_file_data_ref(FileDbId id, FileDbId new_id) {
|
|
|
|
file_pmc().set(PSTRING() << "file" << id.get(), PSTRING() << "@@" << new_id.get());
|
2018-04-04 15:32:14 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit FileDb(std::shared_ptr<SqliteKeyValueSafe> kv_safe, int scheduler_id = -1) {
|
|
|
|
file_kv_safe_ = std::move(kv_safe);
|
|
|
|
CHECK(file_kv_safe_);
|
2019-01-06 21:39:10 +01:00
|
|
|
current_pmc_id_ = FileDbId(to_integer<uint64>(file_kv_safe_->get().get("file_id")));
|
2018-12-31 20:04:05 +01:00
|
|
|
file_db_actor_ =
|
|
|
|
create_actor_on_scheduler<FileDbActor>("FileDbActor", scheduler_id, current_pmc_id_, file_kv_safe_);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
FileDbId create_pmc_id() final {
|
2019-01-06 21:39:10 +01:00
|
|
|
current_pmc_id_ = FileDbId(current_pmc_id_.get() + 1);
|
|
|
|
return current_pmc_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void close(Promise<> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(std::move(file_db_actor_), &FileDbActor::close, std::move(promise));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void get_file_data_impl(string key, Promise<FileData> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(file_db_actor_, &FileDbActor::load_file_data, std::move(key), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Result<FileData> get_file_data_sync_impl(string key) final {
|
2020-06-09 16:04:47 +02:00
|
|
|
return load_file_data_impl(file_db_actor_.get(), file_kv_safe_->get(), key, current_pmc_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void clear_file_data(FileDbId id, const FileData &file_data) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
string remote_key;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (file_data.remote_.type() == RemoteFileLocation::Type::Full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
remote_key = as_key(file_data.remote_.full());
|
|
|
|
}
|
|
|
|
string local_key;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (file_data.local_.type() == LocalFileLocation::Type::Full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
local_key = as_key(file_data.local_.full());
|
|
|
|
}
|
|
|
|
string generate_key;
|
2018-02-04 18:27:05 +01:00
|
|
|
if (file_data.generate_ != nullptr) {
|
|
|
|
generate_key = as_key(*file_data.generate_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
send_closure(file_db_actor_, &FileDbActor::clear_file_data, id, remote_key, local_key, generate_key);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_file_data(FileDbId id, const FileData &file_data, bool new_remote, bool new_local, bool new_generate) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
string remote_key;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (file_data.remote_.type() == RemoteFileLocation::Type::Full && new_remote) {
|
2018-12-31 20:04:05 +01:00
|
|
|
remote_key = as_key(file_data.remote_.full());
|
|
|
|
}
|
|
|
|
string local_key;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (file_data.local_.type() == LocalFileLocation::Type::Full && new_local) {
|
2018-12-31 20:04:05 +01:00
|
|
|
local_key = as_key(file_data.local_.full());
|
|
|
|
}
|
|
|
|
string generate_key;
|
2018-02-04 18:27:05 +01:00
|
|
|
if (file_data.generate_ != nullptr && new_generate) {
|
|
|
|
generate_key = as_key(*file_data.generate_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-06 21:39:10 +01:00
|
|
|
LOG(DEBUG) << "SAVE " << id.get() << " -> " << file_data << " "
|
2019-02-15 21:09:18 +01:00
|
|
|
<< tag("remote_key", format::as_hex_dump<4>(Slice(remote_key)))
|
|
|
|
<< tag("local_key", format::as_hex_dump<4>(Slice(local_key)))
|
|
|
|
<< tag("generate_key", format::as_hex_dump<4>(Slice(generate_key)));
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(file_db_actor_, &FileDbActor::store_file_data, id, serialize(file_data), remote_key, local_key,
|
|
|
|
generate_key);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_file_data_ref(FileDbId id, FileDbId new_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(file_db_actor_, &FileDbActor::store_file_data_ref, id, new_id);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
SqliteKeyValue &pmc() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return file_kv_safe_->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorOwn<FileDbActor> file_db_actor_;
|
2019-01-06 21:39:10 +01:00
|
|
|
FileDbId current_pmc_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<SqliteKeyValueSafe> file_kv_safe_;
|
|
|
|
|
2018-04-04 15:32:14 +02:00
|
|
|
static Result<FileData> load_file_data_impl(ActorId<FileDbActor> file_db_actor_id, SqliteKeyValue &pmc,
|
2020-06-09 16:04:47 +02:00
|
|
|
const string &key, FileDbId current_pmc_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
//LOG(DEBUG) << "Load by key " << format::as_hex_dump<4>(Slice(key));
|
|
|
|
TRY_RESULT(id, get_id(pmc, key));
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
vector<FileDbId> ids;
|
2018-12-31 20:04:05 +01:00
|
|
|
string data_str;
|
2018-01-25 23:09:07 +01:00
|
|
|
int attempt_count = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
2018-04-04 15:32:14 +02:00
|
|
|
if (attempt_count > 100) {
|
2020-06-09 16:04:47 +02:00
|
|
|
LOG(FATAL) << "Cycle in file database? current_pmc_id=" << current_pmc_id << " key=" << key
|
2020-06-13 04:13:11 +02:00
|
|
|
<< " links=" << format::as_array(ids);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-25 23:09:07 +01:00
|
|
|
attempt_count++;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
data_str = pmc.get(PSTRING() << "file" << id.get());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto data_slice = Slice(data_str);
|
|
|
|
|
|
|
|
if (data_slice.substr(0, 2) == "@@") {
|
2018-04-04 15:32:14 +02:00
|
|
|
ids.push_back(id);
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
id = FileDbId(to_integer<uint64>(data_slice.substr(2)));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 15:32:14 +02:00
|
|
|
if (ids.size() > 1) {
|
|
|
|
send_closure(file_db_actor_id, &FileDbActor::optimize_refs, std::move(ids), id);
|
|
|
|
}
|
2021-03-15 01:32:14 +01:00
|
|
|
//LOG(DEBUG) << "By ID " << id.get() << " found data " << format::as_hex_dump<4>(Slice(data_str));
|
2018-04-04 21:37:14 +02:00
|
|
|
//LOG(INFO) << attempt_count;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event::WithVersion<TlParser> parser(data_str);
|
2019-06-11 12:10:14 +02:00
|
|
|
parser.set_version(static_cast<int32>(Version::Initial));
|
2018-12-31 20:04:05 +01:00
|
|
|
FileData data;
|
2019-03-17 22:40:10 +01:00
|
|
|
data.parse(parser, true);
|
|
|
|
parser.fetch_end();
|
|
|
|
auto status = parser.get_status();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status.is_error()) {
|
|
|
|
return std::move(status);
|
|
|
|
}
|
|
|
|
return std::move(data);
|
|
|
|
}
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
static Result<FileDbId> get_id(SqliteKeyValue &pmc, const string &key) TD_WARN_UNUSED_RESULT {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id_str = pmc.get(key);
|
2021-03-15 01:32:14 +01:00
|
|
|
//LOG(DEBUG) << "Found ID " << id_str << " by key " << format::as_hex_dump<4>(Slice(key));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (id_str.empty()) {
|
2018-12-13 23:48:36 +01:00
|
|
|
return Status::Error("There is no such a key in database");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-01-06 21:39:10 +01:00
|
|
|
return FileDbId(to_integer<uint64>(id_str));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<FileDbInterface> create_file_db(std::shared_ptr<SqliteConnectionSafe> connection, int scheduler_id) {
|
|
|
|
auto kv = std::make_shared<SqliteKeyValueSafe>("files", std::move(connection));
|
|
|
|
return std::make_shared<FileDb>(std::move(kv), scheduler_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|