Repository navigation
Expand file tree
/
Copy pathresult-window-buffer.test.ts
More file actions
589 lines (564 loc) · 32.2 KB
/
Copy pathresult-window-buffer.test.ts
File metadata and controls
589 lines (564 loc) · 32.2 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import * as assert from 'assert';
import {
applyChangeEventsToResultWindowBuffer,
applyChangeEventToResultWindowBuffer,
calculateActionName,
createResultWindowBuffer,
getRecommendedResultWindowBufferSize,
getResultWindowBufferQuery,
getResultWindowBufferResults,
resetResultWindowBuffer,
runAction
} from '../../src/index.js';
import type { ResultWindowBuffer, ResultWindowBufferResult, ResultWindowBufferSizes } from '../../src/index.js';
import type { ChangeEvent, DocumentId, QueryParams } from '../../src/types/index.js';
import { seededRandom } from '../helper/test-suite.js';
/**
* Randomized test of the result window buffer against a full query.
* After every event, the visible result must equal the full query,
* and the buffer must be an exact slice of the full result.
*/
type Doc = {
_id: string;
gender: 'm' | 'f';
age: number;
};
type TestQuery = {
sortDirection: 'asc' | 'desc';
skip?: number;
limit?: number;
};
/**
* The query is always: gender === 'f', sorted by age, then by _id.
* Age has only 100 different values, so many rows have the same age
* and the _id decides their order.
*/
function getQueryParams(query: TestQuery): QueryParams<Doc> {
const direction = query.sortDirection === 'asc' ? 1 : -1;
return {
primaryKey: '_id',
sortFields: ['age', '_id'],
skip: query.skip,
limit: query.limit,
queryMatcher: (doc: Doc) => doc.gender === 'f',
sortComparator: (a: Doc, b: Doc) => {
if (a.age !== b.age) {
return (a.age < b.age ? -direction : direction) as 1 | -1;
}
return (a._id < b._id ? -direction : direction) as 1 | -1;
}
};
}
/**
* Full query over all documents: filter, sort, skip, limit.
*/
function runQuery(docs: Map<DocumentId, Doc>, queryParams: QueryParams<Doc>, skip: number, limit: number | undefined): Doc[] {
return Array.from(docs.values())
.filter(queryParams.queryMatcher)
.sort(queryParams.sortComparator)
.slice(skip, limit === undefined ? undefined : skip + limit);
}
function fillResultWindowBuffer(resultWindowBuffer: ResultWindowBuffer<Doc>, docs: Map<DocumentId, Doc>) {
const { skip, limit } = getResultWindowBufferQuery(resultWindowBuffer, {});
resetResultWindowBuffer(resultWindowBuffer, runQuery(docs, resultWindowBuffer.queryParams, skip, limit));
}
function randomChangeEvent(
docs: Map<DocumentId, Doc>,
random: () => number,
newId: () => string,
weights: { insert: number; update: number; delete: number; }
): ChangeEvent<Doc> {
const int = (min: number, max: number) => min + Math.floor(random() * (max - min + 1));
const total = weights.insert + weights.update + weights.delete;
let r = random() * total;
if (docs.size === 0 || r < weights.insert) {
const doc: Doc = { _id: newId(), gender: random() < 0.5 ? 'f' : 'm', age: int(1, 100) };
return { operation: 'INSERT', id: doc._id, doc, previous: null };
}
r = r - weights.insert;
const allDocs = Array.from(docs.values());
const previous = allDocs[int(0, allDocs.length - 1)];
if (r < weights.update) {
const doc: Doc = { ...previous };
if (random() < 0.5) {
// starts or stops matching
doc.gender = doc.gender === 'f' ? 'm' : 'f';
} else {
// moves in the sort order
doc.age = int(1, 100);
}
return { operation: 'UPDATE', id: doc._id, doc, previous };
}
return { operation: 'DELETE', id: previous._id, doc: null, previous };
}
const ids = (docs: Doc[]) => docs.map(d => d._id).join(',');
const doc = (_id: string, age: number): Doc => ({ _id, gender: 'f', age });
const QUERIES: TestQuery[] = [
{ sortDirection: 'asc', limit: 20 },
{ sortDirection: 'desc', limit: 20 },
{ sortDirection: 'asc', skip: 10, limit: 20 },
{ sortDirection: 'desc', skip: 10, limit: 20 },
{ sortDirection: 'asc', limit: 1 },
{ sortDirection: 'desc', skip: 3, limit: 5 },
// skip without limit
{ sortDirection: 'asc', skip: 10 },
{ sortDirection: 'desc', skip: 2 },
// neither skip nor limit
{ sortDirection: 'asc' }
];
const BUFFER_SIZES: ResultWindowBufferSizes[] = [
{ limitBufferSize: 0, skipBufferSize: 0 },
{ limitBufferSize: 5, skipBufferSize: 0 },
{ limitBufferSize: 0, skipBufferSize: 5 },
{ limitBufferSize: 20, skipBufferSize: 3 },
{ limitBufferSize: 50, skipBufferSize: 20 },
{ limitBufferSize: Infinity, skipBufferSize: Infinity }
];
const INITIAL_ROWS = [0, 30, 300];
const EVENTS_PER_RUN = 600;
/**
* First more inserts, then mostly updates, then more deletes,
* so that the result grows, moves around and shrinks to (almost) nothing.
*/
function getWeights(eventIndex: number) {
const phase = eventIndex / EVENTS_PER_RUN;
if (phase < 0.4) {
return { insert: 3, update: 5, delete: 1 };
} else if (phase < 0.7) {
return { insert: 1, update: 8, delete: 1 };
} else {
return { insert: 1, update: 3, delete: 5 };
}
}
describe('result-window-buffer.test.ts', () => {
describe('getRecommendedResultWindowBufferSize()', () => {
it('should return the recommended size for each limit', () => {
const expected: [number | undefined, number][] = [
[undefined, 0], [0, 0], [1, 0], [2, 1], [3, 1], [4, 2], [10, 3], [20, 4],
[100, 7], [200, 11], [500, 20], [1000, 26], [2711, 29], [2712, 30], [100000, 30]
];
expected.forEach(([limit, size]) => {
assert.strictEqual(getRecommendedResultWindowBufferSize(limit), size, 'limit ' + limit);
});
});
});
describe('getResultWindowBufferQuery()', () => {
it('should add the buffer sizes to the skip and limit', () => {
const resultWindowBuffer = createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 10, limit: 20 }),
{ limitBufferSize: 5, skipBufferSize: 3 }
);
assert.deepStrictEqual(getResultWindowBufferQuery(resultWindowBuffer, {}), { skip: 7, limit: 28 });
});
it('should not go below skip 0', () => {
const resultWindowBuffer = createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 2, limit: 20 }),
{ skipBufferSize: 5 }
);
assert.deepStrictEqual(getResultWindowBufferQuery(resultWindowBuffer, {}), { skip: 0, limit: 22 });
});
it('should use whole numbers as buffer sizes', () => {
const query = (size: number) => getResultWindowBufferQuery(createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 10, limit: 20 }),
{ limitBufferSize: size, skipBufferSize: size }
), {});
assert.deepStrictEqual(query(2.5), { skip: 8, limit: 24 });
assert.deepStrictEqual(query(0), { skip: 10, limit: 20 });
// Infinity keeps all rows
assert.deepStrictEqual(query(Infinity), { skip: 0 });
});
it('should throw on negative buffer sizes and values that are not numbers', () => {
const queryParams = getQueryParams({ sortDirection: 'asc', skip: 10, limit: 20 });
[-1, -0.5, -Infinity, NaN, '5', null, {}, true].forEach(size => {
assert.throws(() => createResultWindowBuffer(queryParams, { limitBufferSize: size as any }), /RWB_SIZE/, String(size));
assert.throws(() => createResultWindowBuffer(queryParams, { skipBufferSize: size as any }), /RWB_SIZE/, String(size));
});
// not set means 0
assert.deepStrictEqual(getResultWindowBufferQuery(createResultWindowBuffer(queryParams, {}), {}), { skip: 10, limit: 20 });
});
it('should have no limit for a query without a limit', () => {
const resultWindowBuffer = createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 10 }),
{ limitBufferSize: 5, skipBufferSize: 3 }
);
assert.deepStrictEqual(getResultWindowBufferQuery(resultWindowBuffer, {}), { skip: 7 });
});
it('should keep the other fields of the query and not change it', () => {
const resultWindowBuffer = createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 10, limit: 20 }),
{ limitBufferSize: 5, skipBufferSize: 3 }
);
const query = { selector: { gender: 'm' }, sort: [{ age: 'asc' }], skip: 10, limit: 20 };
assert.deepStrictEqual(
getResultWindowBufferQuery(resultWindowBuffer, query),
{ selector: { gender: 'm' }, sort: [{ age: 'asc' }], skip: 7, limit: 28 }
);
assert.deepStrictEqual(query, { selector: { gender: 'm' }, sort: [{ age: 'asc' }], skip: 10, limit: 20 });
});
it('should remove the limit of the query when all rows after the skip are needed', () => {
const resultWindowBuffer = createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 10, limit: 20 }),
{ limitBufferSize: Infinity }
);
assert.deepStrictEqual(getResultWindowBufferQuery(resultWindowBuffer, { skip: 10, limit: 20 }), { skip: 10 });
});
});
describe('resetResultWindowBuffer()', () => {
it('should ask for a full query before it got rows', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), {});
const result = applyChangeEventToResultWindowBuffer<Doc>(resultWindowBuffer, {
operation: 'INSERT', id: 'a', doc: doc('a', 1), previous: null
});
assert.strictEqual(result, 'runFullQueryAgain');
});
it('should throw when the results are read before the buffer got rows', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), {});
assert.throws(() => getResultWindowBufferResults(resultWindowBuffer), /RWB_OUTDATED/);
});
it('should be complete when the query returned fewer rows than requested', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), { limitBufferSize: 2 });
const rows = [doc('a', 1), doc('b', 2), doc('c', 3)];
resetResultWindowBuffer(resultWindowBuffer, rows);
assert.strictEqual(resultWindowBuffer.complete, true);
assert.deepStrictEqual(getResultWindowBufferResults(resultWindowBuffer), rows.slice(0, 2));
});
it('should not be complete when the query returned as many rows as requested', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 1 }), { limitBufferSize: 1 });
resetResultWindowBuffer(resultWindowBuffer, [doc('a', 1), doc('b', 2)]);
assert.strictEqual(resultWindowBuffer.complete, false);
});
it('should show the rows after the skipped rows of the buffer', () => {
const resultWindowBuffer = createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 1, limit: 2 }),
{ skipBufferSize: 1 }
);
resetResultWindowBuffer(resultWindowBuffer, [doc('a', 1), doc('b', 2), doc('c', 3)]);
assert.strictEqual(resultWindowBuffer.offset, 0);
assert.strictEqual(ids(getResultWindowBufferResults(resultWindowBuffer)), 'b,c');
});
});
describe('applyChangeEventToResultWindowBuffer()', () => {
it('should refill the window from the buffer when a visible row is deleted', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), { limitBufferSize: 2 });
const rows = [doc('a', 1), doc('b', 2), doc('c', 3), doc('d', 4)];
resetResultWindowBuffer(resultWindowBuffer, rows);
const result = applyChangeEventToResultWindowBuffer<Doc>(resultWindowBuffer, {
operation: 'DELETE', id: 'a', doc: null, previous: rows[0]
});
assert.strictEqual(result, 'buffer');
assert.strictEqual(ids(getResultWindowBufferResults(resultWindowBuffer)), 'b,c');
assert.strictEqual(ids(resultWindowBuffer.buffer), 'b,c,d');
});
it('should not append a row that moves behind the last row of the buffer', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), { limitBufferSize: 1 });
const rows = [doc('a', 1), doc('b', 2), doc('c', 3)];
resetResultWindowBuffer(resultWindowBuffer, rows);
const result = applyChangeEventToResultWindowBuffer<Doc>(resultWindowBuffer, {
operation: 'UPDATE', id: 'a', doc: doc('a', 99), previous: rows[0]
});
assert.strictEqual(result, 'buffer');
assert.strictEqual(ids(resultWindowBuffer.buffer), 'b,c');
});
it('should ask for a full query when a row is added in front of the skip window without a skip buffer', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', skip: 1, limit: 1 }), { limitBufferSize: 1 });
resetResultWindowBuffer(resultWindowBuffer, [doc('b', 2), doc('c', 3)]);
const result = applyChangeEventToResultWindowBuffer<Doc>(resultWindowBuffer, {
operation: 'INSERT', id: 'x', doc: doc('x', 0), previous: null
});
assert.strictEqual(result, 'runFullQueryAgain');
});
it('should throw when the results are read after runFullQueryAgain without running the query again', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', skip: 1, limit: 1 }), { limitBufferSize: 1 });
resetResultWindowBuffer(resultWindowBuffer, [doc('b', 2), doc('c', 3)]);
const result = applyChangeEventToResultWindowBuffer<Doc>(resultWindowBuffer, {
operation: 'INSERT', id: 'x', doc: doc('x', 0), previous: null
});
assert.strictEqual(result, 'runFullQueryAgain');
assert.throws(() => getResultWindowBufferResults(resultWindowBuffer), /RWB_OUTDATED/);
});
it('should take the row from the skip buffer when a row is added in front of the window', () => {
const resultWindowBuffer = createResultWindowBuffer(
getQueryParams({ sortDirection: 'asc', skip: 2, limit: 1 }),
{ skipBufferSize: 1 }
);
// skip 1, limit 2: 'a' is skipped and not in the buffer
resetResultWindowBuffer(resultWindowBuffer, [doc('b', 2), doc('c', 3)]);
assert.strictEqual(ids(getResultWindowBufferResults(resultWindowBuffer)), 'c');
const result = applyChangeEventToResultWindowBuffer<Doc>(resultWindowBuffer, {
operation: 'INSERT', id: 'x', doc: doc('x', 0), previous: null
});
assert.strictEqual(result, 'buffer');
assert.strictEqual(resultWindowBuffer.offset, 2);
assert.strictEqual(ids(getResultWindowBufferResults(resultWindowBuffer)), 'b');
});
it('should return no rows for a query with limit 0', () => {
const random = seededRandom(77);
let idCounter = 0;
const newId = () => 'id-' + (idCounter++);
for (const skip of [0, 2]) {
const docs = new Map<DocumentId, Doc>();
for (let i = 0; i < 10; i++) {
const changeEvent = randomChangeEvent(docs, random, newId, { insert: 1, update: 0, delete: 0 });
docs.set(changeEvent.id, changeEvent.doc as Doc);
}
const queryParams = getQueryParams({ sortDirection: 'asc', skip, limit: 0 });
const resultWindowBuffer = createResultWindowBuffer(queryParams, { limitBufferSize: 2, skipBufferSize: 2 });
fillResultWindowBuffer(resultWindowBuffer, docs);
// no rows are kept, so the buffer query asks for none
assert.deepStrictEqual(getResultWindowBufferQuery(resultWindowBuffer, {}), { skip, limit: 0 });
assert.deepStrictEqual(getResultWindowBufferResults(resultWindowBuffer), []);
for (let i = 0; i < 100; i++) {
const changeEvent = randomChangeEvent(docs, random, newId, getWeights(i));
if (changeEvent.operation === 'DELETE') {
docs.delete(changeEvent.id);
} else {
docs.set(changeEvent.id, changeEvent.doc);
}
assert.strictEqual(applyChangeEventToResultWindowBuffer(resultWindowBuffer, changeEvent), 'eventReduce');
assert.deepStrictEqual(getResultWindowBufferResults(resultWindowBuffer), []);
assert.strictEqual(resultWindowBuffer.buffer.length, 0);
}
}
});
it('should handle a query with a skip but without a limit', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', skip: 1 }), { skipBufferSize: 1 });
const rows = [doc('a', 1), doc('b', 2), doc('c', 3)];
resetResultWindowBuffer(resultWindowBuffer, rows);
assert.strictEqual(ids(getResultWindowBufferResults(resultWindowBuffer)), 'b,c');
const result = applyChangeEventToResultWindowBuffer<Doc>(resultWindowBuffer, {
operation: 'INSERT', id: 'x', doc: doc('x', 0), previous: null
});
assert.notStrictEqual(result, 'runFullQueryAgain');
assert.strictEqual(ids(getResultWindowBufferResults(resultWindowBuffer)), 'a,b,c');
});
});
describe('applyChangeEventsToResultWindowBuffer()', () => {
it('should return the new results and if they changed', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), { limitBufferSize: 3 });
resetResultWindowBuffer(resultWindowBuffer, [doc('a', 1), doc('b', 2), doc('c', 3), doc('d', 4), doc('e', 5)]);
// a row after the result window changes
const notVisible = applyChangeEventsToResultWindowBuffer<Doc>(resultWindowBuffer, [
{ operation: 'UPDATE', id: 'e', doc: doc('e', 6), previous: doc('e', 5) }
]);
assert.ok(!notVisible.runFullQueryAgain && !notVisible.changed);
const visible = applyChangeEventsToResultWindowBuffer<Doc>(resultWindowBuffer, [
{ operation: 'DELETE', id: 'a', doc: null, previous: doc('a', 1) },
{ operation: 'DELETE', id: 'b', doc: null, previous: doc('b', 2) }
]);
assert.ok(!visible.runFullQueryAgain && visible.changed);
assert.strictEqual(ids(visible.runFullQueryAgain ? [] : visible.results), 'c,d');
});
it('should report a changed row at the same position as changed', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), {});
resetResultWindowBuffer(resultWindowBuffer, [doc('a', 1), doc('b', 3)]);
const result = applyChangeEventsToResultWindowBuffer<Doc>(resultWindowBuffer, [
{ operation: 'UPDATE', id: 'a', doc: doc('a', 2), previous: doc('a', 1) }
]);
assert.ok(!result.runFullQueryAgain && result.changed);
});
it('should ask for a full query when the result window does not fit into the buffer', () => {
const resultWindowBuffer = createResultWindowBuffer(getQueryParams({ sortDirection: 'asc', limit: 2 }), {});
const empty = applyChangeEventsToResultWindowBuffer<Doc>(resultWindowBuffer, []);
assert.strictEqual(empty.runFullQueryAgain, true);
resetResultWindowBuffer(resultWindowBuffer, [doc('a', 1), doc('b', 2)]);
const result = applyChangeEventsToResultWindowBuffer<Doc>(resultWindowBuffer, [
{ operation: 'DELETE', id: 'a', doc: null, previous: doc('a', 1) }
]);
assert.strictEqual(result.runFullQueryAgain, true);
});
it('should return the same as a full query for random batches of events', () => {
const random = seededRandom(4242);
let idCounter = 0;
const newId = () => 'id-' + (idCounter++);
for (const query of QUERIES) {
const docs = new Map<DocumentId, Doc>();
for (let i = 0; i < 30; i++) {
const changeEvent = randomChangeEvent(docs, random, newId, { insert: 1, update: 0, delete: 0 });
docs.set(changeEvent.id, changeEvent.doc as Doc);
}
const queryParams = getQueryParams(query);
const skip = query.skip ? query.skip : 0;
const resultWindowBuffer = createResultWindowBuffer(queryParams, { limitBufferSize: 3, skipBufferSize: 3 });
fillResultWindowBuffer(resultWindowBuffer, docs);
let previousResults = runQuery(docs, queryParams, skip, query.limit);
for (let batch = 0; batch < 100; batch++) {
const changeEvents: ChangeEvent<Doc>[] = [];
const batchSize = 1 + Math.floor(random() * 5);
for (let i = 0; i < batchSize; i++) {
const changeEvent = randomChangeEvent(docs, random, newId, getWeights(batch * 5 + i));
changeEvents.push(changeEvent);
if (changeEvent.operation === 'DELETE') {
docs.delete(changeEvent.id);
} else {
docs.set(changeEvent.id, changeEvent.doc);
}
}
const result = applyChangeEventsToResultWindowBuffer(resultWindowBuffer, changeEvents);
const expected = runQuery(docs, queryParams, skip, query.limit);
if (result.runFullQueryAgain) {
fillResultWindowBuffer(resultWindowBuffer, docs);
} else {
assert.strictEqual(ids(result.results), ids(expected));
const expectedChanged = expected.length !== previousResults.length ||
expected.some((d, i) => d !== previousResults[i]);
assert.strictEqual(result.changed, expectedChanged);
}
previousResults = expected;
}
}
});
});
describe('a buffer with both sizes 0', () => {
it('should never run the full query when EventReduce alone does not', () => {
let eventReduceFullQueries = 0;
let bufferFullQueries = 0;
let seed = 1000;
for (const query of QUERIES) {
for (const initialRows of INITIAL_ROWS) {
seed++;
const random = seededRandom(seed);
let idCounter = 0;
const newId = () => 'id-' + (idCounter++);
const docs = new Map<DocumentId, Doc>();
for (let i = 0; i < initialRows; i++) {
const changeEvent = randomChangeEvent(docs, random, newId, { insert: 1, update: 0, delete: 0 });
docs.set(changeEvent.id, changeEvent.doc as Doc);
}
const queryParams = getQueryParams(query);
const skip = query.skip ? query.skip : 0;
let previousResults = runQuery(docs, queryParams, skip, query.limit);
let keyDocumentMap = new Map(previousResults.map(d => [d._id, d]));
const resultWindowBuffer = createResultWindowBuffer(queryParams, { limitBufferSize: 0, skipBufferSize: 0 });
fillResultWindowBuffer(resultWindowBuffer, docs);
for (let i = 0; i < EVENTS_PER_RUN; i++) {
const changeEvent = randomChangeEvent(docs, random, newId, getWeights(i));
if (changeEvent.operation === 'DELETE') {
docs.delete(changeEvent.id);
} else {
docs.set(changeEvent.id, changeEvent.doc);
}
const actionName = calculateActionName({ queryParams, changeEvent, previousResults, keyDocumentMap });
const eventReduceNeedsQuery = actionName === 'runFullQueryAgain';
if (eventReduceNeedsQuery) {
eventReduceFullQueries++;
previousResults = runQuery(docs, queryParams, skip, query.limit);
keyDocumentMap = new Map(previousResults.map(d => [d._id, d]));
} else {
runAction(actionName, queryParams, changeEvent, previousResults, keyDocumentMap);
}
const bufferNeedsQuery = applyChangeEventToResultWindowBuffer(resultWindowBuffer, changeEvent) === 'runFullQueryAgain';
if (bufferNeedsQuery) {
bufferFullQueries++;
fillResultWindowBuffer(resultWindowBuffer, docs);
}
assert.ok(
!bufferNeedsQuery || eventReduceNeedsQuery,
'buffer needs a full query but EventReduce does not at event #' + i + ' (' + changeEvent.operation + ')'
);
assert.strictEqual(
ids(getResultWindowBufferResults(resultWindowBuffer)),
ids(runQuery(docs, queryParams, skip, query.limit))
);
}
}
}
assert.ok(bufferFullQueries <= eventReduceFullQueries, bufferFullQueries + ' > ' + eventReduceFullQueries);
});
});
describe('randomized against a full query', () => {
const totalCounts: { [k in ResultWindowBufferResult]: number } = {
eventReduce: 0,
buffer: 0,
runFullQueryAgain: 0
};
let seed = 0;
for (const query of QUERIES) {
for (const sizes of BUFFER_SIZES) {
for (const initialRows of INITIAL_ROWS) {
seed++;
const runSeed = seed;
it(
'sort ' + query.sortDirection + ', skip ' + query.skip + ', limit ' + query.limit +
', limitBufferSize ' + sizes.limitBufferSize + ', skipBufferSize ' + sizes.skipBufferSize +
', ' + initialRows + ' initial rows',
() => {
const random = seededRandom(runSeed);
let idCounter = 0;
const newId = () => {
idCounter++;
return Math.floor(random() * 1e9).toString(36) + '-' + idCounter;
};
const docs = new Map<DocumentId, Doc>();
const applyToDocs = (changeEvent: ChangeEvent<Doc>) => {
if (changeEvent.operation === 'DELETE') {
docs.delete(changeEvent.id);
} else {
docs.set(changeEvent.id, changeEvent.doc);
}
};
for (let i = 0; i < initialRows; i++) {
applyToDocs(randomChangeEvent(docs, random, newId, { insert: 1, update: 0, delete: 0 }));
}
const queryParams = getQueryParams(query);
const skip = query.skip ? query.skip : 0;
const resultWindowBuffer = createResultWindowBuffer(queryParams, sizes);
fillResultWindowBuffer(resultWindowBuffer, docs);
for (let i = 0; i < EVENTS_PER_RUN; i++) {
const changeEvent = randomChangeEvent(docs, random, newId, getWeights(i));
applyToDocs(changeEvent);
const result = applyChangeEventToResultWindowBuffer(resultWindowBuffer, changeEvent);
totalCounts[result]++;
if (result === 'runFullQueryAgain') {
fillResultWindowBuffer(resultWindowBuffer, docs);
}
const context = 'event #' + i + ' (' + changeEvent.operation + ', ' + result + ')';
// the visible result equals the full query
assert.strictEqual(
ids(getResultWindowBufferResults(resultWindowBuffer)),
ids(runQuery(docs, queryParams, skip, query.limit)),
'visible result differs from full query at ' + context
);
// the buffer is an exact slice of the full result
const fullResult = runQuery(docs, queryParams, 0, undefined);
if (resultWindowBuffer.buffer.length > 0) {
assert.strictEqual(
ids(resultWindowBuffer.buffer),
ids(fullResult.slice(resultWindowBuffer.offset, resultWindowBuffer.offset + resultWindowBuffer.buffer.length)),
'buffer is not an exact slice of the full result at ' + context
);
} else {
// an empty buffer only knows an upper bound of the rows before it
assert.ok(resultWindowBuffer.complete, 'empty buffer must be complete at ' + context);
assert.ok(fullResult.length <= resultWindowBuffer.offset, 'offset is no upper bound at ' + context);
}
if (resultWindowBuffer.complete) {
assert.ok(
resultWindowBuffer.offset + resultWindowBuffer.buffer.length >= fullResult.length,
'complete buffer misses rows at ' + context
);
}
// the buffer keeps no more rows than requested on both sides
assert.ok(skip - resultWindowBuffer.offset <= (sizes.skipBufferSize || 0), 'too many rows before the window');
if (query.limit) {
assert.ok(
resultWindowBuffer.buffer.length <=
Math.max(0, skip - resultWindowBuffer.offset) + query.limit + (sizes.limitBufferSize || 0),
'too many rows after the window'
);
}
// the key map matches the buffer
assert.strictEqual(resultWindowBuffer.keyDocumentMap.size, resultWindowBuffer.buffer.length);
resultWindowBuffer.buffer.forEach(d => assert.strictEqual(resultWindowBuffer.keyDocumentMap.get(d._id), d));
}
}
);
}
}
}
it('has used all three paths', () => {
assert.ok(totalCounts.eventReduce > 0, JSON.stringify(totalCounts));
assert.ok(totalCounts.buffer > 0, JSON.stringify(totalCounts));
assert.ok(totalCounts.runFullQueryAgain > 0, JSON.stringify(totalCounts));
});
});
});