summaryrefslogtreecommitdiffstats
path: root/common/store.cpp
blob: 6aae00feff9608c5fc077e20057769d643536ac0 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
 *
 * 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/>.
 */

#include "store.h"

#include <QTime>
#include <QAbstractItemModel>
#include <QUuid>
#include <functional>
#include <memory>

#include "resourceaccess.h"
#include "commands.h"
#include "resourcefacade.h"
#include "definitions.h"
#include "resourceconfig.h"
#include "facadefactory.h"
#include "modelresult.h"
#include "storage.h"
#include "log.h"

SINK_DEBUG_AREA("store")

Q_DECLARE_METATYPE(QSharedPointer<Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>>)
Q_DECLARE_METATYPE(QSharedPointer<Sink::ResourceAccessInterface>);
Q_DECLARE_METATYPE(std::shared_ptr<void>);

namespace Sink {

QString Store::storageLocation()
{
    return Sink::storageLocation();
}

QString Store::getTemporaryFilePath()
{
    return Sink::temporaryFileLocation() + "/" + QUuid::createUuid().toString();
}

/*
 * Returns a map of resource instance identifiers and resource type
 */
static QMap<QByteArray, QByteArray> getResources(const Sink::Query::Filter &query, const QByteArray &type = QByteArray())
{
    const QList<QByteArray> resourceFilter = query.ids;


    const auto filterResource = [&](const QByteArray &res) {
        const auto configuration = ResourceConfig::getConfiguration(res);
        for (const auto &filterProperty : query.propertyFilter.keys()) {
            const auto filter = query.propertyFilter.value(filterProperty);
            if (!filter.matches(configuration.value(filterProperty))) {
                return true;
            }
        }
        return false;
    };

    QMap<QByteArray, QByteArray> resources;
    // Return the global resource (signified by an empty name) for types that don't belong to a specific resource
    if (ApplicationDomain::isGlobalType(type)) {
        resources.insert("", "");
        return resources;
    }
    const auto configuredResources = ResourceConfig::getResources();
    if (resourceFilter.isEmpty()) {
        for (const auto &res : configuredResources.keys()) {
            const auto type = configuredResources.value(res);
            if (filterResource(res)) {
                continue;
            }
            // TODO filter by entity type
            resources.insert(res, type);
        }
    } else {
        for (const auto &res : resourceFilter) {
            if (configuredResources.contains(res)) {
                if (filterResource(res)) {
                    continue;
                }
                resources.insert(res, configuredResources.value(res));
            } else {
                SinkWarning() << "Resource is not existing: " << res;
            }
        }
    }
    SinkTrace() << "Found resources: " << resources;
    return resources;
}


template <class DomainType>
KAsync::Job<void> queryResource(const QByteArray resourceType, const QByteArray &resourceInstanceIdentifier, const Query &query, typename AggregatingResultEmitter<typename DomainType::Ptr>::Ptr aggregatingEmitter)
{
    auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier);
    if (facade) {
        SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier;
        auto result = facade->load(query);
        if (result.second) {
            aggregatingEmitter->addEmitter(result.second);
        } else {
            SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier;
        }
        return result.first;
    } else {
        SinkTrace() << "Couldn' find a facade for " << resourceInstanceIdentifier;
        // Ignore the error and carry on
        return KAsync::null<void>();
    }
}

template <class DomainType>
QSharedPointer<QAbstractItemModel> Store::loadModel(Query query)
{
    query.setType(ApplicationDomain::getTypeName<DomainType>());
    SinkTrace() << "Loading model: " << query;
    auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties);

    //* Client defines lifetime of model
    //* The model lifetime defines the duration of live-queries
    //* The facade needs to life for the duration of any calls being made (assuming we get rid of any internal callbacks
    //* The emitter needs to live or the duration of query (respectively, the model)
    //* The result provider needs to live for as long as results are provided (until the last thread exits).

    // Query all resources and aggregate results
    auto resources = getResources(query.getResourceFilter(), ApplicationDomain::getTypeName<DomainType>());
    auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create();
    model->setEmitter(aggregatingEmitter);

    if (query.liveQuery() && query.getResourceFilter().ids.isEmpty() && !ApplicationDomain::isGlobalType(ApplicationDomain::getTypeName<DomainType>())) {
        SinkTrace() << "Listening for new resources";
        auto facade = FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>();
        Q_ASSERT(facade);
        Sink::Query resourceQuery;
        query.setFlags(Query::LiveQuery);
        auto result = facade->load(resourceQuery);
        auto emitter = result.second;
        emitter->onAdded([query, aggregatingEmitter](const ApplicationDomain::SinkResource::Ptr &resource) {
            SinkTrace() << "Found new resources: " << resource->identifier();
            const auto resourceType = ResourceConfig::getResourceType(resource->identifier());
            Q_ASSERT(!resourceType.isEmpty());
            queryResource<DomainType>(resourceType, resource->identifier(), query, aggregatingEmitter).exec();
        });
        emitter->onModified([](const ApplicationDomain::SinkResource::Ptr &) {
        });
        emitter->onRemoved([](const ApplicationDomain::SinkResource::Ptr &) {
        });
        emitter->onInitialResultSetComplete([](const ApplicationDomain::SinkResource::Ptr &) {
        });
        emitter->onComplete([query, aggregatingEmitter]() {
            SinkTrace() << "Resource query complete";

        });
        model->setProperty("resourceEmitter", QVariant::fromValue(emitter));
        result.first.exec();
    }

    KAsync::value(resources.keys())
        .template each([query, aggregatingEmitter, resources](const QByteArray &resourceInstanceIdentifier) {
            const auto resourceType = resources.value(resourceInstanceIdentifier);
            return queryResource<DomainType>(resourceType, resourceInstanceIdentifier, query, aggregatingEmitter);
        })
        .exec();
    model->fetchMore(QModelIndex());

    return model;
}

