forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery-planner.ts
384 lines (336 loc) · 11.7 KB
/
query-planner.ts
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
import { countUntilNotMatching } from './plugins/utils/index.ts';
import { newRxError } from './rx-error.ts';
import { getSchemaByObjectPath } from './rx-schema-helper.ts';
import type {
FilledMangoQuery,
MangoQuerySelector,
RxDocumentData,
RxJsonSchema,
RxQueryPlan,
RxQueryPlanKey,
RxQueryPlanerOpts
} from './types/index.d.ts';
export const INDEX_MAX = String.fromCharCode(65535);
/**
* Do not use -Infinity here because it would be
* transformed to null on JSON.stringify() which can break things
* when the query plan is send to the storage as json.
* @link https://stackoverflow.com/a/16644751
* Notice that for IndexedDB IDBKeyRange we have
* to transform the value back to -Infinity
* before we can use it in IDBKeyRange.bound.
*/
export const INDEX_MIN = Number.MIN_SAFE_INTEGER;
/**
* Returns the query plan which contains
* information about how to run the query
* and which indexes to use.
*
* This is used in some storage like Memory, dexie.js and IndexedDB.
*/
export function getQueryPlan<RxDocType>(
schema: RxJsonSchema<RxDocumentData<RxDocType>>,
query: FilledMangoQuery<RxDocType>
): RxQueryPlan {
const selector = query.selector;
let indexes: string[][] = schema.indexes ? schema.indexes.slice(0) as any : [];
if (query.index) {
indexes = [query.index];
}
/**
* Most storages do not support descending indexes
* so having a 'desc' in the sorting, means we always have to re-sort the results.
*/
const hasDescSorting = !!query.sort.find(sortField => Object.values(sortField)[0] === 'desc');
/**
* Some fields can be part of the selector while not being relevant for sorting
* because their selector operators specify that in all cases all matching docs
* would have the same value.
* For example the boolean field _deleted.
* TODO similar thing could be done for enums.
*/
const sortIrrelevevantFields = new Set();
Object.keys(selector).forEach(fieldName => {
const schemaPart = getSchemaByObjectPath(schema, fieldName);
if (
schemaPart &&
schemaPart.type === 'boolean' &&
Object.prototype.hasOwnProperty.call((selector as any)[fieldName], '$eq')
) {
sortIrrelevevantFields.add(fieldName);
}
});
const optimalSortIndex = query.sort.map(sortField => Object.keys(sortField)[0]);
const optimalSortIndexCompareString = optimalSortIndex
.filter(f => !sortIrrelevevantFields.has(f))
.join(',');
let currentBestQuality = -1;
let currentBestQueryPlan: RxQueryPlan | undefined;
/**
* Calculate one query plan for each index
* and then test which of the plans is best.
*/
indexes.forEach((index) => {
let inclusiveEnd = true;
let inclusiveStart = true;
const opts: RxQueryPlanerOpts[] = index.map(indexField => {
const matcher = (selector as any)[indexField];
const operators = matcher ? Object.keys(matcher) : [];
let matcherOpts: RxQueryPlanerOpts = {} as any;
if (
!matcher ||
!operators.length
) {
const startKey = inclusiveStart ? INDEX_MIN : INDEX_MAX;
matcherOpts = {
startKey,
endKey: inclusiveEnd ? INDEX_MAX : INDEX_MIN,
inclusiveStart: true,
inclusiveEnd: true
};
} else {
operators.forEach(operator => {
if (LOGICAL_OPERATORS.has(operator)) {
const operatorValue = matcher[operator];
const partialOpts = getMatcherQueryOpts(operator, operatorValue);
matcherOpts = Object.assign(matcherOpts, partialOpts);
}
});
}
// fill missing attributes
if (typeof matcherOpts.startKey === 'undefined') {
matcherOpts.startKey = INDEX_MIN;
}
if (typeof matcherOpts.endKey === 'undefined') {
matcherOpts.endKey = INDEX_MAX;
}
if (typeof matcherOpts.inclusiveStart === 'undefined') {
matcherOpts.inclusiveStart = true;
}
if (typeof matcherOpts.inclusiveEnd === 'undefined') {
matcherOpts.inclusiveEnd = true;
}
if (inclusiveStart && !matcherOpts.inclusiveStart) {
inclusiveStart = false;
}
if (inclusiveEnd && !matcherOpts.inclusiveEnd) {
inclusiveEnd = false;
}
return matcherOpts;
});
const startKeys = opts.map(opt => opt.startKey);
const endKeys = opts.map(opt => opt.endKey);
const queryPlan: RxQueryPlan = {
index,
startKeys,
endKeys,
inclusiveEnd,
inclusiveStart,
sortSatisfiedByIndex: !hasDescSorting && optimalSortIndexCompareString === index.filter(f => !sortIrrelevevantFields.has(f)).join(','),
selectorSatisfiedByIndex: isSelectorSatisfiedByIndex(index, query.selector, startKeys, endKeys)
};
const quality = rateQueryPlan(
schema,
query,
queryPlan
);
if (
(
quality >= currentBestQuality
) ||
query.index
) {
currentBestQuality = quality;
currentBestQueryPlan = queryPlan;
}
});
/**
* In all cases and index must be found
*/
if (!currentBestQueryPlan) {
throw newRxError('SNH', {
query
});
}
return currentBestQueryPlan;
}
export const LOGICAL_OPERATORS = new Set(['$eq', '$gt', '$gte', '$lt', '$lte']);
export const LOWER_BOUND_LOGICAL_OPERATORS = new Set(['$eq', '$gt', '$gte']);
export const UPPER_BOUND_LOGICAL_OPERATORS = new Set(['$eq', '$lt', '$lte']);
export function isSelectorSatisfiedByIndex(
index: string[],
selector: MangoQuerySelector<any>,
startKeys: RxQueryPlanKey[],
endKeys: RxQueryPlanKey[]
): boolean {
/**
* Not satisfied if one or more operators are non-logical
* operators that can never be satisfied by an index.
*/
const selectorEntries = Object.entries(selector);
const hasNonMatchingOperator = selectorEntries
.find(([fieldName, operation]) => {
if (!index.includes(fieldName)) {
return true;
}
const hasNonLogicOperator = Object.entries(operation as any)
.find(([op, _value]) => !LOGICAL_OPERATORS.has(op));
return hasNonLogicOperator;
});
if (hasNonMatchingOperator) {
return false;
}
/**
* Not satisfied if contains $and or $or operations.
*/
if (selector.$and || selector.$or) {
return false;
}
// ensure all lower bound in index
const satisfieldLowerBound: string[] = [];
const lowerOperatorFieldNames = new Set<string>();
for (const [fieldName, operation] of Object.entries(selector)) {
if (!index.includes(fieldName)) {
return false;
}
// If more then one logic op on the same field, we have to selector-match.
const lowerLogicOps = Object.keys(operation as any).filter(key => LOWER_BOUND_LOGICAL_OPERATORS.has(key));
if (lowerLogicOps.length > 1) {
return false;
}
const hasLowerLogicOp = lowerLogicOps[0];
if (hasLowerLogicOp) {
lowerOperatorFieldNames.add(fieldName);
}
if (hasLowerLogicOp !== '$eq') {
if (satisfieldLowerBound.length > 0) {
return false;
} else {
satisfieldLowerBound.push(hasLowerLogicOp);
}
}
}
// ensure all upper bound in index
const satisfieldUpperBound: string[] = [];
const upperOperatorFieldNames = new Set<string>();
for (const [fieldName, operation] of Object.entries(selector)) {
if (!index.includes(fieldName)) {
return false;
}
// If more then one logic op on the same field, we have to selector-match.
const upperLogicOps = Object.keys(operation as any).filter(key => UPPER_BOUND_LOGICAL_OPERATORS.has(key));
if (upperLogicOps.length > 1) {
return false;
}
const hasUperLogicOp = upperLogicOps[0];
if (hasUperLogicOp) {
upperOperatorFieldNames.add(fieldName);
}
if (hasUperLogicOp !== '$eq') {
if (satisfieldUpperBound.length > 0) {
return false;
} else {
satisfieldUpperBound.push(hasUperLogicOp);
}
}
}
/**
* If the index contains a non-relevant field between
* the relevant fields, then the index is not satisfying.
*/
let i = 0;
for (const fieldName of index) {
for (const set of [
lowerOperatorFieldNames,
upperOperatorFieldNames
]) {
if (
!set.has(fieldName) &&
set.size > 0
) {
return false;
}
set.delete(fieldName);
}
const startKey = startKeys[i];
const endKey = endKeys[i];
if (
startKey !== endKey && (
lowerOperatorFieldNames.size > 0 &&
upperOperatorFieldNames.size > 0
)
) {
return false;
}
i++;
}
return true;
}
export function getMatcherQueryOpts(
operator: string,
operatorValue: any
): Partial<RxQueryPlanerOpts> {
switch (operator) {
case '$eq':
return {
startKey: operatorValue,
endKey: operatorValue,
inclusiveEnd: true,
inclusiveStart: true
};
case '$lte':
return {
endKey: operatorValue,
inclusiveEnd: true
};
case '$gte':
return {
startKey: operatorValue,
inclusiveStart: true
};
case '$lt':
return {
endKey: operatorValue,
inclusiveEnd: false
};
case '$gt':
return {
startKey: operatorValue,
inclusiveStart: false
};
default:
throw new Error('SNH');
}
}
/**
* Returns a number that determines the quality of the query plan.
* Higher number means better query plan.
*/
export function rateQueryPlan<RxDocType>(
schema: RxJsonSchema<RxDocumentData<RxDocType>>,
query: FilledMangoQuery<RxDocType>,
queryPlan: RxQueryPlan
): number {
let quality: number = 0;
const addQuality = (value: number) => {
if (value > 0) {
quality = quality + value;
}
};
const pointsPerMatchingKey = 10;
const nonMinKeyCount = countUntilNotMatching(queryPlan.startKeys, keyValue => keyValue !== INDEX_MIN && keyValue !== INDEX_MAX);
addQuality(nonMinKeyCount * pointsPerMatchingKey);
const nonMaxKeyCount = countUntilNotMatching(queryPlan.startKeys, keyValue => keyValue !== INDEX_MAX && keyValue !== INDEX_MIN);
addQuality(nonMaxKeyCount * pointsPerMatchingKey);
const equalKeyCount = countUntilNotMatching(queryPlan.startKeys, (keyValue, idx) => {
if (keyValue === queryPlan.endKeys[idx]) {
return true;
} else {
return false;
}
});
addQuality(equalKeyCount * pointsPerMatchingKey * 1.5);
const pointsIfNoReSortMustBeDone = queryPlan.sortSatisfiedByIndex ? 5 : 0;
addQuality(pointsIfNoReSortMustBeDone);
return quality;
}