Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
80be69f
Fixed _keys duplicates during _load() (which came apparent in forEach…
fabien Jan 22, 2011
1ed10dc
Fixed duplicate keys (issue 17)
fabien Feb 28, 2011
0fb2660
fixing existing tests for current version of node
pathsny Apr 14, 2011
a3a6d26
removing keys array.
pathsny Apr 14, 2011
527e69d
added compacting ability
pathsny Apr 16, 2011
63d851a
tests for compacting behaviour
pathsny Apr 17, 2011
4799ef4
new test for compaction
pathsny Apr 17, 2011
4e8652a
added a compacting filter
pathsny Apr 17, 2011
ec17cc6
adding custom indexes
pathsny Apr 20, 2011
164ba1e
minor refactoring of indexs
pathsny Apr 21, 2011
ee5205f
documentation for indexes
pathsny Apr 21, 2011
93c8d08
added length and redundantLength
pathsny Apr 21, 2011
0e6fad4
documentation change
pathsny Apr 21, 2011
f5169c4
fixed bug which prevented multiple instances of Dirty
pathsny Apr 21, 2011
e508837
changed get to return a clone
pathsny Apr 25, 2011
34c985d
tests to keep clone
pathsny Apr 25, 2011
65af575
added a function to find all the values a particular index has
pathsny Sep 18, 2011
efe891a
bumping version
pathsny Sep 18, 2011
f80f5a5
Merge remote-tracking branch 'felixge/master'
pathsny May 12, 2013
ca53e83
updated dirty to the lastest version from the original repo
pathsny May 13, 2013
c711387
removing unnecessary line
pathsny May 13, 2013
a69cc64
bumping version
pathsny May 13, 2013
8d7988e
find returns clones now
pathsny May 13, 2013
a357b1b
fixing indexvalues and test
pathsny May 13, 2013
18c522a
indexes dont index empty values
pathsny May 23, 2013
cde7e5a
moved to multiple index values per index
pathsny May 23, 2013
f1b9e30
fixing bad test breaking other tests
pathsny May 24, 2013
357b240
get returns a deep clone which should allow fancy datastructures and …
pathsny May 24, 2013
16f473a
bumping version
pathsny May 24, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
new test for compaction
  • Loading branch information
pathsny committed Apr 17, 2011
commit 4799ef4cffa379f87af080657a6665aa2a1bbabd
41 changes: 22 additions & 19 deletions lib/dirty/dirty.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,24 @@ Dirty.prototype._load = function() {
}
self.emit('load', length);
});
this._recreateWriteStream();
};

this._writeStream = fs.createWriteStream(this.path, {
encoding: 'utf-8',
flags: 'a'
});

this._writeStream.on('drain', function() {
self.flushing = false;
Dirty.prototype._recreateWriteStream = function(){
var self = this;
this._writeStream = fs.createWriteStream(this.path, {
encoding: 'utf-8',
flags: 'a'
});

if (!self._queue.length) {
self.emit('drain');
} else {
self._maybeFlush();
}
});
this._writeStream.on('drain', function() {
self.flushing = false;
if (!self._queue.length) {
self.emit('drain');
} else {
self._maybeFlush();
}
});
};

