Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locking mechanism for tempfile operation #535

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion browser-version/browser-specific/lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ function ensureDatafileIntegrity (filename, callback) {
return callback(null);
}

// Nothing to do, no data corruption possible in the brower
function waitForLock (filename, callback) {
return callback(null);
}

// Interface
module.exports.exists = exists;
Expand All @@ -92,4 +96,4 @@ module.exports.readFile = readFile;
module.exports.unlink = unlink;
module.exports.mkdirp = mkdirp;
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity;

module.exports.waitForLock = waitForLock;
6 changes: 6 additions & 0 deletions browser-version/out/nedb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3382,6 +3382,11 @@ function ensureDatafileIntegrity (filename, callback) {
return callback(null);
}

// Nothing to do, no data corruption possible in the brower
function waitForLock (filename, callback) {
return callback(null);
}


// Interface
module.exports.exists = exists;
Expand All @@ -3393,6 +3398,7 @@ module.exports.readFile = readFile;
module.exports.unlink = unlink;
module.exports.mkdirp = mkdirp;
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity;
module.exports.waitForLock = waitForLock;


},{"localforage":18}],13:[function(require,module,exports){
Expand Down
106 changes: 69 additions & 37 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

var fs = require('fs')
, lockfile = require('lockfile')
, mkdirp = require('mkdirp')
, async = require('async')
, path = require('path')
Expand All @@ -21,7 +22,8 @@ storage.unlink = fs.unlink;
storage.appendFile = fs.appendFile;
storage.readFile = fs.readFile;
storage.mkdirp = mkdirp;

storage.lock = lockfile.lock;
storage.unlock = lockfile.unlock;

/**
* Explicit name ...
Expand Down Expand Up @@ -73,6 +75,19 @@ storage.flushToStorage = function (options, callback) {
};


storage.waitForLock = function(filename, cb) {
var callback = cb || function () {};

storage.lock(filename, {retries: 5, retriesWait: 1}, function(err) {
if (err) {
storage.waitForLock(filename, cb);
return;
}

cb();
});
};

/**
* Fully write or rewrite the datafile, immune to crashes during the write operation (data will not be lost)
* @param {String} filename
Expand All @@ -81,28 +96,33 @@ storage.flushToStorage = function (options, callback) {
*/
storage.crashSafeWriteFile = function (filename, data, cb) {
var callback = cb || function () {}
, tempFilename = filename + '~';

async.waterfall([
async.apply(storage.flushToStorage, { filename: path.dirname(filename), isDir: true })
, function (cb) {
storage.exists(filename, function (exists) {
if (exists) {
storage.flushToStorage(filename, function (err) { return cb(err); });
} else {
return cb();
, tempFilename = filename + '~'
, lockFilename = filename + '.lock';

storage.waitForLock(lockFilename,
function() {
async.waterfall([
async.apply(storage.flushToStorage, { filename: path.dirname(filename), isDir: true })
, function (cb) {
storage.exists(filename, function (exists) {
if (exists) {
storage.flushToStorage(filename, function (err) { return cb(err); });
} else {
return cb();
}
});
}
});
}
, function (cb) {
storage.writeFile(tempFilename, data, function (err) { return cb(err); });
}
, async.apply(storage.flushToStorage, tempFilename)
, function (cb) {
storage.rename(tempFilename, filename, function (err) { return cb(err); });
, function (cb) {
storage.writeFile(tempFilename, data, function (err) { return cb(err); });
}
, async.apply(storage.flushToStorage, tempFilename)
, function (cb) {
storage.rename(tempFilename, filename, function (err) { return cb(err); });
}
, async.apply(storage.flushToStorage, { filename: path.dirname(filename), isDir: true })
], function (err) { storage.unlock(lockFilename); return callback(err); })
}
, async.apply(storage.flushToStorage, { filename: path.dirname(filename), isDir: true })
], function (err) { return callback(err); })
);
};


Expand All @@ -112,23 +132,35 @@ storage.crashSafeWriteFile = function (filename, data, cb) {
* @param {Function} callback signature: err
*/
storage.ensureDatafileIntegrity = function (filename, callback) {
var tempFilename = filename + '~';

storage.exists(filename, function (filenameExists) {
// Write was successful
if (filenameExists) { return callback(null); }

storage.exists(tempFilename, function (oldFilenameExists) {
// New database
if (!oldFilenameExists) {
return storage.writeFile(filename, '', 'utf8', function (err) { callback(err); });
}
var tempFilename = filename + '~'
, lockFilename = filename + '.lock';

storage.waitForLock(lockFilename,
function() {
storage.exists(filename, function (filenameExists) {
// Write was successful
if (filenameExists) {
return callback(null);
}

// Write failed, use old version
storage.rename(tempFilename, filename, function (err) { return callback(err); });
});
});
};
storage.exists(tempFilename, function (oldFilenameExists) {
// New database
if (!oldFilenameExists) {
return storage.writeFile(filename, '', 'utf8', function (err) {
callback(err);
});
}

// Write failed, use old version
storage.rename(tempFilename, filename, function (err) {
return callback(err);
});
});
});
storage.unlock(lockFilename);
}
);
}



Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"async": "0.2.10",
"binary-search-tree": "0.2.5",
"localforage": "^1.3.0",
"lockfile": "^1.0.4",
"mkdirp": "~0.5.1",
"underscore": "~1.4.4"
},
Expand Down