From 2a7adfe766f6258e420c2b51dee79447684c40c2 Mon Sep 17 00:00:00 2001 From: alex-z Date: Wed, 10 Jan 2024 18:04:01 +0100 Subject: [PATCH] Give a proper name to a parent path update metho in SyncJournal. Signed-off-by: alex-z --- src/common/preparedsqlquerymanager.h | 6 +-- src/common/syncjournaldb.cpp | 56 ++++++++++++---------------- src/common/syncjournaldb.h | 9 ++--- src/libsync/propagateremotemove.cpp | 2 +- test/testsyncjournaldb.cpp | 5 +-- 5 files changed, 32 insertions(+), 46 deletions(-) diff --git a/src/common/preparedsqlquerymanager.h b/src/common/preparedsqlquerymanager.h index 7300ff3705093..fb6f2042031b4 100644 --- a/src/common/preparedsqlquerymanager.h +++ b/src/common/preparedsqlquerymanager.h @@ -107,12 +107,8 @@ class OCSYNC_EXPORT PreparedSqlQueryManager GetE2EeLockedFolderQuery, GetE2EeLockedFoldersQuery, DeleteE2EeLockedFolderQuery, -<<<<<<< HEAD ListAllTopLevelE2eeFoldersStatusLessThanQuery, -======= - MoveFilesInPathQuery, ->>>>>>> 6e3bb76cc (On folder move execute only one UPDATE query for all nested items.) - + RelocateFolderToNewPathRecursivelyQuery, PreparedQueryCount }; PreparedSqlQueryManager() = default; diff --git a/src/common/syncjournaldb.cpp b/src/common/syncjournaldb.cpp index 66d906fdadbd7..00c1d08e4b65a 100644 --- a/src/common/syncjournaldb.cpp +++ b/src/common/syncjournaldb.cpp @@ -1042,7 +1042,6 @@ Result SyncJournalDb::setFileRecord(const SyncJournalFileRecord & return {}; } -<<<<<<< HEAD bool SyncJournalDb::getRootE2eFolderRecord(const QString &remoteFolderPath, SyncJournalFileRecord *rec) { Q_ASSERT(rec); @@ -1088,28 +1087,13 @@ bool SyncJournalDb::listAllE2eeFoldersWithEncryptionStatusLessThan(const int sta if (!checkConnect()) return false; - const auto query = _queryManager.get(PreparedSqlQueryManager::ListAllTopLevelE2eeFoldersStatusLessThanQuery, - QByteArrayLiteral(GET_FILE_RECORD_QUERY " WHERE type == 2 AND isE2eEncrypted >= ?1 AND isE2eEncrypted < ?2 ORDER BY path||'/' ASC"), -======= -bool SyncJournalDb::updateParentForAllChildren(const QByteArray &oldParentPath, const QByteArray &newParentPath) -{ - qCInfo(lcDb) << "Moving files from path" << oldParentPath << "to path" << newParentPath; - - if (!checkConnect()) { - qCWarning(lcDb) << "Failed to connect database."; - return false; - } - - const auto query = _queryManager.get(PreparedSqlQueryManager::MoveFilesInPathQuery, - QByteArrayLiteral("UPDATE metadata" - " SET path = REPLACE(path, ?1, ?2), phash = path_hash(REPLACE(path, ?1, ?2)), pathlen = path_length(REPLACE(path, ?1, ?2))" - " WHERE " IS_PREFIX_PATH_OF("?1", "path")), ->>>>>>> 6e3bb76cc (On folder move execute only one UPDATE query for all nested items.) - _db); + const auto query = + _queryManager.get(PreparedSqlQueryManager::ListAllTopLevelE2eeFoldersStatusLessThanQuery, + QByteArrayLiteral(GET_FILE_RECORD_QUERY " WHERE type == 2 AND isE2eEncrypted >= ?1 AND isE2eEncrypted < ?2 ORDER BY path||'/' ASC"), + _db); if (!query) { return false; } -<<<<<<< HEAD query->bindValue(1, SyncJournalFileRecord::EncryptionStatus::Encrypted); query->bindValue(2, status); @@ -1159,21 +1143,29 @@ bool SyncJournalDb::findEncryptedAncestorForRecord(const QString &filename, Sync pathComponents.removeLast(); } return true; -======= +} + +bool SyncJournalDb::relocateFolderToNewPathRecursively(const QByteArray &oldParentPath, const QByteArray &newParentPath) +{ + qCInfo(lcDb) << "Relocating folder recursively from path" << oldParentPath << "to path" << newParentPath; + + if (!checkConnect()) { + qCWarning(lcDb) << "Failed to connect database."; + return false; + } + + const auto query = _queryManager.get( + PreparedSqlQueryManager::RelocateFolderToNewPathRecursivelyQuery, + QByteArrayLiteral("UPDATE metadata" + " SET path = REPLACE(path, ?1, ?2), phash = path_hash(REPLACE(path, ?1, ?2)), pathlen = path_length(REPLACE(path, ?1, ?2))" + " WHERE " IS_PREFIX_PATH_OF("?1", "path")), + _db); + if (!query) { + return false; + } query->bindValue(1, oldParentPath); query->bindValue(2, newParentPath); -<<<<<<< HEAD -<<<<<<< HEAD - return query->exec(); ->>>>>>> 6e3bb76cc (On folder move execute only one UPDATE query for all nested items.) -======= - auto res = query->exec(); - auto numRows = query->numRowsAffected(); - return res; ->>>>>>> eb7234f4d (Iteration.) -======= return query->exec(); ->>>>>>> 3f2c9535f (Fix compile issues.) } void SyncJournalDb::keyValueStoreSet(const QString &key, QVariant value) diff --git a/src/common/syncjournaldb.h b/src/common/syncjournaldb.h index 48c3638e1bccf..22123691a32af 100644 --- a/src/common/syncjournaldb.h +++ b/src/common/syncjournaldb.h @@ -70,14 +70,13 @@ class OCSYNC_EXPORT SyncJournalDb : public QObject [[nodiscard]] bool getFilesBelowPath(const QByteArray &path, const std::function &rowCallback); [[nodiscard]] bool listFilesInPath(const QByteArray &path, const std::function &rowCallback); [[nodiscard]] Result setFileRecord(const SyncJournalFileRecord &record); -<<<<<<< HEAD + [[nodiscard]] bool getRootE2eFolderRecord(const QString &remoteFolderPath, SyncJournalFileRecord *rec); [[nodiscard]] bool listAllE2eeFoldersWithEncryptionStatusLessThan(const int status, const std::function &rowCallback); [[nodiscard]] bool findEncryptedAncestorForRecord(const QString &filename, SyncJournalFileRecord *rec); -======= - [[nodiscard]] bool updateParentForAllChildren(const QByteArray &oldParentPath, const QByteArray &newParentPath); ->>>>>>> 6e3bb76cc (On folder move execute only one UPDATE query for all nested items.) - + // use this after moving a folder and all its contents under new parent (e.g. "folderA" move to "parentFolder", such that "folderA" -> "parentFolder/folderA" + // all nested items will have their paths updated accordingly wiht a single UPDATE query + [[nodiscard]] bool relocateFolderToNewPathRecursively(const QByteArray &oldParentPath, const QByteArray &newParentPath); void keyValueStoreSet(const QString &key, QVariant value); [[nodiscard]] qint64 keyValueStoreGetInt(const QString &key, qint64 defaultValue); void keyValueStoreDelete(const QString &key); diff --git a/src/libsync/propagateremotemove.cpp b/src/libsync/propagateremotemove.cpp index eee0f4b58a7a7..2958cdb95639e 100644 --- a/src/libsync/propagateremotemove.cpp +++ b/src/libsync/propagateremotemove.cpp @@ -310,7 +310,7 @@ void PropagateRemoteMove::finalize() } if (_item->isDirectory()) { - if (!propagator()->_journal->updateParentForAllChildren(origin.toUtf8(), _item->_renameTarget.toUtf8())) { + if (!propagator()->_journal->relocateFolderToNewPathRecursively(origin.toUtf8(), _item->_renameTarget.toUtf8())) { done(SyncFileItem::FatalError, tr("Failed to move folder: %1").arg(_item->_file), ErrorCategory::GenericError); return; } diff --git a/test/testsyncjournaldb.cpp b/test/testsyncjournaldb.cpp index d6f77dd7c928e..0aee79b13007a 100644 --- a/test/testsyncjournaldb.cpp +++ b/test/testsyncjournaldb.cpp @@ -499,9 +499,8 @@ private slots: QVERIFY(makeEntry(folder2ContentsMoved.first().first, folder2ContentsMoved.first().second, initialEtag)); // move a folder under new location, all children paths must get updated with one query - QVERIFY(_db.updateParentForAllChildren(folder1Contents.first().first, folder1ContentsMoved.first().first)); - QVERIFY(_db.updateParentForAllChildren(folder2Contents.first().first, folder2ContentsMoved.first().first)); - + QVERIFY(_db.relocateFolderToNewPathRecursively(folder1Contents.first().first, folder1ContentsMoved.first().first)); + QVERIFY(_db.relocateFolderToNewPathRecursively(folder2Contents.first().first, folder2ContentsMoved.first().first)); // verify all moved records exist under new paths for (const auto &folderItemMoved : folder1ContentsMoved) { SyncJournalFileRecord movedRecord;