/** * In-memory index engine for fast document lookups. * * Supports single-field and compound indexes with optional uniqueness. * Indexes are stored as sorted maps from stringified key values to sets of document IDs. */ import type { IndexOptions } from '@digitaldefiance/suite-core-lib'; import type { BsonDocument, DocumentId } from '../interfaces/storage/documentTypes'; import type { IndexSpec } from './types'; /** * Represents a single index on a collection. */ export declare class CollectionIndex { /** Name of the index */ readonly name: string; /** Fields and sort direction */ readonly spec: IndexSpec; /** Whether the index enforces uniqueness */ readonly unique: boolean; /** Whether to skip documents missing the indexed field */ readonly sparse: boolean; /** * Map from stringified key → Set of document IDs. * For unique indexes the set will have at most 1 entry. */ private readonly entries; constructor(spec: IndexSpec, options?: IndexOptions); /** * Extract the index key from a document. * Returns `undefined` if the document is missing a required field (and the index is sparse). */ private extractKey; /** * Add a document to the index. * @throws Error if the index is unique and a duplicate key is found. */ addDocument(doc: BsonDocument): void; /** * Remove a document from the index. */ removeDocument(doc: BsonDocument): void; /** * Look up document IDs by exact field values. * Returns `undefined` if the query cannot use this index. */ lookup(query: Record): Set | undefined; /** * Get all document IDs in the index. */ allIds(): Set; /** * Clear the index. */ clear(): void; /** * Get the number of entries in the index. */ get size(): number; /** * Serialize the index metadata for persistence. */ toJSON(): { name: string; spec: IndexSpec; unique: boolean; sparse: boolean; }; } /** * Manages multiple indexes for a collection. */ export declare class IndexManager { private readonly indexes; /** * Create a new index. * @returns The name of the created index. * @throws Error if an index with the same name already exists with different options. */ createIndex(spec: IndexSpec, options?: IndexOptions): string; /** * Drop an index by name. */ dropIndex(name: string): boolean; /** * Get all index names. */ listIndexes(): string[]; /** * Get an index by name. */ getIndex(name: string): CollectionIndex | undefined; /** * Index a document in all indexes. * @throws DuplicateKeyError if a unique constraint is violated. */ addDocument(doc: BsonDocument): void; /** * Extract the index key from a document for a given index. * Mirrors CollectionIndex.extractKey for pre-validation. */ private extractKeyForIndex; /** * Look up existing entries for a key in a specific index. */ private lookupKeyInIndex; /** * Remove a document from all indexes. */ removeDocument(doc: BsonDocument): void; /** * Rebuild all indexes from a set of documents. */ rebuildAll(docs: BsonDocument[]): void; /** * Try to find an index that can serve the given query filter. * Returns the set of matching document IDs if an index applies, otherwise undefined. */ findCandidates(filter: Record): Set | undefined; /** * Serialize all index metadata for persistence. */ toJSON(): Array<{ name: string; spec: IndexSpec; unique: boolean; sparse: boolean; }>; /** * Restore indexes from serialized metadata and rebuild from docs. */ restoreFromJSON(metadata: Array<{ name: string; spec: IndexSpec; unique: boolean; sparse: boolean; }>, docs: BsonDocument[]): void; } export declare class DuplicateKeyError extends Error { readonly indexName: string; readonly keyValue: string; constructor(indexName: string, keyValue: string); } //# sourceMappingURL=indexing.d.ts.map