-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqueryengine.sol
212 lines (187 loc) · 7.04 KB
/
queryengine.sol
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
pragma solidity ^0.4.11;
import "bson/documentparser.sol";
import "lib/treeflat.sol";
contract QueryEngine {
using DocumentParser for byte[];
using BytesUtils for byte[];
using TreeFlat for TreeFlat.TreeRoot;
using TreeFlat for TreeFlat.TreeIterator;
byte constant or = 0x7c;
byte constant eq = 0x25;
string public version = "master-1.0.0";
function processQuery(byte[] query, byte[] data) constant returns (bool) {
//function processQuery(byte[] query, byte[] data) constant returns (bool) {
TreeFlat.TreeRoot memory treeDoc = data.getDocumentTree();
treeDoc = treeDoc.selectRoot();
TreeFlat.TreeIterator memory it = query.getDocumentTree().begin();
TreeFlat.TreeNode memory n = it.tree.nodes[0];
uint32 i = 0;
byte op = eq;
uint32 orDepth = 0;
bool success = true;
for (; n.depth != it.end().depth; ((it, n) = it.next())) {
// ///////////////////////////
// //// OR CLAUSE AMANGEMENT
if (orDepth != 0 && success == true &&
(orDepth & 0x0F000000) == 0x0F000000 && (orDepth & 0x000FFFFF) == n.depth) { // OR satisfied, reset orDepth
orDepth = 0;
}
if (orDepth != 0) {
if ((success == true && (orDepth & 0x0F000000) == 0x0F000000 && n.depth > (orDepth & 0x000FFFFF)) || // condition satisfied, cotinue till root of the OR
(success == false && n.depth > (orDepth & 0x000FFFFF) + 1)) {// condition failed, cotinue till root of the OR
continue;
}
if (success == false && (orDepth & 0x000FFFFF) == n.depth) {
return false;
} // OR failed, return false
if (success == true && (orDepth & 0x000FFFFF) + 1 == n.depth) {
if ((orDepth & 0x00F00000) == 0x00F00000) { // if oDepth & 15728640 it means we are at least in the second condition
orDepth |= 0x0F000000;
continue;
} else {
orDepth |= 0x00F00000;
}
}
if (success == false && (orDepth & 0x000FFFFF) + 1 == n.depth) { // after the failed clause, restore the orDepth
success = true;
}
}
if (n.name == DocumentParser.getCombinedNameType8(or, 0x04)) {
orDepth = n.depth;
continue;
}
// //// OR CLAUSE AMANGEMENT
// ///////////////////////////
if (n.depth != 0) {
if (orDepth == 0) {
for (i = treeDoc.getCurrentDepth(); i >= n.depth; i--) {
treeDoc = treeDoc.upToParent();
}
(success, treeDoc) = treeDoc.selectChild(n.name);
if (false == success) {
return false;
}
} else {
for (i = treeDoc.getCurrentDepth(); i > n.depth - 2; i--) {
treeDoc = treeDoc.upToParent();
}
if (n.depth > (orDepth & 0x000FFFFF) + 1) {
(success, treeDoc) = treeDoc.selectChild(n.name);
if (false == success) {
continue;
}
}
}
}
for (i = 0; i < n.lastValue && true == success; i++) {
success = checkValues(n.values[i], treeDoc, data, query, op);
if (false == success && orDepth == 0) {
return false;
}
}
}
return success;
}
function checkDocumentValidity(byte[] data, bytes3 idName, bool generateID) constant returns (bool) {
// If the length is less or equal 5 the document is empty.
if (data.length <= 5) {
return false;
}
bytes7 idName7 = bytes7(sha3(bytes32(idName)));
TreeFlat.TreeRoot memory treeDocument = data.getDocumentTree();
// For now we let only up to 8 nested document level
if (treeDocument.maxDepth > 8) {
return false;
}
TreeFlat.TreeIterator memory it = treeDocument.begin();
TreeFlat.TreeNode memory n = it.tree.nodes[0];
uint32 x = 0;
bytes7 name7;
byte bType;
for (; n.depth != it.end().depth; ((it, n) = it.next())) {
for (x = 0; x < n.lastValue; x++) {
(bType, name7) = DocumentParser.getTypeName8(n.values[x].key);
// check type validity
if (bType == 0x01 || bType > 0x12 || (bType >= 0x05 && bType <= 0x07) ||
bType == 0x09 || (bType >= 0x0B && bType <= 0x0F))
return false;
// chack idName
if (generateID == false && n.depth == 0 && idName7 == name7)
return false;
}
}
return true;
}
function checkValues(TreeFlat.KeyIndexMap qki, TreeFlat.TreeRoot td, byte[] d, byte[] q, byte op) private constant returns (bool) {
bool r;
uint32 di;
(r, di) = td.selectKey(qki.key);
if (r != true) {
return false;
}
uint32 qi = qki.value;
byte t;
(t,) = DocumentParser.getTypeName8(qki.key);
return compareValues(t, di, d, qi, q, op);
}
function compareValues(byte t, uint32 di, byte[] d, uint32 qi, byte[] q, byte op) private constant returns (bool) {
if (t == 0x02) { // UTF-8 string
return compareStrings(di, d, qi, q, op);
} else if (t == 0x07) { // ObjectId
return compareObjectID(di, d, qi, q, op);
} else if (t == 0x08) { // Boolean
if (op == eq) { // dummy check, for now we use only equality
return d[di] == q[qi];
}
} else if (t == 0x0A) { // Null value
return true;
} else if (t == 0x10) { // 32-bit integer
return compareint32(di, d, qi, q, op);
} else if (t == 0x11) { // Timestamp
return compareuint64(di, d, qi, q, op);
} else if (t == 0x12) { // 64-bit integer
return compareint64(di, d, qi, q, op);
}
return false;
}
function compareStrings(uint32 di, byte[] d, uint32 qi, byte[] q, byte op) constant internal returns (bool) {
string memory ds = d.getString(di);
string memory qs = q.getString(qi);
if (op == eq) { // dummy check, for now we use only equality
return sha3(ds) == sha3(qs);
}
return false;
}
function compareObjectID(uint32 di, byte[] d, uint32 qi, byte[] q, byte op) constant internal returns (bool) {
bytes12 doid = d.getObjectID(di);
bytes12 qoid = q.getObjectID(qi);
if (op == eq) { // dummy check, for now we use only equality
return doid == qoid;
}
return false;
}
function compareint32(uint32 di, byte[] d, uint32 qi, byte[] q, byte op) constant internal returns (bool) {
int32 dint32 = int32(d.getLittleUint32(di));
int32 qint32 = int32(q.getLittleUint32(qi));
if (op == eq) { // dummy check, for now we use only equality
return dint32 == qint32;
}
return false;
}
function compareint64(uint32 di, byte[] d, uint32 qi, byte[] q, byte op) constant internal returns (bool) {
int64 dint64 = int64(d.getLittleUint64(di));
int64 qint64 = int64(q.getLittleUint64(qi));
if (op == eq) { // dummy check, for now we use only equality
return dint64 == qint64;
}
return false;
}
function compareuint64(uint32 di, byte[] d, uint32 qi, byte[] q, byte op) constant internal returns (bool) {
uint64 duint64 = d.getLittleUint64(di);
uint64 quint64 = q.getLittleUint64(qi);
if (op == eq) { // dummy check, for now we use only equality
return duint64 == quint64;
}
return false;
}
}