File indexing completed on 2024-05-12 05:26:07

0001 /*
0002  * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 #pragma once
0021 
0022 #include "sink_export.h"
0023 
0024 #include "storage.h"
0025 #include <QByteArrayList>
0026 
0027 namespace Sink {
0028 
0029 /**
0030  * A remoteId mapping
0031  */
0032 class SINK_EXPORT SynchronizerStore
0033 {
0034 public:
0035     SynchronizerStore(Sink::Storage::DataStore::Transaction &);
0036 
0037     /**
0038      * Records a localId to remoteId mapping
0039      */
0040     void recordRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId);
0041     void removeRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId);
0042     void updateRemoteId(const QByteArray &bufferType, const QByteArray &localId, const QByteArray &remoteId);
0043 
0044     /**
0045      * Tries to find a local id for the remote id, and creates a new local id otherwise.
0046      *
0047      * The new local id is recorded in the local to remote id mapping.
0048      */
0049     QByteArray resolveRemoteId(const QByteArray &type, const QByteArray &remoteId, bool insertIfMissing = true);
0050 
0051     /**
0052      * Tries to find a remote id for a local id.
0053      *
0054      * This can fail if the entity hasn't been written back to the server yet.
0055      */
0056     QByteArray resolveLocalId(const QByteArray &bufferType, const QByteArray &localId);
0057     QByteArrayList resolveLocalIds(const QByteArray &bufferType, const QByteArrayList &localId);
0058 
0059     void removePrefix(const QByteArray &prefix);
0060     void removeValue(const QByteArray &prefix, const QByteArray &key);
0061     QByteArray readValue(const QByteArray &key);
0062     QByteArray readValue(const QByteArray &prefix, const QByteArray &key);
0063     void writeValue(const QByteArray &key, const QByteArray &value);
0064     void writeValue(const QByteArray &prefix, const QByteArray &key, const QByteArray &value);
0065 
0066     bool contains(const QByteArray &key);
0067     bool contains(const QByteArray &prefix, const QByteArray &key);
0068 
0069 private:
0070     Sink::Storage::DataStore::Transaction &mTransaction;
0071 };
0072 
0073 }