Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization - Implementation of a cache on Snapshot::path() #435

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -17,6 +17,7 @@
*/

#include "snapshot.h"
#include "snapshotitem.h"
ChristopheLarchier marked this conversation as resolved.
Show resolved Hide resolved
#include "libcommonserver/log/log.h"
#include "requests/parameterscache.h"

Expand Down Expand Up @@ -47,6 +48,7 @@ Snapshot &Snapshot::operator=(Snapshot &other) {

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

return *this;
Expand Down Expand Up @@ -236,15 +238,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 +265,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 @@ -19,8 +19,8 @@
#pragma once

#include "syncpal/sharedobject.h"
#include "snapshotitem.h"
#include "db/dbnode.h"
#include "snapshotitem.h"
ChristopheLarchier marked this conversation as resolved.
Show resolved Hide resolved

#include <unordered_map>
#include <unordered_set>
Expand Down 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
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
Loading