gridfs-locks
Advanced tools
Comparing version 1.2.4 to 1.3.0
@@ -0,1 +1,7 @@ | ||
### 1.3.0 | ||
- Added support for the new node.js 2.0.x native MongoDB driver | ||
- Write Locks now clear the `write_req` flag when released so that chained writes won't block all readers for long periods of time. | ||
- Updated unit test dependencies | ||
### 1.2.4 | ||
@@ -2,0 +8,0 @@ |
62
index.js
@@ -23,2 +23,12 @@ /*********************************************************************** | ||
var isMongoDriver20 = function (collection, callback) { | ||
collection.insert({testdoc: true}, function (err, res) { | ||
if (err) { return callback(err); } | ||
collection.findAndRemove({testdoc: true}, [['testdoc', 1]], function (err, res) { | ||
if (err) { return callback(err); } | ||
callback(null, (res.ok === 1)); | ||
}); | ||
}); | ||
}; | ||
// | ||
@@ -66,11 +76,13 @@ // Parameters: | ||
db.collection(collectionName, function(err, collection) { | ||
if (err) { return emitError(self, err); } | ||
// Ensure unique files_id so there can only be one lock doc per file | ||
collection.ensureIndex([['files_id', 1]], {unique:true}, function(err, index) { | ||
isMongoDriver20(collection, function (err, is20) { | ||
if (err) { return emitError(self, err); } | ||
self.collection = collection; | ||
self.emit('ready'); | ||
self._isMongoDriver20 = is20; | ||
// Ensure unique files_id so there can only be one lock doc per file | ||
collection.ensureIndex([['files_id', 1]], {unique:true}, function(err, index) { | ||
if (err) { return emitError(self, err); } | ||
self.collection = collection; | ||
self.emit('ready'); | ||
}); | ||
}); | ||
@@ -177,3 +189,3 @@ }); | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
self.lockType = null; | ||
@@ -232,3 +244,3 @@ self.query = null; | ||
query = {files_id: self.fileId, write_lock: true}, | ||
update = {$set: {write_lock: false, meta: null}, $currentDate: { expires: true }}; | ||
update = {$set: {write_lock: false, write_req: false, meta: null}, $currentDate: { expires: true }}; | ||
@@ -242,2 +254,3 @@ } else { | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
@@ -263,5 +276,5 @@ var lt = self.lockType; | ||
if (err) { console.warn("Error returned from expiration time reset on release", err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
}); | ||
} | ||
self.emit('released', doc); | ||
@@ -310,2 +323,3 @@ }); | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc == null) { return emitError(self, "Lock.renewLock document not found in collection"); } | ||
@@ -348,3 +362,2 @@ self.heldLock = doc; | ||
if (self.lockCollection._isMongo26) { // Begin Mongo 2.6 support | ||
self.timeCreated = new Date(); | ||
@@ -428,3 +441,2 @@ if (self.heldLock) { | ||
var heldLock = self.heldLock; | ||
// console.log("expiring", heldLock); | ||
self.heldLock = null; | ||
@@ -475,4 +487,4 @@ self.expired = true | ||
function (err, doc) { | ||
// if (err) { console.log("Error", err)} | ||
if (err && ((err.name !== 'MongoError') || (err.code !== 11000))) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (!doc) { | ||
@@ -531,4 +543,4 @@ if (new Date().getTime() - self.timeCreated >= self.timeOut) { | ||
function (err, doc) { | ||
// if (err) { console.log("Error", err)} | ||
if (err && ((err.name !== 'MongoError') || (err.code !== 11000))) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc) { | ||
@@ -553,2 +565,3 @@ self.heldLock = doc; | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
} | ||
@@ -558,3 +571,3 @@ ); | ||
} else { | ||
// write_req gets set every time because claimed write locks and timed out write requests clear it | ||
// write_req gets set every time because claimed/released write locks and timed out write requests clear it | ||
self.collection.findAndModify({files_id: self.fileId, write_req: false}, | ||
@@ -566,2 +579,3 @@ [], | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
self.emit('write-req-set'); | ||
@@ -613,2 +627,3 @@ }); | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc) { | ||
@@ -629,2 +644,3 @@ self.lockType = null; | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc) { | ||
@@ -638,6 +654,6 @@ self.lockType = null; | ||
// If another readLock released between the above two findAndModify calls they can both fail... so keep trying. | ||
// console.log("Retrying to release read lock..."); | ||
// Avoid an infinite loop when lock document no longer exists | ||
self.collection.findOne({files_id: self.fileId, read_locks: {$gt: 0}}, function (err, doc) { | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc == null) { | ||
@@ -657,7 +673,7 @@ return emitError(self, "Lock.releaseLock Valid read Lock document not found in collection. " + JSON.stringify(self.heldLock)); | ||
var query = {files_id: self.fileId, write_lock: true}, | ||
update = {$set: {write_lock: false, expires: new Date(), meta: null}}; | ||
update = {$set: {write_lock: false, write_req: false, expires: new Date(), meta: null}}; | ||
self.collection.findAndModify(query, [], update, {w: self.lockCollection.writeConcern, new: true}, function (err, doc) { | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
self.lockType = null; | ||
@@ -695,5 +711,7 @@ self.query = null; | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc == null) { | ||
self.collection.findOne({files_id: self.fileId}, function (err, doc) { | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc == null) { | ||
@@ -727,2 +745,3 @@ return emitError(self, "Lock.renewLock document not found in collection"); | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
self.lockType = 'r'; | ||
@@ -745,2 +764,3 @@ self.expired = false; | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
self.expired = false; | ||
@@ -784,5 +804,5 @@ self.lockType = 'w'; | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (!doc) { | ||
// Try again without trying to update the expire time | ||
// console.log("Second try..."); | ||
self.lockExpireTime = new Date(new Date().getTime() + (self.lockExpiration || never)); | ||
@@ -798,2 +818,3 @@ self.query = {files_id: self.fileId, write_lock: false, write_req: false, expires: { $gt: self.lockExpireTime }}; | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (!doc) { | ||
@@ -833,2 +854,3 @@ if(new Date().getTime() - self.timeCreated >= self.timeOut) { | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
if (doc) { | ||
@@ -852,2 +874,3 @@ self.heldLock = doc; | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
} | ||
@@ -857,3 +880,3 @@ ); | ||
} else { | ||
// write_req gets set every time because claimed write locks and timed out write requests clear it | ||
// write_req gets set every time because claimed/released write locks and timed out write requests clear it | ||
self.collection.findAndModify({files_id: self.fileId, write_req: false}, | ||
@@ -865,2 +888,3 @@ [], | ||
if (err) { return emitError(self, err); } | ||
if (self.lockCollection._isMongoDriver20 && doc && doc.hasOwnProperty('value')) { doc = doc.value; } | ||
self.emit('write-req-set'); | ||
@@ -867,0 +891,0 @@ }); |
{ | ||
"name": "gridfs-locks", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "Distributed read/write locking based on MongoDB, designed to make GridFS safe for concurrent access", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mongodb": ">=1.4.31", | ||
"mongodb": "2.0.16", | ||
"coffee-script": "*", | ||
@@ -11,0 +11,0 @@ "mocha": "*" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89549
748