TdDb: create sqlite db during SqliteDb::change_key

GitOrigin-RevId: bc0bff23a50f17d9111edac6ba4194fbdffaa1b4
This commit is contained in:
Arseny Smirnov 2020-08-14 17:11:58 +03:00
parent 11e09c5699
commit 45bfb1b384
6 changed files with 19 additions and 5 deletions

View File

@ -300,8 +300,9 @@ Status TdDb::init_sqlite(int32 scheduler_id, const TdParameters &parameters, DbK
}
sqlite_path_ = sql_database_path;
TRY_STATUS(SqliteDb::change_key(sqlite_path_, key, old_key));
TRY_RESULT(db_instance, SqliteDb::change_key(sqlite_path_, key, old_key));
sql_connection_ = std::make_shared<SqliteConnectionSafe>(sql_database_path, key);
sql_connection_->set(std::move(db_instance));
auto &db = sql_connection_->get();
TRY_STATUS(init_db(db));

View File

@ -51,6 +51,12 @@ class LazySchedulerLocalStorage {
create_func_ = create_func;
}
void set(T &&t) {
auto &optional_value_ = sls_optional_value_.get();
CHECK(!optional_value_);
optional_value_ = std::move(t);
}
T &get() {
auto &optional_value_ = sls_optional_value_.get();
if (!optional_value_) {

View File

@ -33,6 +33,10 @@ SqliteConnectionSafe::SqliteConnectionSafe(string path, DbKey key)
}) {
}
void SqliteConnectionSafe::set(SqliteDb &&db) {
lsls_connection_.set(std::move(db));
}
SqliteDb &SqliteConnectionSafe::get() {
return lsls_connection_.get();
}

View File

@ -21,6 +21,7 @@ class SqliteConnectionSafe {
explicit SqliteConnectionSafe(string path, DbKey key = DbKey::empty());
SqliteDb &get();
void set(SqliteDb &&db);
void close();

View File

@ -197,12 +197,14 @@ Result<SqliteDb> SqliteDb::do_open_with_key(CSlice path, const DbKey &db_key, bo
return std::move(db);
}
Status SqliteDb::change_key(CSlice path, const DbKey &new_db_key, const DbKey &old_db_key) {
Result<SqliteDb> SqliteDb::change_key(CSlice path, const DbKey &new_db_key, const DbKey &old_db_key) {
PerfWarningTimer perf("change key", 0.001);
// fast path
{
auto r_db = open_with_key(path, new_db_key);
if (r_db.is_ok()) {
return Status::OK();
return r_db;
}
}
@ -245,7 +247,7 @@ Status SqliteDb::change_key(CSlice path, const DbKey &new_db_key, const DbKey &o
TRY_RESULT(new_db, open_with_key(path, new_db_key));
LOG_CHECK(new_db.user_version().ok() == user_version) << new_db.user_version().ok() << " " << user_version;
return Status::OK();
return std::move(new_db);
}
Status SqliteDb::destroy(Slice path) {
return detail::RawSqliteDb::destroy(path);

View File

@ -62,7 +62,7 @@ class SqliteDb {
// Anyway we can't change the key on the fly, so having static functions is more than enough
static Result<SqliteDb> open_with_key(CSlice path, const DbKey &db_key);
static Status change_key(CSlice path, const DbKey &new_db_key, const DbKey &old_db_key);
static Result<SqliteDb> change_key(CSlice path, const DbKey &new_db_key, const DbKey &old_db_key);
Status last_error();