# @dstanesc/store-chunky-bytes

> Building block for content addressable persistence. Provides the ability to persist, modify and retrieve large byte-arrays or fragments thereof

Latest version **0.0.13** (published 2022-11-11) · (Apache-2.0 AND MIT) license · 0 weekly downloads

## Install

```sh
npm install @dstanesc/store-chunky-bytes
pnpm add @dstanesc/store-chunky-bytes
yarn add @dstanesc/store-chunky-bytes
bun add @dstanesc/store-chunky-bytes
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.13 |
| Published | 2022-11-11 |
| First published | 2022-08-30 |
| Weekly downloads | 0 |
| License | (Apache-2.0 AND MIT) |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 366.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | dstanesc |
| Maintainers | dstanesc |

## Links

- npm: https://www.npmjs.com/package/@dstanesc/store-chunky-bytes
- Repository: https://github.com/dstanesc/store-chunky-bytes
- Issues: https://github.com/dstanesc/store-chunky-bytes/issues
- npm.io page: https://npm.io/package/@dstanesc/store-chunky-bytes

## Dependencies (1)

- [binary-search-bounds](https://npm.io/package/binary-search-bounds.md) ^2.0.5

## Recent versions

- 0.0.13 (latest) — 2022-11-11
- 0.0.12 — 2022-11-11
- 0.0.11 — 2022-11-11
- 0.0.10 — 2022-11-11
- 0.0.9 — 2022-11-11
- 0.0.8 — 2022-09-30
- 0.0.7 — 2022-09-12
- 0.0.6 — 2022-09-09
- 0.0.5 — 2022-09-08
- 0.0.4 — 2022-09-08
- 0.0.3 — 2022-09-05
- 0.0.2 — 2022-09-05
- 0.0.1 — 2022-08-30

## README

# 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:

1. __Persist__ large byte arrays, across multiple chunks, in the blob store of your choice (memory, cloud, ipfs, etc.) 
2. __Modify__ the persisted byte arrays, O(n) efficiency where n is the number of impacted chunks
3. __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](https://www.npmjs.com/package/@dstanesc/wasm-chunking-webpack-eval) (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

```js
import { chunkyStore } from '@dstanesc/store-chunky-bytes'
import { codec, blockStore, chunkerFactory } from './util'

const buf = ...

// configure utility functions
const { get, put } = blockStore()
const { encode, decode } = codec()
const { fastcdc, buzhash } = chunkerFactory({ fastAvgSize: 1024 * 16, buzHash: 15 })

// chunky store functionality
const { create, read } = chunkyStore()

// create blocks and store them in the block store
// the root is the cryptographic handle to the logical buffer and needs preserved for later access
const { root, blocks } = await create({ buf, chunk, encode }) 
for (const block of blocks) await put(block)

const startOffset = ...
const sliceLength = ...

// extract any slice of data 
const recordBytes = await read(startOffset, sliceLength, { root, decode, get })

// read all blocks, ie full byte array
const allBytes = await readAll({ root, decode, get })
```

### Append

```js
const buf2 = ... // byte array to append

// append additional data
// same chunking algorithm as in the creation phase required
// mandatory content-defined chunking algorithm (eg. fastcdc. buzhash, etc.)
// returns a new root and the new blocks
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 = ...

// extract any slice of data from the combined byte arrays using the second root
const recordBytes = await read(startOffset, sliceLength, { root: appendRoot, decode, get })
```

### Update

```js
const buf2 = ... // byte array to replace original section

// update original data 
// same chunking algorithm as in the creation phase required
// mandatory content-defined chunking algorithm (eg. fastcdc. buzhash, etc.)
// returns a new root and the new blocks
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

```js
const buf2 = ... // append buffer
const buf3 = ... // update buffer 1
const buf3 = ... // update buffer 2
// combines append and multiple update operations 
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

```js
const startOffset = ...
const sliceLength = ...

// delete a slice from original data 
// same chunking algorithm as in the creation phase required
// mandatory content-defined chunking algorithm (eg. fastcdc. buzhash, etc.)
// returns a new root and the new blocks
const { root: deleteRoot, index: deleteIndex, blocks: deleteBlocks } = await remove({ root, decode, get }, { chunk: fastcdc, encode }, startOffset, sliceLength)
```

For more details see the [remove tests](https://github.com/dstanesc/store-chunky-bytes/blob/002b19771eebe7b573b0f8cc123d889d5a4413d2/src/__tests__/chunky-delete.test.ts#L22)

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](https://github.com/dstanesc/store-chunky-bytes/blob/main/src/__tests__/util.ts) offer basic functionality for reuse and extension.

## Build

```sh
npm run clean
npm install
npm run build
npm run test
```

## Licenses

Licensed under either [Apache](./LICENSE-APACHE) or [MIT](./LICENSE-MIT) at your option.

---
_Source: https://npm.io/package/@dstanesc/store-chunky-bytes · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
