Skip to content

Commit

Permalink
updated dirty to the lastest version from the original repo
Browse files Browse the repository at this point in the history
  • Loading branch information
pathsny committed May 13, 2013
1 parent f80f5a5 commit ca53e83
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 114 deletions.
69 changes: 44 additions & 25 deletions lib/dirty/dirty.js
Expand Up @@ -214,7 +214,6 @@ Dirty.prototype._flush = function() {
bundleStr = '',
key,
cbs = [];

this.flushing = true;

function callbacks(err, cbs) {
Expand Down Expand Up @@ -286,14 +285,13 @@ Dirty.prototype.__defineGetter__('redundantLength', function(){
Dirty.prototype.compact = function(cb) {
if (this.compacting) return;
var self = this;
this.compacting = true;

if (this.flushing) {
this._writeStream.once('drain', function(){
self._startCompacting();
});
this.once('drain', function(){
this.compact(cb);
});
} else {
this._startCompacting();
this.compacting = true;
this._startCompacting();
}
};

Expand All @@ -310,9 +308,6 @@ Dirty.prototype._startCompacting = function() {
ws.on("error", function(){
self.emit('compactingError');
});
ws.on('drain', function(){
self._moveCompactedDataOverOriginal();
});
this._writeCompactedData(ws);
};

Expand All @@ -333,27 +328,51 @@ Dirty.prototype._endCompacting = function() {
};

Dirty.prototype._writeCompactedData = function(ws) {
var bundleLength = 0;
var bundleStr = '';
var keys = [];
var self = this;
for (var k in this._docs) { keys.push(k) };

var writeToStream = function() {
ws.write(bundleStr);
bundleStr = '';
bundleLength = 0;
if (keys.length === 0) {
ws.once('finish', function(){
self._writeStream.once('finish', function(){
self._moveCompactedDataOverOriginal();
});
self._writeStream.end();
})
ws.end();
return;
}
var bundleStr = buildBundle();
var isDrained = ws.write(bundleStr);
if (isDrained) {
process.nextTick(writeToStream);
} else {
ws.once('drain', writeToStream);
}
}
for (var k in this._docs) {
var doc = this._docs[k];
if (this._compactingFilters.some(function(filterFn){
return filterFn(k, doc);

var buildBundle = function() {
var bundleLength = 0,
bundleStr = '';
for (var i=0; i< keys.length; i++) {
var doc = self._docs[keys[i]];
if (self._compactingFilters.some(function(filterFn){
return filterFn(keys[i], doc);
})) {
this._updateDocs(k, undefined, true);
self._updateDocs(keys[i], undefined, true);
continue;
}
bundleStr += JSON.stringify({key: k, val: doc})+'\n';
bundleStr += JSON.stringify({key: keys[i], val: doc})+'\n';
bundleLength++;
if (bundleLength >= this.writeBundle) {
writeToStream();
if (bundleLength >= self.writeBundle) {
keys = keys.slice(i+1);
return bundleStr;
}
};
}
keys = [];
return bundleStr;
}
writeToStream();
};

Expand Down Expand Up @@ -397,7 +416,7 @@ Dirty.prototype._updateIndex = function(index, key, newVal) {
};

Dirty.prototype._updateIndexes = function(key, newVal) {
for (index in this._indexFns) {
for (var index in this._indexFns) {
this._updateIndex(index, key, newVal);
};
};
Expand Down
52 changes: 0 additions & 52 deletions test/system/test-compact.js

This file was deleted.

37 changes: 0 additions & 37 deletions test/system/test-indexes.js

This file was deleted.

31 changes: 31 additions & 0 deletions test/test-api.js
Expand Up @@ -131,7 +131,38 @@ function dirtyAPITests(file) {

});

describe('clone behaviour', function(done){
after(cleanup)
var db = dirty(file);

it('will return numbers as they are', function(done){
db.set('key_n', 5);
assert.strictEqual(5, db.get('key_n'));
done();
});

it('will return strings as they are', function(done){
db.set('key_s', "jello");
assert.strictEqual("jello", db.get('key_s'));
done();
});

it('will return a clone of arrays', function(done){
var val = [1,"hello", 4];
db.set('key_a', val);
assert.deepEqual(val, db.get('key_a'));
done();
});

it('will return a clone of objects', function(done){
var val = {foo: 'bar'};
db.set('key_o', val);
assert.deepEqual(val, db.get('key_o'));
done();
});
});
});

}

dirtyAPITests('');
Expand Down
142 changes: 142 additions & 0 deletions test/test-compact.js
@@ -0,0 +1,142 @@
var config = require('./config'),
path = require('path'),
fs = require('fs'),
dirty = require(config.LIB_DIRTY),
assert = require('assert'),
db;
var exists = (fs.exists) ? fs.exists : path.exists;
var file = config.TMP_PATH + '/compacttest.dirty';
describe('compacting', function(){
beforeEach(function(done){
db = dirty(file);
db.once('load', function(){
db.set('red', 'lightning Bolt');
db.set('black', 'dark ritual');
db.set('blue', 'ancestral recall');
db.set('white', 'healing salve');
db.set('green', 'llanowar Elves');
db.once('drain', function(){
done();
})
});
});

afterEach(function (done) {
exists(file, function(doesExist) {
if (doesExist) {
fs.unlinkSync(file);
}
done();
});
});

var assertPristine = function() {
assert.strictEqual(0, db.redundantLength);
assert.strictEqual(
fs.readFileSync(file, 'utf-8'),
JSON.stringify({key: 'black', 'val': 'dark ritual'})+'\n'+
JSON.stringify({key: 'blue', 'val': 'ancestral recall'})+'\n'+
JSON.stringify({key: 'white', 'val': 'healing salve'})+'\n'+
JSON.stringify({key: 'green', 'val': 'giant growth'})+'\n'+
JSON.stringify({key: 'red', 'val': 'lightning bolt'})+'\n'
);
}

it('should accurately report length as number of rows', function(done){
assert.strictEqual(5, db.length);
done();
});

it('should report redundantLength as empty', function(){
assert.strictEqual(0, db.redundantLength);
});

it('should accurately display redundant length', function(done){
db.set('green', 'llanowar Elves');
db.set('green', 'giant growth');
db.rm('red');
db.rm('red');
assert.strictEqual(5, db.redundantLength);
done();
});

it('should compact out redundant rows', function(done){
db.set('green', 'llanowar Elves');
db.set('green', 'giant growth');
db.rm('red');
db.set('red', 'lightning bolt')
db.once('drain', function(){
db.compact();
db.on('compacted', function(){
assertPristine();
done();
})
});
});

it('should compact correctly with a small bundle write limit', function(done){
db.set('green', 'giant growth');
db.rm('red');
db.set('red', 'lightning bolt')
db.writeBundle = 2;
db.once('drain', function(){
db.compact();
db.once('compacted', function(){
assertPristine();
done();
})
});
});

it('should not compact while flushing', function(done){
var didFlush = false;
db.set('green', 'giant growth');
db.rm('red');
db.once('drain', function(){
assert.ok(!db.compacting);
didFlush = true;
});
db.set('red', 'lightning bolt');
db.compact();
db.once('compacted', function(){
assertPristine();
assert.ok(didFlush);
done();
})
});

it('should not flush while compacting', function(done){
db.compact();
var didCompact = false;
var listener = function(){
db._events['compacted'].splice(0,1);
assert.ok(!db.flushing);
didCompact = true;
};
db._events['compacted'] = [listener, db._events['compacted']];
db.once('drain', function(){
assert.ok(didCompact);
done();
})
db.set('red', 'lightning bolt');
});

it('should get rid of rows while compacting', function(done){
db.addCompactingFilter(function(key, val){
return /magic/.test(val);
});
db.set('purple', "it's magic");
db.set('green', 'giant growth');
db.rm('red');
db.set('red', 'lightning bolt')
assert.strictEqual(6, db.length);
db.on('drain', function(){
db.on('compacted', function(){
assertPristine();
assert.strictEqual(5, db.length);
done();
});
db.compact();
});
});
});

0 comments on commit ca53e83

Please sign in to comment.