Skip to content

Commit

Permalink
Merge pull request #435 from Infomaniak/KDESKTOP-1447-Optimization-Im…
Browse files Browse the repository at this point in the history
…plementation-of-a-cache-on-Snapshot-path-

Optimization - Implementation of a cache on Snapshot::path()
  • Loading branch information
ChristopheLarchier authored Dec 20, 2024
2 parents 321fca3 + 77e6618 commit 4a21e25
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "csvfullfilelistwithcursorjob.h"
#include "libcommonserver/utility/utility.h"
#include "update_detection/file_system_observer/snapshot/snapshotitem.h"

#ifdef _WIN32
#include "reconciliation/platform_inconsistency_checker/platforminconsistencycheckerutility.h"
Expand All @@ -32,7 +31,7 @@ static const std::string endOfFileDelimiter("#EOF");

namespace KDC {

SnapshotItemHandler::SnapshotItemHandler(log4cplus::Logger logger) : _logger(logger) {};
SnapshotItemHandler::SnapshotItemHandler(log4cplus::Logger logger) : _logger(logger) {}

void SnapshotItemHandler::logError(const std::wstring &methodName, const std::wstring &stdErrorType, const std::string &str,
const std::exception &exc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SnapshotItemHandler {
CsvIndexEnd
};

inline static void incrementCsvIndex(CsvIndex &index) { index = static_cast<CsvIndex>(static_cast<int>(index) + 1); };
inline static void incrementCsvIndex(CsvIndex &index) { index = static_cast<CsvIndex>(static_cast<int>(index) + 1); }

struct ParsingState {
CsvIndex index{CsvIndexId}; // The index of the column that is currently read.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#pragma once

#include "abstracttokennetworkjob.h"
#include "update_detection/file_system_observer/snapshot/snapshotitem.h"

namespace KDC {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "localfilesystemobserverworker.h"
#include "libcommon/utility/utility.h"
#include "libcommon/log/sentry/ptraces.h"

#include "libcommonserver/io/filestat.h"
#include "libcommonserver/io/iohelper.h"
#include "libcommonserver/utility/utility.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#include "remotefilesystemobserverworker.h"
#include "jobs/jobmanager.h"
#include "../../jobs/network/API_v2/csvfullfilelistwithcursorjob.h"
#include "../../jobs/network/API_v2/getfileinfojob.h"
#include "../../jobs/network/API_v2/longpolljob.h"
#include "../../jobs/network/API_v2/continuefilelistwithcursorjob.h"
#include "jobs/network/API_v2/csvfullfilelistwithcursorjob.h"
#include "jobs/network/API_v2/getfileinfojob.h"
#include "jobs/network/API_v2/longpolljob.h"
#include "jobs/network/API_v2/continuefilelistwithcursorjob.h"
#ifdef _WIN32
#include "reconciliation/platform_inconsistency_checker/platforminconsistencycheckerutility.h"
#endif
Expand All @@ -32,6 +32,7 @@
#include "requests/parameterscache.h"
#include "requests/exclusiontemplatecache.h"
#include "utility/jsonparserutility.h"

#ifdef __APPLE__
#include "utility/utility.h"
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#pragma once

#include "filesystemobserverworker.h"

#include "jobs/network/networkjobsparams.h"
#include "update_detection/file_system_observer/snapshot/snapshotitem.h"

#include <Poco/JSON/Object.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Snapshot &Snapshot::operator=(Snapshot &other) {

_items = other._items;
_isValid = other._isValid;
_copy = true;
}

return *this;
Expand Down Expand Up @@ -236,15 +237,22 @@ bool Snapshot::path(const NodeId &itemId, SyncPath &path, bool &ignore) const no
}

bool ok = true;
std::deque<SyncName> names;
std::deque<std::pair<NodeId, SyncName>> ancestors;
bool parentIsRoot = false;
NodeId id = itemId;

{
const std::scoped_lock lock(_mutex);
while (!parentIsRoot) {
if (const auto it = _items.find(id); it != _items.end()) {
names.push_back(it->second.name());
if (_copy) {
if (!it->second.path().empty()) {
path = it->second.path();
break;
};
}

ancestors.push_back({it->first, it->second.name()});
id = it->second.parentId();
parentIsRoot = id == _rootFolderId;
continue;
Expand All @@ -256,16 +264,25 @@ bool Snapshot::path(const NodeId &itemId, SyncPath &path, bool &ignore) const no
}

// Construct path
SyncPath tmpParentPath;
while (!names.empty()) {
path /= names.back();
names.pop_back();
SyncPath tmpParentPath(path);
while (!ancestors.empty()) {
path /= ancestors.back().second;
if (_copy) {
const auto it = _items.find(ancestors.back().first);
assert(it != _items.end());
it->second.setPath(path);
}
ancestors.pop_back();

// Trick to ignore items with pattern like "X:" in their name on Windows - Begin
if (path.parent_path() != tmpParentPath) {
ignore = true;
return false;
}
tmpParentPath = path;
// Trick to ignore items with pattern like "X:" in their name on Windows - End
}

return ok;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class Snapshot : public SharedObject {
NodeId _rootFolderId;
std::unordered_map<NodeId, SnapshotItem> _items; // key: id
bool _isValid = false;
bool _copy = false; // false for a real time snapshot, true for a copy

mutable std::recursive_mutex _mutex;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void SnapshotItem::copyExceptChildren(const SnapshotItem &other) {
_contentChecksum = other.contentChecksum();
_canWrite = other.canWrite();
_canShare = other.canShare();
_path = other.path();
}

void SnapshotItem::addChildren(const NodeId &id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace KDC {

class Snapshot;

class SnapshotItem {
public:
SnapshotItem();
Expand Down Expand Up @@ -65,6 +67,7 @@ class SnapshotItem {
void setCanWrite(const bool canWrite) { _canWrite = canWrite; }
[[nodiscard]] bool canShare() const { return _canShare; }
void setCanShare(bool canShare) { _canShare = canShare; }

SnapshotItem &operator=(const SnapshotItem &other);

void copyExceptChildren(const SnapshotItem &other);
Expand All @@ -87,6 +90,14 @@ class SnapshotItem {
bool _canShare = true;

std::unordered_set<NodeId> _childrenIds;

mutable SyncPath _path; // The item relative path. Cached value. To use only on a snapshot copy, not a real time one.

[[nodiscard]] SyncPath path() const { return _path; }
void setPath(const SyncPath &path) const { _path = path; }

friend class Snapshot;
// friend bool Snapshot::path(const NodeId &, SyncPath &, bool &) const noexcept;
};

} // namespace KDC
13 changes: 7 additions & 6 deletions test/libsyncengine/jobs/network/testnetworkjobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@
#include "jobs/network/API_v2/getsizejob.h"
#include "jobs/jobmanager.h"
#include "network/proxy.h"
#include "utility/jsonparserutility.h"
#include "requests/parameterscache.h"
#include "jobs/network/getappversionjob.h"
#include "jobs/network/directdownloadjob.h"
#include "libcommon/keychainmanager/keychainmanager.h"
#include "libcommonserver/utility/utility.h"
#include "libcommonserver/io/filestat.h"
#include "libcommonserver/io/iohelper.h"
#include "libparms/db/parmsdb.h"
#include "utility/jsonparserutility.h"
#include "requests/parameterscache.h"

#include "test_utility/localtemporarydirectory.h"
#include "test_utility/remotetemporarydirectory.h"
#include <iostream>

#include "jobs/network/getappversionjob.h"
#include "test_utility/testhelpers.h"
#include "jobs/network/directdownloadjob.h"

#include <iostream>

using namespace CppUnit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "testsnapshotitemhandler.h"
#include "libsyncengine/jobs/network/API_v2/csvfullfilelistwithcursorjob.h"

#include "libcommonserver/log/log.h"


Expand Down
1 change: 0 additions & 1 deletion test/libsyncengine/jobs/network/testsnapshotitemhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using namespace CppUnit;

namespace KDC {
class SnapshotItem;

class TestSnapshotItemHandler : public CppUnit::TestFixture {
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*/

#include "testoperationsorterworker.h"

#include "test_utility/testhelpers.h"

#include <memory>

using namespace CppUnit;

namespace KDC {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

#include "testconflictfinderworker.h"

#include "test_utility/testhelpers.h"

using namespace CppUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "testsnapshot.h"
#include "test_utility/testhelpers.h"

#include "db/syncdb.h"
#include "requests/parameterscache.h"

Expand Down

0 comments on commit 4a21e25

Please sign in to comment.