Skip to main content

DiskChunkSource

DiskChunkSource is an implementation of ChunkSource that stores and retrieves chunks directly from the file system (temporal).


Constructor​

ParameterTypeDescription
cacheDirstringDirectory where chunks will be stored.

Methods​

MethodReturnsDescription
getChunk(hash)Promise<Buffer>Returns a single chunk by its hash.
getChunks(hashes, options?)Promise<Map<string, Buffer>>Method to fetch multiple chunks concurrently.
hasChunk(hash)booleanChecks if a chunk exists in disk.
setChunk(hash, data)voidAdds or updates a chunk in disk.
clear()voidClears all the chunk stored cache data in disk.

Usage example​

import { DiskChunkSource } from 'rac-delta';
import { Readable } from 'stream';

const source = new DiskChunkSource('./cache');

// Store a Buffer chunk
await source.setChunk('abcd1234', Buffer.from('Hello world'));

// Store a chunk from a stream
const stream = Readable.from(['streamed data']);
await source.setChunk('ef456', stream);

// Retrieve a chunk
const chunk = await source.getChunk('abcd1234');
console.log(chunk.toString()); // 'Hello world'

// Check existence
const exists = await source.hasChunk('abcd1234'); // true

// Retrieve multiple chunks
const results = await source.getChunks(['abcd1234', 'ef456']);
console.log(results.get('ef456')?.toString()); // 'streamed data'

// Clear all cached chunks
await source.clear();