import { Hookified, HookifiedOptions } from "hookified"; import { Keyv, Keyv as Keyv$1, KeyvAny } from "keyv"; import { Hashery } from "hashery"; //#region src/index.d.ts /** * A subset of the native `Map` interface that {@link BigMap} implements. * @template K The type of the keys. * @template V The type of the values. */ type MapInterface = { readonly size: number; clear(): void; delete(key: K): boolean; forEach(callbackfn: (value: V, key: K, map: MapInterface) => void, thisArg?: KeyvAny): void; entries(): IterableIterator<[K, V]>; keys(): IterableIterator; values(): IterableIterator; [Symbol.iterator](): IterableIterator<[K, V]>; get(key: K): V | undefined; has(key: K): boolean; set(key: K, value: V): MapInterface; }; /** * A hash function used to map a key to one of the internal store buckets. * @param {string} key The key to hash (non-string keys are coerced to a string). * @param {number} storeSize The total number of internal stores. * @returns {number} The index of the store to use, in the range `0` to `storeSize - 1`. */ type StoreHashFunction = (key: string, storeSize: number) => number; /** * Events emitted by {@link BigMap}. The class extends * {@link https://github.com/jaredwray/hookified | Hookified}, so you can subscribe * with `bigMap.on(BigMapEvents.SET, listener)`. */ declare enum BigMapEvents { /** Emitted after a value is set. Listeners receive `(key, value)`. */ SET = "set", /** Emitted after an existing entry is deleted. Listeners receive `(key)`. */ DELETE = "delete", /** Emitted after all entries are cleared. Listeners receive no arguments. */ CLEAR = "clear" } /** * O(1) hash function that samples characters from start, middle, and end of key. * Uses multiplicative hashing (Fibonacci) for good bit distribution across shards. * @param {string} key The key to hash. * @param {number} storeSize The total number of internal stores. * @returns {number} The store index for the key, in the range `0` to `storeSize - 1`. */ declare function defaultHashFunction(key: string, storeSize: number): number; /** * Configuration options for {@link BigMap}. */ type BigMapOptions = { /** * Number of internal `Map` instances used to store entries. Must be at least 1. * @default 2 */ storeSize?: number; /** * Custom hash function used to distribute keys across the internal stores. * Defaults to {@link defaultHashFunction} when not provided. * @default defaultHashFunction */ storeHashFunction?: StoreHashFunction; } & HookifiedOptions; /** * A scalable `Map` implementation that distributes entries across multiple internal * `Map` instances, allowing it to scale beyond the ~16.7 million entry limit of a * native `Map`. It mirrors the native `Map` API and emits {@link BigMapEvents} on * mutations via its {@link https://github.com/jaredwray/hookified | Hookified} base. * @template K The type of the keys. * @template V The type of the values. * @example * ```typescript * const bigMap = new BigMap(); * bigMap.on(BigMapEvents.SET, (key, value) => console.log(key, value)); * bigMap.set("key", 100); * ``` */ declare class BigMap extends Hookified implements MapInterface { private _storeSize; private _store; private _storeHashFunction; private _isDefaultHash; private _isPowerOf2; /** * Creates an instance of BigMap. * @param {BigMapOptions} [options] - Optional configuration options for the BigMap. * @throws {Error} If `storeSize` is less than 1. */ constructor(options?: BigMapOptions); /** * Gets the number of internal `Map` instances in the store. * @returns {number} The number of maps in the store. */ get storeSize(): number; /** * Sets the number of internal `Map` instances in the store. Existing entries * are moved into the store their key maps to at the new size, so every entry * stays reachable. * @param {number} size - The new size of the store. * @throws {Error} If the size is less than 1, or if creating the new stores or * moving an entry throws, such as for a size too large to allocate. The map is * left unchanged in every case. */ set storeSize(size: number); /** * Gets the hash function used for distributing keys across the store. * @returns {StoreHashFunction | undefined} The active hash function. */ get storeHashFunction(): StoreHashFunction | undefined; /** * Sets the hash function used for distributing keys across the store. * Passing `undefined` restores the {@link defaultHashFunction}. Existing entries * are moved into the store the new hash function maps them to, so every entry * stays reachable. * @param {StoreHashFunction | undefined} hashFunction - The hash function to use, or `undefined` to reset to the default. * @throws {Error} If creating the new stores or moving an entry throws, such as * when the new hash function throws. The map, including its hash function, is * left unchanged. */ set storeHashFunction(hashFunction: StoreHashFunction | undefined); /** * Gets the internal store, which is an array of `Map` instances. * @returns {Array>} The array of maps in the store. */ get store(): Array>; /** * Gets the total number of entries across all internal stores. * @returns {number} The number of entries in the map. */ get size(): number; /** * Gets the internal `Map` instance at the specified index in the store. * @param {number} index - The index of the map to retrieve. * @returns {Map} The map at the specified index. * @throws {Error} If the index is out of bounds. */ getStoreMap(index: number): Map; /** * Initializes the store with empty `Map` instances, removing every entry * without emitting {@link BigMapEvents.CLEAR}. Called automatically during * construction, and when `storeSize` or `storeHashFunction` changes, before * existing entries are moved into the new stores. * @returns {void} */ initStore(): void; /** * Gets the internal `Map` instance that holds the given key. The bucket is * resolved by applying the hash function to the key and the store size. * @param {K} key - The key for which to resolve the store. * @returns {Map} The store that holds (or would hold) the key. */ getStore(key: K): Map; /** * Returns an iterable of key-value pairs for every entry in the map. * @returns {IterableIterator<[K, V]>} An iterable of key-value pairs in the map. */ entries(): IterableIterator<[K, V]>; /** * Returns an iterable of the keys in the map. * @returns {IterableIterator} An iterable of keys in the map. */ keys(): IterableIterator; /** * Returns an iterable of the values in the map. * @returns {IterableIterator} An iterable of values in the map. */ values(): IterableIterator; /** * Returns an iterator over the key-value pairs in the map. Enables `for...of`. * @returns {IterableIterator<[K, V]>} An iterator over the key-value pairs in the map. */ [Symbol.iterator](): IterableIterator<[K, V]>; /** * Removes all entries from the map and emits {@link BigMapEvents.CLEAR}. * @returns {void} */ clear(): void; /** * Deletes a key-value pair from the map. Emits {@link BigMapEvents.DELETE} * with the key when an entry is removed. * @param {K} key - The key of the entry to delete. * @returns {boolean} `true` if an entry was deleted, `false` if the key was not found. */ delete(key: K): boolean; /** * Calls a provided callback once for each key-value pair in the map. * @param {(value: V, key: K, map: Map) => void} callbackfn - The function to execute for each entry, receiving the value, key, and the map. * @param {any} [thisArg] - An optional value to use as `this` when executing the callback. * @returns {void} */ forEach(callbackfn: (this: KeyvAny, value: V, key: K, map: Map) => void, thisArg?: KeyvAny): void; /** * Gets the value associated with the specified key. * @param {K} key - The key of the entry to get. * @returns {V | undefined} The value associated with the key, or `undefined` if the key does not exist. */ get(key: K): V | undefined; /** * Checks whether the map contains the specified key. * @param {K} key - The key to check for existence. * @returns {boolean} `true` if the key exists, `false` otherwise. */ has(key: K): boolean; /** * Sets the value for a key in the map and emits {@link BigMapEvents.SET} * with the key and value. * @param {K} key - The key of the entry to set. * @param {V} value - The value to associate with the key. * @returns {this} The BigMap instance, enabling method chaining (matches the native `Map.set` API). */ set(key: K, value: V): this; /** * Applies a change to how keys map to stores, then moves every entry into the * store its key now maps to. If creating the new stores or moving an entry * throws, the previous stores and settings are restored and the error is * rethrown, so the map is left unchanged. * @param {() => void} applyChange - Updates the store size or hash function. * @returns {void} */ private _rebuildStore; /** * Resolves the shard Map for a string key. Inlines the default hash * to avoid function call overhead on the hot path. * @param {string} k - The string key to resolve. * @returns {Map} The store that holds (or would hold) the key. */ private _resolve; } /** * Will create a Keyv instance with the BigMap adapter. * @param {BigMapOptions} [options] - Options for the BigMap adapter such as `storeSize` and `storeHashFunction`. * @returns {Keyv} A Keyv instance backed by the BigMap adapter. */ declare function createKeyv(options?: BigMapOptions): Keyv$1; //#endregion export { BigMap, BigMapEvents, BigMapOptions, Hashery, Keyv, MapInterface, StoreHashFunction, createKeyv, defaultHashFunction };