summaryrefslogtreecommitdiffstats
path: root/src/daemon/jsondbmapdefinition.cpp
blob: a3fe0d1411d8432066bbf7a307b7b18875344cd9 (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
/****************************************************************************
**
** 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 <QDebug>
#include <QElapsedTimer>
#include <QRegExp>
#include <QJSValue>
#include <QJSValueIterator>
#include <QStringList>

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include "jsondbpartition.h"
#include "jsondb-strings.h"
#include "jsondb-error.h"
#include "json.h"

#include "jsondbproxy.h"
#include "jsondbobjecttable.h"
#include "jsondbmapdefinition.h"
#include "jsondbsettings.h"
#include "jsondbview.h"

QT_BEGIN_NAMESPACE_JSONDB

JsonDbMapDefinition::JsonDbMapDefinition(const JsonDbOwner *owner, JsonDbPartition *partition, QJsonObject definition, QObject *parent) :
    QObject(parent)
    , mPartition(partition)
    , mOwner(owner)
    , mDefinition(definition)
    , mScriptEngine(0)
    , mUuid(definition.value(JsonDbString::kUuidStr).toString())
    , mTargetType(definition.value("targetType").toString())
    , mTargetTable(mPartition->findObjectTable(mTargetType))
{
    if (!mDefinition.contains("sourceType")) {
        QJsonObject sourceFunctions(mDefinition.contains("join")
                                   ? mDefinition.value("join").toObject()
                                   : mDefinition.value("map").toObject());
        mSourceTypes = sourceFunctions.keys();
        for (int i = 0; i < mSourceTypes.size(); i++) {
            const QString &sourceType = mSourceTypes[i];
            mSourceTables[sourceType] = mPartition->findObjectTable(sourceType);
        }
    } else {
        // TODO: remove this case
        const QString sourceType = mDefinition.value("sourceType").toString();
        mSourceTables[sourceType] = mPartition->findObjectTable(sourceType);
        mSourceTypes.append(sourceType);
    }
}

void JsonDbMapDefinition::definitionCreated()
{
  initScriptEngine();

  mTargetTable->addIndexOnProperty(QLatin1String("_sourceUuids.*"), QLatin1String("string"), mTargetType);

    foreach (const QString &sourceType, mSourceTypes) {
        GetObjectsResult getObjectResponse = mPartition->getObjects(JsonDbString::kTypeStr, sourceType);
        if (!getObjectResponse.error.isNull()) {
            if (jsondbSettings->verbose())
                qDebug() << "createMapDefinition" << mSourceTypes << sourceType << mTargetType << getObjectResponse.error.toString();
            setError(getObjectResponse.error.toString());
            return;
        }
        JsonDbObjectList objects = getObjectResponse.data;
        bool isJoin = mDefinition.contains(QLatin1String("join"));
        for (int i = 0; i < objects.size(); i++) {
            JsonDbObject object(objects.at(i));
            if (isJoin)
                unmapObject(object);
            updateObject(JsonDbObject(), objects.at(i));
        }
    }
}

void JsonDbMapDefinition::definitionRemoved(JsonDbPartition *partition, JsonDbObjectTable *table, const QString targetType, const QString &definitionUuid)
{
    // remove the output objects
    GetObjectsResult getObjectResponse = table->getObjects(QLatin1String("_sourceUuids.*"),
                                                           definitionUuid,
                                                           targetType);
    JsonDbObjectList objects = getObjectResponse.data;
    for (int i = 0; i < objects.size(); i++) {
        JsonDbObject o = objects[i];
        o.markDeleted();
        partition->updateObject(partition->defaultOwner(), o, JsonDbPartition::ViewObject);
    }
}

void JsonDbMapDefinition::initScriptEngine()
{
    if (mScriptEngine)
        return;

    mScriptEngine = new QJSEngine(this);
    QJSValue globalObject = mScriptEngine->globalObject();
    globalObject.setProperty("console", mScriptEngine->newQObject(new Console()));

    if (!mDefinition.contains("sourceType")) {
        QJsonObject sourceFunctions(mDefinition.contains("join")
                                   ? mDefinition.value("join").toObject()
                                   : mDefinition.value("map").toObject());
        for (int i = 0; i < mSourceTypes.size(); i++) {
            const QString &sourceType = mSourceTypes[i];
            const QString &script = sourceFunctions.value(sourceType).toString();
            QJSValue mapFunction =
                mScriptEngine->evaluate(QString("var map_%1 = (%2); map_%1;").arg(QString(sourceType).replace(".", "_")).arg(script));
            if (mapFunction.isError() || !mapFunction.isCallable())
                setError( "Unable to parse map function: " + mapFunction.toString());
            mMapFunctions[sourceType] = mapFunction;
        }

        mJoinProxy = new JsonDbJoinProxy(mOwner, mPartition, this);
        connect(mJoinProxy, SIGNAL(lookupRequested(QJSValue,QJSValue)),
                this, SLOT(lookupRequested(QJSValue,QJSValue)));
        connect(mJoinProxy, SIGNAL(viewObjectEmitted(QJSValue)),
                this, SLOT(viewObjectEmitted(QJSValue)));
        globalObject.setProperty("_jsondb", mScriptEngine->newQObject(mJoinProxy));
        // we use this snippet of javascript so that we can bind "jsondb.emit"
        // even though "emit" is a Qt keyword
        if (mDefinition.contains("join"))
            // only joins can use lookup()
            mScriptEngine->evaluate("var jsondb = { emit: _jsondb.create, lookup: _jsondb.lookup, createUuidFromString: _jsondb.createUuidFromString};");
        else
            mScriptEngine->evaluate("var jsondb = { emit: _jsondb.create, createUuidFromString: _jsondb.createUuidFromString };");

    } else {
        const QString sourceType = mDefinition.value("sourceType").toString();
        const QString &script = mDefinition.value("map").toString();
        QJSValue mapFunction =
            mScriptEngine->evaluate(QString("var map_%1 = (%2); map_%1;").arg(QString(sourceType).replace(".", "_")).arg(script));
        if (mapFunction.isError() || !mapFunction.isCallable())
            setError( "Unable to parse map function: " + mapFunction.toString());
        mMapFunctions[sourceType] = mapFunction;

        mMapProxy = new JsonDbMapProxy(mOwner, mPartition, this);
        connect(mMapProxy, SIGNAL(lookupRequested(QJSValue,QJSValue)),
                this, SLOT(lookupRequested(QJSValue,QJSValue)));
        connect(mMapProxy, SIGNAL(viewObjectEmitted(QJSValue)),
                this, SLOT(viewObjectEmitted(QJSValue)));
        globalObject.setProperty("jsondb", mScriptEngine->newQObject(mMapProxy));
        qWarning() << "Old-style Map from sourceType" << sourceType << " to targetType" << mDefinition.value("targetType");
    }
}

void JsonDbMapDefinition::releaseScriptEngine()
{
    mMapFunctions.clear();
    delete mScriptEngine;
    mScriptEngine = 0;
}

void JsonDbMapDefinition::updateObject(const JsonDbObject &beforeObject, const JsonDbObject &afterObject)
{
    QHash<QString, JsonDbObject> unmappedObjects;
    mEmittedObjects.clear();

    if (!beforeObject.isEmpty()) {
        QJsonValue uuid = beforeObject.value(JsonDbString::kUuidStr);
        GetObjectsResult getObjectResponse = mTargetTable->getObjects("_sourceUuids.*", uuid, mTargetType);
        foreach (const JsonDbObject &unmappedObject, getObjectResponse.data) {
            QString uuid = unmappedObject.value(JsonDbString::kUuidStr).toString();
            unmappedObjects[uuid] = unmappedObject;
        }
    }

    mapObject(afterObject);

    for (QHash<QString, JsonDbObject>::const_iterator it = unmappedObjects.begin();
         it != unmappedObjects.end();
         ++it) {
        JsonDbObject unmappedObject = it.value();
        QString uuid = unmappedObject.value(JsonDbString::kUuidStr).toString();
        JsonDbWriteResult res;
        if (mEmittedObjects.contains(uuid)) {
            JsonDbObject emittedObject(mEmittedObjects.value(uuid));
            emittedObject.insert(JsonDbString::kVersionStr, unmappedObject.value(JsonDbString::kVersionStr));
            emittedObject.insert(JsonDbString::kOwnerStr, unmappedObject.value(JsonDbString::kOwnerStr));
            if (emittedObject == it.value()) {
                // skip duplicates
                continue;
            } else {
                // update changed view objects
                res = mPartition->updateObject(mOwner, emittedObject, JsonDbPartition::ViewObject);
            }

            mEmittedObjects.remove(uuid);
        } else {
            // remove unmatched objects
            unmappedObject.markDeleted();
            res = mPartition->updateObject(mOwner, unmappedObject, JsonDbPartition::ViewObject);
        }

        if (res.code != JsonDbError::NoError)
            setError("Error updating view object: " + res.message);
    }

    for (QHash<QString, JsonDbObject>::const_iterator it = mEmittedObjects.begin();
         it != mEmittedObjects.end();
         ++it) {
        JsonDbObject newItem(it.value());
        JsonDbWriteResult res = mPartition->updateObject(mOwner, newItem, JsonDbPartition::ViewObject);
        if (res.code != JsonDbError::NoError)
            setError("Error creating view object: " + res.message);
    }
}

QJSValue JsonDbMapDefinition::mapFunction(const QString &sourceType) const
{
    if (mMapFunctions.contains(sourceType))
        return mMapFunctions[sourceType];
    else
        return QJSValue();
}

void JsonDbMapDefinition::mapObject(JsonDbObject object)
{
    initScriptEngine();
    const QString &sourceType = object.value(JsonDbString::kTypeStr).toString();

    QJSValue sv = JsonDbObject::toJSValue(object, mScriptEngine);
    QString uuid = object.value(JsonDbString::kUuidStr).toString();
    mSourceUuids.clear();
    mSourceUuids.append(mUuid); // depends on the map definition object
    mSourceUuids.append(uuid);  // depends on the source object
    QJSValue mapped;

    QJSValueList mapArgs;
    mapArgs << sv;
    mapped = mapFunction(sourceType).call(mapArgs);

    if (mapped.isError())
        setError("Error executing map function: " + mapped.toString());
}

void JsonDbMapDefinition::unmapObject(const JsonDbObject &object)
{
    Q_ASSERT(object.value(JsonDbString::kUuidStr).type() == QJsonValue::String);
    initScriptEngine();
    QJsonValue uuid = object.value(JsonDbString::kUuidStr);
    GetObjectsResult getObjectResponse = mTargetTable->getObjects("_sourceUuids.*", uuid, mTargetType);
    JsonDbObjectList dependentObjects = getObjectResponse.data;

    for (int i = 0; i < dependentObjects.size(); i++) {
        JsonDbObject dependentObject = dependentObjects.at(i);
        if (dependentObject.value(JsonDbString::kTypeStr).toString() != mTargetType)
            continue;

        dependentObject.markDeleted();
        mPartition->updateObject(mOwner, dependentObject, JsonDbPartition::ViewObject);
    }
}

void JsonDbMapDefinition::lookupRequested(const QJSValue &query, const QJSValue &context)
{
    QString objectType = query.property("objectType").toString();
    // compatibility for old style maps
    if (mDefinition.value("map").isObject()) {
        if (objectType.isEmpty()) {
            setError("No objectType provided to jsondb.lookup");
            return;
        }
        if (!mSourceTypes.contains(objectType)) {
            setError(QString("lookup requested for type %1 not in source types: %2")
                     .arg(objectType)
                     .arg(mSourceTypes.join(", ")));
            return;
        }
    }
    QString findKey = query.property("index").toString();
    QJSValue findValue = query.property("value");
    GetObjectsResult getObjectResponse =
        mPartition->getObjects(findKey, JsonDbObject::fromJSValue(findValue), objectType, false);
    if (!getObjectResponse.error.isNull()) {
        if (jsondbSettings->verbose())
            qDebug() << "lookupRequested" << mSourceTypes << mTargetType
                     << getObjectResponse.error.toString();
        setError(getObjectResponse.error.toString());
    }
    JsonDbObjectList objectList = getObjectResponse.data;
    for (int i = 0; i < objectList.size(); ++i) {
        JsonDbObject object = objectList.at(i);
        const QString uuid = object.value(JsonDbString::kUuidStr).toString();
        if (mSourceUuids.contains(uuid)) {
            if (jsondbSettings->verbose())
                qDebug() << "Lookup cycle detected" << "key" << findKey << JsonDbObject::fromJSValue(findValue) << "matching object" << uuid << "source uuids" << mSourceUuids;
            continue;
        }
        mSourceUuids.append(uuid);
        QJSValueList mapArgs;
        QJSValue sv = JsonDbObject::toJSValue(object, mScriptEngine);

        mapArgs << sv << context;
        QJSValue mapped = mMapFunctions[objectType].call(mapArgs);

        if (mapped.isError())
            setError("Error executing map function during lookup: " + mapped.toString());

        mSourceUuids.removeLast();
    }
}

void JsonDbMapDefinition::viewObjectEmitted(const QJSValue &value)
{
    JsonDbObject newItem(JsonDbObject::fromJSValue(value).toObject());
    newItem.insert(JsonDbString::kTypeStr, mTargetType);
    QJsonArray sourceUuids;
    foreach (const QString &str, mSourceUuids)
        sourceUuids.append(str);
    newItem.insert("_sourceUuids", sourceUuids);

    if (!newItem.contains(JsonDbString::kUuidStr))
        newItem.generateUuid();

    QString uuid = newItem.value(JsonDbString::kUuidStr).toString();
    mEmittedObjects.insert(uuid, newItem);
}

bool JsonDbMapDefinition::isActive() const
{
    return !mDefinition.contains(JsonDbString::kActiveStr) || mDefinition.value(JsonDbString::kActiveStr).toBool();
}

void JsonDbMapDefinition::setError(const QString &errorMsg)
{
    mDefinition.insert(JsonDbString::kActiveStr, false);
    mDefinition.insert(JsonDbString::kErrorStr, errorMsg);
    if (mPartition)
        mPartition->updateObject(mOwner, mDefinition, JsonDbPartition::ForcedWrite);
}

bool JsonDbMapDefinition::validateDefinition(const JsonDbObject &map, JsonDbPartition *partition, QString &message)
{
    message.clear();
    QString targetType = map.value("targetType").toString();
    QString uuid = map.value(JsonDbString::kUuidStr).toString();
    JsonDbView *view = partition->findView(targetType);
    QStringList sourceTypes;

    if (targetType.isEmpty()) {
        message = QLatin1Literal("targetType property for Map not specified");
    } else if (!view) {
        message = QLatin1Literal("targetType must be of a type that extends View");
    } else if (map.contains("join")) {
        QJsonObject sourceFunctions = map.value("join").toObject();
        sourceTypes = sourceFunctions.keys();
        if (sourceFunctions.isEmpty())
            message = QLatin1Literal("sourceTypes and functions for Map with join not specified");

        foreach (const QString &sourceType, sourceTypes) {
            if (sourceFunctions.value(sourceType).toString().isEmpty())
                message = QString("join function for source type '%1' not specified for Map").arg(sourceType);
            if (view->mMapDefinitionsBySource.contains(sourceType)
                && view->mMapDefinitionsBySource.value(sourceType)->uuid() != uuid)
                message =
                  QString("duplicate Map definition on source %1 and target %2")
                    .arg(sourceType).arg(targetType);
        }

        if (map.contains("map"))
            message = QLatin1Literal("Map 'join' and 'map' options are mutually exclusive");
        else if (map.contains("sourceType"))
            message = QLatin1Literal("Map 'join' and 'sourceType' options are mutually exclusive");
    } else {
        QJsonValue mapValue = map.value("map");
        if (map.value("sourceType").toString().isEmpty() && !mapValue.isObject())
            message = QLatin1String("sourceType property for Map not specified");
        else if (!mapValue.isString() && !mapValue.isObject())
            message = QLatin1String("map function for Map not specified");
    }

    return message.isEmpty();
}

QT_END_NAMESPACE_JSONDB