Index records by the exact keys returned by keysOf(record, id). The selector is called once per record.
Each iterable is consumed once. Keys use Map equality. Repeated keys on one record yield one posting.
IDs denote positions in the input at build time. The index retains keys and IDs after construction.
Expected O(n + t) construction time for n records and t yielded keys, excluding the selector's own work.
With p distinct record/key associations and k distinct keys, both build workspace and retained storage are O(p + k).
A single-key lookup costs expected O(1) plus O(1) per returned ID.
@throws ― {InvertedIndexInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a record ID or the posting
count exceeds 2³² − 1, or when the runtime rejects the key table or packed columns. Errors raised by input
iterators and the selector propagate to the caller.
Index records by the exact keys returned by keysOf(record, id). The selector is called once per record.
Each iterable is consumed once. Keys use Map equality. Repeated keys on one record yield one posting.
IDs denote positions in the input at build time. The index retains keys and IDs after construction.
Expected O(n + t) construction time for n records and t yielded keys, excluding the selector's own work.
With p distinct record/key associations and k distinct keys, both build workspace and retained storage are O(p + k).
A single-key lookup costs expected O(1) plus O(1) per returned ID.
@throws ― {InvertedIndexInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a record ID or the posting
count exceeds 2³² − 1, or when the runtime rejects the key table or packed columns. Errors raised by input
iterators and the selector propagate to the caller.
invertedIndex(
constissues: {
title:string;
labels:string[];
}[]
issues, (
issue: {
title: string;
labels: string[];
}
issue) =>
issue: {
title: string;
labels: string[];
}
issue.
labels: string[]
labels)
constindex:InvertedIndex<string>
index.
InvertedIndex<string>.size: number
Number of distinct keys. O(1).
size// 2 distinct labels
constindex:InvertedIndex<string>
index.
InvertedIndex<string>.recordCount: number
Number of input records, including records with no keys. O(1).
Visit each record matching any query key once, in ascending order. An empty query visits nothing.
With k query keys and u matching records, uses O(k) temporary space and O(k × (u + 1)) time.
Each call owns its cursors. The visitor can query the index recursively. Visitor and iterable errors propagate.
forEachAny(['urgent', 'open'], (
id: number
id) =>
consttitles:string[]
titles.
Array<string>.push(...items: string[]): number
Appends new elements to the end of an array, and returns the new length of the array.
The two posting lists have 86 entries and the union emits 64 unique IDs.
Build the index once when repeated queries over fixed tags make a full scan wasteful.
Hover or focus a chart for exact values. Each change reruns demo.ts against knotkit/inverted-index in your browser; the defaults reproduce the README figure.
Every error this module throws is InvertedIndexInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.
Use knotkit/inverted-index for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/inverted-index'.
Exports:
- `invertedIndex` → `InvertedIndex<K>`: Build once from an iterable and its key selector. Every query yields matching zero-based record positions in ascending order.
Inputs and their sources:
- exact keys for each input record and its zero-based ID: `(record: R, id: number) => Iterable<K>` from `the caller`
Use something else when:
- add or remove associations between queries → Map<K, Set<number>>
- match a delimiter-separated key by longest prefix → knotkit/trie
- combine mutable sets of integer IDs → knotkit/bitset
Errors are InvertedIndexInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/inverted-index.md
Types: node_modules/knotkit/inverted-index/ and https://knotkit.jkomyno.dev/reference/knotkit/inverted-index/