From v.1.1.0, everything has become asynchronous and i've added mutex to protect data during any data change. I will consider adding semaphore if too many data reading cause problems.
const bgraph = new BGraph(5, compareKey);
// It has to be asynchronous
async compareKey(key1, key2)
{
if(key1.length > key2.length) return 1;
else if (key1.length < key2.length) return -1;
else return 0;
}
We also support having a fuction to compare 2 keys for ordering data in bgraph.This will let you have power to build a flexible bgraph structure that can be used to store data
▶️ install
npm i bgraph
👩🎓 Example
Code :
const BGraph = require('./bgraph.js');
async function test()
{
console.time("BGraph");
const bgraph = new BGraph();
await bgraph.insert("a", "a");
await bgraph.insert("b", "b");
await bgraph.insert("c", "c");
await bgraph.insert("d", "d");
await bgraph.insert("e", "e");
await bgraph.insert("f", "f");
await bgraph.delete("a");
await bgraph.delete("f");
await bgraph.insert("g", "g");
let serializedGraph = await bgraph.serialize();
await bgraph.insert("h", "h");
await bgraph.insert("i", "i");
await bgraph.insert("j", "j");
await bgraph.insert("k", "k");
await bgraph.insert("l", "l");
await bgraph.insert("m", "m");
await bgraph.insert("n", "n");
await bgraph.insert("o", "o");
await bgraph.delete("o");
await bgraph.deserialize(serializedGraph);
let size = bgraph.size;
let start = bgraph.start;
console.log(start);
for (let i = 0; i < size; i++)
{
console.log(start.value);
start = start.next;
}
console.log("Search: ", await bgraph.search("d"));
let result = await bgraph.searchRange("b", 7);
for (let data of result)
{
console.log("key: ", data.key, "value: ", data.value);
}
console.timeEnd("BGraph");
}
test();
Result :
<ref *2> {
key: 'b',
value: 'b',
next: <ref *1> {
key: 'c',
value: 'c',
next: { key: 'd', value: 'd', next: [Object], prev: [Circular *1] },
prev: [Circular *2]
},
prev: undefined
}
b
c
d
e
g
Search: d
key: b value: b
key: c value: c
key: d value: d
key: e value: e
key: g value: g
BGraph: 15.503ms
📖 Simple document
function
search(key)
description
This function is for searching value of key that is stored in b-graph
arg: key
string
return
return value if it is found otherwise it will return undefined
function
searchRange(key, total, position)
description
This function is for getting list of data from where the key at.
arg: key
string
arg: total
number, number of data to grab
arg: position
number, number to skip data
return
return filled list when data exist, return empty list for when it couldn't find, otherwise return undefined such as for the case that root node does not exist or key isn't string type
function
searchRangeBackward(key, total, position)
description
This function is for getting list of data from where the key at. It adds data backward.
arg: key
string
arg: total
number, number of data to grab
arg: position
number, number to skip data
return
return filled list when data exist, return empty list for when it couldn't find, otherwise return undefined such as for the case that root node does not exist or key isn't string type