Skip to content

Commit

Permalink
Change getAll to the more relevant unions.
Browse files Browse the repository at this point in the history
  • Loading branch information
GabeIsman committed Mar 22, 2016
1 parent 66bd0d4 commit 71bb0eb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
8 changes: 4 additions & 4 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ var Backend = {
},

/**
Gets the contents of the specified keys and returns them in the same order
passed.
Gets the union of contents of the specified keys in each of the specified buckets and returns
a mapping of bucket to union.
*/
getAll : function(bucket, keys, cb){
unions : function(bucket, keys, cb){
contract(arguments)
.params('string', 'array', 'function')
.params('array', 'array', 'function')
.end();
},

Expand Down
23 changes: 15 additions & 8 deletions lib/memory-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,25 @@ MemoryBackend.prototype = {
},

/**
Gets the contents at the bucket's keys.
Gets the union of the keys in each of the specified buckets
*/
getAll : function(bucket, keys, cb){
unions : function(buckets, keys, cb){
contract(arguments)
.params('string', 'array', 'function')
.params('array', 'array', 'function')
.end();

if(this._buckets[bucket]){
cb(null, _.pick(this._buckets[bucket], keys));
}else{
cb(null, {});
}
var self = this;
var results = {};

buckets.forEach(function(bucket) {
if(self._buckets[bucket]){
results[bucket] = _.uniq(_.flatten(_.values(_.pick(self._buckets[bucket], keys))));
}else{
results[bucket] = [];
}
});

cb(null, results);
},

/**
Expand Down
16 changes: 8 additions & 8 deletions lib/redis-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ RedisBackend.prototype = {
},

/**
Gets an array of the contents of the specified keys.
Gets an object mapping each passed bucket to the union of the specified keys inside that bucket.
*/
getAll : function(bucket, keys, cb){
unions : function(buckets, keys, cb){
contract(arguments)
.params('string', 'array', 'function')
.params('array', 'array', 'function')
.end();

var redisKeys = this.bucketKey(bucket, keys);
var redisKeys = {};
var batch = this.redis.batch();
var self = this;

redisKeys.map(function(key) {
batch.smembers(key, self.redis.print);
buckets.forEach(function(bucket) {
redisKeys[bucket] = self.bucketKey(bucket, keys);
batch.sunion(redisKeys[bucket], noop);
});

batch.exec(function(err, replies) {
var result = Array.isArray(replies) ? _.zipObject(keys, replies) : replies;
var result = Array.isArray(replies) ? _.zipObject(buckets, replies) : {};
cb(err, result);
});
},
Expand Down
41 changes: 27 additions & 14 deletions test/backendtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ var _ = require('lodash');

var testData = {
key1: ["1", "2", "3"],
key2: ["4", "5", "6"],
key3: ["7", "8", "9"]
key2: ["3", "2", "4"],
key3: ["3", "4", "5"]
};
var bucket = 'test-bucket';
var buckets = ['bucket1', 'bucket2'];

exports.getAll = function() {
describe('getAll', function() {
exports.unions = function() {
describe('unions', function() {
before(function(done) {
var backend = this.backend;
if (!backend.getAll) {
if (!backend.unions) {
this.skip();
}

backend.clean(function() {
var transaction = backend.begin();
Object.keys(testData).forEach(function(key) {
backend.add(transaction, bucket, key, testData[key]);
buckets.forEach(function(bucket) {
backend.add(transaction, bucket, key, testData[key]);
});
});
backend.end(transaction, done);
});
Expand All @@ -32,25 +34,36 @@ exports.getAll = function() {
});

it('should respond with an appropriate map', function(done) {
this.backend.getAll(bucket, Object.keys(testData), function(err, result) {
var expected = {
'bucket1': ["1", "2", "3", "4", "5"],
'bucket2': ["1", "2", "3", "4", "5"]
};
this.backend.unions(buckets, Object.keys(testData), function(err, result) {
expect(err).to.be.null;
expect(result).to.be.eql(testData);
expect(result).to.be.eql(expected);
done();
});
});

it('should get only the specified keys', function(done) {
this.backend.getAll(bucket, ['key1'], function(err, result) {
var expected = {
'bucket1': ['1', '2', '3'],
'bucket2': ['1', '2', '3']
}
this.backend.unions(buckets, ['key1'], function(err, result) {
expect(err).to.be.null;
expect(result).to.be.eql(_.pick(testData, 'key1'));
expect(result).to.be.eql(expected);
done();
});
});

it('should be order-independent', function(done) {
this.backend.getAll(bucket, Object.keys(testData).reverse(), function(err, result) {
it('should only get the specified buckets', function(done) {
var expected = {
'bucket1': ['1', '2', '3']
};
this.backend.unions(['bucket1'], ['key1'], function(err, result) {
expect(err).to.be.null;
expect(result).to.be.eql(testData);
expect(result).to.be.eql(expected);
done();
});
});
Expand Down

0 comments on commit 71bb0eb

Please sign in to comment.