From b34cfef4d09865112f66f9965993d6b5571d7534 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 7 Oct 2021 13:18:00 +0300 Subject: [PATCH] Replace begin_transaction with begin_read_transaction and begin_write_transaction. --- td/telegram/DialogDb.cpp | 9 ++++++--- td/telegram/DialogDb.h | 3 ++- td/telegram/LanguagePackManager.cpp | 2 +- td/telegram/MessagesDb.cpp | 6 +++--- td/telegram/MessagesDb.h | 2 +- td/telegram/MessagesManager.cpp | 4 ++-- td/telegram/StickersManager.cpp | 3 +-- td/telegram/files/FileDb.cpp | 28 +++++++++++----------------- tddb/td/db/SqliteDb.cpp | 9 ++++++++- tddb/td/db/SqliteDb.h | 4 +++- tddb/td/db/SqliteKeyValue.h | 7 +++++-- tddb/td/db/SqliteKeyValueAsync.cpp | 2 +- 12 files changed, 44 insertions(+), 35 deletions(-) diff --git a/td/telegram/DialogDb.cpp b/td/telegram/DialogDb.cpp index 2a28a195f..22a042f6f 100644 --- a/td/telegram/DialogDb.cpp +++ b/td/telegram/DialogDb.cpp @@ -288,8 +288,11 @@ class DialogDbImpl final : public DialogDbSyncInterface { return std::move(notification_groups); } - Status begin_transaction() final { - return db_.begin_transaction(); + Status begin_read_transaction() final { + return db_.begin_read_transaction(); + } + Status begin_write_transaction() final { + return db_.begin_write_transaction(); } Status commit_transaction() final { return db_.commit_transaction(); @@ -463,7 +466,7 @@ class DialogDbAsync final : public DialogDbAsyncInterface { if (pending_writes_.empty()) { return; } - sync_db_->begin_transaction().ensure(); + sync_db_->begin_write_transaction().ensure(); for (auto &query : pending_writes_) { query.set_value(Unit()); } diff --git a/td/telegram/DialogDb.h b/td/telegram/DialogDb.h index 5ed117355..37954ee41 100644 --- a/td/telegram/DialogDb.h +++ b/td/telegram/DialogDb.h @@ -55,7 +55,8 @@ class DialogDbSyncInterface { virtual Result get_secret_chat_count(FolderId folder_id) = 0; - virtual Status begin_transaction() = 0; + virtual Status begin_read_transaction() = 0; + virtual Status begin_write_transaction() = 0; virtual Status commit_transaction() = 0; }; diff --git a/td/telegram/LanguagePackManager.cpp b/td/telegram/LanguagePackManager.cpp index 2e214185b..5cce85efe 100644 --- a/td/telegram/LanguagePackManager.cpp +++ b/td/telegram/LanguagePackManager.cpp @@ -1313,7 +1313,7 @@ void LanguagePackManager::save_strings_to_database(SqliteKeyValue *kv, int32 new return; } - kv->begin_transaction().ensure(); + kv->begin_write_transaction().ensure(); for (auto str : strings) { if (!is_valid_key(str.first)) { LOG(ERROR) << "Have invalid key \"" << str.first << '"'; diff --git a/td/telegram/MessagesDb.cpp b/td/telegram/MessagesDb.cpp index a02cb5490..f15dc3d3b 100644 --- a/td/telegram/MessagesDb.cpp +++ b/td/telegram/MessagesDb.cpp @@ -809,8 +809,8 @@ class MessagesDbImpl final : public MessagesDbSyncInterface { return std::move(result); } - Status begin_transaction() final { - return db_.begin_transaction(); + Status begin_write_transaction() final { + return db_.begin_write_transaction(); } Status commit_transaction() final { return db_.commit_transaction(); @@ -1176,7 +1176,7 @@ class MessagesDbAsync final : public MessagesDbAsyncInterface { if (pending_writes_.empty()) { return; } - sync_db_->begin_transaction().ensure(); + sync_db_->begin_write_transaction().ensure(); for (auto &query : pending_writes_) { query.set_value(Unit()); } diff --git a/td/telegram/MessagesDb.h b/td/telegram/MessagesDb.h index 34474b7fe..33abd123b 100644 --- a/td/telegram/MessagesDb.h +++ b/td/telegram/MessagesDb.h @@ -100,7 +100,7 @@ class MessagesDbSyncInterface { virtual Result get_calls(MessagesDbCallsQuery query) = 0; virtual Result get_messages_fts(MessagesDbFtsQuery query) = 0; - virtual Status begin_transaction() = 0; + virtual Status begin_write_transaction() = 0; virtual Status commit_transaction() = 0; }; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 2cf666078..d759c3a9e 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -27387,7 +27387,7 @@ MessagesManager::MessageNotificationGroup MessagesManager::get_message_notificat CHECK(d != nullptr); } else if (G()->parameters().use_message_db) { auto *dialog_db = G()->td_db()->get_dialog_db_sync(); - dialog_db->begin_transaction().ensure(); // read transaction + dialog_db->begin_read_transaction().ensure(); auto r_value = dialog_db->get_notification_group(group_id); if (r_value.is_ok()) { VLOG(notifications) << "Loaded " << r_value.ok() << " from database by " << group_id; @@ -27682,7 +27682,7 @@ vector MessagesManager::get_message_notification_group_key << from_group_key; auto *dialog_db = G()->td_db()->get_dialog_db_sync(); - dialog_db->begin_transaction().ensure(); // read transaction + dialog_db->begin_read_transaction().ensure(); Result> r_notification_group_keys = dialog_db->get_notification_groups_by_last_notification_date(from_group_key, limit); r_notification_group_keys.ensure(); diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 4e669bedd..ccf14257c 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -6924,8 +6924,7 @@ void StickersManager::on_get_emoji_keywords_difference( } version = keywords->version_; auto *pmc = G()->td_db()->get_sqlite_sync_pmc(); - pmc->begin_transaction().ensure(); - // set must be the first operation to start a write transaction + pmc->begin_write_transaction().ensure(); pmc->set(get_emoji_language_code_version_database_key(language_code), to_string(version)); pmc->set(get_emoji_language_code_last_difference_time_database_key(language_code), to_string(G()->unix_time())); for (auto &keyword_ptr : keywords->keywords_) { diff --git a/td/telegram/files/FileDb.cpp b/td/telegram/files/FileDb.cpp index 31fc394e4..df92f5d60 100644 --- a/td/telegram/files/FileDb.cpp +++ b/td/telegram/files/FileDb.cpp @@ -23,7 +23,6 @@ #include "td/utils/format.h" #include "td/utils/logging.h" #include "td/utils/misc.h" -#include "td/utils/ScopeGuard.h" #include "td/utils/Slice.h" #include "td/utils/SliceBuilder.h" #include "td/utils/Status.h" @@ -78,10 +77,7 @@ class FileDb final : public FileDbInterface { void clear_file_data(FileDbId id, const string &remote_key, const string &local_key, const string &generate_key) { auto &pmc = file_pmc(); - pmc.begin_transaction().ensure(); - SCOPE_EXIT { - pmc.commit_transaction().ensure(); - }; + pmc.begin_write_transaction().ensure(); if (id > current_pmc_id_) { pmc.set("file_id", to_string(id.get())); @@ -102,14 +98,13 @@ class FileDb final : public FileDbInterface { if (!generate_key.empty()) { pmc.erase(generate_key); } + + pmc.commit_transaction().ensure(); } void store_file_data(FileDbId id, const string &file_data, const string &remote_key, const string &local_key, const string &generate_key) { auto &pmc = file_pmc(); - pmc.begin_transaction().ensure(); - SCOPE_EXIT { - pmc.commit_transaction().ensure(); - }; + pmc.begin_write_transaction().ensure(); if (id > current_pmc_id_) { pmc.set("file_id", to_string(id.get())); @@ -127,13 +122,12 @@ class FileDb final : public FileDbInterface { if (!generate_key.empty()) { pmc.set(generate_key, to_string(id.get())); } + + pmc.commit_transaction().ensure(); } void store_file_data_ref(FileDbId id, FileDbId new_id) { auto &pmc = file_pmc(); - pmc.begin_transaction().ensure(); - SCOPE_EXIT { - pmc.commit_transaction().ensure(); - }; + pmc.begin_write_transaction().ensure(); if (id > current_pmc_id_) { pmc.set("file_id", to_string(id.get())); @@ -141,18 +135,18 @@ class FileDb final : public FileDbInterface { } do_store_file_data_ref(id, new_id); + + pmc.commit_transaction().ensure(); } void optimize_refs(const std::vector ids, FileDbId main_id) { LOG(INFO) << "Optimize " << ids.size() << " ids in file database to " << main_id.get(); auto &pmc = file_pmc(); - pmc.begin_transaction().ensure(); - SCOPE_EXIT { - pmc.commit_transaction().ensure(); - }; + pmc.begin_write_transaction().ensure(); for (size_t i = 0; i + 1 < ids.size(); i++) { do_store_file_data_ref(ids[i], main_id); } + pmc.commit_transaction().ensure(); } private: diff --git a/tddb/td/db/SqliteDb.cpp b/tddb/td/db/SqliteDb.cpp index 8abf5e4ea..e282ce4b9 100644 --- a/tddb/td/db/SqliteDb.cpp +++ b/tddb/td/db/SqliteDb.cpp @@ -175,13 +175,20 @@ Status SqliteDb::set_user_version(int32 version) { return exec(PSLICE() << "PRAGMA user_version = " << version); } -Status SqliteDb::begin_transaction() { +Status SqliteDb::begin_read_transaction() { if (raw_->on_begin()) { return exec("BEGIN"); } return Status::OK(); } +Status SqliteDb::begin_write_transaction() { + if (raw_->on_begin()) { + return exec("BEGIN IMMEDIATE"); + } + return Status::OK(); +} + Status SqliteDb::commit_transaction() { TRY_RESULT(need_commit, raw_->on_commit()); if (need_commit) { diff --git a/tddb/td/db/SqliteDb.h b/tddb/td/db/SqliteDb.h index 022cab151..a6c899cbe 100644 --- a/tddb/td/db/SqliteDb.h +++ b/tddb/td/db/SqliteDb.h @@ -46,7 +46,9 @@ class SqliteDb { Result has_table(Slice table); Result get_pragma(Slice name); Result get_pragma_string(Slice name); - Status begin_transaction() TD_WARN_UNUSED_RESULT; + + Status begin_read_transaction() TD_WARN_UNUSED_RESULT; + Status begin_write_transaction() TD_WARN_UNUSED_RESULT; Status commit_transaction() TD_WARN_UNUSED_RESULT; Result user_version(); diff --git a/tddb/td/db/SqliteKeyValue.h b/tddb/td/db/SqliteKeyValue.h index a10654c52..b44537d48 100644 --- a/tddb/td/db/SqliteKeyValue.h +++ b/tddb/td/db/SqliteKeyValue.h @@ -51,8 +51,11 @@ class SqliteKeyValue { SeqNo erase(Slice key); - Status begin_transaction() TD_WARN_UNUSED_RESULT { - return db_.begin_transaction(); + Status begin_read_transaction() TD_WARN_UNUSED_RESULT { + return db_.begin_read_transaction(); + } + Status begin_write_transaction() TD_WARN_UNUSED_RESULT { + return db_.begin_write_transaction(); } Status commit_transaction() TD_WARN_UNUSED_RESULT { return db_.commit_transaction(); diff --git a/tddb/td/db/SqliteKeyValueAsync.cpp b/tddb/td/db/SqliteKeyValueAsync.cpp index 5f594e488..3f59b2b81 100644 --- a/tddb/td/db/SqliteKeyValueAsync.cpp +++ b/tddb/td/db/SqliteKeyValueAsync.cpp @@ -121,7 +121,7 @@ class SqliteKeyValueAsync final : public SqliteKeyValueAsyncInterface { wakeup_at_ = 0; cnt_ = 0; - kv_->begin_transaction().ensure(); + kv_->begin_write_transaction().ensure(); for (auto &it : buffer_) { if (it.second) { kv_->set(it.first, it.second.value());