@tanstack/db
Version:
A reactive client store for building super fast apps on sync
103 lines (102 loc) • 3.28 kB
text/typescript
import { BaseIndex, IndexOperation } from './base-index.js';
import { CompareOptions } from '../query/builder/types.js';
import { BasicExpression } from '../query/ir.js';
/**
* Options for range queries
*/
export interface RangeQueryOptions {
from?: any;
to?: any;
fromInclusive?: boolean;
toInclusive?: boolean;
}
/**
* Options for Basic index
*/
export interface BasicIndexOptions {
compareFn?: (a: any, b: any) => number;
compareOptions?: CompareOptions;
}
/**
* 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> {
readonly supportedOperations: Set<"eq" | "gt" | "gte" | "lt" | "lte" | "in" | "like" | "ilike">;
private valueMap;
private sortedValues;
private indexedKeys;
private compareFn;
constructor(id: number, expression: BasicExpression, name?: string, options?: any);
protected initialize(_options?: BasicIndexOptions): void;
/**
* Adds a value to the index
*/
add(key: TKey, item: any): void;
/**
* Removes a value from the index
*/
remove(key: TKey, item: any): void;
/**
* 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;
/**
* Performs a lookup operation
*/
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>;
/**
* Performs a reversed range query
*/
rangeQueryReversed(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>;
/**
* Performs an IN array lookup - O(k) where k is values.length
*/
inArrayLookup(values: Array<any>): Set<TKey>;
get indexedKeysSet(): Set<TKey>;
get orderedEntriesArray(): Array<[any, Set<TKey>]>;
get orderedEntriesArrayReversed(): Array<[any, Set<TKey>]>;
get valueMapData(): Map<any, Set<TKey>>;
}