@tanstack/db
Version:
A reactive client store for building super fast apps on sync
79 lines (78 loc) • 2.44 kB
text/typescript
import { BaseIndex, IndexComparisonOptions, IndexOperation } from "./base-index.cjs";
import { RangeQueryOptions } from "./btree-index.cjs";
export type { RangeQueryOptions } from "./btree-index.cjs";
/**
* Options for Basic index
*/
export type BasicIndexOptions = IndexComparisonOptions;
/**
* Basic index using Map + sorted Array.
*
* - Map for O(1) equality lookups
* - Sorted Array for O(log n) range queries via binary search
* - O(n) updates to maintain sort order
*
* Simpler and smaller than BTreeIndex, good for read-heavy workloads.
* Use BTreeIndex for write-heavy workloads with large collections.
*/
export declare class BasicIndex<TKey extends string | number = string | number> extends BaseIndex<TKey> {
private valueMap;
private sortedValues;
private sortedExactValues;
private indexedKeys;
/**
* Adds a value to the index
*/
add(key: TKey, item: any): void;
private addToBucket;
/**
* Removes a value from the index
*/
remove(key: TKey, item: any): void;
private removeFromBucket;
/**
* Updates a value in the index
*/
update(key: TKey, oldItem: any, newItem: any): void;
/**
* Builds the index from a collection of entries
*/
build(entries: Iterable<[
TKey,
any
]>): void;
/**
* Clears all data from the index
*/
clear(): void;
lookup(operation: IndexOperation, value: any): Set<TKey>;
/**
* Gets the number of indexed keys
*/
get keyCount(): number;
/**
* Performs an equality lookup - O(1)
*/
equalityLookup(value: any): Set<TKey>;
/**
* Performs a range query using binary search - O(log n + m)
*/
rangeQuery(options?: RangeQueryOptions): Set<TKey>;
/**
* Returns the next n items in sorted order
*/
take(n: number, from: any, filterFn?: (key: TKey) => boolean): Array<TKey>;
/**
* Returns the next n items in reverse sorted order
*/
takeReversed(n: number, from: any, filterFn?: (key: TKey) => boolean): Array<TKey>;
/**
* Returns the first n items in sorted order (from the start)
*/
takeFromStart(n: number, filterFn?: (key: TKey) => boolean): Array<TKey>;
/**
* Returns the first n items in reverse sorted order (from the end)
*/
takeReversedFromEnd(n: number, filterFn?: (key: TKey) => boolean): Array<TKey>;
private takeFromIndex;
}