import { Member, type PlatformID } from '@digitaldefiance/ecies-lib'; import { ConstituentBlockListBlock } from '../blocks/cbl'; import { ExtendedCBL } from '../blocks/extendedCbl'; import { InMemoryBlockTuple } from '../blocks/memoryTuple'; import { RandomBlock } from '../blocks/random'; import { WhitenedBlock } from '../blocks/whitened'; import { BlockSize } from '../enumerations/blockSize'; import { Checksum } from '../types/checksum'; import type { CBLService } from './cblService'; import type { ChecksumService } from './checksum.service'; /** * Result of a CopyOnWrite operation. * Contains the new CBL and metadata about what changed. */ export interface CopyOnWriteResult { /** The new CBL referencing both reused and new blocks */ cbl: ConstituentBlockListBlock | ExtendedCBL; /** Number of tuples reused from the original CBL */ reusedTupleCount: number; /** Number of new tuples created for changed data */ newTupleCount: number; /** Total number of tuples in the new CBL */ totalTupleCount: number; } /** * Options for the CopyOnWrite operation. */ export interface CopyOnWriteOptions { /** If true, create an ExtendedCBL with file metadata */ createExtendedCbl?: boolean; /** File name for ExtendedCBL */ fileName?: string; /** MIME type for ExtendedCBL */ mimeType?: string; } /** * CopyOnWriteService provides efficient versioning for CBL-based data. * * When data changes, instead of re-creating all blocks from scratch, this service: * 1. Reads the new data in block-sized chunks * 2. Compares each chunk against the corresponding original block data * (recovered by XORing the tuple's constituent blocks) * 3. Reuses the original tuple addresses for unchanged blocks * 4. Creates new tuples only for blocks whose data has changed * 5. Produces a new CBL that references both old (reused) and new block addresses * * This is analogous to copy-on-write in operating systems: we share unchanged * pages and only allocate new storage for modified pages. * * The tuple size (typically 3) means each "logical block" in the CBL is * represented by `tupleSize` addresses. When we say "reuse a tuple," we * mean we copy all `tupleSize` addresses from the old CBL into the new one. * * @typeParam TID - The platform ID type (defaults to Uint8Array) */ export declare class CopyOnWriteService { private readonly checksumService; private readonly cblService; constructor(checksumService?: ChecksumService, cblService?: CBLService); /** * Create a new version of a CBL by applying changes from new data, * reusing unchanged blocks from the original CBL. * * @param originalCbl - The existing CBL to base the new version on * @param newData - The complete new data as a Uint8Array * @param creator - The member creating the new version * @param blockSize - The block size to use (must match the original CBL's block size) * @param getBlockData - Function to retrieve raw block data by checksum from the store * @param whitenedBlockSource - Function to get whitened blocks for new tuples * @param randomBlockSource - Function to get random blocks for new tuples * @param persistTuple - Function to persist newly created tuples * @param options - Optional settings for extended CBL creation * @returns The new CBL and statistics about reuse */ newVersion(originalCbl: ConstituentBlockListBlock, newData: Uint8Array, creator: Member, blockSize: BlockSize, getBlockData: (checksum: Checksum) => Promise, whitenedBlockSource: () => WhitenedBlock | undefined, randomBlockSource: () => RandomBlock, persistTuple: (tuple: InMemoryBlockTuple) => Promise, options?: CopyOnWriteOptions): Promise>; /** * Recover the original plaintext data from a tuple by XORing all * constituent blocks together. * * In the OFF system, a tuple of `tupleSize` blocks is stored such that * XORing all blocks together recovers the original data: * plaintext = block[0] XOR block[1] XOR ... XOR block[tupleSize-1] * * @param tupleAddresses - The checksums of the blocks in the tuple * @param getBlockData - Function to retrieve raw block data by checksum * @returns The recovered plaintext data */ private recoverTupleData; /** * Compare two block-sized byte arrays for equality. * Uses constant-length comparison (always checks all bytes). */ private blocksEqual; /** * Create a new tuple for a data block by XORing it with whitener/random blocks, * then persist the tuple. * * @returns The checksums of all blocks in the new tuple (in order) */ private createNewTuple; /** * Convert an array of Checksums to a concatenated Uint8Array buffer. */ private addressesToBuffer; /** * Helper to convert a Readable stream to Uint8Array. */ private streamToUint8Array; } //# sourceMappingURL=copyOnWrite.service.d.ts.map