Skip to content

Commit

Permalink
Merge pull request felixge#43 from younggi/master
Browse files Browse the repository at this point in the history
add dirty db close function which closes file handles.
  • Loading branch information
felixge committed Dec 27, 2013
2 parents 727943d + 7e4185f commit 32970c4
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/dirty/dirty.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var Dirty = exports.Dirty = function(path) {
this._queue = [];
this._readStream = null;
this._writeStream = null;
this._fdRead = null;
this._fdWrite = null;

this._load();
};
Expand Down Expand Up @@ -92,9 +94,20 @@ Dirty.prototype.forEach = function(fn) {

};




/**
* Close dirty db file stream, release file handle
*/
Dirty.prototype.close = function() {
this._maybeFlush();

var self = this;
fs.close(this._fdRead, function() {
self.emit('read_close', 0);
});
fs.close(this._fdWrite, function() {
self.emit('write_close', 0);
});
};

// Called when a dirty connection is instantiated
Dirty.prototype._load = function() {
Expand Down Expand Up @@ -163,6 +176,9 @@ Dirty.prototype._load = function() {
self.emit('error', new Error('Corrupted row at the end of the db: '+buffer));
}
self.emit('load', length);
})
.on('open', function(fd) {
self._fdRead = fd;
});

this._writeStream = fs.createWriteStream(this.path, {
Expand All @@ -173,6 +189,10 @@ Dirty.prototype._load = function() {
this._writeStream.on('drain', function() {
self._writeDrain();
});

this._writeStream.on('open', function(fd) {
self._fdWrite = fd;
});
};

Dirty.prototype._writeDrain = function() {
Expand Down

0 comments on commit 32970c4

Please sign in to comment.