Combine TdDb::check_parameters and TdDb::open.
This commit is contained in:
parent
c47da24430
commit
ebb6770935
@ -2987,13 +2987,15 @@ void Td::run_request(uint64 id, tl_object_ptr<td_api::Function> function) {
|
||||
return send_closure(actor_id(this), &Td::send_error, id, std::move(status));
|
||||
}
|
||||
|
||||
VLOG(td_init) << "Begin to check parameters";
|
||||
VLOG(td_init) << "Begin to open database";
|
||||
set_parameters_request_id_ = id;
|
||||
|
||||
auto promise =
|
||||
PromiseCreator::lambda([actor_id = actor_id(this)](Result<TdDb::CheckedParameters> r_checked_parameters) {
|
||||
send_closure(actor_id, &Td::on_parameters_checked, std::move(r_checked_parameters));
|
||||
PromiseCreator::lambda([actor_id = actor_id(this)](Result<TdDb::OpenedDatabase> r_opened_database) {
|
||||
send_closure(actor_id, &Td::init, std::move(r_opened_database));
|
||||
});
|
||||
return TdDb::check_parameters(get_database_scheduler_id(), parameters_, std::move(promise));
|
||||
return TdDb::open(get_database_scheduler_id(), parameters_, as_db_key(std::move(database_encryption_key_)),
|
||||
std::move(promise));
|
||||
}
|
||||
default:
|
||||
if (is_preinitialization_request(function_id)) {
|
||||
@ -3539,26 +3541,6 @@ int32 Td::get_database_scheduler_id() {
|
||||
return min(current_scheduler_id + 1, scheduler_count - 1);
|
||||
}
|
||||
|
||||
void Td::on_parameters_checked(Result<TdDb::CheckedParameters> r_checked_parameters) {
|
||||
CHECK(set_parameters_request_id_ != 0);
|
||||
if (r_checked_parameters.is_error()) {
|
||||
send_closure(actor_id(this), &Td::send_error, set_parameters_request_id_,
|
||||
Status::Error(400, r_checked_parameters.error().message()));
|
||||
return finish_set_parameters();
|
||||
}
|
||||
auto checked_parameters = r_checked_parameters.move_as_ok();
|
||||
|
||||
parameters_.database_directory = std::move(checked_parameters.database_directory);
|
||||
parameters_.files_directory = std::move(checked_parameters.files_directory);
|
||||
|
||||
VLOG(td_init) << "Begin to init database";
|
||||
auto promise = PromiseCreator::lambda([actor_id = actor_id(this)](Result<TdDb::OpenedDatabase> r_opened_database) {
|
||||
send_closure(actor_id, &Td::init, std::move(r_opened_database));
|
||||
});
|
||||
TdDb::open(get_database_scheduler_id(), parameters_, as_db_key(std::move(database_encryption_key_)),
|
||||
std::move(promise));
|
||||
}
|
||||
|
||||
void Td::finish_set_parameters() {
|
||||
CHECK(set_parameters_request_id_ != 0);
|
||||
set_parameters_request_id_ = 0;
|
||||
@ -3583,12 +3565,15 @@ void Td::init(Result<TdDb::OpenedDatabase> r_opened_database) {
|
||||
Status::Error(400, r_opened_database.error().message()));
|
||||
return finish_set_parameters();
|
||||
}
|
||||
auto events = r_opened_database.move_as_ok();
|
||||
|
||||
parameters_.database_directory = std::move(events.database_directory);
|
||||
parameters_.files_directory = std::move(events.files_directory);
|
||||
|
||||
LOG(INFO) << "Successfully inited database in " << tag("database_directory", parameters_.database_directory)
|
||||
<< " and " << tag("files_directory", parameters_.files_directory);
|
||||
VLOG(td_init) << "Successfully inited database";
|
||||
|
||||
auto events = r_opened_database.move_as_ok();
|
||||
G()->init(parameters_, actor_id(this), std::move(events.database)).ensure();
|
||||
|
||||
init_options_and_network();
|
||||
|
@ -1463,8 +1463,6 @@ class Td final : public Actor {
|
||||
|
||||
static int32 get_database_scheduler_id();
|
||||
|
||||
void on_parameters_checked(Result<TdDb::CheckedParameters> r_checked_parameters);
|
||||
|
||||
void finish_set_parameters();
|
||||
|
||||
void init(Result<TdDb::OpenedDatabase> r_opened_database);
|
||||
|
@ -385,7 +385,11 @@ void TdDb::open(int32 scheduler_id, TdParameters parameters, DbKey key, Promise<
|
||||
}
|
||||
|
||||
void TdDb::open_impl(TdParameters parameters, DbKey key, Promise<OpenedDatabase> &&promise) {
|
||||
TRY_STATUS_PROMISE(promise, check_parameters(parameters));
|
||||
|
||||
OpenedDatabase result;
|
||||
result.database_directory = parameters.database_directory;
|
||||
result.files_directory = parameters.files_directory;
|
||||
|
||||
// Init pmc
|
||||
Binlog *binlog_ptr = nullptr;
|
||||
@ -478,16 +482,7 @@ void TdDb::open_impl(TdParameters parameters, DbKey key, Promise<OpenedDatabase>
|
||||
TdDb::TdDb() = default;
|
||||
TdDb::~TdDb() = default;
|
||||
|
||||
void TdDb::check_parameters(int32 scheduler_id, TdParameters parameters, Promise<CheckedParameters> promise) {
|
||||
Scheduler::instance()->run_on_scheduler(
|
||||
scheduler_id, [parameters = std::move(parameters), promise = std::move(promise)](Unit) mutable {
|
||||
TdDb::check_parameters_impl(std::move(parameters), std::move(promise));
|
||||
});
|
||||
}
|
||||
|
||||
void TdDb::check_parameters_impl(TdParameters parameters, Promise<CheckedParameters> promise) {
|
||||
CheckedParameters result;
|
||||
|
||||
Status TdDb::check_parameters(TdParameters ¶meters) {
|
||||
auto prepare_dir = [](string dir) -> Result<string> {
|
||||
CHECK(!dir.empty());
|
||||
if (dir.back() != TD_DIR_SLASH) {
|
||||
@ -504,22 +499,20 @@ void TdDb::check_parameters_impl(TdParameters parameters, Promise<CheckedParamet
|
||||
auto r_database_directory = prepare_dir(parameters.database_directory);
|
||||
if (r_database_directory.is_error()) {
|
||||
VLOG(td_init) << "Invalid database_directory";
|
||||
return promise.set_error(Status::Error(PSLICE()
|
||||
<< "Can't init database in the directory \"" << parameters.database_directory
|
||||
<< "\": " << r_database_directory.error()));
|
||||
return Status::Error(PSLICE() << "Can't init database in the directory \"" << parameters.database_directory
|
||||
<< "\": " << r_database_directory.error());
|
||||
}
|
||||
result.database_directory = r_database_directory.move_as_ok();
|
||||
parameters.database_directory = result.database_directory;
|
||||
parameters.database_directory = r_database_directory.move_as_ok();
|
||||
|
||||
auto r_files_directory = prepare_dir(parameters.files_directory);
|
||||
if (r_files_directory.is_error()) {
|
||||
VLOG(td_init) << "Invalid files_directory";
|
||||
return promise.set_error(Status::Error(PSLICE() << "Can't init files directory \"" << parameters.files_directory
|
||||
<< "\": " << r_files_directory.error()));
|
||||
return Status::Error(PSLICE() << "Can't init files directory \"" << parameters.files_directory
|
||||
<< "\": " << r_files_directory.error());
|
||||
}
|
||||
result.files_directory = r_files_directory.move_as_ok();
|
||||
parameters.files_directory = r_files_directory.move_as_ok();
|
||||
|
||||
promise.set_value(std::move(result));
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
void TdDb::change_key(DbKey key, Promise<> promise) {
|
||||
|
@ -47,13 +47,10 @@ class TdDb {
|
||||
TdDb &operator=(TdDb &&) = delete;
|
||||
~TdDb();
|
||||
|
||||
struct CheckedParameters {
|
||||
struct OpenedDatabase {
|
||||
string database_directory;
|
||||
string files_directory;
|
||||
};
|
||||
static void check_parameters(int32 scheduler_id, TdParameters parameters, Promise<CheckedParameters> promise);
|
||||
|
||||
struct OpenedDatabase {
|
||||
unique_ptr<TdDb> database;
|
||||
|
||||
vector<BinlogEvent> to_secret_chats_manager;
|
||||
@ -126,7 +123,7 @@ class TdDb {
|
||||
|
||||
static void open_impl(TdParameters parameters, DbKey key, Promise<OpenedDatabase> &&promise);
|
||||
|
||||
static void check_parameters_impl(TdParameters parameters, Promise<CheckedParameters> promise);
|
||||
static Status check_parameters(TdParameters ¶meters);
|
||||
|
||||
Status init_sqlite(const TdParameters ¶meters, const DbKey &key, const DbKey &old_key,
|
||||
BinlogKeyValue<Binlog> &binlog_pmc);
|
||||
|
Loading…
x
Reference in New Issue
Block a user