Moved all crash safe operations to storage

pull/2/head
Louis Chatriot 9 years ago
parent 26441f1aae
commit d1e6d14b53
  1. 65
      lib/persistence.js
  2. 90
      lib/storage.js
  3. 1
      package.json
  4. 257
      test/persistence.test.js

@ -28,13 +28,8 @@ function Persistence (options) {
this.filename = this.db.filename; this.filename = this.db.filename;
this.corruptAlertThreshold = options.corruptAlertThreshold !== undefined ? options.corruptAlertThreshold : 0.1; this.corruptAlertThreshold = options.corruptAlertThreshold !== undefined ? options.corruptAlertThreshold : 0.1;
if (!this.inMemoryOnly && this.filename) { if (!this.inMemoryOnly && this.filename && this.filename.charAt(this.filename.length - 1) === '~') {
if (this.filename.charAt(this.filename.length - 1) === '~') { throw "The datafile name can't end with a ~, which is reserved for crash safe backup files";
throw "The datafile name can't end with a ~, which is reserved for automatic backup files";
} else {
this.tempFilename = this.filename + '~';
this.oldFilename = this.filename + '~~';
}
} }
// After serialization and before deserialization hooks with some basic sanity checks // After serialization and before deserialization hooks with some basic sanity checks
@ -65,8 +60,6 @@ function Persistence (options) {
console.log("See https://github.com/rogerwang/node-webkit/issues/500"); console.log("See https://github.com/rogerwang/node-webkit/issues/500");
console.log("=================================================================="); console.log("==================================================================");
this.filename = Persistence.getNWAppFilename(options.nodeWebkitAppName, this.filename); this.filename = Persistence.getNWAppFilename(options.nodeWebkitAppName, this.filename);
this.tempFilename = Persistence.getNWAppFilename(options.nodeWebkitAppName, this.tempFilename);
this.oldFilename = Persistence.getNWAppFilename(options.nodeWebkitAppName, this.oldFilename);
} }
}; };
@ -83,13 +76,6 @@ Persistence.ensureDirectoryExists = function (dir, cb) {
}; };
Persistence.ensureFileDoesntExist = function (file, callback) {
storage.exists(file, function (exists) {
if (!exists) { return callback(null); }
storage.unlink(file, function (err) { return callback(err); });
});
};
/** /**
@ -148,26 +134,7 @@ Persistence.prototype.persistCachedDatabase = function (cb) {
} }
}); });
async.waterfall([ storage.crashSafeWriteFile(this.filename, toPersist, callback);
async.apply(Persistence.ensureFileDoesntExist, self.tempFilename)
, async.apply(Persistence.ensureFileDoesntExist, self.oldFilename)
, function (cb) {
storage.exists(self.filename, function (exists) {
if (exists) {
storage.rename(self.filename, self.oldFilename, function (err) { return cb(err); });
} else {
return cb();
}
});
}
, function (cb) {
storage.writeFile(self.tempFilename, toPersist, function (err) { return cb(err); });
}
, function (cb) {
storage.rename(self.tempFilename, self.filename, function (err) { return cb(err); });
}
, async.apply(Persistence.ensureFileDoesntExist, self.oldFilename)
], function (err) { if (err) { return callback(err); } else { return callback(null); } })
}; };
@ -279,30 +246,6 @@ Persistence.prototype.treatRawData = function (rawData) {
}; };
/**
* Ensure that this.filename contains the most up-to-date version of the data
* Even if a loadDatabase crashed before
*/
Persistence.prototype.ensureDatafileIntegrity = function (callback) {
var self = this ;
storage.exists(self.filename, function (filenameExists) {
// Write was successful
if (filenameExists) { return callback(null); }
storage.exists(self.oldFilename, function (oldFilenameExists) {
// New database
if (!oldFilenameExists) {
return storage.writeFile(self.filename, '', 'utf8', function (err) { callback(err); });
}
// Write failed, use old version
storage.rename(self.oldFilename, self.filename, function (err) { return callback(err); });
});
});
};
/** /**
* Load the database * Load the database
* 1) Create all indexes * 1) Create all indexes
@ -326,7 +269,7 @@ Persistence.prototype.loadDatabase = function (cb) {
async.waterfall([ async.waterfall([
function (cb) { function (cb) {
Persistence.ensureDirectoryExists(path.dirname(self.filename), function (err) { Persistence.ensureDirectoryExists(path.dirname(self.filename), function (err) {
self.ensureDatafileIntegrity(function (exists) { storage.ensureDatafileIntegrity(self.filename, function (exists) {
storage.readFile(self.filename, 'utf8', function (err, rawData) { storage.readFile(self.filename, 'utf8', function (err, rawData) {
if (err) { return cb(err); } if (err) { return cb(err); }

@ -4,12 +4,98 @@
* For a browser-side database it's localStorage when supported * For a browser-side database it's localStorage when supported
* *
* This version is the Node.js/Node Webkit version * This version is the Node.js/Node Webkit version
* It's essentially fs, mkdirp and crash safe write and read functions
*/ */
var fs = require('fs') var fs = require('fs')
, mkdirp = require('mkdirp') , mkdirp = require('mkdirp')
, async = require('async')
, storage = {}
; ;
storage.exists = fs.exists;
storage.rename = fs.rename;
storage.writeFile = fs.writeFile;
storage.unlink = fs.unlink;
storage.appendFile = fs.appendFile;
storage.readFile = fs.readFile;
storage.mkdirp = mkdirp;
module.exports = fs;
module.exports.mkdirp = mkdirp; /**
* Explicit name ...
*/
storage.ensureFileDoesntExist = function (file, callback) {
storage.exists(file, function (exists) {
if (!exists) { return callback(null); }
storage.unlink(file, function (err) { return callback(err); });
});
};
/**
* Fully write or rewrite the datafile, immune to crashes during the write operation (data will not be lost)
* @param {String} filename
* @param {String} data
* @param {Function} cb Optional callback, signature: err
*/
storage.crashSafeWriteFile = function (filename, data, cb) {
var callback = cb || function () {}
, tempFilename = filename + '~'
, oldFilename = filename + '~~'
;
async.waterfall([
async.apply(storage.ensureFileDoesntExist, tempFilename)
, async.apply(storage.ensureFileDoesntExist, oldFilename)
, function (cb) {
storage.exists(filename, function (exists) {
if (exists) {
storage.rename(filename, oldFilename, function (err) { return cb(err); });
} else {
return cb();
}
});
}
, function (cb) {
storage.writeFile(tempFilename, data, function (err) { return cb(err); });
}
, function (cb) {
storage.rename(tempFilename, filename, function (err) { return cb(err); });
}
, async.apply(storage.ensureFileDoesntExist, oldFilename)
], function (err) { if (err) { return callback(err); } else { return callback(null); } })
};
/**
* Ensure the datafile contains all the data, even if there was a crash during a full file write
* @param {String} filename
* @param {Function} callback signature: err
*/
storage.ensureDatafileIntegrity = function (filename, callback) {
var tempFilename = filename + '~'
, oldFilename = filename + '~~'
;
storage.exists(filename, function (filenameExists) {
// Write was successful
if (filenameExists) { return callback(null); }
storage.exists(oldFilename, function (oldFilenameExists) {
// New database
if (!oldFilenameExists) {
return storage.writeFile(filename, '', 'utf8', function (err) { callback(err); });
}
// Write failed, use old version
storage.rename(oldFilename, filename, function (err) { return callback(err); });
});
});
};
// Interface
module.exports = storage;

