From b162768c476fbf724d32552284b6aedbb709e0f5 Mon Sep 17 00:00:00 2001 From: petersirka Date: Sat, 28 Dec 2013 01:27:43 +0100 Subject: [PATCH] fixed: events --- index.js | 12 ++++++++---- minify/nosql/index.js | 2 +- minify/nosql/package.json | 2 +- package.json | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 6430cff..5093de2 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var path = require('path'); var util = require('util'); var events = require('events'); -var VERSION = 'v2.0.6'; +var VERSION = 'v2.0.7'; var STATUS_UNKNOWN = 0; var STATUS_READING = 1; var STATUS_WRITING = 2; @@ -183,10 +183,14 @@ Database.prototype = { return new Date(Date.parse(dt.toString())); } +}; -} - -Database.prototype.__proto__ = new events.EventEmitter(); +Database.prototype.__proto__ = Object.create(events.EventEmitter.prototype, { + constructor: { + value: Database, + enumberable: false + } +}); /* Insert data into database diff --git a/minify/nosql/index.js b/minify/nosql/index.js index fd88ee7..20bc798 100644 --- a/minify/nosql/index.js +++ b/minify/nosql/index.js @@ -19,4 +19,4 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -"use strict";var fs=require("fs");var path=require("path");var util=require("util");var events=require("events");var VERSION="v2.0.6";var STATUS_UNKNOWN=0;var STATUS_READING=1;var STATUS_WRITING=2;var STATUS_LOCKING=3;var STATUS_PENDING=4;var EXTENSION=".nosql";var EXTENSION_VIEW=".nosql";var EXTENSION_BINARY=".nosql-binary";var EXTENSION_TMP=".nosql-tmp";var EXTENSION_CHANGES=".changelog";var EXTENSION_STORED=".nosql-stored";var EXTENSION_META=".meta";var MAX_WRITESTREAM=2;var MAX_READSTREAM=4;var MAX_BUFFER_SIZE=1024*40;var BINARY_HEADER_LENGTH=2e3;var NEWLINE="\n";var STRING="string";var FUNCTION="function";var UNDEFINED="undefined";var BOOLEAN="boolean";if(typeof setImmediate===UNDEFINED){global.setImmediate=function(cb){process.nextTick(cb)}}function Database(filename,directory,changes){if(typeof directory===BOOLEAN){changes=directory;directory=null}this.isReady=false;this.status_prev=STATUS_UNKNOWN;this.status=STATUS_UNKNOWN;this.changes=typeof changes===UNDEFINED?true:changes===true;this.countRead=0;this.countWrite=0;this.pendingRead=[];this.pendingEach=[];this.pendingLock=[];this.pendingDrop=[];this.pendingWrite=[];this.pendingClear=[];this.isPending=false;if(filename.indexOf(EXTENSION)!==-1)filename=filename.replace(EXTENSION,"");this.filename=filename+EXTENSION;this.filenameTemp=filename+EXTENSION_TMP;this.filenameChanges=filename+EXTENSION_CHANGES;this.filenameStored=filename+EXTENSION_STORED;this.filenameMeta=filename+EXTENSION_META;this.name=path.basename(filename);this.directory=path.dirname(filename);this.views=new Views(this);this.meta={version:VERSION,views:{},stored:{},description:"",created:new Date,custom:null};this.binary=(directory||"").length===0?null:new Binary(this,directory);this.changelog=new Changelog(this,this.filenameChanges);this.file=new FileReader(this);this.stored=new Stored(this,this.filenameStored);this._metaLoad()}function Views(db){this.views={};this.db=db;this.directory=db.directory;this.emit=db.emit}function View(db,name,filename){this.db=db;this.status=STATUS_UNKNOWN;this.countRead=0;this.pendingRead=[];this.pendingOperation=[];this.filename=filename;this.name=name;this.emit=db.emit;this.file=new FileReader(db)}function Stored(db,filename){this.filename=filename;this.db=db;this.stored={};this.cache={};this.isReaded=false}function Binary(db,directory){this.db=db;this.directory=directory;if(directory.length===0)return;if(fs.existsSync(directory))return;fs.mkdirSync(directory)}function Changelog(db,filename){this.filename=filename;this.db=db}function FileReader(db){this.db=db}Database.prototype={get created(){var dt=this.meta.created;if(util.isDate(dt))return dt;if(dt===null||typeof dt===UNDEFINED)return null;return new Date(Date.parse(dt.toString()))}};Database.prototype.__proto__=new events.EventEmitter;Database.prototype.insert=function(arr,fnCallback,changes){var self=this;if(typeof fnCallback===STRING){changes=fnCallback;fnCallback=null}if(!(arr instanceof Array))arr=[arr];var length=arr.length;if(self.status===STATUS_LOCKING||self.status===STATUS_PENDING||self.countWrite>=MAX_WRITESTREAM){for(var i=0;i=MAX_READSTREAM){self.pendingRead.push(function(){self.read(fnMap,fnCallback,itemSkip,itemTake,isScalar)});return self}if(typeof fnCallback===UNDEFINED){fnCallback=fnMap;fnMap=function(doc){return doc}}if(typeof fnMap===STRING)fnMap=filterPrepare(fnMap);if(fnMap===null)fnMap=function(doc){return doc};self.emit(name||"read",true,0);self.countRead++;self.status=STATUS_READING;var selected=[];var current="";var count=0;var resume=true;var fnBuffer=function(buffer){current+=buffer;return current};var fnItem=function(err,doc){if(!resume)return;current="";if(err)return;var item=fnMap(doc);if(item===false||item===null||typeof item===UNDEFINED)return;count++;if(skip>0&&count<=skip)return;if(!isScalar)selected.push(item===true?doc:item);if(take>0&&selected.length===take)resume=false};var reader=self.file.open(self.filename,MAX_BUFFER_SIZE,function(buffer){onBuffer(buffer,fnItem,fnBuffer);return resume},function(){self.countRead--;self.next();setImmediate(function(){self.emit(name||"read",false,isScalar?count:selected.length);fnCallback(isScalar?count:selected)})});return self};Database.prototype.all=function(fnMap,fnCallback,itemSkip,itemTake){return this.read(fnMap,fnCallback,itemSkip,itemTake,false,"all")};Database.prototype.one=function(fnMap,fnCallback){var cb=function(selected){fnCallback(selected[0]||null)};return this.read(fnMap,cb,0,1,false,"one")};Database.prototype.top=function(max,fnMap,fnCallback){return this.read(fnMap,fnCallback,0,max,false,"top")};Database.prototype.count=function(fnFilter,fnCallback){return this.read(fnFilter,fnCallback,0,0,true,"count")};Database.prototype.each=function(fnDocument,fnCallback){var self=this;if(self.status===STATUS_LOCKING||self.status===STATUS_PENDING||self.countRead>=MAX_READSTREAM){if(fnDocument)self.pendingEach.push({item:fnDocument,callback:fnCallback});return self}var operation=[];if(fnDocument)operation.push({item:fnDocument,callback:fnCallback});var length=self.pendingEach.length;for(var i=0;i0||itemTake>0)selected=selected.slice(itemSkip,itemSkip+itemTake);fnCallback(selected,count)};var onItem=function(doc){var item=fnMap(doc);if(item===false||item===null||typeof item===UNDEFINED)return;count++;selected.push(item===true?doc:item)};self.each(onItem,onCallback);return self};Database.prototype.clear=function(fnCallback,changes){var self=this;var type=typeof fnCallback;if(type===UNDEFINED)fnCallback=null;if(type===STRING){changes=fnCallback;fnCallback=null}self.pendingClear.push(function(){if(changes)self.changelog.insert(changes);if(fnCallback)fnCallback()});if(self.status!==STATUS_UNKNOWN)return self;self.status=STATUS_LOCKING;var operation=[];var length=self.pendingClear.length;for(var i=0;i0)self.changelog.insert(changes);self.next()})};var fnWrite=function(json,valid){if(lines.length>25||valid){if(lines.length===0){if(completed&&countWrite<=0)fnRename();return}countWrite++;fs.appendFile(self.filenameTemp,lines.join(NEWLINE)+NEWLINE,function(){countWrite--;if(completed&&countWrite<=0)fnRename()});lines=[]}if(typeof json===STRING)lines.push(json)};var fnBuffer=function(buffer){current+=buffer;return current};var fnItem=function(err,doc,json){current="";var skip=false;var value=null;for(var i=0;i0){self.status=STATUS_READING;return}if(self.countWrite>0){self.status=STATUS_WRITING;return}if(self.pendingWrite.length>0){self.insert(self.pendingWrite);self.pendingWrite=[];return}if(self.pendingLock.length>0){self.update();return}if(self.pendingEach.length>0){self.each();return}if(self.pendingRead.length>0){var max=self.pendingRead.length;if(max>MAX_READSTREAM)max=MAX_READSTREAM;for(var i=0;i0){self.drop();return}if(self.pendingClear.length>0){self.clear();return}setImmediate(function(){self.emit("complete",self.status_prev)})};Database.prototype.description=function(value){var self=this;if(typeof value===UNDEFINED)return self.meta.description;self.meta.description=(value||"").toString();self._metaSave();return self};Database.prototype.custom=function(value){var self=this;if(typeof value===UNDEFINED)return self.meta.custom;self.meta.custom=value;self._metaSave();return self};Database.prototype._metaSave=function(){var self=this;if(typeof self.meta.created===UNDEFINED)self.meta.created=new Date;fs.writeFile(self.filenameMeta,JSON.stringify(self.meta),noop);return self};Database.prototype._metaLoad=function(callback){var self=this;fs.readFile(self.filenameMeta,function(err,data){var isReady=self.isReady;self.isReady=true;if(err){if(!isReady){self.emit("ready");self.emit("load")}if(callback)callback(false,self.meta);return}self.meta=JSON.parse(data.toString("utf8"));var keys=Object.keys(self.meta.views);var length=keys.length;for(var i=0;i=MAX_READSTREAM){self.pendingRead.push(function(){self.read(fnMap,fnCallback,itemSkip,itemTake,isScalar)});return self}self.status=STATUS_READING;if(typeof fnMap===STRING)fnMap=filterPrepare(fnMap);if(fnMap===null)fnMap=function(o){return o};self.db.emit("view",true,self.name,0);self.countRead++;var selected=[];var current="";var count=0;var resume=true;var fnBuffer=function(buffer){current+=buffer;return current};var fnItem=function(err,doc){if(!resume)return;current="";if(err)return;var item=fnMap(doc);if(item===false||item===null||typeof item===UNDEFINED)return;count++;if(skip>0&&count<=skip)return;if(!isScalar)selected.push(item===true?doc:item);if(take>0&&selected.length===take)resume=false};var reader=self.file.open(self.filename,MAX_BUFFER_SIZE,function(buffer){if(skipCount&&!resume){count=-1;return false}onBuffer(buffer,fnItem,fnBuffer);return resume},function(){self.countRead--;self.next();setImmediate(function(){self.emit("view",false,self.name,count);fnCallback(selected,count)})});return self};View.prototype.operation=function(fnCallback){var self=this;if(typeof fnCallback!==UNDEFINED)self.pendingOperation.push(fnCallback);if(self.status!==STATUS_UNKNOWN)return self;self.status=STATUS_LOCKING;var operation=self.pendingOperation.shift();operation(function(){self.next()});return self};View.prototype.next=function(){var self=this;self.status=STATUS_UNKNOWN;if(self.countRead>0){self.status=STATUS_READING;return}if(self.pendingOperation.length>0){self.operation();return}if(self.pendingRead.length>0){var max=self.pendingRead.length;if(max>MAX_READSTREAM)max=MAX_READSTREAM;for(var i=0;i=MAX_WRITESTREAM){for(var i=0;i=MAX_READSTREAM){self.pendingRead.push(function(){self.read(fnMap,fnCallback,itemSkip,itemTake,isScalar)});return self}if(typeof fnCallback===UNDEFINED){fnCallback=fnMap;fnMap=function(doc){return doc}}if(typeof fnMap===STRING)fnMap=filterPrepare(fnMap);if(fnMap===null)fnMap=function(doc){return doc};self.emit(name||"read",true,0);self.countRead++;self.status=STATUS_READING;var selected=[];var current="";var count=0;var resume=true;var fnBuffer=function(buffer){current+=buffer;return current};var fnItem=function(err,doc){if(!resume)return;current="";if(err)return;var item=fnMap(doc);if(item===false||item===null||typeof item===UNDEFINED)return;count++;if(skip>0&&count<=skip)return;if(!isScalar)selected.push(item===true?doc:item);if(take>0&&selected.length===take)resume=false};var reader=self.file.open(self.filename,MAX_BUFFER_SIZE,function(buffer){onBuffer(buffer,fnItem,fnBuffer);return resume},function(){self.countRead--;self.next();setImmediate(function(){self.emit(name||"read",false,isScalar?count:selected.length);fnCallback(isScalar?count:selected)})});return self};Database.prototype.all=function(fnMap,fnCallback,itemSkip,itemTake){return this.read(fnMap,fnCallback,itemSkip,itemTake,false,"all")};Database.prototype.one=function(fnMap,fnCallback){var cb=function(selected){fnCallback(selected[0]||null)};return this.read(fnMap,cb,0,1,false,"one")};Database.prototype.top=function(max,fnMap,fnCallback){return this.read(fnMap,fnCallback,0,max,false,"top")};Database.prototype.count=function(fnFilter,fnCallback){return this.read(fnFilter,fnCallback,0,0,true,"count")};Database.prototype.each=function(fnDocument,fnCallback){var self=this;if(self.status===STATUS_LOCKING||self.status===STATUS_PENDING||self.countRead>=MAX_READSTREAM){if(fnDocument)self.pendingEach.push({item:fnDocument,callback:fnCallback});return self}var operation=[];if(fnDocument)operation.push({item:fnDocument,callback:fnCallback});var length=self.pendingEach.length;for(var i=0;i0||itemTake>0)selected=selected.slice(itemSkip,itemSkip+itemTake);fnCallback(selected,count)};var onItem=function(doc){var item=fnMap(doc);if(item===false||item===null||typeof item===UNDEFINED)return;count++;selected.push(item===true?doc:item)};self.each(onItem,onCallback);return self};Database.prototype.clear=function(fnCallback,changes){var self=this;var type=typeof fnCallback;if(type===UNDEFINED)fnCallback=null;if(type===STRING){changes=fnCallback;fnCallback=null}self.pendingClear.push(function(){if(changes)self.changelog.insert(changes);if(fnCallback)fnCallback()});if(self.status!==STATUS_UNKNOWN)return self;self.status=STATUS_LOCKING;var operation=[];var length=self.pendingClear.length;for(var i=0;i0)self.changelog.insert(changes);self.next()})};var fnWrite=function(json,valid){if(lines.length>25||valid){if(lines.length===0){if(completed&&countWrite<=0)fnRename();return}countWrite++;fs.appendFile(self.filenameTemp,lines.join(NEWLINE)+NEWLINE,function(){countWrite--;if(completed&&countWrite<=0)fnRename()});lines=[]}if(typeof json===STRING)lines.push(json)};var fnBuffer=function(buffer){current+=buffer;return current};var fnItem=function(err,doc,json){current="";var skip=false;var value=null;for(var i=0;i0){self.status=STATUS_READING;return}if(self.countWrite>0){self.status=STATUS_WRITING;return}if(self.pendingWrite.length>0){self.insert(self.pendingWrite);self.pendingWrite=[];return}if(self.pendingLock.length>0){self.update();return}if(self.pendingEach.length>0){self.each();return}if(self.pendingRead.length>0){var max=self.pendingRead.length;if(max>MAX_READSTREAM)max=MAX_READSTREAM;for(var i=0;i0){self.drop();return}if(self.pendingClear.length>0){self.clear();return}setImmediate(function(){self.emit("complete",self.status_prev)})};Database.prototype.description=function(value){var self=this;if(typeof value===UNDEFINED)return self.meta.description;self.meta.description=(value||"").toString();self._metaSave();return self};Database.prototype.custom=function(value){var self=this;if(typeof value===UNDEFINED)return self.meta.custom;self.meta.custom=value;self._metaSave();return self};Database.prototype._metaSave=function(){var self=this;if(typeof self.meta.created===UNDEFINED)self.meta.created=new Date;fs.writeFile(self.filenameMeta,JSON.stringify(self.meta),noop);return self};Database.prototype._metaLoad=function(callback){var self=this;fs.readFile(self.filenameMeta,function(err,data){var isReady=self.isReady;self.isReady=true;if(err){if(!isReady){self.emit("ready");self.emit("load")}if(callback)callback(false,self.meta);return}self.meta=JSON.parse(data.toString("utf8"));var keys=Object.keys(self.meta.views);var length=keys.length;for(var i=0;i=MAX_READSTREAM){self.pendingRead.push(function(){self.read(fnMap,fnCallback,itemSkip,itemTake,isScalar)});return self}self.status=STATUS_READING;if(typeof fnMap===STRING)fnMap=filterPrepare(fnMap);if(fnMap===null)fnMap=function(o){return o};self.db.emit("view",true,self.name,0);self.countRead++;var selected=[];var current="";var count=0;var resume=true;var fnBuffer=function(buffer){current+=buffer;return current};var fnItem=function(err,doc){if(!resume)return;current="";if(err)return;var item=fnMap(doc);if(item===false||item===null||typeof item===UNDEFINED)return;count++;if(skip>0&&count<=skip)return;if(!isScalar)selected.push(item===true?doc:item);if(take>0&&selected.length===take)resume=false};var reader=self.file.open(self.filename,MAX_BUFFER_SIZE,function(buffer){if(skipCount&&!resume){count=-1;return false}onBuffer(buffer,fnItem,fnBuffer);return resume},function(){self.countRead--;self.next();setImmediate(function(){self.emit("view",false,self.name,count);fnCallback(selected,count)})});return self};View.prototype.operation=function(fnCallback){var self=this;if(typeof fnCallback!==UNDEFINED)self.pendingOperation.push(fnCallback);if(self.status!==STATUS_UNKNOWN)return self;self.status=STATUS_LOCKING;var operation=self.pendingOperation.shift();operation(function(){self.next()});return self};View.prototype.next=function(){var self=this;self.status=STATUS_UNKNOWN;if(self.countRead>0){self.status=STATUS_READING;return}if(self.pendingOperation.length>0){self.operation();return}if(self.pendingRead.length>0){var max=self.pendingRead.length;if(max>MAX_READSTREAM)max=MAX_READSTREAM;for(var i=0;i