Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
zenboss committed Jul 5, 2015
1 parent 952912f commit 2b1834d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 60 deletions.
91 changes: 52 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ var filekv = function(config){


};

filekv.create = function(config){
return new filekv(config);
};



filekv.prototype.setFileDir = function(path){
var self = this;
if(!path){
Expand Down Expand Up @@ -93,10 +92,10 @@ filekv.prototype.has = function(key,opt,cb){
fs.exists(filePath,function(exists){

if(exists){
queueCB.call(self,null,filePath);
queueCB(null,filePath);
cb(null,filePath);
}else{
queueCB.call(self,null,exists);
queueCB(null,exists);
cb(null,exists)
}
});
Expand All @@ -120,41 +119,47 @@ filekv.prototype.get = function(key,opt,cb){
self._workQueue.queue(function(queueCB){

readFileLine(filePath,function(lineData,lineNum){
if(lineNum==0){
expireTime = parseInt(lineData+'')||0;
if(expireTime>0 && expireTime<=parseInt(Date.now()/1000)){
self.del(key);

queueCB.call(self,new Error('key expire'));

cb(new Error('key expire'));
return false;
}
}else if(lineNum==1){
createTime = parseInt(lineData+'')||0;
}else if(lineNum==2){ //data
try{
valueData = JSON.parse(lineData+'', function(key, value) {
return value && value.type === 'Buffer'
? new Buffer(value.data)
: value; // Buffer类型特殊处理
});
}catch(ex){
queueCB.call(self,ex);

cb(ex);
return false;
}
switch(lineNum){
case 0:

expireTime = parseInt(lineData+'')||0;
if(expireTime!=0 && expireTime<=parseInt(Date.now()/1000)){
self.del(key);

var cbError = new Error('key expired');
queueCB(cbError);
cb(cbError);

return false;
}
break;
case 1:
createTime = parseInt(lineData+'')||0;
break;
case 2: //data
try{
valueData = JSON.parse(lineData+'', function(key, value) {
return value && value.type === 'Buffer'
? new Buffer(value.data)
: value; // Buffer类型特殊处理
});
}catch(ex){
queueCB(ex);
cb(ex);
return false;
}
break;

}
},function(err,endType,nowLineNum){

if(err){
queueCB.call(self,err);
queueCB(err);
cb(err);
}else{
if(endType=='end'){
queueCB.call(self,null,valueData);
cb(null,valueData);
queueCB(null,valueData,createTime,expireTime);
cb(null,valueData,createTime,expireTime);
}
}
});
Expand All @@ -180,12 +185,16 @@ filekv.prototype.set = function(key,value,expireTime,opt,cb){
var filePath = this.fileDir+'/'+_getDataFileSubDir(md5key)+'/';


self.tool.mkdirs(filePath,function(){
self.tool.mkdirs(filePath,function(err){//TODO : this callback function's arguments need check error
if(!!err){
cb(err);
return;
}
var fileAllPath = filePath+'/'+md5key+'.fkv';
var createTime = parseInt(Date.now()/1000);
expireTime = parseInt(expireTime)||0;
if(expireTime!=0)expireTime += createTime;

var fileAllPath = filePath+'/'+md5key+'.fkv';
var valueData = null;
var createTime = parseInt(Date.now()/1000);
expireTime = parseInt(expireTime)||0;
var fileData = '';
fileData += expireTime+'\n';
fileData += createTime+'\n';
Expand All @@ -195,6 +204,10 @@ filekv.prototype.set = function(key,value,expireTime,opt,cb){
fs.writeFile(fileAllPath,fileData,function(err){
cb.apply(self,arguments);
queueCB.apply(self,arguments);

if(expireTime!=0 && expireTime<=createTime){
self.del(key);
}
});

});
Expand All @@ -203,7 +216,8 @@ filekv.prototype.set = function(key,value,expireTime,opt,cb){

};

filekv.prototype.del = function(key,opt,cb){

filekv.prototype.delete = filekv.prototype.del = function(key,opt,cb){
if('function' == typeof opt){
cb = opt;
opt = {};
Expand All @@ -215,5 +229,4 @@ filekv.prototype.del = function(key,opt,cb){
fs.unlink(filePath,cb);
};


module.exports = filekv;
24 changes: 8 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"name": "filekv",
"version": "1.0.5",
"version": "1.0.7",
"description": "This is a key&value storage library, which uses file system to store data.",
"main": "index.js",
"directories": {
"test": "test"
},
"dependencies": {
"readfileline": "^1.0.0",
"workqueue": "^1.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -19,23 +24,10 @@
"key",
"value"
],
"author": {
"name": "zenboss"
},
"author": "zenboss <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/zenboss/filekv/issues"
},
"homepage": "https://github.com/zenboss/filekv",
"dependencies": {
"readfileline": "^1.0.0",
"workqueue": "^1.0.0"
},
"devDependencies": {},
"readme": "# filekv\r\n\r\nThis is a key&value storage library, which uses file system to store data.\r\n\r\n\r\n\r\n\r\n\r\n# Install\r\n\r\n>$ npm install filekv -g\r\n\r\n\r\n\r\n# How to use\r\n\r\n``` javascript\r\n\r\nvar filekv = require('filekv');\r\n\r\nvar filekvClient = new filekv({\r\n\r\n fileDir:__dirname+'/data', //Here is your datafile dir! This config item not have default value!!!\r\n\r\n workQueueMax:1000 //This number can't greater than your OS open file max number!\r\n\r\n});\r\n\r\n\r\n\r\nfilekvClient.set('userinfo',{name:'wzy',sex:1,github:'http://www.github.com/zenboss'},(Date.now()/1000)+3600,function(err){\r\n\r\n\tconsole.log(err);\r\n\r\n});\r\n\r\nfilekvClient.get('userinfo',function(err,data){\r\n\r\n\tconsole.log(err,data);\r\n\r\n});\r\n\r\nfilekvClient.has('userinfo',function(err,isHas){\r\n\r\n\tconsole.log(err,isHas);\r\n\r\n});\r\n\r\nfilekvClient.del('userinfo',function(err){\r\n\r\n\tconsole.log(err);\r\n\r\n});\r\n\r\n```\r\n\r\n# API\r\n\r\n### filekv.prototype.set(key,value/object[,expireTime[,callback]])\r\n\r\n>Use this function setting a key&value\r\n\r\n### filekv.prototype.get(key[,callback])\r\n\r\n>Use this function get a key&value\r\n\r\n### filekv.prototype.has(key[,callback])\r\n\r\n>Use this function check a key exist\r\n\r\n### filekv.prototype.del(key[,callback])\r\n\r\n>Use this function delete a key",
"readmeFilename": "Readme.md",
"gitHead": "46dba247523d7c9633822f7e8f4f3021b60e38b3",
"_id": "[email protected]",
"_shasum": "2921cfe30703dbabcb4a7765501eeb7004997180",
"_from": "filekv@*"
"homepage": "https://github.com/zenboss/filekv"
}
9 changes: 4 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ var fs = require('fs');

var fkvObj = require('../index.js').create({
fileDir:__dirname+'/data',

workMax:1000

});


for(var i =0;i<1000;i++){
(function(){

Expand Down Expand Up @@ -40,7 +39,7 @@ fkvObj.set('test',{name:'zenboss'},function(err1,data1){
});
});

fkvObj.set('test2',{name:'zenboss'},((Date.now()/1000)|0)+1,function(err1,data1){
fkvObj.set('test2',{name:'zenboss'},3600,function(err1,data1){
fkvObj.get('test2',function(err,data){
console.log(data,'test2')
});
Expand All @@ -52,13 +51,13 @@ fkvObj.set('test3','zenboss',function(err1,data1){
});
});

fkvObj.set('test4',{name:'zenboss'},((Date.now()/1000)|0)+1,function(err1,data1){
fkvObj.set('test4',{name:'zenboss'},1,function(err1,data1){
fkvObj.get('test4',function(err,data){
console.log(data,'test4')
});
});

fkvObj.set('test5',{name:'zenboss'},((Date.now()/1000)|0)-1,function(err1,data1){
fkvObj.set('test5',{name:'zenboss'},-1,function(err1,data1){
fkvObj.get('test5',function(err,data){
console.log(data,'test5: expire test')
});
Expand Down

0 comments on commit 2b1834d

Please sign in to comment.