Open language database.

GitOrigin-RevId: 9765471da4bae835d7159ca90aa99106a251b654
This commit is contained in:
levlam 2018-07-20 20:50:38 +03:00
parent 92c04286e3
commit e8f4c31586
4 changed files with 53 additions and 11 deletions

View File

@ -12,14 +12,36 @@
#include "td/telegram/net/NetQueryDispatcher.h" #include "td/telegram/net/NetQueryDispatcher.h"
#include "td/telegram/Td.h" #include "td/telegram/Td.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"
#include "td/telegram/td_api.h" #include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/db/SqliteKeyValue.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"
namespace td { namespace td {
struct LanguagePackManager::LanguageDatabase {
std::mutex mutex_;
string path_;
SqliteKeyValue kv_;
std::unordered_map<string, std::unique_ptr<LanguagePack>> language_packs_;
};
LanguagePackManager::~LanguagePackManager() = default;
static Result<SqliteKeyValue> open_database(const string &path) {
TRY_RESULT(db, SqliteDb::open_with_key(path, DbKey::empty()));
TRY_STATUS(db.exec("PRAGMA synchronous=NORMAL"));
TRY_STATUS(db.exec("PRAGMA temp_store=MEMORY"));
TRY_STATUS(db.exec("PRAGMA encoding=\"UTF-8\""));
TRY_STATUS(db.exec("PRAGMA journal_mode=WAL"));
SqliteKeyValue kv;
TRY_STATUS(kv.init_with_connection(std::move(db), "lang"));
return std::move(kv);
}
void LanguagePackManager::start_up() { void LanguagePackManager::start_up() {
std::lock_guard<std::mutex> lock(language_database_mutex_); std::lock_guard<std::mutex> lock(language_database_mutex_);
manager_count_++; manager_count_++;
@ -29,10 +51,22 @@ void LanguagePackManager::start_up() {
string database_path = G()->shared_config().get_option_string("language_database_path"); string database_path = G()->shared_config().get_option_string("language_database_path");
auto it = language_databases_.find(database_path); auto it = language_databases_.find(database_path);
if (it == language_databases_.end()) { if (it == language_databases_.end()) {
it = language_databases_.emplace(database_path, make_unique<LanguageDatabase>()).first; SqliteKeyValue kv;
it->second->path_ = std::move(database_path); if (!database_path.empty()) {
auto r_kv = open_database(database_path);
if (r_kv.is_error()) {
LOG(ERROR) << "Can't open language database " << database_path << ": " << r_kv.error();
database_path = string();
it = language_databases_.find(database_path);
} else {
kv = r_kv.move_as_ok();
}
}
// TODO open database if (it == language_databases_.end()) {
it = language_databases_.emplace(database_path, make_unique<LanguageDatabase>()).first;
it->second->path_ = std::move(database_path);
}
} }
database_ = it->second.get(); database_ = it->second.get();

View File

@ -15,6 +15,7 @@
#include "td/actor/PromiseFuture.h" #include "td/actor/PromiseFuture.h"
#include "td/utils/Container.h" #include "td/utils/Container.h"
#include "td/utils/Status.h"
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
@ -28,6 +29,11 @@ class LanguagePackManager : public NetQueryCallback {
public: public:
explicit LanguagePackManager(ActorShared<> parent) : parent_(std::move(parent)) { explicit LanguagePackManager(ActorShared<> parent) : parent_(std::move(parent)) {
} }
LanguagePackManager(const LanguagePackManager &) = delete;
LanguagePackManager &operator=(const LanguagePackManager &) = delete;
LanguagePackManager(LanguagePackManager &&) = delete;
LanguagePackManager &operator=(LanguagePackManager &&) = delete;
~LanguagePackManager() override;
void on_language_pack_changed(); void on_language_pack_changed();
@ -66,11 +72,7 @@ class LanguagePackManager : public NetQueryCallback {
std::unordered_map<string, std::unique_ptr<Language>> languages_; std::unordered_map<string, std::unique_ptr<Language>> languages_;
}; };
struct LanguageDatabase { struct LanguageDatabase;
std::mutex mutex_;
string path_;
std::unordered_map<string, std::unique_ptr<LanguagePack>> language_packs_;
};
ActorShared<> parent_; ActorShared<> parent_;

View File

@ -25,7 +25,9 @@
#include "td/utils/Random.h" #include "td/utils/Random.h"
namespace td { namespace td {
namespace { namespace {
std::string get_binlog_path(const TdParameters &parameters) { std::string get_binlog_path(const TdParameters &parameters) {
return PSTRING() << parameters.database_directory << "td" << (parameters.use_test_dc ? "_test" : "") << ".binlog"; return PSTRING() << parameters.database_directory << "td" << (parameters.use_test_dc ? "_test" : "") << ".binlog";
} }
@ -434,4 +436,5 @@ void TdDb::with_db_path(std::function<void(CSlice)> callback) {
SqliteDb::with_db_path(sqlite_path(), callback); SqliteDb::with_db_path(sqlite_path(), callback);
callback(binlog_path()); callback(binlog_path());
} }
} // namespace td } // namespace td

View File

@ -184,6 +184,9 @@ class SqliteKeyValue {
} }
} }
bool empty() const {
return db_.empty();
}
void clear() { void clear() {
*this = SqliteKeyValue(); *this = SqliteKeyValue();
} }