Commit 1836c0e4 authored by Kamron Aroonrua's avatar Kamron Aroonrua 💬

bss lazyopen

parent b02d88d7
...@@ -10,7 +10,6 @@ var importer = require('./importer'); ...@@ -10,7 +10,6 @@ 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;
...@@ -23,23 +22,53 @@ module.exports.create = function(prm) ...@@ -23,23 +22,53 @@ module.exports.create = function(prm)
function BSSEngine(prm) function BSSEngine(prm)
{ {
_self = this; var self = this;
if(typeof prm == 'string'){ if(typeof prm == 'string'){
prm = {'file':prm,'context':null}; prm = {'file':prm,'context':null};
} }
// this.repos_dir = prm.repos_dir;
// this.name = prm.name;
this.file = prm.file; this.file = prm.file;
this.name = prm.name; this.name = prm.name;
this.context = (prm.context)?prm.context:null; this.context = (prm.context)?prm.context:null;
this.concurrent = 0; this.concurrent = 0;
this.serial=prm.serial||''; this.serial=prm.serial||'';
this.outdate=false; this.outdate=false;
this.bss=null;
this.open = thunky(openbss);
this.open();
function openbss (cb) {
if(fs.existsSync(self.file))
{
open()
}else{
BinStream.format(self.file,function(err){
if(!err){
open()
}else{
cb("format error")
}
});
}
function open(){
BinStream.open(self.file,function(err,bss){
if(!err){
self.bss = bss;
}
cb(err,bss);
});
}
}
} }
BSSEngine.prototype.filepath = function() BSSEngine.prototype.filepath = function()
{ {
//return this.repos_dir + '/' + name2path(this.name) + '.bss';
return this.file; return this.file;
} }
...@@ -49,37 +78,10 @@ BSSEngine.prototype.exists = function() ...@@ -49,37 +78,10 @@ BSSEngine.prototype.exists = function()
return fs.existsSync(fp); return fs.existsSync(fp);
} }
BSSEngine.prototype.open = thunky(function(cb)
{
if(_self.exists())
{
open()
}else{
BinStream.format(_self.filepath(),function(err){
if(!err){
open()
}else{
cb("format error")
}
});
}
function open(){
BinStream.open(_self.filepath(),function(err,bss){
if(!err){
_self.bss = bss;
}
cb(err,bss);
});
}
});
BSSEngine.prototype.close = function(cb) BSSEngine.prototype.close = function(cb)
{ {
_self.open((err,bss)=>{ var self = this;
self.open((err,bss)=>{
bss.close(cb); bss.close(cb);
}); });
} }
...@@ -87,13 +89,13 @@ BSSEngine.prototype.close = function(cb) ...@@ -87,13 +89,13 @@ BSSEngine.prototype.close = function(cb)
BSSEngine.prototype.cmd = function(cmd,cb) BSSEngine.prototype.cmd = function(cmd,cb)
{ {
var self = this
var command = cmd.command; var command = cmd.command;
var param = cmd.param; var param = cmd.param;
switch (command) { switch (command) {
case 'write': case 'write':
console.log('CMD WRITE....') self.cmd_write(param,cb);
_self.cmd_write(param,cb);
break; break;
default: default:
cb('invalid cmd'); cb('invalid cmd');
...@@ -102,24 +104,25 @@ BSSEngine.prototype.cmd = function(cmd,cb) ...@@ -102,24 +104,25 @@ 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")}
_self.open((err,bss)=>{ self.open((err,bss)=>{
bss.write(data,{'meta':meta},function(err,obj){ bss.write(data,{'meta':meta},function(err,obj){
if(!err){ if(!err){
var head = obj.getHeader(); var head = obj.getHeader();
var obj_id = new ObjId(head.ID); var obj_id = new ObjId(head.ID);
var resp = { var resp = {
'resource_id' : obj_id.toString(), 'resource_id' : obj_id.toString(),
'storage_name' : _self.name 'storage_name' : self.name
} }
//dataevent.newdata({'resourceId':obj_id.toString(),'storageId':self.name}); //dataevent.newdata({'resourceId':obj_id.toString(),'storageId':self.name});
if(_self.context){ if(self.context){
newdata_event(_self.context,{'resourceId':obj_id.toString(),'storageId':_self.name}); newdata_event(self.context,{'resourceId':obj_id.toString(),'storageId':self.name});
} }
cb(null,resp); cb(null,resp);
......
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