@ -22,6 +22,7 @@
"dependencies": { "dependencies": {
"async": "0.2.10", "async": "0.2.10",
"binary-search-tree": "0.2.4", "binary-search-tree": "0.2.4",
"localforage": "^1.3.0",
"mkdirp": "~0.5.1", "mkdirp": "~0.5.1",
"underscore": "~1.4.4" "underscore": "~1.4.4"
}, },

@ -9,8 +9,9 @@ var should = require('chai').should()
, customUtils = require('../lib/customUtils') , customUtils = require('../lib/customUtils')
, Datastore = require('../lib/datastore') , Datastore = require('../lib/datastore')
, Persistence = require('../lib/persistence') , Persistence = require('../lib/persistence')
, storage = require('../lib/storage')
, child_process = require('child_process') , child_process = require('child_process')
; ;
describe('Persistence', function () { describe('Persistence', function () {
@ -32,22 +33,22 @@ describe('Persistence', function () {
}); });
} }
, function (cb) { , function (cb) {
d.loadDatabase(function (err) { d.loadDatabase(function (err) {
assert.isNull(err); assert.isNull(err);
d.getAllData().length.should.equal(0); d.getAllData().length.should.equal(0);
return cb(); return cb();
}); });
} }
], done); ], done);
}); });
it('Every line represents a document', function () { it('Every line represents a document', function () {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ _id: "3", nested: { today: now } }) model.serialize({ _id: "3", nested: { today: now } })
, treatedData = d.persistence.treatRawData(rawData).data , treatedData = d.persistence.treatRawData(rawData).data
; ;
treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.sort(function (a, b) { return a._id - b._id; });
treatedData.length.should.equal(3); treatedData.length.should.equal(3);
@ -59,10 +60,10 @@ describe('Persistence', function () {
it('Badly formatted lines have no impact on the treated data', function () { it('Badly formatted lines have no impact on the treated data', function () {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
'garbage\n' + 'garbage\n' +
model.serialize({ _id: "3", nested: { today: now } }) model.serialize({ _id: "3", nested: { today: now } })
, treatedData = d.persistence.treatRawData(rawData).data , treatedData = d.persistence.treatRawData(rawData).data
; ;
treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.sort(function (a, b) { return a._id - b._id; });
treatedData.length.should.equal(2); treatedData.length.should.equal(2);
@ -73,10 +74,10 @@ describe('Persistence', function () {
it('Well formatted lines that have no _id are not included in the data', function () { it('Well formatted lines that have no _id are not included in the data', function () {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ nested: { today: now } }) model.serialize({ nested: { today: now } })
, treatedData = d.persistence.treatRawData(rawData).data , treatedData = d.persistence.treatRawData(rawData).data
; ;
treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.sort(function (a, b) { return a._id - b._id; });
treatedData.length.should.equal(2); treatedData.length.should.equal(2);
@ -87,10 +88,10 @@ describe('Persistence', function () {
it('If two lines concern the same doc (= same _id), the last one is the good version', function () { it('If two lines concern the same doc (= same _id), the last one is the good version', function () {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ _id: "1", nested: { today: now } }) model.serialize({ _id: "1", nested: { today: now } })
, treatedData = d.persistence.treatRawData(rawData).data , treatedData = d.persistence.treatRawData(rawData).data
; ;
treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.sort(function (a, b) { return a._id - b._id; });
treatedData.length.should.equal(2); treatedData.length.should.equal(2);
@ -101,11 +102,11 @@ describe('Persistence', function () {
it('If a doc contains $$deleted: true, that means we need to remove it from the data', function () { it('If a doc contains $$deleted: true, that means we need to remove it from the data', function () {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ _id: "2", hello: 'world' }) + '\n' +
model.serialize({ _id: "1", $$deleted: true }) + '\n' + model.serialize({ _id: "1", $$deleted: true }) + '\n' +
model.serialize({ _id: "3", today: now }) model.serialize({ _id: "3", today: now })
, treatedData = d.persistence.treatRawData(rawData).data , treatedData = d.persistence.treatRawData(rawData).data
; ;
treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.sort(function (a, b) { return a._id - b._id; });
treatedData.length.should.equal(2); treatedData.length.should.equal(2);
@ -116,10 +117,10 @@ describe('Persistence', function () {
it('If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before', function () { it('If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before', function () {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", $$deleted: true }) + '\n' + model.serialize({ _id: "2", $$deleted: true }) + '\n' +
model.serialize({ _id: "3", today: now }) model.serialize({ _id: "3", today: now })
, treatedData = d.persistence.treatRawData(rawData).data , treatedData = d.persistence.treatRawData(rawData).data
; ;
treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.sort(function (a, b) { return a._id - b._id; });
treatedData.length.should.equal(2); treatedData.length.should.equal(2);
@ -130,11 +131,11 @@ describe('Persistence', function () {
it('If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options', function () { it('If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options', function () {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ $$indexCreated: { fieldName: "test", unique: true } }) + '\n' + model.serialize({ $$indexCreated: { fieldName: "test", unique: true } }) + '\n' +
model.serialize({ _id: "3", today: now }) model.serialize({ _id: "3", today: now })
, treatedData = d.persistence.treatRawData(rawData).data , treatedData = d.persistence.treatRawData(rawData).data
, indexes = d.persistence.treatRawData(rawData).indexes , indexes = d.persistence.treatRawData(rawData).indexes
; ;
Object.keys(indexes).length.should.equal(1); Object.keys(indexes).length.should.equal(1);
assert.deepEqual(indexes.test, { fieldName: "test", unique: true }); assert.deepEqual(indexes.test, { fieldName: "test", unique: true });
@ -181,7 +182,7 @@ describe('Persistence', function () {
var data = d.getAllData() var data = d.getAllData()
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) , doc1 = _.find(data, function (doc) { return doc.a === 1; })
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) , doc2 = _.find(data, function (doc) { return doc.a === 2; })
; ;
assert.isNull(err); assert.isNull(err);
data.length.should.equal(2); data.length.should.equal(2);
doc1.a.should.equal(1); doc1.a.should.equal(1);
@ -191,7 +192,7 @@ describe('Persistence', function () {
var data = d.getAllData() var data = d.getAllData()
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) , doc1 = _.find(data, function (doc) { return doc.a === 1; })
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) , doc2 = _.find(data, function (doc) { return doc.a === 2; })
; ;
assert.isNull(err); assert.isNull(err);
data.length.should.equal(2); data.length.should.equal(2);
doc1.a.should.equal(1); doc1.a.should.equal(1);
@ -212,7 +213,7 @@ describe('Persistence', function () {
var data = d.getAllData() var data = d.getAllData()
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) , doc1 = _.find(data, function (doc) { return doc.a === 1; })
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) , doc2 = _.find(data, function (doc) { return doc.a === 2; })
; ;
assert.isNull(err); assert.isNull(err);
data.length.should.equal(2); data.length.should.equal(2);
doc1.a.should.equal(1); doc1.a.should.equal(1);
@ -240,7 +241,7 @@ describe('Persistence', function () {
var data = d.getAllData() var data = d.getAllData()
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) , doc1 = _.find(data, function (doc) { return doc.a === 1; })
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) , doc2 = _.find(data, function (doc) { return doc.a === 2; })
; ;
assert.isNull(err); assert.isNull(err);
data.length.should.equal(2); data.length.should.equal(2);
doc1.a.should.equal(1); doc1.a.should.equal(1);
@ -250,9 +251,9 @@ describe('Persistence', function () {
assert.isNull(err); assert.isNull(err);
d.loadDatabase(function (err) { d.loadDatabase(function (err) {
var data = d.getAllData() var data = d.getAllData()
, doc1 = _.find(data, function (doc) { return doc.a === 1; }) , doc1 = _.find(data, function (doc) { return doc.a === 1; })
, doc2 = _.find(data, function (doc) { return doc.a === 2; }) , doc2 = _.find(data, function (doc) { return doc.a === 2; })
, doc3 = _.find(data, function (doc) { return doc.a === 3; }) , doc3 = _.find(data, function (doc) { return doc.a === 3; })
; ;
assert.isNull(err); assert.isNull(err);
data.length.should.equal(1); data.length.should.equal(1);
@ -272,7 +273,7 @@ describe('Persistence', function () {
var corruptTestFilename = 'workspace/corruptTest.db' var corruptTestFilename = 'workspace/corruptTest.db'
, fakeData = '{"_id":"one","hello":"world"}\n' + 'Some corrupt data\n' + '{"_id":"two","hello":"earth"}\n' + '{"_id":"three","hello":"you"}\n' , fakeData = '{"_id":"one","hello":"world"}\n' + 'Some corrupt data\n' + '{"_id":"two","hello":"earth"}\n' + '{"_id":"three","hello":"you"}\n'
, d , d
; ;
fs.writeFileSync(corruptTestFilename, fakeData, "utf8"); fs.writeFileSync(corruptTestFilename, fakeData, "utf8");
// Default corruptAlertThreshold // Default corruptAlertThreshold
@ -305,13 +306,13 @@ describe('Persistence', function () {
it("Declaring only one hook will throw an exception to prevent data loss", function (done) { it("Declaring only one hook will throw an exception to prevent data loss", function (done) {
var hookTestFilename = 'workspace/hookTest.db' var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () { storage.ensureFileDoesntExist(hookTestFilename, function () {
fs.writeFileSync(hookTestFilename, "Some content", "utf8"); fs.writeFileSync(hookTestFilename, "Some content", "utf8");
(function () { (function () {
new Datastore({ filename: hookTestFilename, autoload: true new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as , afterSerialization: as
}); });
}).should.throw(); }).should.throw();
// Data file left untouched // Data file left untouched
@ -320,7 +321,7 @@ describe('Persistence', function () {
(function () { (function () {
new Datastore({ filename: hookTestFilename, autoload: true new Datastore({ filename: hookTestFilename, autoload: true
, beforeDeserialization: bd , beforeDeserialization: bd
}); });
}).should.throw(); }).should.throw();
// Data file left untouched // Data file left untouched
@ -332,14 +333,14 @@ describe('Persistence', function () {
it("Declaring two hooks that are not reverse of one another will cause an exception to prevent data loss", function (done) { it("Declaring two hooks that are not reverse of one another will cause an exception to prevent data loss", function (done) {
var hookTestFilename = 'workspace/hookTest.db' var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () { storage.ensureFileDoesntExist(hookTestFilename, function () {
fs.writeFileSync(hookTestFilename, "Some content", "utf8"); fs.writeFileSync(hookTestFilename, "Some content", "utf8");
(function () { (function () {
new Datastore({ filename: hookTestFilename, autoload: true new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as , afterSerialization: as
, beforeDeserialization: function (s) { return s; } , beforeDeserialization: function (s) { return s; }
}); });
}).should.throw(); }).should.throw();
// Data file left untouched // Data file left untouched
@ -351,18 +352,18 @@ describe('Persistence', function () {
it("A serialization hook can be used to transform data before writing new state to disk", function (done) { it("A serialization hook can be used to transform data before writing new state to disk", function (done) {
var hookTestFilename = 'workspace/hookTest.db' var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () { storage.ensureFileDoesntExist(hookTestFilename, function () {
var d = new Datastore({ filename: hookTestFilename, autoload: true var d = new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as , afterSerialization: as
, beforeDeserialization: bd , beforeDeserialization: bd
}) })
; ;
d.insert({ hello: "world" }, function () { d.insert({ hello: "world" }, function () {
var _data = fs.readFileSync(hookTestFilename, 'utf8') var _data = fs.readFileSync(hookTestFilename, 'utf8')
, data = _data.split('\n') , data = _data.split('\n')
, doc0 = bd(data[0]) , doc0 = bd(data[0])
; ;
data.length.should.equal(2); data.length.should.equal(2);
@ -378,7 +379,7 @@ describe('Persistence', function () {
, data = _data.split('\n') , data = _data.split('\n')
, doc0 = bd(data[0]) , doc0 = bd(data[0])
, doc1 = bd(data[1]) , doc1 = bd(data[1])
; ;
data.length.should.equal(3); data.length.should.equal(3);
@ -401,7 +402,7 @@ describe('Persistence', function () {
, doc0 = bd(data[0]) , doc0 = bd(data[0])
, doc1 = bd(data[1]) , doc1 = bd(data[1])
, idx = bd(data[2]) , idx = bd(data[2])
; ;
data.length.should.equal(4); data.length.should.equal(4);
@ -430,12 +431,12 @@ describe('Persistence', function () {
it("Use serialization hook when persisting cached database or compacting", function (done) { it("Use serialization hook when persisting cached database or compacting", function (done) {
var hookTestFilename = 'workspace/hookTest.db' var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () { storage.ensureFileDoesntExist(hookTestFilename, function () {
var d = new Datastore({ filename: hookTestFilename, autoload: true var d = new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as , afterSerialization: as
, beforeDeserialization: bd , beforeDeserialization: bd
}) })
; ;
d.insert({ hello: "world" }, function () { d.insert({ hello: "world" }, function () {
d.update({ hello: "world" }, { $set: { hello: "earth" } }, {}, function () { d.update({ hello: "world" }, { $set: { hello: "earth" } }, {}, function () {
@ -446,7 +447,7 @@ describe('Persistence', function () {
, doc1 = bd(data[1]) , doc1 = bd(data[1])
, idx = bd(data[2]) , idx = bd(data[2])
, _id , _id
; ;
data.length.should.equal(4); data.length.should.equal(4);
@ -469,7 +470,7 @@ describe('Persistence', function () {
, data = _data.split('\n') , data = _data.split('\n')
, doc0 = bd(data[0]) , doc0 = bd(data[0])
, idx = bd(data[1]) , idx = bd(data[1])
; ;
data.length.should.equal(3); data.length.should.equal(3);
@ -492,12 +493,12 @@ describe('Persistence', function () {
it("Deserialization hook is correctly used when loading data", function (done) { it("Deserialization hook is correctly used when loading data", function (done) {
var hookTestFilename = 'workspace/hookTest.db' var hookTestFilename = 'workspace/hookTest.db'
Persistence.ensureFileDoesntExist(hookTestFilename, function () { storage.ensureFileDoesntExist(hookTestFilename, function () {
var d = new Datastore({ filename: hookTestFilename, autoload: true var d = new Datastore({ filename: hookTestFilename, autoload: true
, afterSerialization: as , afterSerialization: as
, beforeDeserialization: bd , beforeDeserialization: bd
}) })
; ;
d.insert({ hello: "world" }, function (err, doc) { d.insert({ hello: "world" }, function (err, doc) {
var _id = doc._id; var _id = doc._id;
@ -507,16 +508,16 @@ describe('Persistence', function () {
d.ensureIndex({ fieldName: 'idefix' }, function () { d.ensureIndex({ fieldName: 'idefix' }, function () {
var _data = fs.readFileSync(hookTestFilename, 'utf8') var _data = fs.readFileSync(hookTestFilename, 'utf8')
, data = _data.split('\n') , data = _data.split('\n')
; ;
data.length.should.equal(6); data.length.should.equal(6);
// Everything is deserialized correctly, including deletes and indexes // Everything is deserialized correctly, including deletes and indexes
var d = new Datastore({ filename: hookTestFilename var d = new Datastore({ filename: hookTestFilename
, afterSerialization: as , afterSerialization: as
, beforeDeserialization: bd , beforeDeserialization: bd
}) })
; ;
d.loadDatabase(function () { d.loadDatabase(function () {
d.find({}, function (err, docs) { d.find({}, function (err, docs) {
docs.length.should.equal(1); docs.length.should.equal(1);
@ -558,7 +559,7 @@ describe('Persistence', function () {
fs.existsSync('workspace/it.db').should.equal(false); fs.existsSync('workspace/it.db').should.equal(false);
fs.existsSync('workspace/it.db~~').should.equal(false); fs.existsSync('workspace/it.db~~').should.equal(false);
p.ensureDatafileIntegrity(function (err) { storage.ensureDatafileIntegrity(p.filename, function (err) {
assert.isNull(err); assert.isNull(err);
fs.existsSync('workspace/it.db').should.equal(true); fs.existsSync('workspace/it.db').should.equal(true);
@ -581,7 +582,7 @@ describe('Persistence', function () {
fs.existsSync('workspace/it.db').should.equal(true); fs.existsSync('workspace/it.db').should.equal(true);
fs.existsSync('workspace/it.db~~').should.equal(false); fs.existsSync('workspace/it.db~~').should.equal(false);
p.ensureDatafileIntegrity(function (err) { storage.ensureDatafileIntegrity(p.filename, function (err) {
assert.isNull(err); assert.isNull(err);
fs.existsSync('workspace/it.db').should.equal(true); fs.existsSync('workspace/it.db').should.equal(true);
@ -604,7 +605,7 @@ describe('Persistence', function () {
fs.existsSync('workspace/it.db').should.equal(false); fs.existsSync('workspace/it.db').should.equal(false);
fs.existsSync('workspace/it.db~~').should.equal(true); fs.existsSync('workspace/it.db~~').should.equal(true);
p.ensureDatafileIntegrity(function (err) { storage.ensureDatafileIntegrity(p.filename, function (err) {
assert.isNull(err); assert.isNull(err);
fs.existsSync('workspace/it.db').should.equal(true); fs.existsSync('workspace/it.db').should.equal(true);
@ -628,7 +629,7 @@ describe('Persistence', function () {
fs.existsSync('workspace/it.db').should.equal(true); fs.existsSync('workspace/it.db').should.equal(true);
fs.existsSync('workspace/it.db~~').should.equal(true); fs.existsSync('workspace/it.db~~').should.equal(true);
theDb.persistence.ensureDatafileIntegrity(function (err) { storage.ensureDatafileIntegrity(theDb.persistence.filename, function (err) {
assert.isNull(err); assert.isNull(err);
fs.existsSync('workspace/it.db').should.equal(true); fs.existsSync('workspace/it.db').should.equal(true);
@ -761,76 +762,76 @@ describe('Persistence', function () {
var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2; var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2;
async.waterfall([ async.waterfall([
async.apply(Persistence.ensureFileDoesntExist, dbFile) async.apply(storage.ensureFileDoesntExist, dbFile)
, async.apply(Persistence.ensureFileDoesntExist, dbFile + '~') , async.apply(storage.ensureFileDoesntExist, dbFile + '~')
, async.apply(Persistence.ensureFileDoesntExist, dbFile + '~~') , async.apply(storage.ensureFileDoesntExist, dbFile + '~~')
, function (cb) { , function (cb) {
theDb = new Datastore({ filename: dbFile }); theDb = new Datastore({ filename: dbFile });
theDb.loadDatabase(cb); theDb.loadDatabase(cb);
} }
, function (cb) { , function (cb) {
theDb.find({}, function (err, docs) { theDb.find({}, function (err, docs) {
assert.isNull(err); assert.isNull(err);
docs.length.should.equal(0); docs.length.should.equal(0);
return cb(); return cb();
}); });
} }
, function (cb) { , function (cb) {
theDb.insert({ a: 'hello' }, function (err, _doc1) { theDb.insert({ a: 'hello' }, function (err, _doc1) {
assert.isNull(err); assert.isNull(err);
doc1 = _doc1; doc1 = _doc1;
theDb.insert({ a: 'world' }, function (err, _doc2) { theDb.insert({ a: 'world' }, function (err, _doc2) {
assert.isNull(err); assert.isNull(err);
doc2 = _doc2; doc2 = _doc2;
return cb(); return cb();
}); });
}); });
} }
, function (cb) { , function (cb) {
theDb.find({}, function (err, docs) { theDb.find({}, function (err, docs) {
assert.isNull(err); assert.isNull(err);
docs.length.should.equal(2); docs.length.should.equal(2);
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); _.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello');
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); _.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world');
return cb(); return cb();
}); });
} }
, function (cb) { , function (cb) {
theDb.loadDatabase(cb); theDb.loadDatabase(cb);
} }
, function (cb) { // No change , function (cb) { // No change
theDb.find({}, function (err, docs) { theDb.find({}, function (err, docs) {
assert.isNull(err); assert.isNull(err);
docs.length.should.equal(2); docs.length.should.equal(2);
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); _.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello');
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); _.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world');
return cb(); return cb();
}); });
} }
, function (cb) { , function (cb) {
fs.existsSync(dbFile).should.equal(true); fs.existsSync(dbFile).should.equal(true);
fs.existsSync(dbFile + '~').should.equal(false); fs.existsSync(dbFile + '~').should.equal(false);
fs.existsSync(dbFile + '~~').should.equal(false); fs.existsSync(dbFile + '~~').should.equal(false);
return cb(); return cb();
} }
, function (cb) { , function (cb) {
theDb2 = new Datastore({ filename: dbFile }); theDb2 = new Datastore({ filename: dbFile });
theDb2.loadDatabase(cb); theDb2.loadDatabase(cb);
} }
, function (cb) { // No change in second db , function (cb) { // No change in second db
theDb2.find({}, function (err, docs) { theDb2.find({}, function (err, docs) {
assert.isNull(err); assert.isNull(err);
docs.length.should.equal(2); docs.length.should.equal(2);
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello'); _.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello');
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world'); _.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world');
return cb(); return cb();
}); });
} }
, function (cb) { , function (cb) {
fs.existsSync(dbFile).should.equal(true); fs.existsSync(dbFile).should.equal(true);
fs.existsSync(dbFile + '~').should.equal(false); fs.existsSync(dbFile + '~').should.equal(false);
fs.existsSync(dbFile + '~~').should.equal(false); fs.existsSync(dbFile + '~~').should.equal(false);
return cb(); return cb();
} }
], done); ], done);
}); });
@ -891,7 +892,7 @@ describe('Persistence', function () {
describe('ensureFileDoesntExist', function () { describe('ensureFileDoesntExist', function () {
it('Doesnt do anything if file already doesnt exist', function (done) { it('Doesnt do anything if file already doesnt exist', function (done) {
Persistence.ensureFileDoesntExist('workspace/nonexisting', function (err) { storage.ensureFileDoesntExist('workspace/nonexisting', function (err) {
assert.isNull(err); assert.isNull(err);
fs.existsSync('workspace/nonexisting').should.equal(false); fs.existsSync('workspace/nonexisting').should.equal(false);
done(); done();
@ -902,7 +903,7 @@ describe('Persistence', function () {
fs.writeFileSync('workspace/existing', 'hello world', 'utf8'); fs.writeFileSync('workspace/existing', 'hello world', 'utf8');
fs.existsSync('workspace/existing').should.equal(true); fs.existsSync('workspace/existing').should.equal(true);
Persistence.ensureFileDoesntExist('workspace/existing', function (err) { storage.ensureFileDoesntExist('workspace/existing', function (err) {
assert.isNull(err); assert.isNull(err);
fs.existsSync('workspace/existing').should.equal(false); fs.existsSync('workspace/existing').should.equal(false);
done(); done();

Loading…
Cancel
Save