Yotta DB is a local file based key value database, written in Node.js.
npm install yottayotta
==========
Yotta DB is a local file based key value database, written in Node.js.
npm install yottanpm install yotta -g
$ yotta mydb
mydb>
`
or
`
$ yotta
> open mydb
mydb>
`$3
`
mydb> exit
> $
`$3
`
$ yotta
>
close exit find findFromValue
findKeys findKeysFromValue findfromvalue findkeys
findkeysfromvalue get open put
quit rebuildValueIndex rebuildvalueindex remove
stats test use vacuum
version>
`$3
`
mydb> put a 1
mydb> put b 2
mydb> put c 3
mydb> put d 4
mydb> put e 5
`$3
`
mydb> get d
4
`$3
`
mydb> remove c
mydb> get c
undefined
`$3
`
mydb> findkeys "key==='a' || key==='d'"
[ 'a', 'd' ]
`$3
`
mydb> findkeys true
[ 'a', 'b', 'd', 'e' ]
`$3
`
mydb> find "key==='a' || key==='d'"
{ a: '1', d: '4' }
`$3
`
mydb> find true
{ a: '1', b: '2', d: '4', e: '5' }
`$3
`
$ yotta
> use a
a> put a 1
a> put b 2
a> rebuildvalueindex value 'function(value){return value;}
a> put c 3
a> findkeysfromvalue value "value==='3'"
[ 'c' ]
a> findkeysfromvalue value "value>=2"
[ 'b', 'c' ]
a>
`$3
`
$ yotta
> use a
a> put a 1
a> put b 2
a> rebuildvalueindex value 'function(value){return value;}
a> put c 3
a> findfromvalue value "value>=2"
{ b: '2', c: '3' }
a>
`$3
`
mydb> vacuum
`$3
`
mydb> version
0.1.9
`Yotta CLI
$3
`
$ yotta mydb put a 1
$ yotta mydb put b 2
$ yotta mydb put c 3
$ yotta mydb put d 4
$ yotta mydb put e 5
`$3
`
$ yotta mydb get d
4
`$3
`
$ yotta mydb remove c
$ yotta mydb get c
$
`$3
`
$ yotta mydb findkeys "key==='a' || key==='d'"
[ 'a', 'd' ]
`$3
`
$ yotta mydb findkeys true
[ 'a', 'b', 'd', 'e' ]
`$3
`
$ yotta mydb find "key==='a' || key==='d'"
{ a: '1', d: '4' }
`$3
`
$ yotta mydb find true
{ a: '1', b: '2', d: '4', e: '5' }
`$3
`
$ yotta mydb vacuum
$
`$3
`
$ yotta mydb version
0.1.9
`Module
$3
`javascript
var Yotta = require('yotta').Yotta;// specify data file location
var yottadb = new Yotta('./testdb');
yottadb.open();
yottadb.put("key0", "value0");
yottadb.put("key1", "value1");
yottadb.put("key2", "value2");
var v = yottadb.get("key0");
console.log(v); //value0
//find keys
var keysFound = yottadb.findKeys(function(key, index, keys) {
return key === 'key0' || key === 'key2';
});
console.log(keysFound); //[ 'key0', 'key2' ]
//find
var found = yottadb.find(function(key, index, keys) {
return key === 'key0' || key === 'key2';
});
console.log(found); //{ key0: 'value0', key2: 'value2' }
// remove
yottadb.remove('key0');
yottadb.close();
// or yottadb.close(true) to vacuum
`$3
`javascript
var Yotta = require('yotta').Yotta;// specify data file location
var yottadb = new Yotta('./testdb');
yottadb.open();
yottadb.put("key0a", "value0a", function() {
yottadb.get("key0a", function(err, v) {
console.log(v); // value0a
});
});
//find keys
var keysFound = yottadb.findKeys(function(key, index, keys) {
return key === 'key0' || key === 'key2';
}, function(err, keysFound) {
console.log(keysFound); //[ 'key0', 'key2' ]
});
//find
var found = yottadb.find(function(key, index, keys) {
return key === 'key0' || key === 'key2';
}, function(err, found) {
console.log(found); //{ key0: 'value0', key2: 'value2' }
});
// remove
//yottadb.remove('key0a', function (err) {
// console.log('removed.');
//});
setTimeout(function(){
yottadb.close();
// or yottadb.close(true) to vacuum
}, 1000);
`More APIs
vacuum
Each remove operation causes fragments in the data file, vacuum will help to
compact the data file.
`javascript
// synchronously
yottadb.vacuum();// or asynchronously
yottadb.vacuum(function(){
console.log('vacuum done');
});
`stats
Get the hole size information from the data file.
`javascript
var statsInfo = yottadb.stats();
console.log(statsInfo);
// { holdSize: 630, dataFileSize: 672, holdRatio: 0.9375 }
`
Now it looks to be a perfect chance to do a vacuum.
`javascript
yottadb.vacuum(function(){
console.log(yottadb.stats());
});
// { holdSize: 0, dataFileSize: 42, holdRatio: 0 }
`rebuildValueIndex
Yotta DB does not assume what values are put in the database. A value index
needs to be built before you can search against the values. rebuildValueIndex
will help to build a value index.
`javascript
var indexPath = 'raw';
var test = function(value){
return value;
};
yottadb.rebuildValueIndex(indexPath, test);
`
This will create a value index file raw.idx. The original value will be
indexed. Of course you can also create a more sophisticated function index by
giving the test function more logic.findKeysFromValue
findKeysFromValue helps to find an array of keys against the values in the
database, according to the search logic defined by the test function.
`javascript
var indexPath = 'raw';
var test = function(value){
return value === 1;
};
var cb = function(err, ret){
if (err) console.log(err);
console.log(ret);
};
yottadb.findKeysFromValue(indexPath, test, cb)
`
This will return an array of keys which values equals to 1. if cb is null,
findKeysFromValue is called synchronously, and the array of keys is returned.findFromValue
findFromValue works exactly as findKeysFromValue, with the only exception
that it returns a key value object, as opposed to an array of keys, like
returned by findKeysFromValue.
`javascript
var indexPath = 'raw';
var test = function(value){
return value === 1;
};
var cb = function(err, ret){
if (err) console.log(err);
console.log(ret);
};
yottadb.findFromValue(indexPath, test, cb)
``1. Implementation in other languages like Golang, Java and C, currently working on Golang binding;
Patches and ideas welcome.