1.1.1 • Published 1 year ago

bgraph v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

B-Graph

B-Graph. B-Tree that can perform ranged search.

B-Graph Image

👨‍🏫 Notice

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

functionsearch(key)
descriptionThis function is for searching value of key that is stored in b-graph
arg: keystring
returnreturn value if it is found otherwise it will return undefined
functionsearchRange(key, total, position)
descriptionThis function is for getting list of data from where the key at.
arg: keystring
arg: totalnumber, number of data to grab
arg: positionnumber, number to skip data
returnreturn 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
functionsearchRangeBackward(key, total, position)
descriptionThis function is for getting list of data from where the key at. It adds data backward.
arg: keystring
arg: totalnumber, number of data to grab
arg: positionnumber, number to skip data
returnreturn 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
functionsearchKeyContains(substring, total, position, lastKey)
descriptionThis function is for finding list of data that its key contains substring.
arg: substringstring
arg: totalnumber, number of data to grab
arg: positionnumber, number to skip data
arg: lastKeystring, last key where it should start search from
returnreturn list of data, if root node does not exist it will return undefined
functionsearchValueContains(substring, total, position, lastKey)
descriptionThis function is for finding list of data that its value contains substring.
arg: substringstring
arg: totalnumber, number of data to grab
arg: positionnumber, number to skip data
arg: lastKeystring, last key where it should start search from
returnreturn list of data, if root node does not exist it will return undefined
functioninsert(key, value)
descriptionThis function inserts data into b-graph
arg: keystring, name of value
arg: valueanything but recommend the value that can be JSON.stringfy()
returnfalse for failure of inserting data, true for success
functionupdate(key, value)
descriptionTThis function updates data into b-graph
arg: keystring, name of value
arg: valuenew value. anything but recommend the value that can be JSON.stringfy()
returnfalse for failure of updating data, true for success
functiondelete(key)
descriptionThis function deletes the key of data from b-graph
arg: keystring, name of value
returnfalse for failure of deleting data, true for success
functiongetAllKeys()
descriptionThis function returns a list of all keys
returnreturns list of key, it can be empty
functiongetAllValues()
descriptionThis function returns a list of all values
returnreturns list of value, it can be empty
functiongetAll()
descriptionThis function returns a list of all data
returnreturns list of data, it can be empty
functionserialize()
descriptionserialize b-graph to string.
returnstring, serialized b-graph
functiondeserialize(string)
descriptiondeserialize string b-graph data.
arg: stringstring, serialized b-graph
functionserializeToObj()
descriptionserialize b-graph to object.
returnobject, serialized b-graph object
functiondeserializeFromObj(data)
descriptiondeserialize object b-graph data.
arg: dataobject, serialized b-graph object

💪 Sponsor

Github sponsor page

👨‍💻 Author

Victor Chanil Park

💯 License

MIT, See LICENSE.

1.1.1

1 year ago

1.1.0

1 year ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago