Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information
sergeyksv committed Mar 15, 2013
0 parents commit b26b2e1fe257240aadab0220531c56afbadacbee
Showing with 6,290 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +12 −0 lib/db.js
  3. +9 −0 lib/main.js
  4. +99 −0 lib/tcoll.js
  5. +26 −0 lib/tdb.js
  6. +21 −0 package.json
  7. +121 −0 test/basic.js
  8. +6,000 −0 test/data/test
@@ -0,0 +1,2 @@
node_modules
*.swp
@@ -0,0 +1,12 @@
var safe = require('safe');

function tdb() {
var _path = "";
}

module.exports = tdb;

tdb.prototype.init = function (path, options, cb) {
this._path = path;
cb(null)
}
@@ -0,0 +1,9 @@
var safe = require('safe');
var tdb = require('./tdb.js');

module.exports.open = function (dbpath, options, cb) {
var db = new tdb();
db.init(dbpath,options,safe.sure(cb, function () {
cb(null, db)
}))
}
@@ -0,0 +1,99 @@
var safe = require('safe');
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var async = require('async');

function tcoll() {
var self = this;
this._tdb = null;
this._name = null;
this._store = {};
this._fd = null;
this._fsize = null;
this._wqueue = new async.queue(function (task,cb) {
var item = task.item;
var id = task.id;
var kitem = {"id":item.id};
var sobj = JSON.stringify(item);
var skey = JSON.stringify(kitem);
var zeros = "0000000000";
var lobj = sobj.length.toString();
var lkey = skey.length.toString();
lobj = zeros.substr(0,zeros.length - lobj.length)+lobj;
lkey = zeros.substr(0,zeros.length - lkey.length)+lkey;
var h1={k:lkey,o:lobj,v:"001"};
var buf = new Buffer(JSON.stringify(h1)+"\n"+skey+"\n"+sobj+"\n");

fs.write(self._fd,buf, 0, buf.length, self._fsize, safe.sure( cb, function (written) {
self._store[id]=self._fsize;
self._fsize+=written;
cb(null);
}))
},1);
}

module.exports = tcoll;

tcoll.prototype.init = function (tdb, name, options, cb) {
var self= this;
this._tdb = tdb;
this._name = name;
var pos = 0;
fs.open(path.join(this._tdb._path,this._name), "a+", safe.sure(cb, function (fd) {
self._fd = fd;
var b1 = new Buffer(45);
async.whilst(function () { return self._fsize==null; }, function(cb) {
fs.read(fd, b1, 0, 45, pos, safe.sure(cb, function (bytes, data) {
if (bytes==0) {
self._fsize = pos;
return cb();
}
var h1 = JSON.parse(data.toString());
h1.o = parseInt(h1.o,10);
h1.k = parseInt(h1.k,10);
var b2 = new Buffer(h1.k);
fs.read(fd,b2,0,h1.k,pos+45+1, safe.sure(cb, function (bytes, data) {
var obj = JSON.parse(data.toString());
self._store[obj.id]=pos;
pos+=45+3+h1.o+h1.k;
cb();
}))
}))
}, cb)
}));
}

tcoll.prototype.addIndex = function () {
console.log(arguments);
_(arguments).last()(null);
}

tcoll.prototype.get = function (id, cb) {
var self = this;
var pos = this._store[id];
if (pos == null) return cb(null,null);
var b1 = new Buffer(45);
fs.read(self._fd, b1, 0, 45, pos, safe.sure(cb, function (bytes, data) {
var h1 = JSON.parse(data.toString());
h1.o = parseInt(h1.o,10);
h1.k = parseInt(h1.k,10);
var b2 = new Buffer(h1.o);
fs.read(self._fd,b2,0,h1.o,pos+45+2+h1.k, safe.sure(cb, function (bytes, data) {
var obj = JSON.parse(data.toString());
cb(null,obj);
}))
}))
}

tcoll.prototype.scan = function (worker) {
worker(null, null, null)
}

