You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

chunky-trees

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chunky-trees - npm Package Compare versions

Comparing version
0.3.2
to
1.0.0
+21
README.md
# Chunky Trees
While this library has 100% test coverage and is relatively stable, it's
not recommended for broad use. The implementation internals are all very
exposed and have not been properly documented. Without thorough documentation
there's a lot of mistakes consumers will make regarding caching and block
storage.
Some time in the near future I will fully document the library.
# Notes
## ranges
Range queries match `>=` the `start` key and `<` the `end` key.
In other words, ranges do not include matches against the end key
but do match against the start key. This is so that more advanced
range queries can be built with only appending to keys rather than
needing to do more advanced key modifications to reduce the closing
match.
+1
-1
'use strict';
var utils = require('../src/utils.js');
var base = require('../src/base.js');
var utils = require('../src/utils.js');
var assert = require('assert');

@@ -6,0 +6,0 @@

@@ -130,3 +130,4 @@ 'use strict';

for (const cid of list) {
assert.deepStrictEqual(await root.get(cid), cid);
const {result} = await root.get(cid);
assert.deepStrictEqual(result, cid);
}

@@ -133,0 +134,0 @@ });

@@ -247,3 +247,3 @@ 'use strict';

}).filter(x => x);
const result = await root.get(key[0]);
const {result} = await root.get(key[0]);
assert.deepStrictEqual(result, expected);

@@ -257,5 +257,7 @@ }

});
let [result] = await leaf.get('zz');
let {
result: [result]
} = await leaf.get('zz');
assert.deepStrictEqual(result.id, 9);
const results = await leaf.range('z', 'zzzzz');
const {result: results} = await leaf.range('z', 'zzzzz');
assert.deepStrictEqual(results.length, 1);

