Commit 88af03b3 authored by Kamron Aroonrua's avatar Kamron Aroonrua 💬

bss_engine bsspool

parent 0fddbb6e
{ {
"name": "node-bigstream", "name": "node-bigstream",
"description": "", "description": "",
"version": "0.0.1", "version": "1.2.2",
"main": "./bigstream.js", "main": "./bigstream.js",
"author": { "author": {
"name": "Kamron Aroonrua", "name": "Kamron Aroonrua",
......
...@@ -4,11 +4,13 @@ var ctx = require('../../context'); ...@@ -4,11 +4,13 @@ var ctx = require('../../context');
var BinStream = ctx.getLib('lib/bss/binarystream_v1_1'); var BinStream = ctx.getLib('lib/bss/binarystream_v1_1');
var ObjId = ctx.getLib('lib/bss/objid'); var ObjId = ctx.getLib('lib/bss/objid');
var bsdata = ctx.getLib('lib/model/bsdata'); var bsdata = ctx.getLib('lib/model/bsdata');
var thunky = require('thunky');
var importer = require('./importer'); var importer = require('./importer');
var dataevent = require('./dataevent'); var dataevent = require('./dataevent');
var sutils = require('./storage-utils'); var sutils = require('./storage-utils');
var _self = null;
module.exports.create = function(prm) module.exports.create = function(prm)
{ {
var ins = prm; var ins = prm;
...@@ -21,6 +23,7 @@ module.exports.create = function(prm) ...@@ -21,6 +23,7 @@ module.exports.create = function(prm)
function BSSEngine(prm) function BSSEngine(prm)
{ {
_self = this;
if(typeof prm == 'string'){ if(typeof prm == 'string'){
prm = {'file':prm,'context':null}; prm = {'file':prm,'context':null};
} }
...@@ -46,15 +49,14 @@ BSSEngine.prototype.exists = function() ...@@ -46,15 +49,14 @@ BSSEngine.prototype.exists = function()
return fs.existsSync(fp); return fs.existsSync(fp);
} }
BSSEngine.prototype.open = function(cb) BSSEngine.prototype.open = thunky(function(cb)
{ {
var self = this;
if(self.exists()) if(_self.exists())
{ {
open() open()
}else{ }else{
BinStream.format(self.filepath(),function(err){ BinStream.format(_self.filepath(),function(err){
if(!err){ if(!err){
open() open()
}else{ }else{
...@@ -64,20 +66,22 @@ BSSEngine.prototype.open = function(cb) ...@@ -64,20 +66,22 @@ BSSEngine.prototype.open = function(cb)
} }
function open(){ function open(){
BinStream.open(self.filepath(),function(err,bss){ BinStream.open(_self.filepath(),function(err,bss){
if(!err){ if(!err){
self.bss = bss; _self.bss = bss;
} }
cb(err); cb(err,bss);
}); });
} }
} });
BSSEngine.prototype.close = function(cb) BSSEngine.prototype.close = function(cb)
{ {
this.bss.close(cb); _self.open((err,bss)=>{
bss.close(cb);
});
} }
...@@ -86,11 +90,9 @@ BSSEngine.prototype.cmd = function(cmd,cb) ...@@ -86,11 +90,9 @@ BSSEngine.prototype.cmd = function(cmd,cb)
var command = cmd.command; var command = cmd.command;
var param = cmd.param; var param = cmd.param;
var self=this;
switch (command) { switch (command) {
case 'write': case 'write':
self.cmd_write(param,cb); _self.cmd_write(param,cb);
break; break;
default: default:
cb('invalid cmd'); cb('invalid cmd');
...@@ -99,31 +101,34 @@ BSSEngine.prototype.cmd = function(cmd,cb) ...@@ -99,31 +101,34 @@ BSSEngine.prototype.cmd = function(cmd,cb)
BSSEngine.prototype.cmd_write = function(prm,cb) BSSEngine.prototype.cmd_write = function(prm,cb)
{ {
var self = this;
var data = parseData(prm.data); var data = parseData(prm.data);
var meta = prm.meta; var meta = prm.meta;
if(!data){return cb("null data")} if(!data){return cb("null data")}
this.bss.write(data,{'meta':meta},function(err,obj){ _self.open((err,bss)=>{
if(!err){ bss.write(data,{'meta':meta},function(err,obj){
var head = obj.getHeader(); if(!err){
var obj_id = new ObjId(head.ID); var head = obj.getHeader();
var resp = { var obj_id = new ObjId(head.ID);
'resource_id' : obj_id.toString(), var resp = {
'storage_name' : self.name 'resource_id' : obj_id.toString(),
} 'storage_name' : _self.name
}
//dataevent.newdata({'resourceId':obj_id.toString(),'storageId':self.name});
if(self.context){ //dataevent.newdata({'resourceId':obj_id.toString(),'storageId':self.name});
newdata_event(self.context,{'resourceId':obj_id.toString(),'storageId':self.name}); if(_self.context){
newdata_event(_self.context,{'resourceId':obj_id.toString(),'storageId':_self.name});
}
cb(null,resp);
}else {
cb("write error");
} }
});
cb(null,resp);
}else {
cb("write error");
}
}); });
} }
function newdata_event(ctx,prm) function newdata_event(ctx,prm)
......
...@@ -12,7 +12,7 @@ function BSSPool(prm) ...@@ -12,7 +12,7 @@ function BSSPool(prm)
BSSPool.prototype.get = function(name,opt,cb) BSSPool.prototype.get = function(name,opt,cb)
{ {
if(!cb){cb=opt;opt={};} if(!cb){cb=opt;opt={};}
var self=this; var self=this;
var filepath = this.repos_dir + '/' + name2path(name) + '.bss' var filepath = this.repos_dir + '/' + name2path(name) + '.bss'
var bssname = name; var bssname = name;
...@@ -32,18 +32,14 @@ BSSPool.prototype.get = function(name,opt,cb) ...@@ -32,18 +32,14 @@ BSSPool.prototype.get = function(name,opt,cb)
'name' : bssname, 'name' : bssname,
'newInstance':opt.newInstance 'newInstance':opt.newInstance
}); });
self.pool.push({
bss_engine.open(function(err){ 'name' : name,
if(!err){ 'engine':bss_engine
self.pool.push({
'name' : name,
'engine':bss_engine
});
}
self.clean(function(err){
cb(err,bss_engine);
});
}); });
self.clean(function(err){
cb(err,bss_engine);
});
} }
} }
......
var thunky = require('thunky')
var _self = null;
function TestThunk()
{
_self = this;
this.rnumber = 0;
}
TestThunk.prototype.init = function()
{
console.log('waiting 1s and returning random number');
}
TestThunk.prototype.open = thunky(function (callback) { // the inner function should only accept a callback
_self.init();
setTimeout(function () {
var ran = Math.random();
_self.rnumber = ran;
callback(null,ran)
}, 1000)
})
TestThunk.prototype.test = function(x){
_self.open((err,num)=>{
console.log(_self.rnumber + ' ' + x)
});
}
var tt = new TestThunk();
tt.test(1);
tt.test(2);
tt.test(3);
tt.test(4);
tt.test(5);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment