From c63144f22fc9d065a8108be3ee099321bf61ba62 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 3 Oct 2019 20:15:15 +0300 Subject: [PATCH] Add SqliteConnectionSafe.cpp. GitOrigin-RevId: 6e56f276c9467383c1eaa462ee7eb3530fafca55 --- tddb/CMakeLists.txt | 5 ++-- tddb/td/db/SqliteConnectionSafe.cpp | 45 +++++++++++++++++++++++++++++ tddb/td/db/SqliteConnectionSafe.h | 37 +++++------------------- 3 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 tddb/td/db/SqliteConnectionSafe.cpp diff --git a/tddb/CMakeLists.txt b/tddb/CMakeLists.txt index 50e17120..34a47c58 100644 --- a/tddb/CMakeLists.txt +++ b/tddb/CMakeLists.txt @@ -8,17 +8,18 @@ set(TDDB_SOURCE td/db/binlog/detail/BinlogEventsBuffer.cpp td/db/binlog/detail/BinlogEventsProcessor.cpp + td/db/SqliteConnectionSafe.cpp td/db/SqliteDb.cpp - td/db/SqliteStatement.cpp td/db/SqliteKeyValue.cpp td/db/SqliteKeyValueAsync.cpp + td/db/SqliteStatement.cpp td/db/detail/RawSqliteDb.cpp td/db/binlog/Binlog.h - td/db/binlog/BinlogInterface.h td/db/binlog/BinlogEvent.h td/db/binlog/BinlogHelper.h + td/db/binlog/BinlogInterface.h td/db/binlog/ConcurrentBinlog.h td/db/binlog/detail/BinlogEventsBuffer.h td/db/binlog/detail/BinlogEventsProcessor.h diff --git a/tddb/td/db/SqliteConnectionSafe.cpp b/tddb/td/db/SqliteConnectionSafe.cpp new file mode 100644 index 00000000..f178ad0f --- /dev/null +++ b/tddb/td/db/SqliteConnectionSafe.cpp @@ -0,0 +1,45 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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/SqliteConnectionSafe.h" + +#include "td/utils/common.h" +#include "td/utils/format.h" +#include "td/utils/logging.h" + +namespace td { + +SqliteConnectionSafe::SqliteConnectionSafe(string name, DbKey key) + : name_(std::move(name)), lsls_connection_([name = name_, key = std::move(key)] { + auto r_db = SqliteDb::open_with_key(name, key); + if (r_db.is_error()) { + LOG(FATAL) << "Can't open database " << name << ": " << r_db.error(); + } + auto db = r_db.move_as_ok(); + db.exec("PRAGMA synchronous=NORMAL").ensure(); + db.exec("PRAGMA temp_store=MEMORY").ensure(); + db.exec("PRAGMA secure_delete=1").ensure(); + db.exec("PRAGMA recursive_triggers=1").ensure(); + return db; + }) { +} + +SqliteDb &SqliteConnectionSafe::get() { + return lsls_connection_.get(); +} + +void SqliteConnectionSafe::close() { + LOG(INFO) << "Close SQLite database " << tag("path", name_); + lsls_connection_.clear_values(); +} + +void SqliteConnectionSafe::close_and_destroy() { + close(); + LOG(INFO) << "Destroy SQLite database " << tag("path", name_); + SqliteDb::destroy(name_).ignore(); +} + +} // namespace td diff --git a/tddb/td/db/SqliteConnectionSafe.h b/tddb/td/db/SqliteConnectionSafe.h index 0df90afa..f0a78104 100644 --- a/tddb/td/db/SqliteConnectionSafe.h +++ b/tddb/td/db/SqliteConnectionSafe.h @@ -8,50 +8,27 @@ #include "td/actor/SchedulerLocalStorage.h" +#include "td/db/DbKey.h" #include "td/db/SqliteDb.h" #include "td/utils/common.h" -#include "td/utils/format.h" -#include "td/utils/logging.h" namespace td { class SqliteConnectionSafe { public: SqliteConnectionSafe() = default; - explicit SqliteConnectionSafe(string name, DbKey key = DbKey::empty()) - : lsls_connection_([name = name, key = std::move(key)] { - auto r_db = SqliteDb::open_with_key(name, key); - if (r_db.is_error()) { - LOG(FATAL) << "Can't open database " << name << ": " << r_db.error(); - } - auto db = r_db.move_as_ok(); - db.exec("PRAGMA synchronous=NORMAL").ensure(); - db.exec("PRAGMA temp_store=MEMORY").ensure(); - db.exec("PRAGMA secure_delete=1").ensure(); - db.exec("PRAGMA recursive_triggers=1").ensure(); - return db; - }) - , name_(std::move(name)) { - } + explicit SqliteConnectionSafe(string name, DbKey key = DbKey::empty()); - SqliteDb &get() { - return lsls_connection_.get(); - } + SqliteDb &get(); - void close() { - LOG(INFO) << "Close SQLite database " << tag("path", name_); - lsls_connection_.clear_values(); - } - void close_and_destroy() { - close(); - LOG(INFO) << "Destroy SQLite database " << tag("path", name_); - SqliteDb::destroy(name_).ignore(); - } + void close(); + + void close_and_destroy(); private: - LazySchedulerLocalStorage lsls_connection_; string name_; + LazySchedulerLocalStorage lsls_connection_; }; } // namespace td