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

bss lazyopen

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