@tanstack/db
Version:
A reactive client store for building super fast apps on sync
78 lines (77 loc) • 3.73 kB
text/typescript
/**
* Mutable B+ tree used by BTreeIndex for sorted value buckets. Keys use the
* supplied comparator, which must return a number that is not NaN; BTreeIndex
* checks every comparator result. Point operations cost O(log size). This fork has
* no copy-on-write sharing, cloning, optional-value storage, early-exit range
* callbacks, or in-place range edits: only the operations BTreeIndex uses,
* plus `has()` and the `get()` fallback that the Map oracle observes
* (tests/btree-map-oracle.test.ts).
* @author David Piepgrass
*/
export declare class BTree<K = any, V = any> {
private _root;
_size: number;
_maxNodeSize: number;
/**
* provides a total order over keys (and a strict partial order over the type K)
* @returns a negative value if a < b, 0 if a === b and a positive value if a > b
*/
_compare: (a: K, b: K) => number;
/**
* Initializes an empty B+ tree.
* @param compare Custom function to compare pairs of elements in the tree.
* @param maxNodeSize Branching factor (maximum items or children per node)
* Must be in range 4..256. If undefined or <4 then default is used; if >256 then 256.
*/
constructor(compare: (a: K, b: K) => number, maxNodeSize?: number);
/** Gets the number of key-value pairs in the tree. */
get size(): number;
/** Releases the tree so that its size is 0. */
clear(): void;
/**
* Finds a pair in the tree and returns the associated value.
* @param defaultValue a value to return if the key was not found.
* @returns the value, or defaultValue if the key was not found.
* @description Computational complexity: O(log size)
*/
get(key: K, defaultValue?: V): V | undefined;
/** Returns true if the key exists in the B+ tree. */
has(key: K): boolean;
/**
* Adds or overwrites a key-value pair in the B+ tree. Overwriting also
* replaces the stored key.
* @returns true if a new key-value pair was added.
* @description Computational complexity: O(log size)
*/
set(key: K, value: V): boolean;
/**
* Removes a single key-value pair from the B+ tree.
* @returns true if a pair was found and removed, false otherwise.
* @description Computational complexity: O(log size)
*/
delete(key: K): boolean;
/** Gets the lowest key in the tree. Complexity: O(log size) */
minKey(): K | undefined;
/** Gets the highest key in the tree. Complexity: O(1) */
maxKey(): K | undefined;
/** Returns the next pair whose key is larger than the specified key (or undefined if there is none).
* If key === undefined, this function returns the lowest pair.
*/
nextHigherPair(key: K | undefined): [K, V] | undefined;
/** Returns the next pair whose key is smaller than the specified key (or undefined if there is none).
* If key === undefined, this function returns the highest pair.
*/
nextLowerPair(key: K | undefined): [K, V] | undefined;
/**
* Scans the specified range of keys, in ascending order by key.
* Note: the callback `onFound` must not insert or remove items in the
* collection. Doing so may cause incorrect data to be sent to the
* callback afterward.
* @param low The first key scanned will be greater than or equal to `low`.
* @param high Scanning stops when a key larger than this is reached.
* @param includeHigh If the `high` key is present, `onFound` is called for
* that final pair if and only if this parameter is true.
* @description Computational complexity: O(number of items scanned + log size)
*/
forRange(low: K, high: K, includeHigh: boolean, onFound: (k: K, v: V) => void): void;
}