tcoll.prototype.put = function (id, item, cb) {
this._wqueue.push({id:id,item:item},cb);
}

tcoll.prototype.size = function (cb) {
cb(null, _.size(this._store));
}
@@ -0,0 +1,26 @@
var safe = require('safe');
var tcoll = require('./tcoll.js');
var path = require('path');

function tdb() {
this._path = "";
this._cols = {};
}

module.exports = tdb;

tdb.prototype.init = function (path_, options, cb) {
this._path = path.resolve(path_);
cb(null)
}

tdb.prototype.ensure = function (cname, options, cb) {
var self = this;
var c = self._cols[cname];
if (c) return cb(null, c);
c = new tcoll();
c.init(this, cname, options, safe.sure(cb, function () {
self._cols[cname] = c;
cb(null, c);
}))
}
@@ -0,0 +1,21 @@
{
"author": "Sergey Korotkov <sergeyksv@gmail.com>",
"name": "tingodb",
"version": "0.0.5",
"repository": {
"type": "git",
"url": "git://github.com/sergeyksv/tingodb.git"
},
"contributors": [
],
"main": "./lib/main",
"engines": {
"node": ">= v0.6.x"
},
"dependencies": {
"safe": "",
"underscore": ""
},
"devDependencies": {
}
}
@@ -0,0 +1,121 @@
var vows = require('vows');
var assert = require('assert');
var main = require('../lib/main');
var temp = require('temp');
var _ = require('underscore');
var async = require('async');

function dummyDataCheck(index) {
var context = {
topic: function (coll) {
coll.get(index, this.callback);
}
};
context['ok'] = function (err, v) {
assert.equal(v.id,index);
}
return context;
}

function randomRead(max,size) {
var context = {
topic: function (coll) {
return coll;
}
}

context['at ' + 0] = dummyDataCheck(0);
context['at ' + (max-1)] = dummyDataCheck(max-1);

for (i=0; i<size;i++) {
var index = Math.floor(Math.random() * max);
context['at ' + index] = dummyDataCheck(index)
}

return context;
}

var path = "./data";
vows.describe('Basic').addBatch({
'New store':{
topic: function () {
var self = this;
temp.mkdir('test', function (err, path_) {
// path = path_;
main.open(path, {}, self.callback)
})
},
"can be created by path":function (db) {
assert.notEqual(db,null);
},
"collection":{
topic:function (db) {
db.ensure("test", {}, this.callback)
},
"can be created":function (coll) {
assert.notEqual(coll,null);
},
"populated with test data 1":{
topic:function (coll) {
var i=0;
async.whilst(function () { return i<1000},
function (cb) {
coll.put(i, {id:i,sin:Math.sin(i),cos:Math.cos(i)}, cb);
i++;
},
this.callback
)
},
"ok":function() {},
},
"populated with test data":{
topic:function (coll) {
var i=0;
async.whilst(function () { return i<1000},
function (cb) {
coll.put(i, {id:i,sin:Math.sin(i),cos:Math.cos(i)}, cb);
i++;
},
this.callback
)
},
"ok":function() {},
"has proper size":{
topic:function (coll) {
coll.size(this.callback);
},
"ok":function (err, size) {
assert.equal(size, 1000);
}
},
"random read":randomRead(1000,1),
}
}
}
}).addBatch({
'Existing store':{
topic: function () {
main.open(path, {}, this.callback)
},
"can be created by path":function (db) {
assert.notEqual(db,null);
},
"test collection":{
topic:function (db) {
db.ensure("test", {}, this.callback)
},
"exists":function (coll) {
assert.notEqual(coll,null);
},
"has proper size":{
topic:function (coll) {
coll.size(this.callback);
},
"ok":function (err, size) {
assert.equal(size, 1000);
}
},
"random read":randomRead(1000,1),
}
}
}).export(module);

0 comments on commit b26b2e1

Please sign in to comment.
You can’t perform that action at this time.