Store Chunky Bytes
Current library is a building block for content addressable persistence. Provides ability to persist, modify and retrieve large byte-arrays or fragments thereof. This is work in progress, features are still added, contributions welcome.
Current features:
- Persist large byte arrays, across multiple chunks, in the blob store of your choice (memory, cloud, ipfs, etc.)
- Modify the persisted byte arrays, O(n) efficiency where n is the number of impacted chunks
- Retrieve slices of data, based on the offset, independent of the individual chunk boundaries
Persisted data is content addressable, hence immutable and versionable.
Used in conjunction with content defined chunkers (eg. Fastcdc and Buzhash) offers chunk deduplication across versions of data.
The intended usage is to persist and access collections of fixed size records. In this case the records can be retrieved extremely efficient (O1) based on the offsets computed externally using mathematic formulas rather than scanning the data.
Usage
Create & Retrieve
import { chunkyStore } from '@dstanesc/store-chunky-bytes'
import { codec, blockStore, chunkerFactory } from './util'
const buf = ...
const { get, put } = blockStore()
const { encode, decode } = codec()
const { fastcdc, buzhash } = chunkerFactory({ fastAvgSize: 1024 * 16, buzHash: 15 })
const { create, read } = chunkyStore()
const { root, blocks } = await create({ buf, chunk, encode })
for (const block of blocks) await put(block)
const startOffset = ...
const sliceLength = ...
const recordBytes = await read(startOffset, sliceLength, { root, decode, get })
const allBytes = await readAll({ root, decode, get })
Append
const buf2 = ...
const { root: appendRoot, blocks: appendBlocks } = await append({ root: origRoot, decode, get }, { buf: buf2, chunk: fastcdc, encode })
for (const block of appendBlocks) await put(block)
const startOffset = ...
const sliceLength = ...
const recordBytes = await read(startOffset, sliceLength, { root: appendRoot, decode, get })
Update
const buf2 = ...
const { root: updateRoot, index: updateIndex, blocks: updateBlocks } = await update({ root, decode, get }, { buf: buf2, chunk: fastcdc, encode }, RECORD_UPDATE_OFFSET)
Note: Update alg. tuned heuristically for best stability (ie. compare chunk offsets after update w/ full chunking of the updated buffer) results w/ fastcdc
.
Bulk
Note the additional put
argument to store the intermediate blocks
const buf2 = ...
const buf3 = ...
const buf3 = ...
const { root: bulkRoot, index: bulkIndex, blocks: bulkBlocks } = await bulk({ root: origRoot, decode, get, put }, { chunk: fastcdc, encode }, buf2, [{ updateBuffer: buf3, updateStartOffset: RECORD_UPDATE_OFFSET }, { updateBuffer: buf4, updateStartOffset: RECORD_UPDATE_NEXT_OFFSET }])
Remove
const startOffset = ...
const sliceLength = ...
const { root: deleteRoot, index: deleteIndex, blocks: deleteBlocks } = await remove({ root, decode, get }, { chunk: fastcdc, encode }, startOffset, sliceLength)
For more details see the remove tests
To keep library size, dependencies and flexibility under control the blockStore
, the content identifier encode/decode
and the chunking
functionality are not part of the library. However, all batteries are included. The test utilities offer basic functionality for reuse and extension.
Build
npm run clean
npm install
npm run build
npm run test
Licenses
Licensed under either Apache or MIT at your option.