@@ -288,9 +290,10 @@ result = results[0];

};
let entries = await root.range('b', 'z');
const range = async (...args) => (await root.range(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.range('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.range('a', 'zz');
entries = await range('a', 'zz');
verify(entries);
entries = await root.range('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 5);

@@ -314,3 +317,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -359,8 +362,9 @@ });

const ids = results => results.map(({id}) => id);
assert.deepStrictEqual(ids(await root.get('a')), [
const getval = async k => (await root.get(k)).result;
assert.deepStrictEqual(ids(await getval('a')), [
0,
40
]);
assert.deepStrictEqual(ids(await root.get('b')), [1]);
assert.deepStrictEqual(ids(await root.get('z')), [41]);
assert.deepStrictEqual(ids(await getval('b')), [1]);
assert.deepStrictEqual(ids(await getval('z')), [41]);
bulk = [

@@ -387,4 +391,4 @@ {

await Promise.all(newBlocks.map(b => put(b)));
assert.deepStrictEqual(ids(await newRoot.get('zz')), [42]);
assert.deepStrictEqual(ids((await newRoot.get('zz')).result), [42]);
});
});

@@ -194,3 +194,3 @@ 'use strict';

for (const {key} of list) {
assert.deepStrictEqual(await root.get(key), key.length);
assert.deepStrictEqual((await root.get(key)).result, key.length);
}

@@ -210,3 +210,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
'a',

@@ -221,3 +221,3 @@ 'zz'

assert.deepStrictEqual(zz.value, 2);
const values = await root.getMany([
const {result: values} = await root.getMany([
'a',

@@ -248,9 +248,10 @@ 'zz'

};
let entries = await root.getRangeEntries('b', 'z');
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.getRangeEntries('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.getRangeEntries('a', 'zz');
entries = await range('a', 'zz');
verify(entries, 0, 9);
entries = await root.getRangeEntries('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 3);

@@ -275,3 +276,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -296,3 +297,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -311,4 +312,5 @@ const bulk = [

await Promise.all(blocks.map(block => put(block)));
assert.deepStrictEqual(await root.get('dd'), 2);
assert.deepStrictEqual(await root.get('d'), -1);
const _get = async k => (await root.get(k)).result;
assert.deepStrictEqual(await _get('dd'), 2);
assert.deepStrictEqual(await _get('d'), -1);
const expected = [

@@ -353,3 +355,3 @@ [

for (const [key, value] of expected) {
assert.deepStrictEqual(await root.get(key), value);
assert.deepStrictEqual(await _get(key), value);
}

@@ -394,3 +396,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -406,3 +408,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -451,3 +454,3 @@ }

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -467,3 +470,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -501,3 +505,3 @@ }

while (front.length) {
const entries = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
const {result: entries} = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
assert.deepStrictEqual(entries.map(({key}) => key), front);

@@ -507,9 +511,9 @@ front.shift();

while (front.length) {
const entries = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
const {result: entries} = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
assert.deepStrictEqual(entries.map(({key}) => key), back);
back.pop();
}
let entries = await last.getRangeEntries('9999999', '9999999999999999');
let {result: entries} = await last.getRangeEntries('9999999', '9999999999999999');
assert.deepStrictEqual(entries, []);
entries = await last.getRangeEntries('.', '.');
entries = (await last.getRangeEntries('.', '.')).result;
});

@@ -565,5 +569,5 @@ it('getEntry', async () => {

}
let result = await root.get('c');
assert.deepStrictEqual(result, 1);
result = await root.getMany([
const res = await root.get('c');
assert.deepStrictEqual(res.result, 1);
const {result} = await root.getMany([
'c',

@@ -587,4 +591,4 @@ 'cc',

await Promise.all(blocks.map(block => put(block)));
assert.deepStrictEqual(await rr.get('aaa'), 3);
assert.deepStrictEqual((await rr.get('aaa')).result, 3);
});
});

@@ -189,3 +189,3 @@ 'use strict';

for (const {key} of list) {
assert.deepStrictEqual(await root.get(key), v);
assert.deepStrictEqual((await root.get(key)).result, v);
}

@@ -205,3 +205,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
2,

@@ -216,3 +216,3 @@ 10000

assert.deepStrictEqual(b.value, v);
const values = await root.getMany([
const {result: values} = await root.getMany([
2,

@@ -243,9 +243,10 @@ 10000

};
let entries = await root.getRangeEntries(2, 400);
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range(2, 400);
verify(entries, 1, 9);
entries = await root.getRangeEntries(0, 99999);
entries = await range(0, 99999);
verify(entries);
entries = await root.getRangeEntries(1, 10000);
entries = await range(1, 10000);
verify(entries, 0, 9);
entries = await root.getRangeEntries(1, 15);
entries = await range(1, 15);
verify(entries, 0, 3);

@@ -279,3 +280,3 @@ assert.deepStrictEqual(await root.getLength(), 10001);

await Promise.all(blocks.map(put));
assert.deepStrictEqual(await lRoot.get(10001), 'test');
assert.deepStrictEqual((await lRoot.get(10001)).result, 'test');
const {

@@ -289,4 +290,4 @@ blocks: _blocks,

await Promise.all(_blocks.map(put));
assert.deepStrictEqual(await bRoot.get(10001), 'test2');
assert.deepStrictEqual((await bRoot.get(10001)).result, 'test2');
});
});
'use strict';
var utils = require('../src/utils.js');
var base = require('../src/base.js');
var utils = require('../src/utils.js');
var assert = require('assert');

@@ -6,0 +6,0 @@

@@ -130,3 +130,4 @@ 'use strict';

for (const cid of list) {
assert.deepStrictEqual(await root.get(cid), cid);
const {result} = await root.get(cid);
assert.deepStrictEqual(result, cid);
}

@@ -133,0 +134,0 @@ });

@@ -247,3 +247,3 @@ 'use strict';

}).filter(x => x);
const result = await root.get(key[0]);
const {result} = await root.get(key[0]);
assert.deepStrictEqual(result, expected);

@@ -257,5 +257,7 @@ }

});
let [result] = await leaf.get('zz');
let {
result: [result]
} = await leaf.get('zz');
assert.deepStrictEqual(result.id, 9);
const results = await leaf.range('z', 'zzzzz');
const {result: results} = await leaf.range('z', 'zzzzz');
assert.deepStrictEqual(results.length, 1);

@@ -288,9 +290,10 @@ result = results[0];

};
let entries = await root.range('b', 'z');
const range = async (...args) => (await root.range(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.range('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.range('a', 'zz');
entries = await range('a', 'zz');
verify(entries);
entries = await root.range('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 5);

@@ -314,3 +317,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -359,8 +362,9 @@ });

const ids = results => results.map(({id}) => id);
assert.deepStrictEqual(ids(await root.get('a')), [
const getval = async k => (await root.get(k)).result;
assert.deepStrictEqual(ids(await getval('a')), [
0,
40
]);
assert.deepStrictEqual(ids(await root.get('b')), [1]);
assert.deepStrictEqual(ids(await root.get('z')), [41]);
assert.deepStrictEqual(ids(await getval('b')), [1]);
assert.deepStrictEqual(ids(await getval('z')), [41]);
bulk = [

@@ -387,4 +391,4 @@ {

await Promise.all(newBlocks.map(b => put(b)));
assert.deepStrictEqual(ids(await newRoot.get('zz')), [42]);
assert.deepStrictEqual(ids((await newRoot.get('zz')).result), [42]);
});
});

@@ -194,3 +194,3 @@ 'use strict';

for (const {key} of list) {
assert.deepStrictEqual(await root.get(key), key.length);
assert.deepStrictEqual((await root.get(key)).result, key.length);
}

@@ -210,3 +210,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
'a',

@@ -221,3 +221,3 @@ 'zz'

assert.deepStrictEqual(zz.value, 2);
const values = await root.getMany([
const {result: values} = await root.getMany([
'a',

@@ -248,9 +248,10 @@ 'zz'

};
let entries = await root.getRangeEntries('b', 'z');
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.getRangeEntries('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.getRangeEntries('a', 'zz');
entries = await range('a', 'zz');
verify(entries, 0, 9);
entries = await root.getRangeEntries('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 3);

@@ -275,3 +276,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -296,3 +297,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -311,4 +312,5 @@ const bulk = [

await Promise.all(blocks.map(block => put(block)));
assert.deepStrictEqual(await root.get('dd'), 2);
assert.deepStrictEqual(await root.get('d'), -1);
const _get = async k => (await root.get(k)).result;
assert.deepStrictEqual(await _get('dd'), 2);
assert.deepStrictEqual(await _get('d'), -1);
const expected = [

@@ -353,3 +355,3 @@ [

for (const [key, value] of expected) {
assert.deepStrictEqual(await root.get(key), value);
assert.deepStrictEqual(await _get(key), value);
}

@@ -394,3 +396,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -406,3 +408,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -451,3 +454,3 @@ }

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -467,3 +470,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -501,3 +505,3 @@ }

while (front.length) {
const entries = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
const {result: entries} = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
assert.deepStrictEqual(entries.map(({key}) => key), front);

@@ -507,9 +511,9 @@ front.shift();

while (front.length) {
const entries = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
const {result: entries} = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
assert.deepStrictEqual(entries.map(({key}) => key), back);
back.pop();
}
let entries = await last.getRangeEntries('9999999', '9999999999999999');
let {result: entries} = await last.getRangeEntries('9999999', '9999999999999999');
assert.deepStrictEqual(entries, []);
entries = await last.getRangeEntries('.', '.');
entries = (await last.getRangeEntries('.', '.')).result;
});

@@ -565,5 +569,5 @@ it('getEntry', async () => {

}
let result = await root.get('c');
assert.deepStrictEqual(result, 1);
result = await root.getMany([
const res = await root.get('c');
assert.deepStrictEqual(res.result, 1);
const {result} = await root.getMany([
'c',

@@ -587,4 +591,4 @@ 'cc',

await Promise.all(blocks.map(block => put(block)));
assert.deepStrictEqual(await rr.get('aaa'), 3);
assert.deepStrictEqual((await rr.get('aaa')).result, 3);
});
});

@@ -189,3 +189,3 @@ 'use strict';

for (const {key} of list) {
assert.deepStrictEqual(await root.get(key), v);
assert.deepStrictEqual((await root.get(key)).result, v);
}

@@ -205,3 +205,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
2,

@@ -216,3 +216,3 @@ 10000

assert.deepStrictEqual(b.value, v);
const values = await root.getMany([
const {result: values} = await root.getMany([
2,

@@ -243,9 +243,10 @@ 10000

};
let entries = await root.getRangeEntries(2, 400);
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range(2, 400);
verify(entries, 1, 9);
entries = await root.getRangeEntries(0, 99999);
entries = await range(0, 99999);
verify(entries);
entries = await root.getRangeEntries(1, 10000);
entries = await range(1, 10000);
verify(entries, 0, 9);
entries = await root.getRangeEntries(1, 15);
entries = await range(1, 15);
verify(entries, 0, 3);

@@ -279,3 +280,3 @@ assert.deepStrictEqual(await root.getLength(), 10001);

await Promise.all(blocks.map(put));
assert.deepStrictEqual(await lRoot.get(10001), 'test');
assert.deepStrictEqual((await lRoot.get(10001)).result, 'test');
const {

@@ -289,4 +290,4 @@ blocks: _blocks,

await Promise.all(_blocks.map(put));
assert.deepStrictEqual(await bRoot.get(10001), 'test2');
assert.deepStrictEqual((await bRoot.get(10001)).result, 'test2');
});
});

@@ -6,2 +6,3 @@ 'use strict';

var block = require('multiformats/block');
var utils = require('./utils.js');

@@ -129,3 +130,11 @@ class Entry {

}
async getEntry(key) {
async getEntry(key, cids = new utils.CIDCounter()) {
const result = await this._getEntry(key, cids);
return {
result,
cids: await cids.all()
};
}
async _getEntry(key, cids) {
cids.add(this);
let node = this;

@@ -138,2 +147,3 @@ while (!node.isLeaf) {

node = await this.getNode(await entry.address);
cids.add(node);
}

@@ -146,3 +156,11 @@ const result = node.entryList.find(key, this.compare);

}
getAllEntries() {
async getAllEntries(cids = new utils.CIDCounter()) {
const result = await this._getAllEntries(cids);
return {
result,
cids: await cids.all()
};
}
_getAllEntries(cids) {
cids.add(this);
if (this.isLeaf) {

@@ -152,7 +170,15 @@ return this.entryList.entries;

const {entries} = this.entryList;
const mapper = async entry => this.getNode(await entry.address).then(node => node.getAllEntries());
const mapper = async entry => this.getNode(await entry.address).then(node => node._getAllEntries(cids));
return Promise.all(entries.map(mapper)).then(results => results.flat());
}
}
async getEntries(keys, sorted = false) {
async getEntries(keys, sorted = false, cids = new utils.CIDCounter()) {
const result = await this._getEntries(keys, sorted, cids);
return {
result,
cids: await cids.all()
};
}
async _getEntries(keys, sorted, cids) {
cids.add(this);
if (!sorted)

@@ -167,3 +193,3 @@ keys = keys.sort(this.compare);

const p = this.getNode(await entry.address);
entries.push(p.then(node => node.getEntries(keys.reverse(), true)));
entries.push(p.then(node => node._getEntries(keys.reverse(), true, cids)));
}

@@ -173,3 +199,11 @@ entries = await Promise.all(entries);

}
getRangeEntries(start, end) {
async getRangeEntries(start, end, cids = new utils.CIDCounter()) {
const result = await this._getRangeEntries(start, end, cids);
return {
result,
cids: await cids.all()
};
}
_getRangeEntries(start, end, cids) {
cids.add(this);
const {entries} = this.entryList.findRange(start, end, this.compare);

@@ -188,3 +222,3 @@ if (this.isLeaf) {

const thenRange = async entry => this.getNode(await entry.address).then(node => {
return node.getRangeEntries(start, end);
return node._getRangeEntries(start, end, cids);
});

@@ -196,4 +230,4 @@ const results = [thenRange(entries.shift())];

while (entries.length) {
const thenAll = async entry => this.getNode(await entry.address).then(node => {
return node.getAllEntries();
const thenAll = async entry => this.getNode(await entry.address).then(async node => {
return node._getAllEntries(cids);
});

@@ -444,4 +478,10 @@ results.push(thenAll(entries.shift()));

async get(key) {
const entry = await this.getEntry(key);
return entry.key;
const {
result: entry,
cids
} = await this.getEntry(key);
return {
result: entry.key,
cids
};
}

@@ -448,0 +488,0 @@ async encode() {

@@ -5,4 +5,4 @@ 'use strict';

var utils = require('./utils.js');
var base = require('./base.js');
var utils = require('./utils.js');

@@ -9,0 +9,0 @@ const compare = ({bytes: a}, {bytes: b}) => utils.binaryCompare(a, b);

@@ -25,7 +25,13 @@ 'use strict';

];
const entries = await node.getRangeEntries(start, end);
return entries.map(entry => ({
id: entry.key[1],
row: entry.value
}));
const {
result: entries,
cids
} = await node.getRangeEntries(start, end);
return {
result: entries.map(entry => ({
id: entry.key[1],
row: entry.value
})),
cids
};
};

@@ -41,4 +47,7 @@ const getRange = async (node, start, end) => {

];
const entries = await node.getRangeEntries(start, end);
return entries.map(entry => {
const {
result: entries,
cids
} = await node.getRangeEntries(start, end);
const result = entries.map(entry => {
const [id, key] = entry.key;

@@ -51,2 +60,6 @@ return {

});
return {
result,
cids
};
};

@@ -53,0 +66,0 @@ class DBIndexLeaf extends map.MapLeaf {

@@ -5,4 +5,4 @@ 'use strict';

var utils = require('./utils.js');
var base = require('./base.js');
var utils = require('./utils.js');

@@ -37,8 +37,20 @@ class MapEntry extends base.Entry {

const getValue = async (node, key) => {
const entry = await node.getEntry(key);
return entry.value;
const {
result: entry,
cids
} = await node.getEntry(key);
return {
result: entry.value,
cids
};
};
const getManyValues = async (node, keys) => {
const entries = await node.getEntries(keys);
return entries.map(entry => entry.value);
const {
result: entries,
cids
} = await node.getEntries(keys);
return {
result: entries.map(entry => entry.value),
cids
};
};

@@ -45,0 +57,0 @@ class MapLeaf extends base.IPLDLeaf {

@@ -56,3 +56,25 @@ 'use strict';

};
class CIDCounter {
constructor() {
this._cids = new Set();
}
add(node) {
if (!node.address) {
throw new Error('Cannot add node without address');
}
if (node.address.then) {
const p = node.address.then(cid => this._cids.add(cid.toString()));
this._cids.add(p);
p.then(() => this._cids.delete(p));
} else {
this._cids.add(node.address.toString());
}
}
async all() {
await Promise.all([...this._cids]);
return this._cids;
}
}
exports.CIDCounter = CIDCounter;
exports.bf = bf;

@@ -59,0 +81,0 @@ exports.binaryCompare = binaryCompare;

@@ -111,3 +111,4 @@ import { deepStrictEqual as same } from 'assert';

for (const cid of list) {
same(await root.get(cid), cid);
const {result} = await root.get(cid);
same(result, cid);
}

@@ -114,0 +115,0 @@ });

@@ -225,3 +225,3 @@ import { deepStrictEqual as same } from 'assert';

}).filter(x => x);
const result = await root.get(key[0]);
const {result} = await root.get(key[0]);
same(result, expected);

@@ -235,5 +235,7 @@ }

});
let [result] = await leaf.get('zz');
let {
result: [result]
} = await leaf.get('zz');
same(result.id, 9);
const results = await leaf.range('z', 'zzzzz');
const {result: results} = await leaf.range('z', 'zzzzz');
same(results.length, 1);

@@ -266,9 +268,10 @@ result = results[0];

};
let entries = await root.range('b', 'z');
const range = async (...args) => (await root.range(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.range('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.range('a', 'zz');
entries = await range('a', 'zz');
verify(entries);
entries = await root.range('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 5);

@@ -292,3 +295,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -337,8 +340,9 @@ });

const ids = results => results.map(({id}) => id);
same(ids(await root.get('a')), [
const getval = async k => (await root.get(k)).result;
same(ids(await getval('a')), [
0,
40
]);
same(ids(await root.get('b')), [1]);
same(ids(await root.get('z')), [41]);
same(ids(await getval('b')), [1]);
same(ids(await getval('z')), [41]);
bulk = [

@@ -365,4 +369,4 @@ {

await Promise.all(newBlocks.map(b => put(b)));
same(ids(await newRoot.get('zz')), [42]);
same(ids((await newRoot.get('zz')).result), [42]);
});
});

@@ -178,3 +178,3 @@ import { deepStrictEqual as same } from 'assert';

for (const {key} of list) {
same(await root.get(key), key.length);
same((await root.get(key)).result, key.length);
}

@@ -194,3 +194,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
'a',

@@ -205,3 +205,3 @@ 'zz'

same(zz.value, 2);
const values = await root.getMany([
const {result: values} = await root.getMany([
'a',

@@ -232,9 +232,10 @@ 'zz'

};
let entries = await root.getRangeEntries('b', 'z');
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.getRangeEntries('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.getRangeEntries('a', 'zz');
entries = await range('a', 'zz');
verify(entries, 0, 9);
entries = await root.getRangeEntries('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 3);

@@ -259,3 +260,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -280,3 +281,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -295,4 +296,5 @@ const bulk = [

await Promise.all(blocks.map(block => put(block)));
same(await root.get('dd'), 2);
same(await root.get('d'), -1);
const _get = async k => (await root.get(k)).result;
same(await _get('dd'), 2);
same(await _get('d'), -1);
const expected = [

@@ -337,3 +339,3 @@ [

for (const [key, value] of expected) {
same(await root.get(key), value);
same(await _get(key), value);
}

@@ -378,3 +380,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -390,3 +392,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -435,3 +438,3 @@ }

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -451,3 +454,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -485,3 +489,3 @@ }

while (front.length) {
const entries = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
const {result: entries} = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
same(entries.map(({key}) => key), front);

@@ -491,9 +495,9 @@ front.shift();

while (front.length) {
const entries = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
const {result: entries} = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
same(entries.map(({key}) => key), back);
back.pop();
}
let entries = await last.getRangeEntries('9999999', '9999999999999999');
let {result: entries} = await last.getRangeEntries('9999999', '9999999999999999');
same(entries, []);
entries = await last.getRangeEntries('.', '.');
entries = (await last.getRangeEntries('.', '.')).result;
});

@@ -549,5 +553,5 @@ it('getEntry', async () => {

}
let result = await root.get('c');
same(result, 1);
result = await root.getMany([
const res = await root.get('c');
same(res.result, 1);
const {result} = await root.getMany([
'c',

@@ -571,4 +575,4 @@ 'cc',

await Promise.all(blocks.map(block => put(block)));
same(await rr.get('aaa'), 3);
same((await rr.get('aaa')).result, 3);
});
});

@@ -170,3 +170,3 @@ import { deepStrictEqual as same } from 'assert';

for (const {key} of list) {
same(await root.get(key), v);
same((await root.get(key)).result, v);
}

@@ -186,3 +186,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
2,

@@ -197,3 +197,3 @@ 10000

same(b.value, v);
const values = await root.getMany([
const {result: values} = await root.getMany([
2,

@@ -224,9 +224,10 @@ 10000

};
let entries = await root.getRangeEntries(2, 400);
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range(2, 400);
verify(entries, 1, 9);
entries = await root.getRangeEntries(0, 99999);
entries = await range(0, 99999);
verify(entries);
entries = await root.getRangeEntries(1, 10000);
entries = await range(1, 10000);
verify(entries, 0, 9);
entries = await root.getRangeEntries(1, 15);
entries = await range(1, 15);
verify(entries, 0, 3);

@@ -260,3 +261,3 @@ same(await root.getLength(), 10001);

await Promise.all(blocks.map(put));
same(await lRoot.get(10001), 'test');
same((await lRoot.get(10001)).result, 'test');
const {

@@ -270,4 +271,4 @@ blocks: _blocks,

await Promise.all(_blocks.map(put));
same(await bRoot.get(10001), 'test2');
same((await bRoot.get(10001)).result, 'test2');
});
});

@@ -111,3 +111,4 @@ import { deepStrictEqual as same } from 'assert';

for (const cid of list) {
same(await root.get(cid), cid);
const {result} = await root.get(cid);
same(result, cid);
}

@@ -114,0 +115,0 @@ });

@@ -225,3 +225,3 @@ import { deepStrictEqual as same } from 'assert';

}).filter(x => x);
const result = await root.get(key[0]);
const {result} = await root.get(key[0]);
same(result, expected);

@@ -235,5 +235,7 @@ }

});
let [result] = await leaf.get('zz');
let {
result: [result]
} = await leaf.get('zz');
same(result.id, 9);
const results = await leaf.range('z', 'zzzzz');
const {result: results} = await leaf.range('z', 'zzzzz');
same(results.length, 1);

@@ -266,9 +268,10 @@ result = results[0];

};
let entries = await root.range('b', 'z');
const range = async (...args) => (await root.range(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.range('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.range('a', 'zz');
entries = await range('a', 'zz');
verify(entries);
entries = await root.range('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 5);

@@ -292,3 +295,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -337,8 +340,9 @@ });

const ids = results => results.map(({id}) => id);
same(ids(await root.get('a')), [
const getval = async k => (await root.get(k)).result;
same(ids(await getval('a')), [
0,
40
]);
same(ids(await root.get('b')), [1]);
same(ids(await root.get('z')), [41]);
same(ids(await getval('b')), [1]);
same(ids(await getval('z')), [41]);
bulk = [

@@ -365,4 +369,4 @@ {

await Promise.all(newBlocks.map(b => put(b)));
same(ids(await newRoot.get('zz')), [42]);
same(ids((await newRoot.get('zz')).result), [42]);
});
});

@@ -178,3 +178,3 @@ import { deepStrictEqual as same } from 'assert';

for (const {key} of list) {
same(await root.get(key), key.length);
same((await root.get(key)).result, key.length);
}

@@ -194,3 +194,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
'a',

@@ -205,3 +205,3 @@ 'zz'

same(zz.value, 2);
const values = await root.getMany([
const {result: values} = await root.getMany([
'a',

@@ -232,9 +232,10 @@ 'zz'

};
let entries = await root.getRangeEntries('b', 'z');
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range('b', 'z');
verify(entries, 1, 8);
entries = await root.getRangeEntries('', 'zzz');
entries = await range('', 'zzz');
verify(entries);
entries = await root.getRangeEntries('a', 'zz');
entries = await range('a', 'zz');
verify(entries, 0, 9);
entries = await root.getRangeEntries('a', 'c');
entries = await range('a', 'c');
verify(entries, 0, 3);

@@ -259,3 +260,3 @@ });

};
const entries = await root.getAllEntries();
const {result: entries} = await root.getAllEntries();
verify(entries);

@@ -280,3 +281,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -295,4 +296,5 @@ const bulk = [

await Promise.all(blocks.map(block => put(block)));
same(await root.get('dd'), 2);
same(await root.get('d'), -1);
const _get = async k => (await root.get(k)).result;
same(await _get('dd'), 2);
same(await _get('d'), -1);
const expected = [

@@ -337,3 +339,3 @@ [

for (const [key, value] of expected) {
same(await root.get(key), value);
same(await _get(key), value);
}

@@ -378,3 +380,3 @@ });

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -390,3 +392,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -435,3 +438,3 @@ }

};
const entries = await last.getAllEntries();
const {result: entries} = await last.getAllEntries();
verify(entries);

@@ -451,3 +454,4 @@ const base = last;

await Promise.all(blocks.map(block => put(block)));
verify(await root.getAllEntries());
const {result} = await root.getAllEntries();
verify(result);
i++;

@@ -485,3 +489,3 @@ }

while (front.length) {
const entries = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
const {result: entries} = await last.getRangeEntries(front[0], front[front.length - 1] + '999');
same(entries.map(({key}) => key), front);

@@ -491,9 +495,9 @@ front.shift();

while (front.length) {
const entries = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
const {result: entries} = await last.getRangeEntries(back[0], back[back.length - 1] + '.');
same(entries.map(({key}) => key), back);
back.pop();
}
let entries = await last.getRangeEntries('9999999', '9999999999999999');
let {result: entries} = await last.getRangeEntries('9999999', '9999999999999999');
same(entries, []);
entries = await last.getRangeEntries('.', '.');
entries = (await last.getRangeEntries('.', '.')).result;
});

@@ -549,5 +553,5 @@ it('getEntry', async () => {

}
let result = await root.get('c');
same(result, 1);
result = await root.getMany([
const res = await root.get('c');
same(res.result, 1);
const {result} = await root.getMany([
'c',

@@ -571,4 +575,4 @@ 'cc',

await Promise.all(blocks.map(block => put(block)));
same(await rr.get('aaa'), 3);
same((await rr.get('aaa')).result, 3);
});
});

@@ -170,3 +170,3 @@ import { deepStrictEqual as same } from 'assert';

for (const {key} of list) {
same(await root.get(key), v);
same((await root.get(key)).result, v);
}

@@ -186,3 +186,3 @@ });

}
const entries = await root.getEntries([
const {result: entries} = await root.getEntries([
2,

@@ -197,3 +197,3 @@ 10000

same(b.value, v);
const values = await root.getMany([
const {result: values} = await root.getMany([
2,

@@ -224,9 +224,10 @@ 10000

};
let entries = await root.getRangeEntries(2, 400);
const range = async (...args) => (await root.getRangeEntries(...args)).result;
let entries = await range(2, 400);
verify(entries, 1, 9);
entries = await root.getRangeEntries(0, 99999);
entries = await range(0, 99999);
verify(entries);
entries = await root.getRangeEntries(1, 10000);
entries = await range(1, 10000);
verify(entries, 0, 9);
entries = await root.getRangeEntries(1, 15);
entries = await range(1, 15);
verify(entries, 0, 3);

@@ -260,3 +261,3 @@ same(await root.getLength(), 10001);

await Promise.all(blocks.map(put));
same(await lRoot.get(10001), 'test');
same((await lRoot.get(10001)).result, 'test');
const {

@@ -270,4 +271,4 @@ blocks: _blocks,

await Promise.all(_blocks.map(put));
same(await bRoot.get(10001), 'test2');
same((await bRoot.get(10001)).result, 'test2');
});
});
import { encode } from 'multiformats/block';
import { CIDCounter } from './utils.js';
class Entry {

@@ -123,3 +124,11 @@ constructor({key, address}, opts = {}) {

}
async getEntry(key) {
async getEntry(key, cids = new CIDCounter()) {
const result = await this._getEntry(key, cids);
return {
result,
cids: await cids.all()
};
}
async _getEntry(key, cids) {
cids.add(this);
let node = this;

@@ -132,2 +141,3 @@ while (!node.isLeaf) {

node = await this.getNode(await entry.address);
cids.add(node);
}

@@ -140,3 +150,11 @@ const result = node.entryList.find(key, this.compare);

}
getAllEntries() {
async getAllEntries(cids = new CIDCounter()) {
const result = await this._getAllEntries(cids);
return {
result,
cids: await cids.all()
};
}
_getAllEntries(cids) {
cids.add(this);
if (this.isLeaf) {

@@ -146,7 +164,15 @@ return this.entryList.entries;

const {entries} = this.entryList;
const mapper = async entry => this.getNode(await entry.address).then(node => node.getAllEntries());
const mapper = async entry => this.getNode(await entry.address).then(node => node._getAllEntries(cids));
return Promise.all(entries.map(mapper)).then(results => results.flat());
}
}
async getEntries(keys, sorted = false) {
async getEntries(keys, sorted = false, cids = new CIDCounter()) {
const result = await this._getEntries(keys, sorted, cids);
return {
result,
cids: await cids.all()
};
}
async _getEntries(keys, sorted, cids) {
cids.add(this);
if (!sorted)

@@ -161,3 +187,3 @@ keys = keys.sort(this.compare);

const p = this.getNode(await entry.address);
entries.push(p.then(node => node.getEntries(keys.reverse(), true)));
entries.push(p.then(node => node._getEntries(keys.reverse(), true, cids)));
}

@@ -167,3 +193,11 @@ entries = await Promise.all(entries);

}
getRangeEntries(start, end) {
async getRangeEntries(start, end, cids = new CIDCounter()) {
const result = await this._getRangeEntries(start, end, cids);
return {
result,
cids: await cids.all()
};
}
_getRangeEntries(start, end, cids) {
cids.add(this);
const {entries} = this.entryList.findRange(start, end, this.compare);

@@ -182,3 +216,3 @@ if (this.isLeaf) {

const thenRange = async entry => this.getNode(await entry.address).then(node => {
return node.getRangeEntries(start, end);
return node._getRangeEntries(start, end, cids);
});

@@ -190,4 +224,4 @@ const results = [thenRange(entries.shift())];

while (entries.length) {
const thenAll = async entry => this.getNode(await entry.address).then(node => {
return node.getAllEntries();
const thenAll = async entry => this.getNode(await entry.address).then(async node => {
return node._getAllEntries(cids);
});

@@ -438,4 +472,10 @@ results.push(thenAll(entries.shift()));

async get(key) {
const entry = await this.getEntry(key);
return entry.key;
const {
result: entry,
cids
} = await this.getEntry(key);
return {
result: entry.key,
cids
};
}

@@ -442,0 +482,0 @@ async encode() {

@@ -27,7 +27,13 @@ import {

];
const entries = await node.getRangeEntries(start, end);
return entries.map(entry => ({
id: entry.key[1],
row: entry.value
}));
const {
result: entries,
cids
} = await node.getRangeEntries(start, end);
return {
result: entries.map(entry => ({
id: entry.key[1],
row: entry.value
})),
cids
};
};

@@ -43,4 +49,7 @@ const getRange = async (node, start, end) => {

];
const entries = await node.getRangeEntries(start, end);
return entries.map(entry => {
const {
result: entries,
cids
} = await node.getRangeEntries(start, end);
const result = entries.map(entry => {
const [id, key] = entry.key;

@@ -53,2 +62,6 @@ return {

});
return {
result,
cids
};
};

@@ -55,0 +68,0 @@ class DBIndexLeaf extends MapLeaf {

@@ -37,8 +37,20 @@ import {

const getValue = async (node, key) => {
const entry = await node.getEntry(key);
return entry.value;
const {
result: entry,
cids
} = await node.getEntry(key);
return {
result: entry.value,
cids
};
};
const getManyValues = async (node, keys) => {
const entries = await node.getEntries(keys);
return entries.map(entry => entry.value);
const {
result: entries,
cids
} = await node.getEntries(keys);
return {
result: entries.map(entry => entry.value),
cids
};
};

@@ -45,0 +57,0 @@ class MapLeaf extends IPLDLeaf {

@@ -52,2 +52,23 @@ const readUInt32LE = buffer => {

};
class CIDCounter {
constructor() {
this._cids = new Set();
}
add(node) {
if (!node.address) {
throw new Error('Cannot add node without address');
}
if (node.address.then) {
const p = node.address.then(cid => this._cids.add(cid.toString()));
this._cids.add(p);
p.then(() => this._cids.delete(p));
} else {
this._cids.add(node.address.toString());
}
}
async all() {
await Promise.all([...this._cids]);
return this._cids;
}
}
export {

@@ -58,3 +79,4 @@ readUInt32LE,

binaryCompare,
simpleCompare
simpleCompare,
CIDCounter
};
{
"name": "chunky-trees",
"version": "0.3.2",
"version": "1.0.0",
"scripts": {

@@ -29,2 +29,3 @@ "lint": "standard",

"dependencies": {
"bl": "^4.0.3",
"node-sql-parser": "^3.1.0"

@@ -31,0 +32,0 @@ },