import { BaseBlock } from '../blocks/base'; import { BlockHandle } from '../blocks/handle'; import { RawDataBlock } from '../blocks/rawData'; import { BlockSize } from '../enumerations/blockSize'; import { IFecService } from '../interfaces/services/fecService'; import { BlockStoreOptions, BrightenResult, IBlockMetadata, RecoveryResult } from '../interfaces/storage/blockMetadata'; import { IBlockStore } from '../interfaces/storage/blockStore'; import { CBLMagnetComponents, CBLStorageResult, CBLWhiteningOptions } from '../interfaces/storage/cblWhitening'; import type { IHeadRegistry } from '../interfaces/storage/headRegistry'; import { Checksum } from '../types/checksum'; import { MemoryBlockMetadataStore } from './memoryBlockMetadataStore'; /** * Browser-compatible in-memory block store using Uint8Array. * * This implementation supports: * - Core block operations (store, retrieve, delete) * - Metadata management for tracking block lifecycle and access patterns * - FEC (Forward Error Correction) operations using Reed-Solomon encoding (when FEC service is provided) * - Replication tracking for distributed storage * * @see IBlockStore for the interface definition */ export declare class MemoryBlockStore implements IBlockStore { /** * Per-size block maps. Only maps for configured supportedBlockSizes are initialized. */ private readonly blocks; private readonly _supportedBlockSizes; /** * Optional FEC service for parity generation and recovery. * If not provided, FEC operations will return errors or no-ops. */ private readonly fecService; /** * Metadata store for tracking block lifecycle and access patterns. */ private readonly metadataStore; /** * In-memory storage for parity block data. * Key is the data block ID, value is array of parity data. */ private readonly parityBlocks; /** * Create a new MemoryBlockStore. * @param blockSizeOrSizes - A single BlockSize (backward compat) or an array of supported BlockSize values * @param fecService - Optional FEC service for parity generation and recovery * @param metadataStore - Optional metadata store (creates new one if not provided) */ constructor(blockSizeOrSizes: BlockSize | readonly BlockSize[], fecService?: IFecService | null, metadataStore?: MemoryBlockMetadataStore); get supportedBlockSizes(): readonly BlockSize[]; /** * Backward-compatible getter: returns the first supported block size. * @deprecated Use supportedBlockSizes instead. */ get blockSize(): BlockSize; /** * Get the FEC service if available. * @returns The FEC service or null if not available */ getFecService(): IFecService | null; /** * Get the metadata store. * @returns The metadata store */ getMetadataStore(): MemoryBlockMetadataStore; /** * Convert a key to hex string format. * @param key - The key as Checksum or string * @returns The key as hex string */ private keyToHex; /** * Convert a hex string to Checksum. * @param hex - The hex string * @returns The Checksum */ private hexToChecksum; /** * Find a block by key across all configured size maps. * @returns The block if found, or undefined */ private findBlock; /** * Check if a block exists in any size map (synchronous, no metadata). */ private hasBlock; /** * Set a block directly in the correct size map (for internal use like recovery). */ private setBlock; /** * Check if a block exists (searches across all configured size maps) */ has(key: Checksum | string): Promise; /** * Get a block's data (searches across all configured size maps) */ getData(key: Checksum): Promise; /** * Store a block's data with optional durability settings. * Validates that block.blockSize is in supportedBlockSizes before accepting. * @param block - The block to store * @param options - Optional storage options including durability level and expiration */ setData(block: RawDataBlock, options?: BlockStoreOptions): Promise; /** * Delete a block's data (and associated parity blocks and metadata). * Searches across all configured size maps. */ deleteData(key: Checksum): Promise; /** * Get random block checksums from the store for a specific block size. * @param count - Number of random block checksums to return * @param blockSize - The block size pool to draw from (must be in supportedBlockSizes) */ getRandomBlocks(count: number, blockSize: BlockSize): Promise; /** * Store raw data with a key (convenience method). * Picks the smallest supported block size that fits the data. Throws * `StoreError(BlockValidationFailed)` when no configured size can hold * the data. */ put(key: Checksum | string, data: Uint8Array, options?: BlockStoreOptions): Promise; /** * Delete a block (alias for deleteData) */ delete(key: Checksum | string): Promise; /** * Generate a random ID */ static randomId(): string; /** * Clear all blocks, metadata, and parity data across all size maps */ clear(): void; /** * Get total number of blocks across all size maps */ size(): number; /** * Get a handle to a block (searches across all configured size maps) */ get(key: Checksum | string): BlockHandle; /** * Get metadata for a block * @param key - The block's checksum or ID * @returns The block's metadata, or null if not found */ getMetadata(key: Checksum | string): Promise; /** * Update metadata for a block * @param key - The block's checksum or ID * @param updates - Partial metadata updates to apply */ updateMetadata(key: Checksum | string, updates: Partial): Promise; /** * Generate parity blocks for a data block using Reed-Solomon encoding. * @param key - The data block's checksum * @param parityCount - Number of parity blocks to generate * @returns Array of parity block checksums (as hex strings converted to Checksum) * @throws FecError if FEC service is not available or encoding fails */ generateParityBlocks(key: Checksum | string, parityCount: number): Promise; /** * Get parity block checksums for a data block. * @param key - The data block's checksum or ID * @returns Array of parity block checksums */ getParityBlocks(key: Checksum | string): Promise; /** * Attempt to recover a corrupted or missing block using parity data. * @param key - The block's checksum or ID * @returns Recovery result with the recovered block or error */ recoverBlock(key: Checksum | string): Promise; /** * Verify block integrity against its parity data. * @param key - The block's checksum or ID * @returns True if the block data matches its parity data */ verifyBlockIntegrity(key: Checksum | string): Promise; /** * Get blocks that are pending replication (status = Pending). * @returns Array of block checksums pending replication */ getBlocksPendingReplication(): Promise; /** * Get blocks that are under-replicated (status = UnderReplicated). * @returns Array of block checksums that need additional replicas */ getUnderReplicatedBlocks(): Promise; /** * Record that a block has been replicated to a node. * @param key - The block's checksum or ID * @param nodeId - The ID of the node that now holds a replica */ recordReplication(key: Checksum | string, nodeId: string): Promise; /** * Record that a replica node is no longer available. * @param key - The block's checksum or ID * @param nodeId - The ID of the node that lost the replica */ recordReplicaLoss(key: Checksum | string, nodeId: string): Promise; /** * Brighten a block by XORing it with random blocks from the store. * This is used to implement Owner-Free storage patterns where the * original data cannot be reconstructed without all the random blocks. * * @param key - The source block's checksum or ID * @param randomBlockCount - Number of random blocks to XOR with * @returns Result containing the brightened block ID and the random block IDs used * @throws StoreError if the source block is not found or insufficient random blocks are available */ brightenBlock(key: Checksum | string, randomBlockCount: number): Promise; /** * Store a CBL with XOR whitening for Owner-Free storage. * * This method: * 1. Pads the CBL to block size with length prefix * 2. Generates a cryptographically random block (R) of the same size * 3. XORs the padded CBL with R to produce the second block (CBL XOR R) * 4. Stores both blocks separately * 5. Generates parity blocks if durability options specify redundancy * 6. Returns the IDs and a magnet URL for reconstruction * * @param cblData - The original CBL data as Uint8Array * @param options - Optional storage options (durability, expiration, encryption flag) * @returns Result containing block IDs, parity IDs (if any), and magnet URL * @throws StoreError if storage fails */ storeCBLWithWhitening(cblData: Uint8Array, options?: CBLWhiteningOptions): Promise; /** * Select an existing block from the store as a randomizer, or generate a new one. * * Following OFFSystem principles: * - Preferentially select existing blocks from the cache as randomizers * - Only generate new random blocks if insufficient blocks exist * - All blocks in the store look random and can be used as randomizers * * @param size - The required size of the randomizer block * @returns A Uint8Array containing the randomizer data */ private selectOrGenerateRandomizer; /** * Retrieve and reconstruct a CBL from its whitened components. * * This method: * 1. Retrieves both blocks (using parity recovery if needed) * 2. XORs the blocks to reconstruct the padded CBL * 3. Unpads to get the original CBL data * * @param blockId1 - First block ID (Checksum or hex string) * @param blockId2 - Second block ID (Checksum or hex string) * @param block1ParityIds - Optional parity block IDs for block 1 recovery * @param block2ParityIds - Optional parity block IDs for block 2 recovery * @returns The original CBL data as Uint8Array * @throws StoreError if either block is not found or reconstruction fails */ retrieveCBL(blockId1: Checksum | string, blockId2: Checksum | string, block1ParityIds?: string[], block2ParityIds?: string[]): Promise; /** * Parse a whitened CBL magnet URL and extract component IDs. * * Expected format: magnet:?xt=urn:brightchain:cbl&bs=&b1=&b2=[&p1=][&p2=][&enc=1] * * @param magnetUrl - The magnet URL to parse * @returns Object containing block IDs, block size, parity IDs (if any), and encryption flag * @throws Error if the URL format is invalid */ parseCBLMagnetUrl(magnetUrl: string): CBLMagnetComponents; /** * Generate a magnet URL for a whitened CBL. * * @param blockId1 - First block ID (Checksum or hex string) * @param blockId2 - Second block ID (Checksum or hex string) * @param blockSize - Block size in bytes * @param block1ParityIds - Optional parity block IDs for block 1 * @param block2ParityIds - Optional parity block IDs for block 2 * @param isEncrypted - Whether the CBL is encrypted * @returns The magnet URL string */ generateCBLMagnetUrl(blockId1: Checksum | string, blockId2: Checksum | string, blockSize: number, block1ParityIds?: string[], block2ParityIds?: string[], isEncrypted?: boolean): string; createHeadRegistry(): IHeadRegistry; /** * List all block IDs in the store with optional cursor-based pagination. */ listBlockIds(options?: { limit?: number; cursor?: string; }): AsyncIterable; /** * Get the total number of blocks in the store. */ getBlockCount(): Promise; } //# sourceMappingURL=memoryBlockStore.d.ts.map