From 73ad9440244579ba625df970aa280e162f6f1c86 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 28 Nov 2016 23:37:33 +0100 Subject: Renamed RemoteIdMap to SynchronizerStore --- common/CMakeLists.txt | 2 +- common/inspector.h | 1 + common/remoteidmap.cpp | 103 ------------------------------------------- common/remoteidmap.h | 66 --------------------------- common/synchronizer.cpp | 6 +-- common/synchronizer.h | 8 ++-- common/synchronizerstore.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++ common/synchronizerstore.h | 66 +++++++++++++++++++++++++++ 8 files changed, 178 insertions(+), 177 deletions(-) delete mode 100644 common/remoteidmap.cpp delete mode 100644 common/remoteidmap.h create mode 100644 common/synchronizerstore.cpp create mode 100644 common/synchronizerstore.h (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index df44ce5..e8bb055 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -69,7 +69,7 @@ set(command_SRCS changereplay.cpp adaptorfactoryregistry.cpp synchronizer.cpp - remoteidmap.cpp + synchronizerstore.cpp mailpreprocessor.cpp specialpurposepreprocessor.cpp datastorequery.cpp diff --git a/common/inspector.h b/common/inspector.h index ff167b1..a746f03 100644 --- a/common/inspector.h +++ b/common/inspector.h @@ -25,6 +25,7 @@ #include "notification.h" #include "resourcecontext.h" +#include "synchronizerstore.h" namespace Sink { diff --git a/common/remoteidmap.cpp b/common/remoteidmap.cpp deleted file mode 100644 index a16473d..0000000 --- a/common/remoteidmap.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2016 Christian Mollekopf - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) version 3, or any - * later version accepted by the membership of KDE e.V. (or its - * successor approved by the membership of KDE e.V.), which shall - * act as a proxy defined in Section 6 of version 3 of the license. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ -#include "remoteidmap.h" - -#include -#include "index.h" -#include "log.h" - -using namespace Sink; - -SINK_DEBUG_AREA("remoteidmap") - -RemoteIdMap::RemoteIdMap(Sink::Storage::DataStore::Transaction &transaction) - : mTransaction(transaction) -{ - -} - -void RemoteIdMap::recordRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) -{ - Index("rid.mapping." + bufferType, mTransaction).add(remoteId, localId); - Index("localid.mapping." + bufferType, mTransaction).add(localId, remoteId); -} - -void RemoteIdMap::removeRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) -{ - Index("rid.mapping." + bufferType, mTransaction).remove(remoteId, localId); - Index("localid.mapping." + bufferType, mTransaction).remove(localId, remoteId); -} - -void RemoteIdMap::updateRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) -{ - const auto oldRemoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); - removeRemoteId(bufferType, localId, oldRemoteId); - recordRemoteId(bufferType, localId, remoteId); -} - -QByteArray RemoteIdMap::resolveRemoteId(const QByteArray &bufferType, const QByteArray &remoteId) -{ - // Lookup local id for remote id, or insert a new pair otherwise - Index index("rid.mapping." + bufferType, mTransaction); - QByteArray sinkId = index.lookup(remoteId); - if (sinkId.isEmpty()) { - sinkId = Sink::Storage::DataStore::generateUid(); - index.add(remoteId, sinkId); - Index("localid.mapping." + bufferType, mTransaction).add(sinkId, remoteId); - } - return sinkId; -} - -QByteArray RemoteIdMap::resolveLocalId(const QByteArray &bufferType, const QByteArray &localId) -{ - QByteArray remoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); - if (remoteId.isEmpty()) { - SinkWarning() << "Couldn't find the remote id for " << localId; - return QByteArray(); - } - return remoteId; -} - -QByteArrayList RemoteIdMap::resolveLocalIds(const QByteArray &bufferType, const QByteArrayList &localIds) -{ - QByteArrayList result; - for (const auto &l : localIds) { - result << resolveLocalId(bufferType, l); - } - return result; -} - -QByteArray RemoteIdMap::readValue(const QByteArray &key) -{ - QByteArray value; - mTransaction.openDatabase("values").scan(key, [&value](const QByteArray &, const QByteArray &v) { - value = v; - return false; - }, [](const Sink::Storage::DataStore::Error &) { - //Ignore errors because we may not find the value - }); - return value; -} - -void RemoteIdMap::writeValue(const QByteArray &key, const QByteArray &value) -{ - mTransaction.openDatabase("values").write(key, value); -} - diff --git a/common/remoteidmap.h b/common/remoteidmap.h deleted file mode 100644 index 52e05d7..0000000 --- a/common/remoteidmap.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2016 Christian Mollekopf - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) version 3, or any - * later version accepted by the membership of KDE e.V. (or its - * successor approved by the membership of KDE e.V.), which shall - * act as a proxy defined in Section 6 of version 3 of the license. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ -#pragma once - -#include "sink_export.h" - -#include "storage.h" -#include - -namespace Sink { - -/** - * A remoteId mapping - */ -class SINK_EXPORT RemoteIdMap -{ -public: - RemoteIdMap(Sink::Storage::DataStore::Transaction &); - - /** - * Records a localId to remoteId mapping - */ - void recordRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId); - void removeRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId); - void updateRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId); - - /** - * Tries to find a local id for the remote id, and creates a new local id otherwise. - * - * The new local id is recorded in the local to remote id mapping. - */ - QByteArray resolveRemoteId(const QByteArray &type, const QByteArray &remoteId); - - /** - * Tries to find a remote id for a local id. - * - * This can fail if the entity hasn't been written back to the server yet. - */ - QByteArray resolveLocalId(const QByteArray &bufferType, const QByteArray &localId); - QByteArrayList resolveLocalIds(const QByteArray &bufferType, const QByteArrayList &localId); - - QByteArray readValue(const QByteArray &key); - void writeValue(const QByteArray &key, const QByteArray &value); - -private: - Sink::Storage::DataStore::Transaction &mTransaction; -}; - -} diff --git a/common/synchronizer.cpp b/common/synchronizer.cpp index f7dd816..11c7caf 100644 --- a/common/synchronizer.cpp +++ b/common/synchronizer.cpp @@ -22,7 +22,7 @@ #include "definitions.h" #include "commands.h" #include "bufferutils.h" -#include "remoteidmap.h" +#include "synchronizerstore.h" #include "datastorequery.h" #include "createentity_generated.h" #include "modifyentity_generated.h" @@ -66,10 +66,10 @@ Storage::EntityStore &Synchronizer::store() return *mEntityStore; } -RemoteIdMap &Synchronizer::syncStore() +SynchronizerStore &Synchronizer::syncStore() { if (!mSyncStore) { - mSyncStore = QSharedPointer::create(syncTransaction()); + mSyncStore = QSharedPointer::create(syncTransaction()); } return *mSyncStore; } diff --git a/common/synchronizer.h b/common/synchronizer.h index ae597bd..989f902 100644 --- a/common/synchronizer.h +++ b/common/synchronizer.h @@ -28,10 +28,10 @@ #include #include #include "changereplay.h" -#include "remoteidmap.h" +#include "synchronizerstore.h" namespace Sink { -class RemoteIdMap; +class SynchronizerStore; /** * Synchronize and add what we don't already have to local queue @@ -51,7 +51,7 @@ public: Storage::EntityStore &store(); //Read/Write access to sync storage - RemoteIdMap &syncStore(); + SynchronizerStore &syncStore(); void commit(); Sink::Storage::DataStore::Transaction &syncTransaction(); @@ -171,7 +171,7 @@ private: Sink::ResourceContext mResourceContext; Sink::Storage::EntityStore::Ptr mEntityStore; - QSharedPointer mSyncStore; + QSharedPointer mSyncStore; Sink::Storage::DataStore mSyncStorage; Sink::Storage::DataStore::Transaction mSyncTransaction; std::function mEnqueue; diff --git a/common/synchronizerstore.cpp b/common/synchronizerstore.cpp new file mode 100644 index 0000000..fcb3ed0 --- /dev/null +++ b/common/synchronizerstore.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ +#include "synchronizerstore.h" + +#include +#include "index.h" +#include "log.h" + +using namespace Sink; + +SINK_DEBUG_AREA("synchronizerstore") + +SynchronizerStore::SynchronizerStore(Sink::Storage::DataStore::Transaction &transaction) + : mTransaction(transaction) +{ + +} + +void SynchronizerStore::recordRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) +{ + Index("rid.mapping." + bufferType, mTransaction).add(remoteId, localId); + Index("localid.mapping." + bufferType, mTransaction).add(localId, remoteId); +} + +void SynchronizerStore::removeRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) +{ + Index("rid.mapping." + bufferType, mTransaction).remove(remoteId, localId); + Index("localid.mapping." + bufferType, mTransaction).remove(localId, remoteId); +} + +void SynchronizerStore::updateRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId) +{ + const auto oldRemoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); + removeRemoteId(bufferType, localId, oldRemoteId); + recordRemoteId(bufferType, localId, remoteId); +} + +QByteArray SynchronizerStore::resolveRemoteId(const QByteArray &bufferType, const QByteArray &remoteId) +{ + // Lookup local id for remote id, or insert a new pair otherwise + Index index("rid.mapping." + bufferType, mTransaction); + QByteArray sinkId = index.lookup(remoteId); + if (sinkId.isEmpty()) { + sinkId = Sink::Storage::DataStore::generateUid(); + index.add(remoteId, sinkId); + Index("localid.mapping." + bufferType, mTransaction).add(sinkId, remoteId); + } + return sinkId; +} + +QByteArray SynchronizerStore::resolveLocalId(const QByteArray &bufferType, const QByteArray &localId) +{ + QByteArray remoteId = Index("localid.mapping." + bufferType, mTransaction).lookup(localId); + if (remoteId.isEmpty()) { + SinkWarning() << "Couldn't find the remote id for " << localId; + return QByteArray(); + } + return remoteId; +} + +QByteArrayList SynchronizerStore::resolveLocalIds(const QByteArray &bufferType, const QByteArrayList &localIds) +{ + QByteArrayList result; + for (const auto &l : localIds) { + result << resolveLocalId(bufferType, l); + } + return result; +} + +QByteArray SynchronizerStore::readValue(const QByteArray &key) +{ + QByteArray value; + mTransaction.openDatabase("values").scan(key, [&value](const QByteArray &, const QByteArray &v) { + value = v; + return false; + }, [](const Sink::Storage::DataStore::Error &) { + //Ignore errors because we may not find the value + }); + return value; +} + +void SynchronizerStore::writeValue(const QByteArray &key, const QByteArray &value) +{ + mTransaction.openDatabase("values").write(key, value); +} + diff --git a/common/synchronizerstore.h b/common/synchronizerstore.h new file mode 100644 index 0000000..be33562 --- /dev/null +++ b/common/synchronizerstore.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ +#pragma once + +#include "sink_export.h" + +#include "storage.h" +#include + +namespace Sink { + +/** + * A remoteId mapping + */ +class SINK_EXPORT SynchronizerStore +{ +public: + SynchronizerStore(Sink::Storage::DataStore::Transaction &); + + /** + * Records a localId to remoteId mapping + */ + void recordRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId); + void removeRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId); + void updateRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId); + + /** + * Tries to find a local id for the remote id, and creates a new local id otherwise. + * + * The new local id is recorded in the local to remote id mapping. + */ + QByteArray resolveRemoteId(const QByteArray &type, const QByteArray &remoteId); + + /** + * Tries to find a remote id for a local id. + * + * This can fail if the entity hasn't been written back to the server yet. + */ + QByteArray resolveLocalId(const QByteArray &bufferType, const QByteArray &localId); + QByteArrayList resolveLocalIds(const QByteArray &bufferType, const QByteArrayList &localId); + + QByteArray readValue(const QByteArray &key); + void writeValue(const QByteArray &key, const QByteArray &value); + +private: + Sink::Storage::DataStore::Transaction &mTransaction; +}; + +} -- cgit v1.2.3