Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replaced tabs with 2 spaces for consistency

  • Loading branch information...
commit 8fe71c654125cf7bbb84a11f699602bf9daee687 1 parent 25b1e66
Christian Boulanger authored
Showing with 66 additions and 63 deletions.
  1. +48 −48 lib/mongodb-backend.js
  2. +18 −15 test/run_tests.js
View
96 lib/mongodb-backend.js
@@ -1,5 +1,5 @@
/**
- MongoDB Backend.
+ MongoDB Backend.
Implementation of the storage backend using MongoDB
*/
@@ -12,24 +12,24 @@ function MongoDBBackend(db, prefix){
}
MongoDBBackend.prototype = {
- /**
+ /**
Begins a transaction.
*/
begin : function(){
// returns a transaction object(just an array of functions will do here.)
return [];
},
-
+
/**
Ends a transaction (and executes it)
*/
end : function(transaction, cb){
- contract(arguments).params('array', 'function').end();
+ contract(arguments).params('array', 'function').end();
async.series(transaction,function(err){
- cb(err instanceof Error? err : undefined);
+ cb(err instanceof Error? err : undefined);
});
},
-
+
/**
Cleans the whole storage.
*/
@@ -42,14 +42,14 @@ MongoDBBackend.prototype = {
},cb);
});
},
-
+
/**
Gets the contents at the bucket's key.
*/
get : function(bucket, key, cb){
- contract(arguments)
- .params('string', 'string', 'function')
- .end();
+ contract(arguments)
+ .params('string', 'string', 'function')
+ .end();
this.db.collection(bucket,function(err,collection){
if(err instanceof Error) return cb(err);
collection.findOne({key:key},function(err, doc){
@@ -60,17 +60,17 @@ MongoDBBackend.prototype = {
});
},
- /**
- Returns the union of the values in the given keys.
- */
+ /**
+ Returns the union of the values in the given keys.
+ */
union : function(bucket, keys, cb){
contract(arguments)
- .params('string', 'array', 'function')
- .end();
-
+ .params('string', 'array', 'function')
+ .end();
+
this.db.collection(bucket,function(err,collection){
if(err instanceof Error) return cb(err);
- collection.find({key: { $in: keys }}).toArray(function(err,docs){
+ collection.find({key: { $in: keys }}).toArray(function(err,docs){
if(err instanceof Error) return cb(err);
if( ! docs.length ) return cb(undefined, []);
var keyArrays = [];
@@ -80,27 +80,27 @@ MongoDBBackend.prototype = {
cb(undefined, _.without(_.union(keyArrays),"key","_id"));
});
});
- },
-
+ },
+
/**
- Adds values to a given key inside a bucket.
- */
- add : function(transaction, bucket, key, values){
- contract(arguments)
- .params('array', 'string', 'string','string|array')
+ Adds values to a given key inside a bucket.
+ */
+ add : function(transaction, bucket, key, values){
+ contract(arguments)
+ .params('array', 'string', 'string','string|array')
.end();
-
+
if(key=="key") throw new Error("Key name 'key' is not allowed.");
- var self=this;
+ var self=this;
transaction.push(function(cb){
values = makeArray(values);
self.db.collection(bucket, function(err,collection){
if(err instanceof Error) return cb(err);
-
- // build doc from array values
+
+ // build doc from array values
var doc = {};
values.forEach(function(value){doc[value]=true;});
-
+
// update document
collection.update({key:key},{$set:doc},{safe:true,upsert:true},function(err){
if(err instanceof Error) return cb(err);
@@ -108,46 +108,46 @@ MongoDBBackend.prototype = {
});
});
});
- },
-
+ },
+
/**
Delete the given key(s) at the bucket
*/
del : function(transaction, bucket, keys){
- contract(arguments)
- .params('array', 'string', 'string|array')
- .end();
+ contract(arguments)
+ .params('array', 'string', 'string|array')
+ .end();
keys = makeArray(keys);
var self= this;
transaction.push(function(cb){
self.db.collection(bucket,function(err,collection){
if(err instanceof Error) return cb(err);
collection.remove({key:{$in:keys}},{safe:true},function(err){
- if(err instanceof Error) return cb(err);
+ if(err instanceof Error) return cb(err);
cb(undefined);
- });
+ });
});
});
},
-
- /**
- Removes values from a given key inside a bucket.
- */
- remove : function(transaction, bucket, key, values){
- contract(arguments)
- .params('array', 'string', 'string','string|array')
+
+ /**
+ Removes values from a given key inside a bucket.
+ */
+ remove : function(transaction, bucket, key, values){
+ contract(arguments)
+ .params('array', 'string', 'string','string|array')
.end();
-
+
var self=this;
values = makeArray(values);
transaction.push(function(cb){
self.db.collection(bucket,function(err,collection){
if(err instanceof Error) return cb(err);
-
- // build doc from array values
+
+ // build doc from array values
var doc = {};
values.forEach(function(value){doc[value]=true;});
-
+
// update document
collection.update({key:key},{$unset:doc},{safe:true,upsert:true},function(err){
if(err instanceof Error) return cb(err);
@@ -155,7 +155,7 @@ MongoDBBackend.prototype = {
});
});
});
- }
+ }
}
function makeArray(arr){
View
33 test/run_tests.js
@@ -1,36 +1,39 @@
-
var testBackend = require('./acl_test').testBackend,
- async = require('async');
+ async = require('async');
var tests = [
-
+
/* Memory */
- function(cb){
- console.log("Testing memory backend");
+ function(cb) {
+ console.log("Testing memory backend");
testBackend("memory", {}, cb);
},
/* Redis */
- function(cb){
- console.log("Testing Redis backend");
- var options = { host:'127.0.0.1', port:6379, password: null};
+ function(cb) {
+ console.log("Testing Redis backend");
+ var options = {
+ host: '127.0.0.1',
+ port: 6379,
+ password: null
+ };
testBackend("redis", options, cb);
},
/* MongoDB */
- function(cb){
- console.log("Testing MongoDB backend");
- var url = "mongodb://127.0.0.1:27017/acltest";
+ function(cb) {
+ console.log("Testing MongoDB backend");
+ var url = "mongodb://127.0.0.1:27017/acltest";
testBackend("mongodb", url, cb);
}
+
];
// run tests
-async.forEachSeries(tests,function(test,cb){
- test(function(results){
+async.forEachSeries(tests, function(test, cb) {
+ test(function(results) {
var exitCode = results.honored === results.total ? 0 : -1;
exitCode && process.exit(exitCode);
cb();
});
-});
-
+});
Please sign in to comment.
Something went wrong with that request. Please try again.