summaryrefslogtreecommitdiffstats
path: root/src/daemon/jsondbview.cpp
blob: 4adc5d65c5139822975cfd3fd08bab13748e4f50 (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QObject>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QString>
#include <QElapsedTimer>

#include "jsondb-global.h"
#include "jsondbpartition.h"
#include "jsondbobject.h"
#include "jsondbview.h"
#include "jsondbmapdefinition.h"
#include "jsondbobjecttable.h"
#include "jsondbreducedefinition.h"
#include "jsondbsettings.h"

QT_BEGIN_NAMESPACE_JSONDB

JsonDbView::JsonDbView(JsonDbPartition *partition, const QString &viewType, QObject *parent) :
    QObject(parent)
  , mPartition(partition)
  , mViewObjectTable(0)
  , mMainObjectTable(mPartition->mainObjectTable())
  , mViewType(viewType)
  , mUpdating(false)
{
    mViewObjectTable = new JsonDbObjectTable(mPartition);
}

JsonDbView::~JsonDbView()
{
    close();
    delete mViewObjectTable;
}

void JsonDbView::open()
{
    QFileInfo fi(mPartition->filename());
    QString dirName = fi.dir().path();
    QString baseName = fi.fileName();
    baseName.replace(".db", "");
    if (!mViewObjectTable->open(QString("%1/%2-%3-View.db")
                               .arg(dirName)
                               .arg(baseName)
                               .arg(mViewType),
                               JsonDbBtree::Default)) {
        qCritical() << "viewDb->open" << mViewObjectTable->errorMessage();
        return;
    }
}

void JsonDbView::close()
{
    if (mViewObjectTable)
        mViewObjectTable->close();
}

void JsonDbView::initViews(JsonDbPartition *partition)
{
    if (jsondbSettings->verbose())
        qDebug() << "Initializing views on partition" << partition->name();
    {
        JsonDbObjectList mrdList = partition->getObjects(JsonDbString::kTypeStr, JsonDbString::kMapTypeStr).data;

        for (int i = 0; i < mrdList.size(); ++i) {
            JsonDbObject mrd = mrdList.at(i);
            JsonDbView *view = partition->addView(mrd.value("targetType").toString());
            view->createMapDefinition(mrd);
        }
    }
    {
        JsonDbObjectList mrdList = partition->getObjects(JsonDbString::kTypeStr, JsonDbString::kReduceTypeStr).data;

        for (int i = 0; i < mrdList.size(); ++i) {
            JsonDbObject mrd = mrdList.at(i);
            JsonDbView *view = partition->addView(mrd.value("targetType").toString());
            view->createReduceDefinition(mrd);
        }
    }
}

void JsonDbView::createDefinition(JsonDbPartition *partition, QJsonObject definition)
{
    QString definitionType = definition.value(JsonDbString::kTypeStr).toString();
    QString targetType = definition.value("targetType").toString();
    JsonDbView *view = partition->findView(targetType);
     if (!view)
        return;
    if (jsondbSettings->verbose())
        qDebug() << "createDefinition" << targetType;
    if (definitionType == JsonDbString::kMapTypeStr)
        view->createMapDefinition(definition);
    else
        view->createReduceDefinition(definition);
}
void JsonDbView::removeDefinition(JsonDbPartition *partition, QJsonObject definition)
{
    QString definitionType = definition.value(JsonDbString::kTypeStr).toString();
    QString targetType = definition.value("targetType").toString();
    JsonDbView *view = partition->findView(targetType);
     if (!view)
        return;
    if (jsondbSettings->verbose())
        qDebug() << "removeDefinition" << targetType;

    if (definitionType == JsonDbString::kMapTypeStr)
        view->removeMapDefinition(definition);
    else
        view->removeReduceDefinition(definition);
}


void JsonDbView::createMapDefinition(QJsonObject mapDefinition)
{
    QString targetType = mapDefinition.value("targetType").toString();
    QString uuid = mapDefinition.value(JsonDbString::kUuidStr).toString();
    if (jsondbSettings->verbose())
        qDebug() << "createMapDefinition" << uuid << targetType << "{";

    JsonDbOwner *owner = new JsonDbOwner(this);
    owner->setAllowAll(true);
    JsonDbMapDefinition *def = new JsonDbMapDefinition(owner, mPartition, mapDefinition, this);
    def->initIndexes();

    QStringList sourceTypes = def->sourceTypes();
    for (int i = 0; i < sourceTypes.size(); i++) {
        const QString sourceType = sourceTypes[i];
        mMapDefinitionsBySource.insert(sourceType, def);
    }
    mMapDefinitions.insert(def->uuid(), def);
    updateSourceTypesList();
    if (jsondbSettings->verbose())
        qDebug() << "createMapDefinition" << uuid << targetType << "}";
}

void JsonDbView::removeMapDefinition(QJsonObject mapDefinition)
{
    QString targetType = mapDefinition.value("targetType").toString();
    QString uuid = mapDefinition.value(JsonDbString::kUuidStr).toString();
    if (jsondbSettings->verbose())
        qDebug() << "removeMapDefinition" << uuid << targetType << "{";
    foreach (JsonDbMapDefinition *d, mMapDefinitions) {
        if (d->uuid() == uuid) {
            JsonDbMapDefinition *def = d;
            mMapDefinitions.remove(def->uuid());
            const QStringList &sourceTypes = def->sourceTypes();
            for (int i = 0; i < sourceTypes.size(); i++)
                mMapDefinitionsBySource.remove(sourceTypes[i]);
            break;
        }
    }

    updateSourceTypesList();
    if (jsondbSettings->verbose())
        qDebug() << "removeMapDefinition" << uuid << targetType << "}";
}

void JsonDbView::createReduceDefinition(QJsonObject reduceDefinition)
{
    QString targetType = reduceDefinition.value("targetType").toString();
    QString sourceType = reduceDefinition.value("sourceType").toString();
    if (jsondbSettings->debug())
        qDebug() << "createReduceDefinition" << sourceType << targetType << sourceType << "{";

    JsonDbOwner *owner = new JsonDbOwner(this);
    owner->setAllowAll(true);
    JsonDbReduceDefinition *def = new JsonDbReduceDefinition(owner, mPartition, reduceDefinition, this);
    def->initIndexes();
    mReduceDefinitionsBySource.insert(sourceType, def);
    mReduceDefinitions.insert(def->uuid(), def);

    updateSourceTypesList();
    if (jsondbSettings->verbose())
        qDebug() << "createReduceDefinition" << sourceType << targetType << "}";
}

void JsonDbView::removeReduceDefinition(QJsonObject reduceDefinition)
{
    QString targetType = reduceDefinition.value("targetType").toString();
    QString sourceType = reduceDefinition.value("sourceType").toString();
    QString uuid = reduceDefinition.value(JsonDbString::kUuidStr).toString();

    if (jsondbSettings->verbose())
        qDebug() << "removeReduceDefinition" << sourceType <<  targetType << "{";

    foreach (JsonDbReduceDefinition *d, mReduceDefinitionsBySource.values(sourceType)) {
        if (d->uuid() == uuid) {
            JsonDbReduceDefinition *def = d;
            mReduceDefinitionsBySource.remove(def->sourceType());
            mReduceDefinitions.remove(def->uuid());
            break;
        }
    }

    updateSourceTypesList();
    //TODO: actually remove the table
    if (jsondbSettings->verbose())
        qDebug() << "removeReduceDefinition" << sourceType <<  targetType << "}";
}

void JsonDbView::updateSourceTypesList()
{
    QSet<QString> sourceTypes;
    foreach (const JsonDbMapDefinition *d, mMapDefinitions) {
        foreach (const QString sourceType, d->sourceTypes())
            sourceTypes.insert(sourceType);
    }
    foreach (const JsonDbReduceDefinition *d, mReduceDefinitions)
        sourceTypes.insert(d->sourceType());

    ObjectTableSourceTypeMap objectTableSourceTypeMap;
    for (QSet<QString>::const_iterator it = sourceTypes.begin();
         it != sourceTypes.end();
         ++it) {
        QString sourceType = *it;
        JsonDbObjectTable *objectTable = mPartition->findObjectTable(sourceType);
        objectTableSourceTypeMap[objectTable].insert(sourceType);
    }
    mSourceTypes = sourceTypes.toList();
    mObjectTableSourceTypeMap = objectTableSourceTypeMap;
}

void JsonDbView::updateView(quint32 desiredStateNumber)
{
    QElapsedTimer timer;
    if (jsondbSettings->performanceLog())
        timer.start();
    if (jsondbSettings->verbose())
        qDebug() << endl << "updateView" << mViewType << "{" << endl;
    // current state of the main object table of the partition
    quint32 partitionStateNumber = mMainObjectTable->stateNumber();
    quint32 viewStateNumber = mViewObjectTable->stateNumber();

    // if the view is up to date, then return
    if ((desiredStateNumber && viewStateNumber >= desiredStateNumber)
        || viewStateNumber == partitionStateNumber) {
        if (jsondbSettings->verbose())
            qDebug() << "updateView" << mViewType << "}";
        return;
    }
    if (mUpdating) {
        if (jsondbSettings->verbose())
            qDebug() << endl << "Update already in progess" << endl
                     << "updateView" << mViewType << "}";
        return;
    }
    mUpdating = true;

    // update the source types in case they are views
    for (JsonDbView::ObjectTableSourceTypeMap::const_iterator it = mObjectTableSourceTypeMap.begin();
         it != mObjectTableSourceTypeMap.end();
         ++it) {
        const QSet<QString> &sourceTypes = it.value();
        QString sourceType = *sourceTypes.begin();
        // update the source tables (as indicated by one of the source types)
        mPartition->updateView(sourceType);
    }

    // The Uuids of the definitions processed for the first time by processUpdatedDefinitions.
    QSet<QString> processedDefinitionUuids;

    // find any Map or Reduce definitions that have been updated from
    // states viewStateNumber to partitionStateNumber and process them.
    bool inTransaction =
      processUpdatedDefinitions(mViewType, viewStateNumber, processedDefinitionUuids);
    // Do not process them again during this update of the view.

    if (jsondbSettings->verbose())
        qDebug() << "processedDefinitionUuids" << processedDefinitionUuids;
    // if there were any updated definitions, then it would have
    // already begun the transaction on the view's mObjectTable.
    if (!inTransaction)
      mViewObjectTable->begin();
    inTransaction = true;

    // now process the changes on each of the source tables
    for (ObjectTableSourceTypeMap::const_iterator it = mObjectTableSourceTypeMap.begin();
         it != mObjectTableSourceTypeMap.end();
         ++it) {
        JsonDbObjectTable *sourceTable = it.key();
        const QSet<QString> &sourceTypes = it.value();
        if (sourceTable->stateNumber() == viewStateNumber)
            // up-to-date with respect this source table
            continue;

        QJsonObject changesSince(sourceTable->changesSince(viewStateNumber, sourceTypes));
        QJsonObject changes(changesSince.value("result").toObject());
        QJsonArray changeList(changes.value("changes").toArray());
        quint32 count = changeList.size();
        for (quint32 i = 0; i < count; i++) {
            QJsonObject change = changeList.at(i).toObject();
            QJsonValue before = change.value("before");
            QJsonValue after = change.value("after");

            QJsonObject beforeObject = before.toObject();
            QJsonObject afterObject = after.toObject();
            QString beforeType = beforeObject.value(JsonDbString::kTypeStr).toString();
            QString afterType = afterObject.value(JsonDbString::kTypeStr).toString();

            if (mMapDefinitionsBySource.contains(beforeType)) {
                JsonDbMapDefinition *def = mMapDefinitionsBySource.value(beforeType);
                if (processedDefinitionUuids.contains(def->uuid()))
                    continue;
                def->updateObject(beforeObject, afterObject);
            } else if (mMapDefinitionsBySource.contains(afterType)) {
                JsonDbMapDefinition *def = mMapDefinitionsBySource.value(afterType);
                if (processedDefinitionUuids.contains(def->uuid()))
                    continue;
                def->updateObject(beforeObject, afterObject);
            }

            if (mReduceDefinitionsBySource.contains(beforeType)) {
                JsonDbReduceDefinition *def = mReduceDefinitionsBySource.value(beforeType);
                if (processedDefinitionUuids.contains(def->uuid()))
                    continue;
                def->updateObject(beforeObject, afterObject);
            } else if (mReduceDefinitionsBySource.contains(afterType)) {
                JsonDbReduceDefinition *def = mReduceDefinitionsBySource.value(afterType);
                if (processedDefinitionUuids.contains(def->uuid()))
                    continue;
                def->updateObject(beforeObject, afterObject);
            }
        }
    }
    if (inTransaction)
        mViewObjectTable->commit(partitionStateNumber);
    if (jsondbSettings->verbose())
        qDebug() << endl << "}" << "updateView" << mViewType << endl;
    if (jsondbSettings->performanceLog())
        qDebug() << "updateView" << mViewType << timer.elapsed() << "ms";
    mUpdating = false;

    emit updated(mViewType);
}

bool JsonDbView::processUpdatedDefinitions(const QString &viewType, quint32 targetStateNumber,
                                           QSet<QString> &processedDefinitionUuids)
{
    bool inTransaction = false;
    quint32 stateNumber = mMainObjectTable->stateNumber();
    if (stateNumber == targetStateNumber)
        return inTransaction;
    QSet<QString> limitTypes;
    limitTypes << JsonDbString::kMapTypeStr << JsonDbString::kReduceTypeStr;
    QJsonObject changes = mMainObjectTable->changesSince(targetStateNumber, limitTypes).value("result").toObject();
    quint32 count = changes.value("count").toDouble();
    QJsonArray changeList = changes.value("changes").toArray();
    for (quint32 i = 0; i < count; i++) {
        QJsonObject change = changeList.at(i).toObject();
        QString definitionUuid;
        if (jsondbSettings->verbose())
            qDebug() << "change" << change;
        if (change.contains("before")) {
            QJsonObject before = change.value("before").toObject();
            QString beforeType = before.value(JsonDbString::kTypeStr).toString();
            if ((limitTypes.contains(beforeType))
                && (before.value("targetType").toString() == viewType)) {
                if (!inTransaction) {
                    mViewObjectTable->begin();
                    inTransaction = true;
                }
                definitionUuid = before.value(JsonDbString::kUuidStr).toString();
                QString definitionType = before.value(JsonDbString::kTypeStr).toString();
                QString targetType = before.value("targetType").toString();
                if (definitionType == JsonDbString::kMapTypeStr)
                    JsonDbMapDefinition::definitionRemoved(mPartition, mViewObjectTable, targetType, definitionUuid);
                else
                    JsonDbReduceDefinition::definitionRemoved(mPartition, mViewObjectTable, targetType, definitionUuid);
            }
        }
        if (change.contains("after")) {
            QJsonObject after = change.value("after").toObject();
            QString afterType = after.value(JsonDbString::kTypeStr).toString();
            if (jsondbSettings->verbose())
                qDebug() << "afterVersion" << after.value(JsonDbString::kVersionStr).toString();
            if ((limitTypes.contains(afterType))
                && (after.value("targetType").toString() == viewType)) {
                if (!inTransaction) {
                    mViewObjectTable->begin();
                    inTransaction = true;
                }
                definitionUuid = after.value(JsonDbString::kUuidStr).toString();
                QString definitionType = after.value(JsonDbString::kTypeStr).toString();
                if (!after.contains(JsonDbString::kActiveStr) || after.value(JsonDbString::kActiveStr).toBool()) {
                    if (definitionType == JsonDbString::kMapTypeStr)
                        mMapDefinitions.value(definitionUuid)->definitionCreated();
                    else
                        mReduceDefinitions.value(definitionUuid)->definitionCreated();
                }
            }
        }
        if (!definitionUuid.isEmpty())
            processedDefinitionUuids.insert(definitionUuid);
    }
    return inTransaction;
}

void JsonDbView::reduceMemoryUsage()
{
    mViewObjectTable->flushCaches();

    for (QMap<QString,JsonDbMapDefinition*>::iterator it = mMapDefinitions.begin();
         it != mMapDefinitions.end();
         ++it)
        it.value()->releaseScriptEngine();
    for (QMap<QString,JsonDbReduceDefinition*>::iterator it = mReduceDefinitions.begin();
         it != mReduceDefinitions.end();
         ++it)
        it.value()->releaseScriptEngine();
}

bool JsonDbView::isActive() const
{
    foreach (JsonDbMapDefinition *mapDef, mMapDefinitions) {
        if (mapDef->isActive())
            return true;
    }

    foreach (JsonDbReduceDefinition *reduceDef, mReduceDefinitions) {
        if (reduceDef->isActive())
            return true;
    }

    return false;
}

QT_END_NAMESPACE_JSONDB