summaryrefslogtreecommitdiffstats
path: root/src/partition/jsondbindexquery.h
blob: 06a3c83d710f6e3f9bff68aef1cbc09ecf9fa1c0 (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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef JSONDB_INDEXQUERY_H
#define JSONDB_INDEXQUERY_H

#include <QJsonValue>
#include <QSet>
#include <QVector>
#include <QStringList>

#include "jsondbpartitionglobal.h"
#include "jsondbobject.h"
#include "jsondbobjectkey.h"
#include "jsondbquery.h"
#include "jsondbbtree.h"
#include "jsondbquery.h"

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE_JSONDB_PARTITION

class JsonDbObjectTable;
class JsonDbOwner;
class JsonDbPartition;
class JsonDbQuery;

class Q_JSONDB_PARTITION_EXPORT JsonDbQueryConstraint {
public:
    virtual ~JsonDbQueryConstraint() { }
    virtual bool matches(const QJsonValue &value) = 0;
    virtual bool sparseMatchPossible() const { return false; }
};

class Q_JSONDB_PARTITION_EXPORT JsonDbIndexQuery
{
protected:
    JsonDbIndexQuery(JsonDbPartition *partition, JsonDbObjectTable *table,
               const QString &propertyName, const QString &propertyType,
               const JsonDbOwner *owner, const JsonDbQuery &query);
public:
    static JsonDbIndexQuery *indexQuery(JsonDbPartition *partition, JsonDbObjectTable *table,
                                  const QString &propertyName, const QString &propertyType,
                                  const JsonDbOwner *owner, const JsonDbQuery &query);
    virtual ~JsonDbIndexQuery();

    JsonDbObjectTable *objectTable() const { return mObjectTable; }
    QString partition() const;
    void addConstraint(JsonDbQueryConstraint *qc) { mQueryConstraints.append(qc); }
    QString propertyName() const { return mPropertyName; }
    QString propertyType() const { return mPropertyType; }
    void setTypeNames(const QSet<QString> typeNames) { mTypeNames = typeNames; }
    void setMin(const QJsonValue &minv);
    void setMax(const QJsonValue &maxv);
    QString aggregateOperation() const { return mAggregateOperation; }
    void setAggregateOperation(QString op) { mAggregateOperation = op; }
    void setResultExpressionList(const QStringList &resultExpressionList);
    void setResultKeyList(QStringList resultKeyList) { mResultKeyList = resultKeyList; }

    JsonDbObject first(); // returns first matching object
    JsonDbObject next(); // returns next matching object
    JsonDbObject first(QByteArray *key); // returns first matching object and its key
    JsonDbObject next(QByteArray *key); // returns next matching object and its keyb
    JsonDbObject seek(const QByteArray &key); // returns the object matching key
    bool matches(const QJsonValue &value);
    QJsonValue fieldValue() const { return mFieldValue; }

    const JsonDbQuery &query() const { return mQuery; }

    const JsonDbQuery &residualQuery() const { return mResidualQuery; }
    void setResidualQuery(const JsonDbQuery &residualQuery)
    { Q_ASSERT(!residualQuery.query.isEmpty()); mResidualQuery = residualQuery; }

    virtual quint32 stateNumber() const;

    void compileOrQueryTerm(const JsonDbQueryTerm &queryTerm);
    JsonDbObject resultObject(const JsonDbObject &object);

    static bool lessThan(const QJsonValue &a, const QJsonValue &b);
    static bool greaterThan(const QJsonValue &a, const QJsonValue &b);

protected:
    virtual bool seekToStart(QJsonValue &fieldValue);
    virtual bool seekToStart(QJsonValue &fieldValue, QByteArray *key);
    virtual bool seekToNext(QJsonValue &fieldValue);
    virtual bool seekToNext(QJsonValue &fieldValue, QByteArray *key);
    virtual bool seekTo(const QByteArray &key, QJsonValue &fieldValue);
    virtual JsonDbObject currentObjectAndTypeNumber(ObjectKey &objectKey);

protected:
    JsonDbPartition *mPartition;
    JsonDbObjectTable   *mObjectTable;
    JsonDbBtree *mBdbIndex;
    bool isOwnTransaction;
    JsonDbBtree::Transaction *mTxn;
    JsonDbBtree::Cursor *mCursor;
    const JsonDbOwner *mOwner;
    QJsonValue      mMin, mMax;
    QSet<QString> mTypeNames;
    QString       mUuid;
    QVector<JsonDbQueryConstraint*> mQueryConstraints;
    QString       mAggregateOperation;
    QString       mPropertyName;
    QString       mPropertyType;
    QJsonValue     mFieldValue; // value of field for the object the cursor is pointing at
    bool          mSparseMatchPossible;
    QHash<QString, JsonDbObject> mObjectCache;
    QStringList  mResultExpressionList;
    QStringList  mResultKeyList;
    QVector<QVector<QStringList> > mJoinPaths;
    JsonDbQuery  mQuery;
    JsonDbQuery  mResidualQuery;

    Q_DISABLE_COPY(JsonDbIndexQuery)
};

class JsonDbUuidQuery : public JsonDbIndexQuery {
protected:
    JsonDbUuidQuery(JsonDbPartition *partition, JsonDbObjectTable *table,
                    const QString &propertyName, const JsonDbOwner *owner,
                    const JsonDbQuery &query);
    virtual bool seekToStart(QJsonValue &fieldValue);
    virtual bool seekToStart(QJsonValue &fieldValue, QByteArray *key);
    virtual bool seekToNext(QJsonValue &fieldValue);
    virtual bool seekToNext(QJsonValue &fieldValue, QByteArray *key);
    virtual bool seekTo(const QByteArray &key, QJsonValue &fieldValue) { return false; }
    virtual JsonDbObject currentObjectAndTypeNumber(ObjectKey &objectKey);
    virtual quint32 stateNumber() const;
    friend class JsonDbIndexQuery;
};

QT_END_NAMESPACE_JSONDB_PARTITION

QT_END_HEADER

#endif // JSONDB_INDEXQUERY_H