summaryrefslogtreecommitdiffstats
path: root/src/partition/jsondbpartition_p.h
blob: efa19070b4d28896428cd87e9c5c287a60a72925 (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
/****************************************************************************
**
** 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_PARTITION_P_H
#define JSONDB_PARTITION_P_H

#include <QMultiHash>
#include <QStringList>
#include <QSet>
#include <QTimer>
#include <QVector>
#include <QPointer>

#include "jsondberrors.h"
#include "jsondbnotification.h"
#include "jsondbobjectkey.h"
#include "jsondbowner.h"
#include "jsondbpartition.h"
#include "jsondbpartitionglobal.h"
#include "jsondbschemamanager_p.h"
#include "jsondbstat.h"

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE_JSONDB_PARTITION

class JsonDbBtree;
class JsonDbOwner;
class JsonDbObjectTable;
class JsonDbIndexSpec;
class JsonDbIndex;
class JsonDbIndexQuery;
class JsonDbView;

class Q_JSONDB_PARTITION_EXPORT JsonDbPartitionPrivate
{
    Q_DECLARE_PUBLIC(JsonDbPartition)
public:
    JsonDbPartitionPrivate(JsonDbPartition *q);
    ~JsonDbPartitionPrivate();

    bool beginTransaction();
    JsonDbPartition::TxnCommitResult commitTransaction(quint32 stateNumber = 0);
    bool abortTransaction();

    void initIndexes();
    void initSchemas();

    JsonDbObjectTable *findObjectTable(const QString &objectType) const;

    bool addIndex(const JsonDbIndexSpec &indexSpec);
    bool removeIndex(const QString &indexName, const QString &objectType = QString());

    JsonDbView *addView(const QString &viewType);
    void removeView(const QString &viewType);

    bool getObject(const QString &uuid, JsonDbObject &object, const QString &objectType = QString(), bool includeDeleted = false) const;
    bool getObject(const ObjectKey & objectKey, JsonDbObject &object, const QString &objectType = QString(), bool includeDeleted = false) const;

    GetObjectsResult getObjects(const QString &keyName, const QJsonValue &key, const QString &type = QString(),
                                bool updateViews = true);

    JsonDbIndexQuery *compileIndexQuery(const JsonDbOwner *owner, const JsonDbQuery &query);

    void doIndexQuery(const JsonDbOwner *owner, JsonDbObjectList &results, int &limit, int &offset,
                      JsonDbIndexQuery *indexQuery);

    static void sortValues(const JsonDbQuery &query, JsonDbObjectList &results, JsonDbObjectList &joinedResults);

    bool checkCanAddSchema(const JsonDbObject &schema, const JsonDbObject &oldSchema, QString &errorMsg);
    bool checkCanRemoveSchema(const JsonDbObject &schema, QString &errorMsg);
    bool validateSchema(const QString &schemaName, const JsonDbObject &object, QString &errorMsg);
    bool checkNaturalObjectType(const JsonDbObject &object, QString &errorMsg);

    JsonDbError::ErrorCode checkBuiltInTypeValidity(const JsonDbObject &object, const JsonDbObject &oldObject, QString &errorMsg);
    JsonDbError::ErrorCode checkBuiltInTypeAccessControl(bool forCreation, const JsonDbOwner *owner, const JsonDbObject &object,
                                                         const JsonDbObject &oldObject, QString &errorMsg);
    void updateBuiltInTypes(const JsonDbObject &object, const JsonDbObject &oldObject);
    void setSchema(const QString &schemaName, const QJsonObject &schema);
    void removeSchema(const QString &schemaName);
    void updateSpaceStatus();
    bool hasSpace();

    void updateEagerViews(const QSet<QString> &eagerViewTypes, const JsonDbUpdateList &changes);
    void updateEagerViewTypes(const QString &viewType, quint32 stateNumber, int increment = 1);
    void updateEagerViewStateNumbers();
    void notifyHistoricalChanges(JsonDbNotification *n);

    void _q_mainSyncTimer();
    void _q_indexSyncTimer();
    void _q_objectsUpdated(bool viewUpdated, const JsonDbUpdateList &changes);

    class EdgeCount {
    public:
        EdgeCount() : count(0){}
        int count;
        bool operator >(int val) const { return count > val; }
        bool operator ==(int val) const { return count == val; }
        EdgeCount &operator +=(int delta) { count += delta; if (count < 0) count = 0; return *this; }
    };
    typedef QHash<QString, EdgeCount>        ViewEdgeWeights;
    typedef QHash<QString, ViewEdgeWeights>  WeightedSourceViewGraph;

    JsonDbPartition *q_ptr;
    JsonDbObjectTable     *mObjectTable;
    QVector<JsonDbObjectTable *> mTableTransactions;

    JsonDbPartitionSpec mSpec;
    QString      mFilename;
    int          mTransactionDepth;
    bool         mTransactionOk;
    QHash<QString,QPointer<JsonDbView> > mViews;
    QSet<QString> mViewTypes;
    QMultiHash<QString, QPointer<JsonDbNotification> > mKeyedNotifications;
    WeightedSourceViewGraph mEagerViewSourceGraph;
    JsonDbSchemaManager   mSchemas;
    QTimer      *mMainSyncTimer;
    QTimer      *mIndexSyncTimer;
    JsonDbOwner *mDefaultOwner;
    bool         mIsOpen;
    JsonDbPartition::DiskSpaceStatus mDiskSpaceStatus;

};

class WithTransaction {
public:
    WithTransaction(JsonDbPartitionPrivate *partition = 0, QString name=QString())
        : mPartition(0)
    {
        Q_UNUSED(name)
        if (partition && partition->mIsOpen)
            setPartition(partition);
    }

    ~WithTransaction()
    {
        if (mPartition)
            mPartition->commitTransaction();
    }

    void setPartition(JsonDbPartitionPrivate *partition)
    {
        Q_ASSERT(!mPartition);
        mPartition = partition;
        if (!mPartition->beginTransaction())
            mPartition = 0;
    }

    bool addObjectTable(JsonDbObjectTable *table);

    void abort()
    {
        if (mPartition)
            mPartition->abortTransaction();
        mPartition = 0;
    }

    JsonDbPartition::TxnCommitResult commit(quint32 stateNumber = 0)
    {
        JsonDbPartition::TxnCommitResult result;
        if (mPartition)
            result = mPartition->commitTransaction(stateNumber);
        mPartition = 0;
        return result;
    }

private:
    JsonDbPartitionPrivate *mPartition;
};

QDebug &operator<<(QDebug &, const ObjectKey &);

QT_END_NAMESPACE_JSONDB_PARTITION

QT_END_HEADER

#endif // JSONDB_PARTITION_P_H