Dirty.prototype._maybeFlush = function() {
Expand Down Expand Up @@ -207,7 +210,7 @@ Dirty.prototype._startCompacting = function() {
flags: 'w'
});
ws.on("error", function(){
self.emit('compactingFailed');
self.emit('compactingError');
});
ws.on('drain', function(){
self._moveCompactedDataOverOriginal();
Expand All @@ -217,14 +220,14 @@ Dirty.prototype._startCompacting = function() {

Dirty.prototype._moveCompactedDataOverOriginal = function() {
var self = this;
fs.rename(this._compactPath, self.path, function(err){
if (err) self.emit('compactingFailed');
fs.rename(this._compactPath, this.path, function(err){
self._recreateWriteStream();
if (err) self.emit('compactingError');
else self.emit('compacted');
});
}

Dirty.prototype.on('compacted', function(){
this._queueBackup = [];
this._endCompacting();
});

Expand All @@ -233,9 +236,9 @@ Dirty.prototype.on('compactingError', function(){
this._endCompacting();
});

Dirty.prototype._endCompacting = function(cb) {
Dirty.prototype._endCompacting = function() {
this._queueBackup = [];
this.compacting = false;
if (cb) cb();
this._maybeFlush();
};

Expand Down
129 changes: 85 additions & 44 deletions test/simple/test-dirty.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require('../common');
fsStub = global.GENTLY.stub('fs');
var Dirty = require('dirty'),
EventEmitter = require('events').EventEmitter,
dirtyLoad = Dirty.prototype._load,
Expand Down Expand Up @@ -54,7 +53,6 @@ test(function _load() {
(function testWithPath() {
var PATH = dirty.path = '/dirty.db',
READ_STREAM = {},
WRITE_STREAM = {},
readStreamEmit = {};

gently.expect(HIJACKED.fs, 'createReadStream', function (path, options) {
Expand All @@ -71,44 +69,11 @@ test(function _load() {
readStreamEmit[event] = cb;
return this;
});

gently.expect(HIJACKED.fs, 'createWriteStream', function (path, options) {
assert.equal(path, PATH);
assert.equal(options.flags, 'a');
assert.equal(options.encoding, 'utf-8');

return WRITE_STREAM;
});

gently.expect(WRITE_STREAM, 'on', function (event, cb) {
assert.strictEqual(event, 'drain');

(function testQueueEmpty() {
dirty._queue = [];
dirty.flushing = true;

gently.expect(dirty, 'emit', function (event) {
assert.strictEqual(event, 'drain');
});

cb();
assert.strictEqual(dirty.flushing, false);
})();

(function testQueueNotEmpty() {
dirty._queue = [1];
dirty.flushing = true;

gently.expect(dirty, '_maybeFlush');

cb();
assert.strictEqual(dirty.flushing, false);
})();
});

gently.expect(dirty, '_recreateWriteStream');

dirtyLoad.call(dirty);

assert.strictEqual(dirty._writeStream, WRITE_STREAM);
assert.strictEqual(dirty._readStream, READ_STREAM);

(function testReading() {
Expand Down Expand Up @@ -188,6 +153,48 @@ test(function _load() {
})();
});

test(function _recreateWriteStream(){
var WRITE_STREAM = {};
var PATH = 'foo/bar.baz';
dirty.path = PATH;

gently.expect(HIJACKED.fs, 'createWriteStream', function (path, options) {
assert.equal(path, PATH);
assert.equal(options.flags, 'a');
assert.equal(options.encoding, 'utf-8');

return WRITE_STREAM;
});

gently.expect(WRITE_STREAM, 'on', function (event, cb) {
assert.strictEqual(event, 'drain');

(function testQueueEmpty() {
dirty._queue = [];
dirty.flushing = true;

gently.expect(dirty, 'emit', function (event) {
assert.strictEqual(event, 'drain');
});

cb();
assert.strictEqual(dirty.flushing, false);
})();

(function testQueueNotEmpty() {
dirty._queue = [1];
dirty.flushing = true;

gently.expect(dirty, '_maybeFlush');

cb();
assert.strictEqual(dirty.flushing, false);
})();
});
dirty._recreateWriteStream();
assert.strictEqual(dirty._writeStream, WRITE_STREAM);
});

test(function get() {
var KEY = 'example', VAL = {};
dirty._docs[KEY] = VAL;
Expand Down Expand Up @@ -382,7 +389,7 @@ test(function _startCompacting(){
var ws = {};
function expectWriteStream(){
dirty.path = 'foo/bar.baz';
gently.expect(fsStub, "createWriteStream", function(path, obj){
gently.expect(HIJACKED.fs, "createWriteStream", function(path, obj){
assert.strictEqual("foo/bar.baz.compact", path);
assert.strictEqual('utf-8', obj.encoding);
assert.strictEqual('w', obj.flags);
Expand Down Expand Up @@ -413,7 +420,7 @@ test(function _startCompacting(){
gently.expect(ws, 'on', function(type, cb){
assert.strictEqual('error', type);
gently.expect(dirty, 'emit', function(evt){
assert.strictEqual('compactingFailed', evt);
assert.strictEqual('compactingError', evt);
});
cb();
gently.expect(ws, 'on');
Expand Down Expand Up @@ -447,25 +454,27 @@ test(function _startCompacting(){
test(function _moveCompactedDataOverOriginal() {
(function testRenamesCompactedFileToOriginal(){
dirty.path = 'foo/bar.baz'
gently.expect(fsStub, 'rename', function(src, dst){
gently.expect(HIJACKED.fs, 'rename', function(src, dst){
assert.strictEqual('foo/bar.baz.compact', src);
assert.strictEqual('foo/bar.baz', dst);
});
dirty._moveCompactedDataOverOriginal();
})();

(function testEmitsCompactingFailedIfRenameErrorsOut(){
gently.expect(fsStub, 'rename', function(a,b,cb){
gently.expect(HIJACKED.fs, 'rename', function(a,b,cb){
gently.expect(dirty, '_recreateWriteStream');
gently.expect(dirty, 'emit', function(evt){
assert.strictEqual('compactingFailed', evt);
assert.strictEqual('compactingError', evt);
});
cb(new Error(''));
});
dirty._moveCompactedDataOverOriginal();
})();

(function testEmitsCompactedIfRenameErrorsOut(){
gently.expect(fsStub, 'rename', function(a,b,cb){
gently.expect(HIJACKED.fs, 'rename', function(a,b,cb){
gently.expect(dirty, '_recreateWriteStream');
gently.expect(dirty, 'emit', function(evt){
assert.strictEqual('compacted', evt);
});
Expand All @@ -475,8 +484,40 @@ test(function _moveCompactedDataOverOriginal() {
})();
});

// test(function )
var _onCompactingComplete = function (evt){
(function testEmptiesTheBackupQueue(){
dirty._queueBackup = [1,2,3];
gently.expect(dirty, "_maybeFlush");
evt();
assert.deepEqual([],dirty._queueBackup);
})();

(function testSetsCompactingToFalse(){
dirty._queueBackup = [1,2,3];
gently.expect(dirty, "_maybeFlush");
evt();
assert.strictEqual(false, dirty.compacting);
})();
}

test(function _onCompactedResetsTheState(){
_onCompactingComplete(function(){
dirty.emit("compacted");
});
});

test(function _onCompactingErrorResetsTheStateToBeforeCompacting(){
(function testPrependsTheBackupQueueToTheQueue(){
dirty._queueBackup = ["tap", "2R"];
dirty._queue = ["destroy", "one", "land"];
dirty.emit('compactingError');
assert.deepEqual(["tap", "2R", "destroy", "one", "land"], dirty._queue);
})();

_onCompactingComplete(function(){
dirty.emit('compactingError');
});
});

test(function rm() {
var KEY = 'foo', CB = function() {};
Expand Down