Commit 827cd1c2 authored by Martin Höher's avatar Martin Höher

Fix: Sync of libraries

The `library.json` file is now properly saved when creating a new
library. This was a regression introduced with the cache mechanism. Also
include a fixup mechanism, which allows existing "bad" libraries to
reconstruct their JSON file from the existing LMDB cache.
parent 3fc88a9f
Pipeline #47427271 failed with stages
in 11 minutes and 24 seconds
Subproject commit 6c6480bcd1062cab7f25e0b40551db18c4d7f75c
Subproject commit 21daf4cf29c4c869f46a54cb24fd57d69e04b794
......@@ -24,6 +24,7 @@
#include "datamodel/todolist.h"
#include "datastorage/cache.h"
#include "datastorage/deleteitemsquery.h"
#include "datastorage/librariesitemsquery.h"
#include "datastorage/insertorupdateitemsquery.h"
#include "datastorage/libraryloader.h"
#include "sync/synchronizer.h"
......@@ -180,6 +181,26 @@ QList<QSharedPointer<Library> > Application::librariesFromConfig()
} else {
qCWarning(log) << "Failed to load library from directory"
<< directory;
// Try to restore JSON from cache - fix for
// https://gitlab.com/rpdev/opentodolist/issues/222
auto query = new LibrariesItemsQuery();
query->setIncludeCalculatedValues(false);
connect(query, &LibrariesItemsQuery::librariesAvailable,
[=](QVariantList libraries) {
for (const auto &entry : libraries) {
auto cacheEntry = entry.value<LibraryCacheEntry>();
if (cacheEntry.valid) {
auto lib = Library::decache(cacheEntry);
if (lib->directory() == directory) {
lib->save();
qCWarning(log) << "Restored library file in "
<< directory;
}
}
}
});
m_cache->run(query);
}
}
}
......@@ -329,7 +350,7 @@ Library *Application::addLibrary(const QVariantMap &parameters)
watchLibraryForChanges(result);
}
auto q = new InsertOrUpdateItemsQuery();
q->add(result);
q->add(result, InsertOrUpdateItemsQuery::Save);
m_cache->run(q);
syncLibrary(result);
result->setCache(m_cache);
......
......@@ -540,7 +540,7 @@ void Library::onChanged()
{
if (m_cache != nullptr) {
auto q = new InsertOrUpdateItemsQuery();
q->add(this);
q->add(this, InsertOrUpdateItemsQuery::Save);
m_cache->run(q);
}
}
......
......@@ -45,12 +45,15 @@ void InsertOrUpdateItemsQuery::add(Item *item, InsertFlags flags)
/**
* @brief Add the @p library to the list of libraries to be cached.
*/
void InsertOrUpdateItemsQuery::add(Library *library)
void InsertOrUpdateItemsQuery::add(Library *library, InsertFlags flags)
{
if (library != nullptr) {
auto entry = library->encache();
if (entry.valid) {
m_libEntries.append(entry);
if (flags.testFlag(Save)) {
m_save.insert(library->uid());
}
}
}
}
......@@ -72,6 +75,14 @@ void InsertOrUpdateItemsQuery::run()
}
childrenCursor.put(Cache::RootId, id,
QLMDB::Cursor::NoDuplicateData);
if (m_save.contains(lib.id)) {
auto library = Library::decache(lib);
if (library->isValid()) {
library->save();
delete library;
}
}
}
for (auto item : m_itemEntries) {
if (m_calcWeight.contains(item.id)) {
......
......@@ -59,7 +59,7 @@ public:
public slots:
void add(Item *item, InsertFlags flags = NoFlags);
void add(Library* library);
void add(Library* library, InsertFlags flags = NoFlags);
protected:
// ItemsQuery interface
......
......@@ -149,7 +149,7 @@ void LibrariesModel::libraryChanged()
emit dataChanged(index(idx), index(idx));
if (!m_updating && m_cache != nullptr) {
auto q = new InsertOrUpdateItemsQuery();
q->add(lib);
q->add(lib, InsertOrUpdateItemsQuery::Save);
m_cache->run(q);
}
}
......
......@@ -366,8 +366,11 @@ bool WebDAVClient::deleteEntry(const QString& filename)
* in the set, no changes compared to the last sync run were detected
* and hence only local changes need to be pushed.
*/
bool WebDAVClient::syncDirectory(const QString& directory, QRegularExpression directoryFilter,
bool pushOnly, QSet<QString> *changedDirs)
bool WebDAVClient::syncDirectory(
const QString& directory,
QRegularExpression directoryFilter,
bool pushOnly,
QSet<QString> *changedDirs)
{
m_stopRequested = false;
QSet<QString> _changedDirs;
......
......@@ -228,7 +228,7 @@ void WebDAVSynchronizer::stopSync()
*
* This method will search for existing libraries in the current
* remote directory and the OpenTodoList sub-folder (relative to the
* remote directory). The
* remote directory).
*/
void WebDAVSynchronizer::findExistingLibraries()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment