Replace begin_transaction with begin_read_transaction and begin_write_transaction.

This commit is contained in:
levlam 2021-10-07 13:18:00 +03:00
parent 4c803a1a75
commit b34cfef4d0
12 changed files with 44 additions and 35 deletions

View File

@ -288,8 +288,11 @@ class DialogDbImpl final : public DialogDbSyncInterface {
return std::move(notification_groups); return std::move(notification_groups);
} }
Status begin_transaction() final { Status begin_read_transaction() final {
return db_.begin_transaction(); return db_.begin_read_transaction();
}
Status begin_write_transaction() final {
return db_.begin_write_transaction();
} }
Status commit_transaction() final { Status commit_transaction() final {
return db_.commit_transaction(); return db_.commit_transaction();
@ -463,7 +466,7 @@ class DialogDbAsync final : public DialogDbAsyncInterface {
if (pending_writes_.empty()) { if (pending_writes_.empty()) {
return; return;
} }
sync_db_->begin_transaction().ensure(); sync_db_->begin_write_transaction().ensure();
for (auto &query : pending_writes_) { for (auto &query : pending_writes_) {
query.set_value(Unit()); query.set_value(Unit());
} }

View File

@ -55,7 +55,8 @@ class DialogDbSyncInterface {
virtual Result<int32> get_secret_chat_count(FolderId folder_id) = 0; virtual Result<int32> 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; virtual Status commit_transaction() = 0;
}; };

View File

@ -1313,7 +1313,7 @@ void LanguagePackManager::save_strings_to_database(SqliteKeyValue *kv, int32 new
return; return;
} }
kv->begin_transaction().ensure(); kv->begin_write_transaction().ensure();
for (auto str : strings) { for (auto str : strings) {
if (!is_valid_key(str.first)) { if (!is_valid_key(str.first)) {
LOG(ERROR) << "Have invalid key \"" << str.first << '"'; LOG(ERROR) << "Have invalid key \"" << str.first << '"';

View File

@ -809,8 +809,8 @@ class MessagesDbImpl final : public MessagesDbSyncInterface {
return std::move(result); return std::move(result);
} }
Status begin_transaction() final { Status begin_write_transaction() final {
return db_.begin_transaction(); return db_.begin_write_transaction();
} }
Status commit_transaction() final { Status commit_transaction() final {
return db_.commit_transaction(); return db_.commit_transaction();
@ -1176,7 +1176,7 @@ class MessagesDbAsync final : public MessagesDbAsyncInterface {
if (pending_writes_.empty()) { if (pending_writes_.empty()) {
return; return;
} }
sync_db_->begin_transaction().ensure(); sync_db_->begin_write_transaction().ensure();
for (auto &query : pending_writes_) { for (auto &query : pending_writes_) {
query.set_value(Unit()); query.set_value(Unit());
} }

View File

@ -100,7 +100,7 @@ class MessagesDbSyncInterface {
virtual Result<MessagesDbCallsResult> get_calls(MessagesDbCallsQuery query) = 0; virtual Result<MessagesDbCallsResult> get_calls(MessagesDbCallsQuery query) = 0;
virtual Result<MessagesDbFtsResult> get_messages_fts(MessagesDbFtsQuery query) = 0; virtual Result<MessagesDbFtsResult> get_messages_fts(MessagesDbFtsQuery query) = 0;
virtual Status begin_transaction() = 0; virtual Status begin_write_transaction() = 0;
virtual Status commit_transaction() = 0; virtual Status commit_transaction() = 0;
}; };

View File

@ -27387,7 +27387,7 @@ MessagesManager::MessageNotificationGroup MessagesManager::get_message_notificat
CHECK(d != nullptr); CHECK(d != nullptr);
} else if (G()->parameters().use_message_db) { } else if (G()->parameters().use_message_db) {
auto *dialog_db = G()->td_db()->get_dialog_db_sync(); 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); auto r_value = dialog_db->get_notification_group(group_id);
if (r_value.is_ok()) { if (r_value.is_ok()) {
VLOG(notifications) << "Loaded " << r_value.ok() << " from database by " << group_id; VLOG(notifications) << "Loaded " << r_value.ok() << " from database by " << group_id;
@ -27682,7 +27682,7 @@ vector<NotificationGroupKey> MessagesManager::get_message_notification_group_key
<< from_group_key; << from_group_key;
auto *dialog_db = G()->td_db()->get_dialog_db_sync(); auto *dialog_db = G()->td_db()->get_dialog_db_sync();
dialog_db->begin_transaction().ensure(); // read transaction dialog_db->begin_read_transaction().ensure();
Result<vector<NotificationGroupKey>> r_notification_group_keys = Result<vector<NotificationGroupKey>> r_notification_group_keys =
dialog_db->get_notification_groups_by_last_notification_date(from_group_key, limit); dialog_db->get_notification_groups_by_last_notification_date(from_group_key, limit);
r_notification_group_keys.ensure(); r_notification_group_keys.ensure();

View File

@ -6924,8 +6924,7 @@ void StickersManager::on_get_emoji_keywords_difference(
} }
version = keywords->version_; version = keywords->version_;
auto *pmc = G()->td_db()->get_sqlite_sync_pmc(); auto *pmc = G()->td_db()->get_sqlite_sync_pmc();
pmc->begin_transaction().ensure(); pmc->begin_write_transaction().ensure();
// set must be the first operation to start a write transaction
pmc->set(get_emoji_language_code_version_database_key(language_code), to_string(version)); 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())); pmc->set(get_emoji_language_code_last_difference_time_database_key(language_code), to_string(G()->unix_time()));
for (auto &keyword_ptr : keywords->keywords_) { for (auto &keyword_ptr : keywords->keywords_) {

View File

@ -23,7 +23,6 @@
#include "td/utils/format.h" #include "td/utils/format.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h" #include "td/utils/SliceBuilder.h"
#include "td/utils/Status.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) { void clear_file_data(FileDbId id, const string &remote_key, const string &local_key, const string &generate_key) {
auto &pmc = file_pmc(); auto &pmc = file_pmc();
pmc.begin_transaction().ensure(); pmc.begin_write_transaction().ensure();
SCOPE_EXIT {
pmc.commit_transaction().ensure();
};
if (id > current_pmc_id_) { if (id > current_pmc_id_) {
pmc.set("file_id", to_string(id.get())); pmc.set("file_id", to_string(id.get()));
@ -102,14 +98,13 @@ class FileDb final : public FileDbInterface {
if (!generate_key.empty()) { if (!generate_key.empty()) {
pmc.erase(generate_key); 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, void store_file_data(FileDbId id, const string &file_data, const string &remote_key, const string &local_key,
const string &generate_key) { const string &generate_key) {
auto &pmc = file_pmc(); auto &pmc = file_pmc();
pmc.begin_transaction().ensure(); pmc.begin_write_transaction().ensure();
SCOPE_EXIT {
pmc.commit_transaction().ensure();
};
if (id > current_pmc_id_) { if (id > current_pmc_id_) {
pmc.set("file_id", to_string(id.get())); pmc.set("file_id", to_string(id.get()));
@ -127,13 +122,12 @@ class FileDb final : public FileDbInterface {
if (!generate_key.empty()) { if (!generate_key.empty()) {
pmc.set(generate_key, to_string(id.get())); pmc.set(generate_key, to_string(id.get()));
} }
pmc.commit_transaction().ensure();
} }
void store_file_data_ref(FileDbId id, FileDbId new_id) { void store_file_data_ref(FileDbId id, FileDbId new_id) {
auto &pmc = file_pmc(); auto &pmc = file_pmc();
pmc.begin_transaction().ensure(); pmc.begin_write_transaction().ensure();
SCOPE_EXIT {
pmc.commit_transaction().ensure();
};
if (id > current_pmc_id_) { if (id > current_pmc_id_) {
pmc.set("file_id", to_string(id.get())); 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); do_store_file_data_ref(id, new_id);
pmc.commit_transaction().ensure();
} }
void optimize_refs(const std::vector<FileDbId> ids, FileDbId main_id) { void optimize_refs(const std::vector<FileDbId> ids, FileDbId main_id) {
LOG(INFO) << "Optimize " << ids.size() << " ids in file database to " << main_id.get(); LOG(INFO) << "Optimize " << ids.size() << " ids in file database to " << main_id.get();
auto &pmc = file_pmc(); auto &pmc = file_pmc();
pmc.begin_transaction().ensure(); pmc.begin_write_transaction().ensure();
SCOPE_EXIT {
pmc.commit_transaction().ensure();
};
for (size_t i = 0; i + 1 < ids.size(); i++) { for (size_t i = 0; i + 1 < ids.size(); i++) {
do_store_file_data_ref(ids[i], main_id); do_store_file_data_ref(ids[i], main_id);
} }
pmc.commit_transaction().ensure();
} }
private: private:

View File

@ -175,13 +175,20 @@ Status SqliteDb::set_user_version(int32 version) {
return exec(PSLICE() << "PRAGMA user_version = " << version); return exec(PSLICE() << "PRAGMA user_version = " << version);
} }
Status SqliteDb::begin_transaction() { Status SqliteDb::begin_read_transaction() {
if (raw_->on_begin()) { if (raw_->on_begin()) {
return exec("BEGIN"); return exec("BEGIN");
} }
return Status::OK(); return Status::OK();
} }
Status SqliteDb::begin_write_transaction() {
if (raw_->on_begin()) {
return exec("BEGIN IMMEDIATE");
}
return Status::OK();
}
Status SqliteDb::commit_transaction() { Status SqliteDb::commit_transaction() {
TRY_RESULT(need_commit, raw_->on_commit()); TRY_RESULT(need_commit, raw_->on_commit());
if (need_commit) { if (need_commit) {

View File

@ -46,7 +46,9 @@ class SqliteDb {
Result<bool> has_table(Slice table); Result<bool> has_table(Slice table);
Result<string> get_pragma(Slice name); Result<string> get_pragma(Slice name);
Result<string> get_pragma_string(Slice name); Result<string> 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; Status commit_transaction() TD_WARN_UNUSED_RESULT;
Result<int32> user_version(); Result<int32> user_version();

View File

@ -51,8 +51,11 @@ class SqliteKeyValue {
SeqNo erase(Slice key); SeqNo erase(Slice key);
Status begin_transaction() TD_WARN_UNUSED_RESULT { Status begin_read_transaction() TD_WARN_UNUSED_RESULT {
return db_.begin_transaction(); 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 { Status commit_transaction() TD_WARN_UNUSED_RESULT {
return db_.commit_transaction(); return db_.commit_transaction();

View File

@ -121,7 +121,7 @@ class SqliteKeyValueAsync final : public SqliteKeyValueAsyncInterface {
wakeup_at_ = 0; wakeup_at_ = 0;
cnt_ = 0; cnt_ = 0;
kv_->begin_transaction().ensure(); kv_->begin_write_transaction().ensure();
for (auto &it : buffer_) { for (auto &it : buffer_) {
if (it.second) { if (it.second) {
kv_->set(it.first, it.second.value()); kv_->set(it.first, it.second.value());