Skip to content

Commit

Permalink
urkel: expose disk urkel.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jan 27, 2025
1 parent b5b10a5 commit fb42a3e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ const defineGet = (name, get) => {
};

definePath('_disk', './tree');
definePath('_memory', './memory');
definePath('_urkel', './urkel');
definePath('_memory', './urkel');
definePath('BLAKE2b', './blake2b');

defineGet('Tree', () => nurkel._disk.Tree);
defineGet('MemTree', () => nurkel._memory.Tree);
defineGet('UrkelTree', () => nurkel._memory.Tree);
defineGet('UrkelTree', () => nurkel._urkel.Tree);

nurkel.create = (options) => {
if (options.memory)
return new nurkel.MemTree();

if (options.urkel) {
return new nurkel.UrkelTree({
prefix: options.prefix
});
}

return new nurkel.Tree({
prefix: options.prefix
});
Expand Down
2 changes: 1 addition & 1 deletion lib/memory.js → lib/urkel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* memory.js - urkel wrapper in nurkel fashion.
* urkel.js - urkel wrapper in nurkel fashion.
* Copyright (c) 2022, Nodari Chkuaselidze (MIT License)
* https://github.com/nodech/nurkel
*/
Expand Down
45 changes: 32 additions & 13 deletions test/urkel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,41 @@ function random(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

for (const memory of [false, true]) {
describe(`Urkel (${memory ? 'MemTree' : 'Tree'})`, function() {
const treeCreateOptions = {
'nurkel': {},
// use legacy tree
'urkel': {
urkel: true
},
// legacy tree as in memory tree
'memory': {
memory: true
}
};

for (const [name, treeTestOptions] of Object.entries(treeCreateOptions)) {
describe(`Urkel (${name})`, function() {
this.timeout(20000);

const Tree = memory ? nurkel.MemTree : nurkel.Tree;
let prefix, tree;
const Tree = name === 'nurkel' ? nurkel.Tree : nurkel.UrkelTree;

let prefix, prefix2;

beforeEach(async () => {
prefix = testdir('tree');
if (!memory)
fs.mkdirSync(prefix);
prefix2 = testdir('tree2');
});

afterEach(async () => {
if (!memory && isTreeDir(prefix))
if (name !== 'memory' && isTreeDir(prefix))
rmTreeDir(prefix);

if (name !== 'memory' && isTreeDir(prefix2))
rmTreeDir(prefix2);
});

it('should test tree', async () => {
const tree = nurkel.create({ prefix, memory });
const tree = nurkel.create({ prefix, ...treeTestOptions });
await tree.open();

let batch = tree.batch();
Expand Down Expand Up @@ -237,7 +252,7 @@ describe(`Urkel (${memory ? 'MemTree' : 'Tree'})`, function() {

it('should test max value size', async () => {
const MAX_VALUE = 0x3ff;
const tree = nurkel.create({ prefix, memory });
const tree = nurkel.create({ prefix, ...treeTestOptions });

await tree.open();

Expand Down Expand Up @@ -281,7 +296,7 @@ describe(`Urkel (${memory ? 'MemTree' : 'Tree'})`, function() {
});

it('should pummel tree', async () => {
const tree = nurkel.create({ prefix, memory });
const tree = nurkel.create({ prefix, ...treeTestOptions });

const items = [];
const set = new Set();
Expand Down Expand Up @@ -477,7 +492,11 @@ describe(`Urkel (${memory ? 'MemTree' : 'Tree'})`, function() {
});

it('should test history independence', async () => {
const opts = { prefix, memory };
const opts1 = { prefix, ...treeTestOptions };
const opts2 = {
prefix: prefix2,
...treeTestOptions
};

const items = [];
const removed = [];
Expand All @@ -489,10 +508,10 @@ describe(`Urkel (${memory ? 'MemTree' : 'Tree'})`, function() {
items.push([key, value]);
}

const tree1 = nurkel.create(opts);
const tree1 = nurkel.create(opts1);
await tree1.open();

const tree2 = nurkel.create(opts);
const tree2 = nurkel.create(opts2);
await tree2.open();

let root = null;
Expand Down
3 changes: 3 additions & 0 deletions test/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ common.serializeU32 = (num) => {
};

common.isTreeDir = (dir, locked) => {
if (!fs.existsSync(dir))
return false;

const files = new Set(fs.readdirSync(dir));

if (files.length < 2)
Expand Down

0 comments on commit fb42a3e

Please sign in to comment.