| 9 | * - IN lookup: `property IN [value1, value2, ...]` |
| 10 | */ |
| 11 | export class HashIndex implements Index { |
| 12 | public readonly type = "hash" as const; |
| 13 | public readonly label: string; |
| 14 | public readonly property: string; |
| 15 | |
| 16 | /** |
| 17 | * Maps property values to sets of element IDs. |
| 18 | */ |
| 19 | #index: Map<unknown, Set<ElementId>> = new Map(); |
| 20 | |
| 21 | /** |
| 22 | * Reverse mapping from element ID to its indexed value. |
| 23 | * Used for efficient updates and removals. |
| 24 | */ |
| 25 | #reverse: Map<ElementId, unknown> = new Map(); |
| 26 | |
| 27 | public constructor(label: string, property: string) { |
| 28 | this.label = label; |
| 29 | this.property = property; |
| 30 | } |
| 31 | |
| 32 | public add(elementId: ElementId, value: unknown): void { |
| 33 | if (value === undefined || value === null) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | let ids = this.#index.get(value); |
| 38 | if (!ids) { |
| 39 | ids = new Set(); |
| 40 | this.#index.set(value, ids); |
| 41 | } |
| 42 | ids.add(elementId); |
| 43 | this.#reverse.set(elementId, value); |
| 44 | } |
| 45 | |
| 46 | public remove(elementId: ElementId, _value: unknown): void { |
| 47 | // Use stored value from reverse index to ensure we remove correctly |
| 48 | const storedValue = this.#reverse.get(elementId); |
| 49 | if (storedValue === undefined) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | const ids = this.#index.get(storedValue); |
| 54 | if (ids) { |
| 55 | ids.delete(elementId); |
| 56 | if (ids.size === 0) { |
| 57 | this.#index.delete(storedValue); |
| 58 | } |
| 59 | } |
| 60 | this.#reverse.delete(elementId); |
| 61 | } |
| 62 | |
| 63 | public update(elementId: ElementId, oldValue: unknown, newValue: unknown): void { |
| 64 | this.remove(elementId, oldValue); |
| 65 | this.add(elementId, newValue); |
| 66 | } |
| 67 | |
| 68 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected