forked from louischatriot/nedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes.test.js
executable file
·773 lines (633 loc) · 27.3 KB
/
indexes.test.js
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
var Index = require('../lib/indexes')
, customUtils = require('../lib/customUtils')
, should = require('chai').should()
, assert = require('chai').assert
, _ = require('underscore')
, async = require('async')
, model = require('../lib/model')
;
describe('Indexes', function () {
describe('Insertion', function () {
it('Can insert pointers to documents in the index correctly when they have the field', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
// The underlying BST now has 3 nodes which contain the docs where it's expected
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('hello'), [{ a: 5, tf: 'hello' }]);
assert.deepEqual(idx.tree.search('world'), [{ a: 8, tf: 'world' }]);
assert.deepEqual(idx.tree.search('bloup'), [{ a: 2, tf: 'bloup' }]);
// The nodes contain pointers to the actual documents
idx.tree.search('world')[0].should.equal(doc2);
idx.tree.search('bloup')[0].a = 42;
doc3.a.should.equal(42);
});
it('Inserting twice for the same fieldName in a unique index will result in an error thrown', function () {
var idx = new Index({ fieldName: 'tf', unique: true })
, doc1 = { a: 5, tf: 'hello' }
;
idx.insert(doc1);
idx.tree.getNumberOfKeys().should.equal(1);
(function () { idx.insert(doc1); }).should.throw();
});
it('Inserting twice for a fieldName the docs dont have with a unique index results in an error thrown', function () {
var idx = new Index({ fieldName: 'nope', unique: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 5, tf: 'world' }
;
idx.insert(doc1);
idx.tree.getNumberOfKeys().should.equal(1);
(function () { idx.insert(doc2); }).should.throw();
});
it('Inserting twice for a fieldName the docs dont have with a unique and sparse index will not throw, since the docs will be non indexed', function () {
var idx = new Index({ fieldName: 'nope', unique: true, sparse: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 5, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.tree.getNumberOfKeys().should.equal(0); // Docs are not indexed
});
it('Works with dot notation', function () {
var idx = new Index({ fieldName: 'tf.nested' })
, doc1 = { a: 5, tf: { nested: 'hello' } }
, doc2 = { a: 8, tf: { nested: 'world', additional: true } }
, doc3 = { a: 2, tf: { nested: 'bloup', age: 42 } }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
// The underlying BST now has 3 nodes which contain the docs where it's expected
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('hello'), [doc1]);
assert.deepEqual(idx.tree.search('world'), [doc2]);
assert.deepEqual(idx.tree.search('bloup'), [doc3]);
// The nodes contain pointers to the actual documents
idx.tree.search('bloup')[0].a = 42;
doc3.a.should.equal(42);
});
it('Can insert an array of documents', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
idx.insert([doc1, doc2, doc3]);
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('hello'), [doc1]);
assert.deepEqual(idx.tree.search('world'), [doc2]);
assert.deepEqual(idx.tree.search('bloup'), [doc3]);
});
it('When inserting an array of elements, if an error is thrown all inserts need to be rolled back', function () {
var idx = new Index({ fieldName: 'tf', unique: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc2b = { a: 84, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
try {
idx.insert([doc1, doc2, doc2b, doc3]);
} catch (e) {
e.errorType.should.equal('uniqueViolated');
}
idx.tree.getNumberOfKeys().should.equal(0);
assert.deepEqual(idx.tree.search('hello'), []);
assert.deepEqual(idx.tree.search('world'), []);
assert.deepEqual(idx.tree.search('bloup'), []);
});
describe('Array fields', function () {
it('Inserts one entry per array element in the index', function () {
var obj = { tf: ['aa', 'bb'], really: 'yeah' }
, obj2 = { tf: 'normal', yes: 'indeed' }
, idx = new Index({ fieldName: 'tf' })
;
idx.insert(obj);
idx.getAll().length.should.equal(2);
idx.getAll()[0].should.equal(obj);
idx.getAll()[1].should.equal(obj);
idx.insert(obj2);
idx.getAll().length.should.equal(3);
});
it('Inserts one entry per array element in the index, type-checked', function () {
var obj = { tf: ['42', 42, new Date(42), 42], really: 'yeah' }
, idx = new Index({ fieldName: 'tf' })
;
idx.insert(obj);
idx.getAll().length.should.equal(3);
idx.getAll()[0].should.equal(obj);
idx.getAll()[1].should.equal(obj);
idx.getAll()[2].should.equal(obj);
});
it('Inserts one entry per unique array element in the index, the unique constraint only holds across documents', function () {
var obj = { tf: ['aa', 'aa'], really: 'yeah' }
, obj2 = { tf: ['cc', 'yy', 'cc'], yes: 'indeed' }
, idx = new Index({ fieldName: 'tf', unique: true })
;
idx.insert(obj);
idx.getAll().length.should.equal(1);
idx.getAll()[0].should.equal(obj);
idx.insert(obj2);
idx.getAll().length.should.equal(3);
});
it('The unique constraint holds across documents', function () {
var obj = { tf: ['aa', 'aa'], really: 'yeah' }
, obj2 = { tf: ['cc', 'aa', 'cc'], yes: 'indeed' }
, idx = new Index({ fieldName: 'tf', unique: true })
;
idx.insert(obj);
idx.getAll().length.should.equal(1);
idx.getAll()[0].should.equal(obj);
(function () { idx.insert(obj2); }).should.throw();
});
it('When removing a document, remove it from the index at all unique array elements', function () {
var obj = { tf: ['aa', 'aa'], really: 'yeah' }
, obj2 = { tf: ['cc', 'aa', 'cc'], yes: 'indeed' }
, idx = new Index({ fieldName: 'tf' })
;
idx.insert(obj);
idx.insert(obj2);
idx.getMatching('aa').length.should.equal(2);
idx.getMatching('aa').indexOf(obj).should.not.equal(-1);
idx.getMatching('aa').indexOf(obj2).should.not.equal(-1);
idx.getMatching('cc').length.should.equal(1);
idx.remove(obj2);
idx.getMatching('aa').length.should.equal(1);
idx.getMatching('aa').indexOf(obj).should.not.equal(-1);
idx.getMatching('aa').indexOf(obj2).should.equal(-1);
idx.getMatching('cc').length.should.equal(0);
});
it('If a unique constraint is violated when inserting an array key, roll back all inserts before the key', function () {
var obj = { tf: ['aa', 'bb'], really: 'yeah' }
, obj2 = { tf: ['cc', 'dd', 'aa', 'ee'], yes: 'indeed' }
, idx = new Index({ fieldName: 'tf', unique: true })
;
idx.insert(obj);
idx.getAll().length.should.equal(2);
idx.getMatching('aa').length.should.equal(1);
idx.getMatching('bb').length.should.equal(1);
idx.getMatching('cc').length.should.equal(0);
idx.getMatching('dd').length.should.equal(0);
idx.getMatching('ee').length.should.equal(0);
(function () { idx.insert(obj2); }).should.throw();
idx.getAll().length.should.equal(2);
idx.getMatching('aa').length.should.equal(1);
idx.getMatching('bb').length.should.equal(1);
idx.getMatching('cc').length.should.equal(0);
idx.getMatching('dd').length.should.equal(0);
idx.getMatching('ee').length.should.equal(0);
});
}); // ==== End of 'Array fields' ==== //
}); // ==== End of 'Insertion' ==== //
describe('Removal', function () {
it('Can remove pointers from the index, even when multiple documents have the same key', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc4 = { a: 23, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
idx.tree.getNumberOfKeys().should.equal(3);
idx.remove(doc1);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('hello').length.should.equal(0);
idx.remove(doc2);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('world').length.should.equal(1);
idx.tree.search('world')[0].should.equal(doc4);
});
it('If we have a sparse index, removing a non indexed doc has no effect', function () {
var idx = new Index({ fieldName: 'nope', sparse: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 5, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.tree.getNumberOfKeys().should.equal(0);
idx.remove(doc1);
idx.tree.getNumberOfKeys().should.equal(0);
});
it('Works with dot notation', function () {
var idx = new Index({ fieldName: 'tf.nested' })
, doc1 = { a: 5, tf: { nested: 'hello' } }
, doc2 = { a: 8, tf: { nested: 'world', additional: true } }
, doc3 = { a: 2, tf: { nested: 'bloup', age: 42 } }
, doc4 = { a: 2, tf: { nested: 'world', fruits: ['apple', 'carrot'] } }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
idx.tree.getNumberOfKeys().should.equal(3);
idx.remove(doc1);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('hello').length.should.equal(0);
idx.remove(doc2);
idx.tree.getNumberOfKeys().should.equal(2);
idx.tree.search('world').length.should.equal(1);
idx.tree.search('world')[0].should.equal(doc4);
});
it('Can remove an array of documents', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
idx.insert([doc1, doc2, doc3]);
idx.tree.getNumberOfKeys().should.equal(3);
idx.remove([doc1, doc3]);
idx.tree.getNumberOfKeys().should.equal(1);
assert.deepEqual(idx.tree.search('hello'), []);
assert.deepEqual(idx.tree.search('world'), [doc2]);
assert.deepEqual(idx.tree.search('bloup'), []);
});
}); // ==== End of 'Removal' ==== //
describe('Update', function () {
it('Can update a document whose key did or didnt change', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc4 = { a: 23, tf: 'world' }
, doc5 = { a: 1, tf: 'changed' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('world'), [doc2]);
idx.update(doc2, doc4);
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('world'), [doc4]);
idx.update(doc1, doc5);
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('hello'), []);
assert.deepEqual(idx.tree.search('changed'), [doc5]);
});
it('If a simple update violates a unique constraint, changes are rolled back and an error thrown', function () {
var idx = new Index({ fieldName: 'tf', unique: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, bad = { a: 23, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('hello'), [doc1]);
assert.deepEqual(idx.tree.search('world'), [doc2]);
assert.deepEqual(idx.tree.search('bloup'), [doc3]);
try {
idx.update(doc3, bad);
} catch (e) {
e.errorType.should.equal('uniqueViolated');
}
// No change
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('hello'), [doc1]);
assert.deepEqual(idx.tree.search('world'), [doc2]);
assert.deepEqual(idx.tree.search('bloup'), [doc3]);
});
it('Can update an array of documents', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc1b = { a: 23, tf: 'world' }
, doc2b = { a: 1, tf: 'changed' }
, doc3b = { a: 44, tf: 'bloup' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
idx.update([{ oldDoc: doc1, newDoc: doc1b }, { oldDoc: doc2, newDoc: doc2b }, { oldDoc: doc3, newDoc: doc3b }]);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('world')[0].should.equal(doc1b);
idx.getMatching('changed').length.should.equal(1);
idx.getMatching('changed')[0].should.equal(doc2b);
idx.getMatching('bloup').length.should.equal(1);
idx.getMatching('bloup')[0].should.equal(doc3b);
});
it('If a unique constraint is violated during an array-update, all changes are rolled back and an error thrown', function () {
var idx = new Index({ fieldName: 'tf', unique: true })
, doc0 = { a: 432, tf: 'notthistoo' }
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc1b = { a: 23, tf: 'changed' }
, doc2b = { a: 1, tf: 'changed' } // Will violate the constraint (first try)
, doc2c = { a: 1, tf: 'notthistoo' } // Will violate the constraint (second try)
, doc3b = { a: 44, tf: 'alsochanged' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
try {
idx.update([{ oldDoc: doc1, newDoc: doc1b }, { oldDoc: doc2, newDoc: doc2b }, { oldDoc: doc3, newDoc: doc3b }]);
} catch (e) {
e.errorType.should.equal('uniqueViolated');
}
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('hello')[0].should.equal(doc1);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('world')[0].should.equal(doc2);
idx.getMatching('bloup').length.should.equal(1);
idx.getMatching('bloup')[0].should.equal(doc3);
try {
idx.update([{ oldDoc: doc1, newDoc: doc1b }, { oldDoc: doc2, newDoc: doc2b }, { oldDoc: doc3, newDoc: doc3b }]);
} catch (e) {
e.errorType.should.equal('uniqueViolated');
}
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('hello')[0].should.equal(doc1);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('world')[0].should.equal(doc2);
idx.getMatching('bloup').length.should.equal(1);
idx.getMatching('bloup')[0].should.equal(doc3);
});
it('If an update doesnt change a document, the unique constraint is not violated', function () {
var idx = new Index({ fieldName: 'tf', unique: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, noChange = { a: 8, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('world'), [doc2]);
idx.update(doc2, noChange); // No error thrown
idx.tree.getNumberOfKeys().should.equal(3);
assert.deepEqual(idx.tree.search('world'), [noChange]);
});
it('Can revert simple and batch updates', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc1b = { a: 23, tf: 'world' }
, doc2b = { a: 1, tf: 'changed' }
, doc3b = { a: 44, tf: 'bloup' }
, batchUpdate = [{ oldDoc: doc1, newDoc: doc1b }, { oldDoc: doc2, newDoc: doc2b }, { oldDoc: doc3, newDoc: doc3b }]
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
idx.update(batchUpdate);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('world')[0].should.equal(doc1b);
idx.getMatching('changed').length.should.equal(1);
idx.getMatching('changed')[0].should.equal(doc2b);
idx.getMatching('bloup').length.should.equal(1);
idx.getMatching('bloup')[0].should.equal(doc3b);
idx.revertUpdate(batchUpdate);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('hello')[0].should.equal(doc1);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('world')[0].should.equal(doc2);
idx.getMatching('bloup').length.should.equal(1);
idx.getMatching('bloup')[0].should.equal(doc3);
// Now a simple update
idx.update(doc2, doc2b);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('hello')[0].should.equal(doc1);
idx.getMatching('changed').length.should.equal(1);
idx.getMatching('changed')[0].should.equal(doc2b);
idx.getMatching('bloup').length.should.equal(1);
idx.getMatching('bloup')[0].should.equal(doc3);
idx.revertUpdate(doc2, doc2b);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('hello')[0].should.equal(doc1);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('world')[0].should.equal(doc2);
idx.getMatching('bloup').length.should.equal(1);
idx.getMatching('bloup')[0].should.equal(doc3);
});
}); // ==== End of 'Update' ==== //
describe('Get matching documents', function () {
it('Get all documents where fieldName is equal to the given value, or an empty array if no match', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, doc4 = { a: 23, tf: 'world' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
assert.deepEqual(idx.getMatching('bloup'), [doc3]);
assert.deepEqual(idx.getMatching('world'), [doc2, doc4]);
assert.deepEqual(idx.getMatching('nope'), []);
});
it('Can get all documents for a given key in a unique index', function () {
var idx = new Index({ fieldName: 'tf', unique: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
assert.deepEqual(idx.getMatching('bloup'), [doc3]);
assert.deepEqual(idx.getMatching('world'), [doc2]);
assert.deepEqual(idx.getMatching('nope'), []);
});
it('Can get all documents for which a field is undefined', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 2, nottf: 'bloup' }
, doc3 = { a: 8, tf: 'world' }
, doc4 = { a: 7, nottf: 'yes' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
assert.deepEqual(idx.getMatching('bloup'), []);
assert.deepEqual(idx.getMatching('hello'), [doc1]);
assert.deepEqual(idx.getMatching('world'), [doc3]);
assert.deepEqual(idx.getMatching('yes'), []);
assert.deepEqual(idx.getMatching(undefined), [doc2]);
idx.insert(doc4);
assert.deepEqual(idx.getMatching('bloup'), []);
assert.deepEqual(idx.getMatching('hello'), [doc1]);
assert.deepEqual(idx.getMatching('world'), [doc3]);
assert.deepEqual(idx.getMatching('yes'), []);
assert.deepEqual(idx.getMatching(undefined), [doc2, doc4]);
});
it('Can get all documents for which a field is null', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 2, tf: null }
, doc3 = { a: 8, tf: 'world' }
, doc4 = { a: 7, tf: null }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
assert.deepEqual(idx.getMatching('bloup'), []);
assert.deepEqual(idx.getMatching('hello'), [doc1]);
assert.deepEqual(idx.getMatching('world'), [doc3]);
assert.deepEqual(idx.getMatching('yes'), []);
assert.deepEqual(idx.getMatching(null), [doc2]);
idx.insert(doc4);
assert.deepEqual(idx.getMatching('bloup'), []);
assert.deepEqual(idx.getMatching('hello'), [doc1]);
assert.deepEqual(idx.getMatching('world'), [doc3]);
assert.deepEqual(idx.getMatching('yes'), []);
assert.deepEqual(idx.getMatching(null), [doc2, doc4]);
});
it('Can get all documents for a given key in a sparse index, but not unindexed docs (= field undefined)', function () {
var idx = new Index({ fieldName: 'tf', sparse: true })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 2, nottf: 'bloup' }
, doc3 = { a: 8, tf: 'world' }
, doc4 = { a: 7, nottf: 'yes' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
assert.deepEqual(idx.getMatching('bloup'), []);
assert.deepEqual(idx.getMatching('hello'), [doc1]);
assert.deepEqual(idx.getMatching('world'), [doc3]);
assert.deepEqual(idx.getMatching('yes'), []);
assert.deepEqual(idx.getMatching(undefined), []);
});
it('Can get all documents whose key is in an array of keys', function () {
// For this test only we have to use objects with _ids as the array version of getMatching
// relies on the _id property being set, otherwise we have to use a quadratic algorithm
// or a fingerprinting algorithm, both solutions too complicated and slow given that live nedb
// indexes documents with _id always set
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello', _id: '1' }
, doc2 = { a: 2, tf: 'bloup', _id: '2' }
, doc3 = { a: 8, tf: 'world', _id: '3' }
, doc4 = { a: 7, tf: 'yes', _id: '4' }
, doc5 = { a: 7, tf: 'yes', _id: '5' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
idx.insert(doc5);
assert.deepEqual(idx.getMatching([]), []);
assert.deepEqual(idx.getMatching(['bloup']), [doc2]);
assert.deepEqual(idx.getMatching(['bloup', 'yes']), [doc2, doc4, doc5]);
assert.deepEqual(idx.getMatching(['hello', 'no']), [doc1]);
assert.deepEqual(idx.getMatching(['nope', 'no']), []);
});
it('Can get all documents whose key is between certain bounds', function () {
var idx = new Index({ fieldName: 'a' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 2, tf: 'bloup' }
, doc3 = { a: 8, tf: 'world' }
, doc4 = { a: 7, tf: 'yes' }
, doc5 = { a: 10, tf: 'yes' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.insert(doc4);
idx.insert(doc5);
assert.deepEqual(idx.getBetweenBounds({ $lt: 10, $gte: 5 }), [ doc1, doc4, doc3 ]);
assert.deepEqual(idx.getBetweenBounds({ $lte: 8 }), [ doc2, doc1, doc4, doc3 ]);
assert.deepEqual(idx.getBetweenBounds({ $gt: 7 }), [ doc3, doc5 ]);
});
}); // ==== End of 'Get matching documents' ==== //
describe('Resetting', function () {
it('Can reset an index without any new data, the index will be empty afterwards', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('bloup').length.should.equal(1);
idx.reset();
idx.tree.getNumberOfKeys().should.equal(0);
idx.getMatching('hello').length.should.equal(0);
idx.getMatching('world').length.should.equal(0);
idx.getMatching('bloup').length.should.equal(0);
});
it('Can reset an index and initialize it with one document', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, newDoc = { a: 555, tf: 'new' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('bloup').length.should.equal(1);
idx.reset(newDoc);
idx.tree.getNumberOfKeys().should.equal(1);
idx.getMatching('hello').length.should.equal(0);
idx.getMatching('world').length.should.equal(0);
idx.getMatching('bloup').length.should.equal(0);
idx.getMatching('new')[0].a.should.equal(555);
});
it('Can reset an index and initialize it with an array of documents', function () {
var idx = new Index({ fieldName: 'tf' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
, newDocs = [{ a: 555, tf: 'new' }, { a: 666, tf: 'again' }]
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
idx.tree.getNumberOfKeys().should.equal(3);
idx.getMatching('hello').length.should.equal(1);
idx.getMatching('world').length.should.equal(1);
idx.getMatching('bloup').length.should.equal(1);
idx.reset(newDocs);
idx.tree.getNumberOfKeys().should.equal(2);
idx.getMatching('hello').length.should.equal(0);
idx.getMatching('world').length.should.equal(0);
idx.getMatching('bloup').length.should.equal(0);
idx.getMatching('new')[0].a.should.equal(555);
idx.getMatching('again')[0].a.should.equal(666);
});
}); // ==== End of 'Resetting' ==== //
it('Get all elements in the index', function () {
var idx = new Index({ fieldName: 'a' })
, doc1 = { a: 5, tf: 'hello' }
, doc2 = { a: 8, tf: 'world' }
, doc3 = { a: 2, tf: 'bloup' }
;
idx.insert(doc1);
idx.insert(doc2);
idx.insert(doc3);
assert.deepEqual(idx.getAll(), [{ a: 2, tf: 'bloup' }, { a: 5, tf: 'hello' }, { a: 8, tf: 'world' }]);
});
});