(
rows: Iterable<{ rowid: number; kind: string; name: string }>,
)
| 72 | * a name holds the separator, so the caller keeps asking SQLite. |
| 73 | */ |
| 74 | export function buildNameSubstringIndex( |
| 75 | rows: Iterable<{ rowid: number; kind: string; name: string }>, |
| 76 | ): NameSubstringIndex | null { |
| 77 | const kindCodes = new Map(NAME_SUBSTRING_KINDS.map((kind, i) => [kind, i])); |
| 78 | const nameIds = new Map<string, number>(); |
| 79 | const names: string[] = []; |
| 80 | const rowids: number[] = []; |
| 81 | const kinds: number[] = []; |
| 82 | const lengths: number[] = []; |
| 83 | const nameOf: number[] = []; |
| 84 | for (const row of rows) { |
| 85 | const code = kindCodes.get(row.kind); |
| 86 | if (code === undefined) continue; |
| 87 | const lowered = asciiLower(row.name); |
| 88 | if (lowered.includes(SEPARATOR)) return null; |
| 89 | let id = nameIds.get(lowered); |
| 90 | if (id === undefined) { |
| 91 | id = names.length; |
| 92 | nameIds.set(lowered, id); |
| 93 | names.push(lowered); |
| 94 | } |
| 95 | rowids.push(row.rowid); |
| 96 | kinds.push(code); |
| 97 | lengths.push(sqliteLength(row.name)); |
| 98 | nameOf.push(id); |
| 99 | } |
| 100 | |
| 101 | const starts = new Int32Array(names.length + 1); |
| 102 | let offset = 0; |
| 103 | for (let i = 0; i < names.length; i++) { |
| 104 | starts[i] = offset; |
| 105 | offset += names[i]!.length + SEPARATOR.length; |
| 106 | } |
| 107 | starts[names.length] = offset; |
| 108 | |
| 109 | // Group entries by name, keeping index order inside each group. |
| 110 | const nameEntryStart = new Int32Array(names.length + 1); |
| 111 | for (const id of nameOf) nameEntryStart[id + 1]!++; |
| 112 | for (let i = 0; i < names.length; i++) nameEntryStart[i + 1]! += nameEntryStart[i]!; |
| 113 | const fill = nameEntryStart.slice(0, names.length); |
| 114 | const nameEntries = new Int32Array(nameOf.length); |
| 115 | for (let entry = 0; entry < nameOf.length; entry++) nameEntries[fill[nameOf[entry]!]!++] = entry; |
| 116 | |
| 117 | return { |
| 118 | kinds: NAME_SUBSTRING_KINDS, |
| 119 | hay: names.join(SEPARATOR) + SEPARATOR, |
| 120 | starts, |
| 121 | nameEntryStart, |
| 122 | nameEntries, |
| 123 | entryRowid: Float64Array.from(rowids), |
| 124 | entryKind: Uint8Array.from(kinds), |
| 125 | entryLength: Int32Array.from(lengths), |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | /** The distinct name whose span in `hay` holds `offset`. */ |
| 130 | function nameAt(starts: Int32Array, offset: number): number { |
no test coverage detected