Skip to content

Commit

Permalink
Merge pull request #434 from Infomaniak/KDESKTOP-1445-Sync-optimizati…
Browse files Browse the repository at this point in the history
…on-SyncDb-path

Kdesktop 1445 sync optimization sync db path
  • Loading branch information
ChristopheLarchier authored Dec 20, 2024
2 parents 0cb30a0 + 4aa3b6c commit 321fca3
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 514 deletions.
89 changes: 28 additions & 61 deletions src/libcommonserver/db/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ bool Db::init(const std::string &version) {
#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21 << 8))
#endif

if (!prepareQuery(CHECK_TABLE_EXISTENCE_REQUEST_ID, CHECK_TABLE_EXISTENCE_REQUEST)) return false;
if (!prepareQuery(CHECK_COLUMN_EXISTENCE_REQUEST_ID, CHECK_COLUMN_EXISTENCE_REQUEST)) return false;
if (!createAndPrepareRequest(CHECK_TABLE_EXISTENCE_REQUEST_ID, CHECK_TABLE_EXISTENCE_REQUEST)) return false;
if (!createAndPrepareRequest(CHECK_COLUMN_EXISTENCE_REQUEST_ID, CHECK_COLUMN_EXISTENCE_REQUEST)) return false;

if (!version.empty()) {
// Check if DB is already initialized
Expand All @@ -325,7 +325,7 @@ bool Db::init(const std::string &version) {
if (dbExists) {
// Check version
LOG_DEBUG(_logger, "Check DB version");
if (!prepareQuery(SELECT_VERSION_REQUEST_ID, SELECT_VERSION_REQUEST)) return false;
if (!createAndPrepareRequest(SELECT_VERSION_REQUEST_ID, SELECT_VERSION_REQUEST)) return false;

bool found = false;
if (!selectVersion(_fromVersion, found)) {
Expand All @@ -349,7 +349,7 @@ bool Db::init(const std::string &version) {
}

// Update version
if (!prepareQuery(UPDATE_VERSION_REQUEST_ID, UPDATE_VERSION_REQUEST)) return false;
if (!createAndPrepareRequest(UPDATE_VERSION_REQUEST_ID, UPDATE_VERSION_REQUEST)) return false;
if (!updateVersion(version, found)) {
LOG_WARN(_logger, "Error in Db::updateVersion");
return false;
Expand All @@ -364,7 +364,7 @@ bool Db::init(const std::string &version) {
} else {
// Create version table
LOG_DEBUG(_logger, "Create version table");
if (!prepareQuery(CREATE_VERSION_TABLE_ID, CREATE_VERSION_TABLE)) return false;
if (!createAndPrepareRequest(CREATE_VERSION_TABLE_ID, CREATE_VERSION_TABLE)) return false;

int errId = -1;
std::string error;
Expand All @@ -376,7 +376,7 @@ bool Db::init(const std::string &version) {

// Insert version
LOG_DEBUG(_logger, "Insert version " << version);
if (!prepareQuery(INSERT_VERSION_REQUEST_ID, INSERT_VERSION_REQUEST)) return false;
if (!createAndPrepareRequest(INSERT_VERSION_REQUEST_ID, INSERT_VERSION_REQUEST)) return false;
if (!insertVersion(version)) {
LOG_WARN(_logger, "Error in Db::insertVersion");
return false;
Expand Down Expand Up @@ -501,15 +501,7 @@ bool Db::checkConnect(const std::string &version) {
}

// SELECT_SQLITE_VERSION
int errId;
std::string error;

ASSERT(queryCreate(SELECT_SQLITE_VERSION_ID));
if (!queryPrepare(SELECT_SQLITE_VERSION_ID, SELECT_SQLITE_VERSION, false, errId, error)) {
LOG_WARN(_logger, "Error preparing query:" << SELECT_SQLITE_VERSION_ID);
queryFree(SELECT_SQLITE_VERSION_ID);
return false;
}
if (!createAndPrepareRequest(SELECT_SQLITE_VERSION_ID, SELECT_SQLITE_VERSION)) return false;
bool hasData;
if (!queryNext(SELECT_SQLITE_VERSION_ID, hasData) || !hasData) {
LOG_WARN(_logger, "Error getting query result: " << SELECT_SQLITE_VERSION_ID);
Expand All @@ -522,12 +514,7 @@ bool Db::checkConnect(const std::string &version) {
LOG_DEBUG(_logger, "sqlite3 version=" << result.c_str());

// PRAGMA_LOCKING_MODE
ASSERT(queryCreate(PRAGMA_LOCKING_MODE_ID));
if (!queryPrepare(PRAGMA_LOCKING_MODE_ID, PRAGMA_LOCKING_MODE, false, errId, error)) {
LOG_WARN(_logger, "Error preparing query:" << PRAGMA_LOCKING_MODE_ID);
queryFree(PRAGMA_LOCKING_MODE_ID);
return false;
}
if (!createAndPrepareRequest(PRAGMA_LOCKING_MODE_ID, PRAGMA_LOCKING_MODE)) return false;
if (!queryNext(PRAGMA_LOCKING_MODE_ID, hasData) || !hasData) {
LOG_WARN(_logger, "Error getting query result: " << PRAGMA_LOCKING_MODE_ID);
queryFree(PRAGMA_LOCKING_MODE_ID);
Expand All @@ -539,12 +526,7 @@ bool Db::checkConnect(const std::string &version) {

// PRAGMA_JOURNAL_MODE
std::string sql(PRAGMA_JOURNAL_MODE + _journalMode + ";");
ASSERT(queryCreate(PRAGMA_JOURNAL_MODE_ID));
if (!queryPrepare(PRAGMA_JOURNAL_MODE_ID, sql, false, errId, error)) {
LOG_WARN(_logger, "Error preparing query:" << PRAGMA_JOURNAL_MODE_ID);
queryFree(PRAGMA_JOURNAL_MODE_ID);
return false;
}
if (!createAndPrepareRequest(PRAGMA_JOURNAL_MODE_ID, sql.c_str())) return false;
if (!queryNext(PRAGMA_JOURNAL_MODE_ID, hasData) || !hasData) {
LOG_WARN(_logger, "Error getting query result: " << PRAGMA_JOURNAL_MODE_ID);
queryFree(PRAGMA_JOURNAL_MODE_ID);
Expand All @@ -559,12 +541,7 @@ bool Db::checkConnect(const std::string &version) {
std::string synchronousMode = "FULL";
if (_journalMode.compare("WAL") == 0) synchronousMode = "NORMAL";
sql = PRAGMA_SYNCHRONOUS + synchronousMode + ";";
ASSERT(queryCreate(PRAGMA_SYNCHRONOUS_ID));
if (!queryPrepare(PRAGMA_SYNCHRONOUS_ID, sql, false, errId, error)) {
LOG_WARN(_logger, "Error preparing query:" << PRAGMA_SYNCHRONOUS_ID);
queryFree(PRAGMA_SYNCHRONOUS_ID);
return false;
}
if (!createAndPrepareRequest(PRAGMA_SYNCHRONOUS_ID, sql.c_str())) return false;
if (!queryNext(PRAGMA_SYNCHRONOUS_ID, hasData)) {
LOG_WARN(_logger, "Error getting query result: " << PRAGMA_SYNCHRONOUS_ID);
queryFree(PRAGMA_SYNCHRONOUS_ID);
Expand All @@ -574,12 +551,7 @@ bool Db::checkConnect(const std::string &version) {
LOG_DEBUG(_logger, "sqlite3 synchronous=" << synchronousMode.c_str());

// PRAGMA_CASE_SENSITIVE_LIKE
ASSERT(queryCreate(PRAGMA_CASE_SENSITIVE_LIKE_ID));
if (!queryPrepare(PRAGMA_CASE_SENSITIVE_LIKE_ID, PRAGMA_CASE_SENSITIVE_LIKE, false, errId, error)) {
LOG_WARN(_logger, "Error preparing query:" << PRAGMA_CASE_SENSITIVE_LIKE_ID);
queryFree(PRAGMA_CASE_SENSITIVE_LIKE_ID);
return false;
}
if (!createAndPrepareRequest(PRAGMA_CASE_SENSITIVE_LIKE_ID, PRAGMA_CASE_SENSITIVE_LIKE)) return false;
if (!queryNext(PRAGMA_CASE_SENSITIVE_LIKE_ID, hasData)) {
LOG_WARN(_logger, "Error getting query result: " << PRAGMA_CASE_SENSITIVE_LIKE_ID);
queryFree(PRAGMA_CASE_SENSITIVE_LIKE_ID);
Expand All @@ -589,12 +561,7 @@ bool Db::checkConnect(const std::string &version) {
LOG_DEBUG(_logger, "sqlite3 case_sensitivity=ON");

// PRAGMA_FOREIGN_KEYS
ASSERT(queryCreate(PRAGMA_FOREIGN_KEYS_ID));
if (!queryPrepare(PRAGMA_FOREIGN_KEYS_ID, PRAGMA_FOREIGN_KEYS, false, errId, error)) {
LOG_WARN(_logger, "Error preparing query:" << PRAGMA_FOREIGN_KEYS_ID);
queryFree(PRAGMA_FOREIGN_KEYS_ID);
return false;
}
if (!createAndPrepareRequest(PRAGMA_FOREIGN_KEYS_ID, PRAGMA_FOREIGN_KEYS)) return false;
if (!queryNext(PRAGMA_FOREIGN_KEYS_ID, hasData)) {
LOG_WARN(_logger, "Error getting query result: " << PRAGMA_FOREIGN_KEYS_ID);
queryFree(PRAGMA_FOREIGN_KEYS_ID);
Expand All @@ -606,21 +573,6 @@ bool Db::checkConnect(const std::string &version) {
return true;
}

bool Db::prepareQuery(const std::string &queryId, const std::string &query) {
if (!queryCreate(queryId)) {
LOG_WARN(_logger, "SQL Error - Fail to create query " << queryId.c_str());
return false;
}

int errId = 0;
std::string error;
if (!queryPrepare(queryId, query, false, errId, error)) {
queryFree(queryId);
return sqlFail(queryId, error);
}
return true;
}

bool Db::addIntegerColumnIfMissing(const std::string &tableName, const std::string &columnName, bool *columnAdded /*= nullptr*/) {
const auto requestId = tableName + "add_column_" + columnName;
const auto request = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " INTEGER;";
Expand All @@ -633,7 +585,7 @@ bool Db::addColumnIfMissing(const std::string &tableName, const std::string &col
if (!columnExists(tableName, columnName, exist)) return false;
if (!exist) {
LOG_INFO(_logger, "Adding column " << columnName.c_str() << " into table " << tableName.c_str());
if (!prepareQuery(requestId, request)) return false;
if (!createAndPrepareRequest(requestId.c_str(), request.c_str())) return false;
int errId = 0;
std::string error;
if (!queryExec(requestId, errId, error)) {
Expand All @@ -647,6 +599,21 @@ bool Db::addColumnIfMissing(const std::string &tableName, const std::string &col
return true;
}

bool Db::createAndPrepareRequest(const char *requestId, const char *query) {
int errId = 0;
std::string error;

if (!queryCreate(requestId)) {
LOG_FATAL(_logger, "ENFORCE: \"queryCreate(" << requestId << ")\".");
}
if (!queryPrepare(requestId, query, false, errId, error)) {
queryFree(requestId);
return sqlFail(requestId, error);
}

return true;
}

bool Db::tableExists(const std::string &tableName, bool &exist) {
const std::scoped_lock lock(_mutex);

Expand Down
4 changes: 3 additions & 1 deletion src/libcommonserver/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ class COMMONSERVER_EXPORT Db {
bool sqlFail(const std::string &log, const std::string &error);
bool checkConnect(const std::string &version);

bool prepareQuery(const std::string &queryId, const std::string &query);
bool addIntegerColumnIfMissing(const std::string &tableName, const std::string &columnName, bool *columnAdded = nullptr);
bool addColumnIfMissing(const std::string &tableName, const std::string &columnName, const std::string &requestId,
const std::string &request, bool *columnAdded = nullptr);

// Helpers
bool createAndPrepareRequest(const char *requestId, const char *query);

log4cplus::Logger _logger;
std::shared_ptr<SqliteDb> _sqliteDb;
std::filesystem::path _dbPath;
Expand Down
17 changes: 17 additions & 0 deletions src/libcommonserver/utility/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,21 @@ struct COMMONSERVER_EXPORT Utility {

inline static log4cplus::Logger logger() { return Log::isSet() ? Log::instance()->getLogger() : _logger; }
};

struct TimeCounter {
explicit TimeCounter(const std::string &name) : _name(name) {}
void start() { _start = clock(); }
void end() {
_end = clock();
_total += (double) (_end - _start) / CLOCKS_PER_SEC;
}
void trace() { LOG_DEBUG(Log::instance()->getLogger(), "Time counter " << _name.c_str() << " value:" << _total); }

private:
std::string _name;
clock_t _start = 0;
clock_t _end = 0;
double _total = 0;
};

} // namespace KDC
Loading

0 comments on commit 321fca3

Please sign in to comment.