File indexing completed on 2024-05-12 05:25:57

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 #include <QObject>
0024 #include <KAsync/Async>
0025 
0026 #include "storage.h"
0027 #include "resourcecontext.h"
0028 
0029 namespace Sink {
0030 
0031 /**
0032  * Replays changes from the storage one by one.
0033  *
0034  * Uses a local database to:
0035  * * Remember what changes have been replayed already.
0036  * * store a mapping of remote to local buffers
0037  */
0038 class SINK_EXPORT ChangeReplay : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042     ChangeReplay(const ResourceContext &resourceContext, const Sink::Log::Context &ctx= {});
0043 
0044     qint64 getLastReplayedRevision();
0045     virtual bool allChangesReplayed();
0046 
0047     KAsync::Job<void> replayNextRevision();
0048 
0049 signals:
0050     void changesReplayed();
0051     void replayingChanges();
0052 
0053 public slots:
0054     virtual void revisionChanged();
0055 
0056 protected:
0057     virtual KAsync::Job<void> replay(const QByteArray &type, const QByteArray &key, const QByteArray &value) = 0;
0058     virtual void notReplaying(const QByteArray &type, const QByteArray &key, const QByteArray &value) = 0;
0059     virtual bool canReplay(const QByteArray &type, const QByteArray &key, const QByteArray &value) = 0;
0060     virtual void reportProgress(int progress, int total, const QByteArrayList &applicableEntities = {}){};
0061     Sink::Storage::DataStore mStorage;
0062 
0063 private:
0064     void recordReplayedRevision(qint64 revision);
0065     Sink::Storage::DataStore mChangeReplayStore;
0066     bool mReplayInProgress;
0067     Sink::Storage::DataStore::Transaction mMainStoreTransaction;
0068     Sink::Log::Context mLogCtx;
0069     QObject mGuard;
0070 };
0071 
0072 class NullChangeReplay : public ChangeReplay
0073 {
0074 public:
0075     NullChangeReplay(const ResourceContext &resourceContext) : ChangeReplay(resourceContext) {}
0076     KAsync::Job<void> replay(const QByteArray &type, const QByteArray &key, const QByteArray &value) Q_DECL_OVERRIDE { return KAsync::null<void>(); }
0077     bool canReplay(const QByteArray &type, const QByteArray &key, const QByteArray &value) Q_DECL_OVERRIDE { return false; }
0078 };
0079 
0080 }
0081