template <class DomainType>
static std::shared_ptr<StoreFacade<DomainType>> getFacade(const QByteArray &resourceInstanceIdentifier)
{
    if (ApplicationDomain::isGlobalType(ApplicationDomain::getTypeName<DomainType>())) {
        if (auto facade = FacadeFactory::instance().getFacade<DomainType>()) {
            return facade;
        }
    }
    if (auto facade = FacadeFactory::instance().getFacade<DomainType>(ResourceConfig::getResourceType(resourceInstanceIdentifier), resourceInstanceIdentifier)) {
        return facade;
    }
    return std::make_shared<NullFacade<DomainType>>();
}

template <class DomainType>
KAsync::Job<void> Store::create(const DomainType &domainObject)
{
    SinkTrace() << "Create: " << domainObject;
    // Potentially move to separate thread as well
    auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
    return facade->create(domainObject).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to create"; });
}

template <class DomainType>
KAsync::Job<void> Store::modify(const DomainType &domainObject)
{
    SinkTrace() << "Modify: " << domainObject;
    // Potentially move to separate thread as well
    auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
    return facade->modify(domainObject).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to modify"; });
}

template <class DomainType>
KAsync::Job<void> Store::remove(const DomainType &domainObject)
{
    SinkTrace() << "Remove: " << domainObject;
    // Potentially move to separate thread as well
    auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
    return facade->remove(domainObject).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to remove"; });
}

KAsync::Job<void> Store::removeDataFromDisk(const QByteArray &identifier)
{
    // All databases are going to become invalid, nuke the environments
    // TODO: all clients should react to a notification the resource
    Sink::Storage::DataStore::clearEnv();
    SinkTrace() << "Remove data from disk " << identifier;
    auto time = QSharedPointer<QTime>::create();
    time->start();
    auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier));
    resourceAccess->open();
    return resourceAccess->sendCommand(Sink::Commands::RemoveFromDiskCommand)
        .addToContext(resourceAccess)
        .then<void>([resourceAccess](KAsync::Future<void> &future) {
            if (resourceAccess->isReady()) {
                //Wait for the resource shutdown
                QObject::connect(resourceAccess.data(), &ResourceAccess::ready, [&future](bool ready) {
                    if (!ready) {
                        future.setFinished();
                    }
                });
            } else {
                future.setFinished();
            }
        })
        .syncThen<void>([time]() {
            SinkTrace() << "Remove from disk complete." << Log::TraceTime(time->elapsed());
        });
}

KAsync::Job<void> Store::synchronize(const Sink::Query &query)
{
    auto resources = getResources(query.getResourceFilter()).keys();
    SinkTrace() << "synchronize" << resources;
    return KAsync::value(resources)
        .template each([query](const QByteArray &resource) {
            SinkTrace() << "Synchronizing " << resource;
            auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource));
            return resourceAccess->synchronizeResource(true, false)
                .addToContext(resourceAccess)
                .then<void>([](const KAsync::Error &error) {
                        if (error) {
                            SinkWarning() << "Error during sync.";
                            return KAsync::error<void>(error);
                        }
                        SinkTrace() << "synced.";
                        return KAsync::null<void>();
                    });
        });
}

KAsync::Job<void> Store::synchronize(const Sink::SyncScope &scope)
{
    auto resources = getResources(scope.getResourceFilter()).keys();
    SinkTrace() << "synchronize" << resources;
    return KAsync::value(resources)
        .template each([scope](const QByteArray &resource) {
            SinkTrace() << "Synchronizing " << resource;
            auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource));
            return resourceAccess->synchronizeResource(scope)
                .addToContext(resourceAccess)
                .then<void>([](const KAsync::Error &error) {
                        if (error) {
                            SinkWarning() << "Error during sync.";
                            return KAsync::error<void>(error);
                        }
                        SinkTrace() << "synced.";
                        return KAsync::null<void>();
                    });
        });
}

