import { ObjectType } from './types'; export type LeafDataItem = { type: ObjectType; id: number; version: number; }; /** * HashTree is a data structure that organizes items into leaves based on their IDs, */ declare class HashTree { leaves: Array>>; leafHashes: Array; readonly numLeaves: number; /** * Constructs a new HashTree. * @param {LeafDataItem[]} leafData Initial data to populate the tree. * @param {number} numLeaves The number of leaf nodes in the tree. Must be 0 or a power of 2. * @throws {Error} If numLeaves is not 0 or a power of 2. */ constructor(leafData: LeafDataItem[], numLeaves: number); /** * Internal logic for adding or updating an item, without computing the leaf hash. * @param {LeafDataItem} item The item to add or update. * @returns {{put: boolean, index: (number|null)}} Object indicating if put and the leaf index. * @private */ private _putItemInternal; /** * Adds or updates a single item in the hash tree. * @param {LeafDataItem} item The item to add or update. * @returns {boolean} True if the item was added or updated, false otherwise (e.g., older version or tree has 0 leaves). */ putItem(item: LeafDataItem): boolean; /** * Adds or updates multiple items in the hash tree. * @param {LeafDataItem[]} items The array of items to add or update. * @returns {boolean[]} An array of booleans indicating success for each item. */ putItems(items: LeafDataItem[]): boolean[]; /** * Internal logic for removing an item, without computing the leaf hash. * @param {LeafDataItem} item The item to remove. * @returns {{removed: boolean, index: (number|null)}} Object indicating if removed and the leaf index. * @private */ private _removeItemInternal; /** * Removes a single item from the hash tree. * The removal is based on matching type, id, and the provided item's version * being greater than or equal to the existing item's version. * @param {LeafDataItem} item The item to remove. * @returns {boolean} True if the item was removed, false otherwise. */ removeItem(item: LeafDataItem): boolean; /** * Removes multiple items from the hash tree. * @param {LeafDataItem[]} items The array of items to remove. * @returns {boolean[]} An array of booleans indicating success for each item. */ removeItems(items: LeafDataItem[]): boolean[]; /** * Updates multiple items in the hash tree. * This method can handle both updating and removing items based on the `operation` flag. * * @param {object[]} itemUpdates An array of objects containing `operation` flag and the `item` to update. * @returns {boolean[]} An array of booleans indicating success for each operation. */ updateItems(itemUpdates: { operation: 'update' | 'remove'; item: LeafDataItem; }[]): boolean[]; /** * Computes the hash for a specific leaf. * The hash is based on the sorted items within the leaf. * @param {number} index The index of the leaf to compute the hash for. * @returns {void} */ computeLeafHash(index: number): void; /** * Computes all internal and leaf node hashes of the tree. * Internal node hashes are computed bottom-up from the leaf hashes. * @returns {string[]} An array of hash strings, with internal node hashes first, followed by leaf hashes. * Returns `[EMPTY_HASH]` if the tree has 0 leaves. */ computeTreeHashes(): string[]; /** * Returns all hashes in the tree (internal nodes then leaf nodes). * @returns {string[]} An array of hash strings. */ getHashes(): string[]; /** * Computes and returns the hash value of the root node. * @returns {string} The root hash of the entire tree. Returns `EMPTY_HASH` if the tree has 0 leaves. */ getRootHash(): string; /** * Gets the number of leaves in the tree. * @returns {number} The number of leaves. */ getLeafCount(): number; /** * Calculates the total number of items stored in the tree. * @returns {number} The total number of items. */ getTotalItemCount(): number; /** * Retrieves all data items from a specific leaf. * @param {number} leafIndex The index of the leaf. * @returns {LeafDataItem[]} An array of LeafDataItem in the specified leaf, sorted by ID, * or an empty array if the index is invalid or leaf is empty. */ getLeafData(leafIndex: number): LeafDataItem[]; /** * Retrieves the version of a specific item by its id and type. * @param {number} id The ID of the item. * @param {ObjectType} type The type of the item. * @returns {number | undefined} The version of the item if found, undefined otherwise. */ getItemVersion(id: number, type: ObjectType): number | undefined; /** * Resizes the HashTree to have a new number of leaf nodes, redistributing all existing items. * @param {number} newNumLeaves The new number of leaf nodes (must be 0 or a power of 2). * @returns {boolean} true if the tree was resized, false if the size didn't change. * @throws {Error} if newNumLeaves is not 0 or a power of 2. */ resize(newNumLeaves: number): boolean; /** * Compares the tree's leaf hashes with an external set of hashes and returns the indices of differing leaf nodes. * The externalHashes array is expected to contain all node hashes (internal followed by leaves), * similar to the output of getHashes(). * @param {string[]} externalHashes An array of hash strings (internal node hashes then leaf hashes). * @returns {number[]} An array of indices of the leaf nodes that have different hashes. */ diffHashes(externalHashes: string[]): number[]; } export default HashTree;