JavaScript implementation of hamt for use in sharding
> npm install hamt-sharding
import { createHAMT } from 'hamt-sharding'
import crypto from 'crypto-promise'
// decide how to hash buffers made from keys, can return a Promise
const hashFn = async (buf) => {
return crypto
.createHash('sha256')
.update(buf)
.digest()
}
const bucket = createHAMT({
hashFn: hashFn
})
await bucket.put('key', 'value')
const output = await bucket.get('key')
// output === 'value'
import { createHAMT } from 'hamt-sharding'
import { createHAMT } from 'hamt-sharding'
const bucket = createHAMT({...})
await bucket.put('key', 'value')
import { createHAMT } from 'hamt-sharding'
const bucket = createHAMT({...})
await bucket.put('key', 'value')
console.info(await bucket.get('key')) // 'value'
import { createHAMT } from 'hamt-sharding'
const bucket = createHAMT({...})
await bucket.put('key', 'value')
await bucket.del('key', 'value')
console.info(await bucket.get('key')) // undefined
import { createHAMT } from 'hamt-sharding'
const bucket = createHAMT({...})
console.info(bucket.leafCount()) // 0
await bucket.put('key', 'value')
console.info(bucket.leafCount()) // 1
import { createHAMT } from 'hamt-sharding'
const bucket = createHAMT({...})
console.info(bucket.childrenCount()) // 0
await bucket.put('key', 'value')
console.info(bucket.childrenCount()) // 234 -- dependent on hashing algorithm
import { createHAMT } from 'hamt-sharding'
const bucket = createHAMT({...})
await bucket.put('key', 'value')
for await (const child of bucket.eachLeafSeries()) {
console.info(child.value) // 'value'
}
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.