|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { BasicIndex } from '../src/indexes/basic-index.js' |
| 3 | +import { BTreeIndex } from '../src/indexes/btree-index.js' |
| 4 | +import { PropRef } from '../src/query/ir.js' |
| 5 | +import { normalizeValue } from '../src/utils/comparison.js' |
| 6 | +import type { BaseIndex } from '../src/indexes/base-index.js' |
| 7 | + |
| 8 | +type IndexConstructor = new ( |
| 9 | + id: number, |
| 10 | + expression: PropRef, |
| 11 | + name?: string, |
| 12 | + options?: unknown, |
| 13 | +) => BaseIndex<string> & { |
| 14 | + valueMapData: Map<unknown, Set<string>> |
| 15 | +} |
| 16 | + |
| 17 | +const indexTypes: Array<[string, IndexConstructor]> = [ |
| 18 | + [`BasicIndex`, BasicIndex as IndexConstructor], |
| 19 | + [`BTreeIndex`, BTreeIndex as IndexConstructor], |
| 20 | +] |
| 21 | + |
| 22 | +describe.each(indexTypes)(`%s update`, (_indexName, IndexType) => { |
| 23 | + function createIndex(options?: unknown) { |
| 24 | + return new IndexType(1, new PropRef([`value`]), `test_index`, options) |
| 25 | + } |
| 26 | + |
| 27 | + it(`keeps the existing bucket when the indexed value does not change`, () => { |
| 28 | + const index = createIndex() |
| 29 | + index.add(`a`, { value: 1, version: 1 }) |
| 30 | + const bucket = index.valueMapData.get(1) |
| 31 | + const lastUpdated = index.getStats().lastUpdated |
| 32 | + |
| 33 | + index.update(`a`, { value: 1, version: 1 }, { value: 1, version: 2 }) |
| 34 | + |
| 35 | + expect(index.valueMapData.get(1)).toBe(bucket) |
| 36 | + expect(index.getStats().lastUpdated).toBe(lastUpdated) |
| 37 | + expect(index.lookup(`eq`, 1)).toEqual(new Set([`a`])) |
| 38 | + }) |
| 39 | + |
| 40 | + it.each([ |
| 41 | + [`undefined`, undefined, undefined], |
| 42 | + [`NaN`, Number.NaN, Number.NaN], |
| 43 | + [`signed zero`, 0, -0], |
| 44 | + [`equal dates`, new Date(1000), new Date(1000)], |
| 45 | + [`equal byte arrays`, new Uint8Array([1, 2]), new Uint8Array([1, 2])], |
| 46 | + ])(`keeps the existing bucket for %s`, (_caseName, oldValue, newValue) => { |
| 47 | + const index = createIndex() |
| 48 | + index.add(`a`, { value: oldValue }) |
| 49 | + const bucket = index.valueMapData.get(normalizeValue(oldValue)) |
| 50 | + expect(bucket).toBeDefined() |
| 51 | + |
| 52 | + index.update(`a`, { value: oldValue }, { value: newValue }) |
| 53 | + |
| 54 | + expect(index.valueMapData.get(normalizeValue(newValue))).toBe(bucket) |
| 55 | + }) |
| 56 | + |
| 57 | + it(`moves the key when the indexed value changes`, () => { |
| 58 | + const index = createIndex() |
| 59 | + index.add(`a`, { value: 1 }) |
| 60 | + |
| 61 | + index.update(`a`, { value: 1 }, { value: 2 }) |
| 62 | + |
| 63 | + expect(index.lookup(`eq`, 1)).toEqual(new Set()) |
| 64 | + expect(index.lookup(`eq`, 2)).toEqual(new Set([`a`])) |
| 65 | + }) |
| 66 | + |
| 67 | + it(`does not conflate undefined and null`, () => { |
| 68 | + const index = createIndex() |
| 69 | + index.add(`a`, { value: undefined }) |
| 70 | + |
| 71 | + index.update(`a`, { value: undefined }, { value: null }) |
| 72 | + |
| 73 | + expect(index.lookup(`eq`, undefined)).toEqual(new Set()) |
| 74 | + expect(index.lookup(`eq`, null)).toEqual(new Set([`a`])) |
| 75 | + }) |
| 76 | + |
| 77 | + it(`does not use comparator equality to skip an update`, () => { |
| 78 | + const index = createIndex({ |
| 79 | + compareFn: (a: string, b: string) => |
| 80 | + a.toLowerCase().localeCompare(b.toLowerCase()), |
| 81 | + }) |
| 82 | + index.add(`a`, { value: `A` }) |
| 83 | + |
| 84 | + index.update(`a`, { value: `A` }, { value: `a` }) |
| 85 | + |
| 86 | + expect(index.lookup(`eq`, `A`)).toEqual(new Set()) |
| 87 | + expect(index.lookup(`eq`, `a`)).toEqual(new Set([`a`])) |
| 88 | + }) |
| 89 | + |
| 90 | + it(`preserves the previous error behavior when evaluation fails`, () => { |
| 91 | + const index = createIndex() |
| 92 | + index.add(`a`, { value: 1 }) |
| 93 | + const newItem = Object.defineProperty({}, `value`, { |
| 94 | + get() { |
| 95 | + throw new Error(`evaluation failed`) |
| 96 | + }, |
| 97 | + }) |
| 98 | + |
| 99 | + expect(() => index.update(`a`, { value: 1 }, newItem)).toThrow( |
| 100 | + `evaluation failed`, |
| 101 | + ) |
| 102 | + |
| 103 | + expect(index.lookup(`eq`, 1)).toEqual(new Set()) |
| 104 | + expect(index.keyCount).toBe(0) |
| 105 | + }) |
| 106 | +}) |
| 107 | + |
| 108 | +describe(`BasicIndex update bookkeeping`, () => { |
| 109 | + it(`repairs indexed key membership after a failed removal`, () => { |
| 110 | + const index = new BasicIndex<string>(1, new PropRef([`value`])) |
| 111 | + index.add(`a`, { value: 1 }) |
| 112 | + const itemWithThrowingValue = Object.defineProperty({}, `value`, { |
| 113 | + get() { |
| 114 | + throw new Error(`evaluation failed`) |
| 115 | + }, |
| 116 | + }) |
| 117 | + const warn = vi.spyOn(console, `warn`).mockImplementation(() => {}) |
| 118 | + |
| 119 | + try { |
| 120 | + index.remove(`a`, itemWithThrowingValue) |
| 121 | + } finally { |
| 122 | + warn.mockRestore() |
| 123 | + } |
| 124 | + |
| 125 | + expect(index.lookup(`eq`, 1)).toEqual(new Set([`a`])) |
| 126 | + expect(index.keyCount).toBe(0) |
| 127 | + |
| 128 | + index.update(`a`, { value: 1 }, { value: 1 }) |
| 129 | + |
| 130 | + expect(index.lookup(`eq`, 1)).toEqual(new Set([`a`])) |
| 131 | + expect(index.keyCount).toBe(1) |
| 132 | + }) |
| 133 | +}) |
0 commit comments