/** * Batch Write Coalescing for Storage Backends * * Buffers writes in memory and flushes them in batches to reduce I/O operations. * This is especially beneficial for: * - OPFS where each write has overhead * - IndexedDB where transactions have cost * - Network storage where round-trips are expensive * * Features: * - Configurable flush thresholds (count and size) * - Automatic flushing when thresholds are reached * - Explicit flush for durability guarantees * - Coalesces multiple writes to the same key */ import type { StorageBackend } from './StorageBackend.js'; export interface BatchWriterOptions { /** Maximum number of pending writes before auto-flush (default: 100) */ maxPendingWrites?: number; /** Maximum total size of pending data in bytes before auto-flush (default: 1MB) */ maxPendingBytes?: number; /** Auto-flush interval in milliseconds (0 = disabled, default: 0) */ autoFlushInterval?: number; } /** * BatchWriter - Coalesces multiple writes into batched flushes */ export declare class BatchWriter { private backend; private pendingWrites; private pendingAppends; private pendingBytes; private maxPendingWrites; private maxPendingBytes; private autoFlushInterval; private flushTimer; private isFlushing; private flushPromise; /** * Serializes mutations, reads, deletes, and flushes. A delete must not be * able to race a flush that already captured the same key, otherwise the * captured write can be applied after the delete and resurrect the key. */ private operationLock; private backendQueue; private keyGeneration; constructor(backend: StorageBackend, options?: BatchWriterOptions); private withOperationLock; private bumpKeyGeneration; /** Queue a backend operation without allowing a rejected operation to * poison subsequent operations. The returned promise still reports the * operation's own error to its caller. */ private enqueueBackendOperation; /** * Write data to a key (buffered) * Multiple writes to the same key will coalesce to the last value */ write(key: string, data: ArrayBuffer | Uint8Array): Promise; /** * Append data to a key (buffered) * Multiple appends to the same key will be concatenated */ append(key: string, data: ArrayBuffer | Uint8Array): Promise; /** * Delete a key (buffered) * Clears any pending writes/appends for this key */ delete(key: string): Promise; /** * Read data from a key * Returns pending data if available, otherwise reads from backend */ read(key: string): Promise; /** * Check if thresholds are exceeded and flush if needed */ private thresholdsExceeded; /** * Flush all pending writes to the backend */ flush(): Promise; private flushInternal; private doFlush; /** * Get statistics about pending writes */ getStats(): { pendingWrites: number; pendingAppends: number; pendingBytes: number; maxPendingWrites: number; maxPendingBytes: number; }; /** * Check if there are pending writes */ hasPendingWrites(): boolean; /** * Start auto-flush timer */ private startAutoFlush; /** * Stop auto-flush timer */ stopAutoFlush(): void; /** * Close the batch writer, flushing any pending writes */ close(): Promise; /** * Get the underlying storage backend */ getBackend(): StorageBackend; } /** * Create a batch writer that wraps an existing storage backend */ export declare function createBatchWriter(backend: StorageBackend, options?: BatchWriterOptions): BatchWriter; //# sourceMappingURL=BatchWriter.d.ts.map