summaryrefslogtreecommitdiffstats
path: root/common/entityreader.h
blob: a47967934ecd659301f1c5c008faca6bbabc1ad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
#pragma once

#include "sink_export.h"
#include <domainadaptor.h>

#include "storage.h"
#include "resultprovider.h"
#include "adaptorfactoryregistry.h"

namespace Sink {

namespace EntityReaderUtils {
    SINK_EXPORT QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision);
    SINK_EXPORT QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision);
    SINK_EXPORT QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> getPrevious(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, qint64 revision, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision);
};

/**
 * A synchronous interface to read entities from the storage.
 *
 * All callbacks will be called before the end of the function.
 * The caller must ensure passed in references remain valid for the lifetime of the object.
 *
 * This class is meaent to be instantiated temporarily during reads on the stack.
 *
 * Note that all objects returned in callbacks are only valid during the execution of the callback and may start pointing into invalid memory if shallow-copied.
 */
template<typename DomainType>
class SINK_EXPORT EntityReader
{
    typedef std::function<bool(const typename DomainType::Ptr &domainObject, Sink::Operation operation)> ResultCallback;

public:
    EntityReader(const QByteArray &resourceType, const QByteArray &mResourceInstanceIdentifier, Sink::Storage::Transaction &transaction);
    EntityReader(DomainTypeAdaptorFactoryInterface &domainTypeAdaptorFactory, const QByteArray &resourceInstanceIdentifier, Sink::Storage::Transaction &transaction);

    /**
     * Reads the latest revision of an entity identified by @param uid
     */
    DomainType read(const QByteArray &uid) const;

    /**
     * Reads the revision of the entity identified by @param key (uid + revision)
     */
    DomainType readFromKey(const QByteArray &key) const;

    /**
     * Reads the (revision - 1) of an entity identified by @param uid
     */
    DomainType readPrevious(const QByteArray &uid, qint64 revision) const;

    /**
     * Reads all entities that match @param query.
     */
    void query(const Sink::Query &query, const std::function<bool(const DomainType &)> &callback);

    /**
     * Returns all entities that match @param query.
     * 
     * @param offset and @param batchsize can be used to return paginated results.
     */
    QPair<qint64, qint64> executeInitialQuery(const Sink::Query &query, int offset, int batchsize, const ResultCallback &callback);

    /**
     * Returns all changed entities that match @param query starting from @param lastRevision
     */
    QPair<qint64, qint64> executeIncrementalQuery(const Sink::Query &query, qint64 lastRevision, const ResultCallback &callback);

private:
    qint64 replaySet(ResultSet &resultSet, int offset, int batchSize, const ResultCallback &callback);

    void readEntity(const Sink::Storage::NamedDatabase &db, const QByteArray &key,
        const std::function<void(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &, Sink::Operation)> &resultCallback);

    ResultSet loadInitialResultSet(const Sink::Query &query, QSet<QByteArray> &remainingFilters, QByteArray &remainingSorting);
    ResultSet loadIncrementalResultSet(qint64 baseRevision, const Sink::Query &query, QSet<QByteArray> &remainingFilters);

    ResultSet filterAndSortSet(ResultSet &resultSet, const std::function<bool(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter,
        const Sink::Storage::NamedDatabase &db, bool initialQuery, const QByteArray &sortProperty);
    std::function<bool(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> getFilter(const QSet<QByteArray> remainingFilters, const Sink::Query &query);
    QPair<qint64, qint64> load(const Sink::Query &query, const std::function<ResultSet(QSet<QByteArray> &, QByteArray &)> &baseSetRetriever, bool initialQuery, int offset, int batchSize, const ResultCallback &callback);

private:
    QByteArray mResourceInstanceIdentifier;
    Sink::Storage::Transaction &mTransaction;
    std::shared_ptr<DomainTypeAdaptorFactoryInterface> mDomainTypeAdaptorFactoryPtr;
    DomainTypeAdaptorFactoryInterface &mDomainTypeAdaptorFactory;
};

}