Code coverage report for node_acl/lib/redis-backend.js

Statements: 85.11% (40 / 47)      Branches: 83.33% (10 / 12)      Functions: 77.78% (14 / 18)      Lines: 86.67% (39 / 45)      Ignored: none     

All files » node_acl/lib/ » redis-backend.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148              1   1   1 1 1     1           42             42 42                                           66       66   66             139       139 139             72       72   72 38 78     34               19       19   19   19 29     19             26       26   26 4 4     22                 332 332 139 167     193         1  
/**
	Redis Backend.
	
  Implementation of the storage backend using Redis
*/
"use strict";
 
var contract = require('./contract');
 
function noop(){};
 
function RedisBackend(redis, prefix){
  this.redis = redis;
  this.prefix = prefix || 'acl';
}
 
RedisBackend.prototype = {
  
  /**  
     Begins a transaction
  */
  begin : function(){
    return this.redis.multi();
  },
  
  /**
     Ends a transaction (and executes it)
  */
  end : function(transaction, cb){
		contract(arguments).params('object', 'function').end();
    transaction.exec(function(){cb()});
  },
  
  /**
    Cleans the whole storage.
  */
  clean : function(cb){
    contract(arguments).params('function').end();
    var self = this;
    self.redis.keys(self.prefix+'*', function(err, keys){
      if(keys.length){
        self.redis.del(keys, function(){cb()});
      }else{
        cb();
      }
    });
  },
  
  /**
     Gets the contents at the bucket's key.
  */
  get : function(bucket, key, cb){
		contract(arguments)
	      .params('string', 'string', 'function')
	      .end();
 
    key = this.bucketKey(bucket, key);
    
    this.redis.smembers(key, cb);
  },
  
	/**
		Returns the union of the values in the given keys.
	*/
	union : function(bucket, keys, cb){
		contract(arguments)
	      .params('string', 'array', 'function')
	      .end();
    
    keys = this.bucketKey(bucket, keys);
    this.redis.sunion(keys, cb);
	},
  
	/**
		Adds values to a given key inside a bucket.
	*/
	add : function(transaction, bucket, key, values){
		contract(arguments)
	      .params('object', 'string', 'string','string|array')
        .end();
            
    key = this.bucketKey(bucket, key);
            
    if (Array.isArray(values)){
      values.forEach(function(value){
        transaction.sadd(key, value);
      });
    }else{
      transaction.sadd(key, values);
    }
	},
  
  /**
     Delete the given key(s) at the bucket
  */
  del : function(transaction, bucket, keys){
		contract(arguments)
	      .params('object', 'string', 'string|array')
	      .end();
            
    var self = this;
    
    keys = Array.isArray(keys) ? keys : [keys]
    
    keys = keys.map(function(key){
      return self.bucketKey(bucket, key);
    });
  
    transaction.del(keys);
  },
  
	/**
		Removes values from a given key inside a bucket.
	*/
	remove : function(transaction, bucket, key, values){
		contract(arguments)
	      .params('object', 'string', 'string','string|array')
        .end();
                  
    key = this.bucketKey(bucket, key);
        
    if (Array.isArray(values)){
      values.forEach(function(value){
        transaction.srem(key, value);
      });
    }else{
      transaction.srem(key, values);
    }
  },
  
  //
  // Private methods
  //
    
  bucketKey : function(bucket, keys){
    var self = this;
    if(Array.isArray(keys)){
      return keys.map(function(key){
        return self.prefix+'_'+bucket+'@'+key;
      });
    }else{
      return self.prefix+'_'+bucket+'@'+keys;
    }
  }
}
 
exports = module.exports = RedisBackend;