template <class DomainType>
KAsync::Job<DomainType> Store::fetchOne(const Sink::Query &query)
{
    return fetch<DomainType>(query, 1).template then<DomainType, QList<typename DomainType::Ptr>>([](const QList<typename DomainType::Ptr> &list) {
        return KAsync::value(*list.first());
    });
}

template <class DomainType>
KAsync::Job<QList<typename DomainType::Ptr>> Store::fetchAll(const Sink::Query &query)
{
    return fetch<DomainType>(query);
}

template <class DomainType>
KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &query, int minimumAmount)
{
    auto model = loadModel<DomainType>(query);
    auto list = QSharedPointer<QList<typename DomainType::Ptr>>::create();
    auto context = QSharedPointer<QObject>::create();
    return KAsync::start<QList<typename DomainType::Ptr>>([model, list, context, minimumAmount](KAsync::Future<QList<typename DomainType::Ptr>> &future) {
        if (model->rowCount() >= 1) {
            for (int i = 0; i < model->rowCount(); i++) {
                list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value<typename DomainType::Ptr>());
            }
        } else {
            QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, context.data(), [model, &future, list](const QModelIndex &index, int start, int end) {
                for (int i = start; i <= end; i++) {
                    list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value<typename DomainType::Ptr>());
                }
            });
            QObject::connect(model.data(), &QAbstractItemModel::dataChanged, context.data(),
                [model, &future, list, minimumAmount](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
                    if (roles.contains(ModelResult<DomainType, typename DomainType::Ptr>::ChildrenFetchedRole)) {
                        if (list->size() < minimumAmount) {
                            future.setError(1, "Not enough values.");
                        } else {
                            future.setValue(*list);
                            future.setFinished();
                        }
                    }
                });
        }
        if (model->data(QModelIndex(), ModelResult<DomainType, typename DomainType::Ptr>::ChildrenFetchedRole).toBool()) {
            if (list->size() < minimumAmount) {
                future.setError(1, "Not enough values.");
            } else {
                future.setValue(*list);
            }
            future.setFinished();
        }
    });
}

template <class DomainType>
DomainType Store::readOne(const Sink::Query &query)
{
    const auto list = read<DomainType>(query);
    if (!list.isEmpty()) {
        return list.first();
    }
    return DomainType();
}

template <class DomainType>
QList<DomainType> Store::read(const Sink::Query &q)
{
    auto query = q;
    query.setFlags(Query::SynchronousQuery);
    QList<DomainType> list;
    auto resources = getResources(query.getResourceFilter(), ApplicationDomain::getTypeName<DomainType>());
    auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create();
    aggregatingEmitter->onAdded([&list](const typename DomainType::Ptr &value){
        SinkTrace() << "Found value: " << value->identifier();
        list << *value;
    });
    for (const auto &resourceInstanceIdentifier : resources.keys()) {
        const auto resourceType = resources.value(resourceInstanceIdentifier);
        SinkTrace() << "Querying resource:  " << resourceType << resourceInstanceIdentifier;
        auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier);
        if (facade) {
            SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier;
            auto result = facade->load(query);
            if (result.second) {
                aggregatingEmitter->addEmitter(result.second);
            } else {
                SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier;
            }
            result.first.exec();
        } else {
            SinkTrace() << "Couldn't find a facade for " << resourceInstanceIdentifier;
            // Ignore the error and carry on
        }
    }
    aggregatingEmitter->fetch(typename DomainType::Ptr());
    return list;
}

#define REGISTER_TYPE(T)                                                          \
    template KAsync::Job<void> Store::remove<T>(const T &domainObject);           \
    template KAsync::Job<void> Store::create<T>(const T &domainObject);           \
    template KAsync::Job<void> Store::modify<T>(const T &domainObject);           \
    template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(Query query); \
    template KAsync::Job<T> Store::fetchOne<T>(const Query &);                    \
    template KAsync::Job<QList<T::Ptr>> Store::fetchAll<T>(const Query &);        \
    template KAsync::Job<QList<T::Ptr>> Store::fetch<T>(const Query &, int);      \
    template T Store::readOne<T>(const Query &);                                  \
    template QList<T> Store::read<T>(const Query &);

REGISTER_TYPE(ApplicationDomain::Event);
REGISTER_TYPE(ApplicationDomain::Mail);
REGISTER_TYPE(ApplicationDomain::Folder);
REGISTER_TYPE(ApplicationDomain::SinkResource);
REGISTER_TYPE(ApplicationDomain::SinkAccount);
REGISTER_TYPE(ApplicationDomain::Identity);

} // namespace Sink