Skip to content

Commit 71bb0eb

Browse files
committed
Change getAll to the more relevant unions.
1 parent 66bd0d4 commit 71bb0eb

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

lib/backend.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ var Backend = {
3939
},
4040

4141
/**
42-
Gets the contents of the specified keys and returns them in the same order
43-
passed.
42+
Gets the union of contents of the specified keys in each of the specified buckets and returns
43+
a mapping of bucket to union.
4444
*/
45-
getAll : function(bucket, keys, cb){
45+
unions : function(bucket, keys, cb){
4646
contract(arguments)
47-
.params('string', 'array', 'function')
47+
.params('array', 'array', 'function')
4848
.end();
4949
},
5050

lib/memory-backend.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,25 @@ MemoryBackend.prototype = {
6060
},
6161

6262
/**
63-
Gets the contents at the bucket's keys.
63+
Gets the union of the keys in each of the specified buckets
6464
*/
65-
getAll : function(bucket, keys, cb){
65+
unions : function(buckets, keys, cb){
6666
contract(arguments)
67-
.params('string', 'array', 'function')
67+
.params('array', 'array', 'function')
6868
.end();
6969

70-
if(this._buckets[bucket]){
71-
cb(null, _.pick(this._buckets[bucket], keys));
72-
}else{
73-
cb(null, {});
74-
}
70+
var self = this;
71+
var results = {};
72+
73+
buckets.forEach(function(bucket) {
74+
if(self._buckets[bucket]){
75+
results[bucket] = _.uniq(_.flatten(_.values(_.pick(self._buckets[bucket], keys))));
76+
}else{
77+
results[bucket] = [];
78+
}
79+
});
80+
81+
cb(null, results);
7582
},
7683

7784
/**

lib/redis-backend.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,23 @@ RedisBackend.prototype = {
6161
},
6262

6363
/**
64-
Gets an array of the contents of the specified keys.
64+
Gets an object mapping each passed bucket to the union of the specified keys inside that bucket.
6565
*/
66-
getAll : function(bucket, keys, cb){
66+
unions : function(buckets, keys, cb){
6767
contract(arguments)
68-
.params('string', 'array', 'function')
68+
.params('array', 'array', 'function')
6969
.end();
7070

71-
var redisKeys = this.bucketKey(bucket, keys);
71+
var redisKeys = {};
7272
var batch = this.redis.batch();
7373
var self = this;
74-
75-
redisKeys.map(function(key) {
76-
batch.smembers(key, self.redis.print);
74+
buckets.forEach(function(bucket) {
75+
redisKeys[bucket] = self.bucketKey(bucket, keys);
76+
batch.sunion(redisKeys[bucket], noop);
7777
});
7878

7979
batch.exec(function(err, replies) {
80-
var result = Array.isArray(replies) ? _.zipObject(keys, replies) : replies;
80+
var result = Array.isArray(replies) ? _.zipObject(buckets, replies) : {};
8181
cb(err, result);
8282
});
8383
},

test/backendtests.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@ var _ = require('lodash');
55

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

13-
exports.getAll = function() {
14-
describe('getAll', function() {
13+
exports.unions = function() {
14+
describe('unions', function() {
1515
before(function(done) {
1616
var backend = this.backend;
17-
if (!backend.getAll) {
17+
if (!backend.unions) {
1818
this.skip();
1919
}
2020

2121
backend.clean(function() {
2222
var transaction = backend.begin();
2323
Object.keys(testData).forEach(function(key) {
24-
backend.add(transaction, bucket, key, testData[key]);
24+
buckets.forEach(function(bucket) {
25+
backend.add(transaction, bucket, key, testData[key]);
26+
});
2527
});
2628
backend.end(transaction, done);
2729
});
@@ -32,25 +34,36 @@ exports.getAll = function() {
3234
});
3335

3436
it('should respond with an appropriate map', function(done) {
35-
this.backend.getAll(bucket, Object.keys(testData), function(err, result) {
37+
var expected = {
38+
'bucket1': ["1", "2", "3", "4", "5"],
39+
'bucket2': ["1", "2", "3", "4", "5"]
40+
};
41+
this.backend.unions(buckets, Object.keys(testData), function(err, result) {
3642
expect(err).to.be.null;
37-
expect(result).to.be.eql(testData);
43+
expect(result).to.be.eql(expected);
3844
done();
3945
});
4046
});
4147

4248
it('should get only the specified keys', function(done) {
43-
this.backend.getAll(bucket, ['key1'], function(err, result) {
49+
var expected = {
50+
'bucket1': ['1', '2', '3'],
51+
'bucket2': ['1', '2', '3']
52+
}
53+
this.backend.unions(buckets, ['key1'], function(err, result) {
4454
expect(err).to.be.null;
45-
expect(result).to.be.eql(_.pick(testData, 'key1'));
55+
expect(result).to.be.eql(expected);
4656
done();
4757
});
4858
});
4959

50-
it('should be order-independent', function(done) {
51-
this.backend.getAll(bucket, Object.keys(testData).reverse(), function(err, result) {
60+
it('should only get the specified buckets', function(done) {
61+
var expected = {
62+
'bucket1': ['1', '2', '3']
63+
};
64+
this.backend.unions(['bucket1'], ['key1'], function(err, result) {
5265
expect(err).to.be.null;
53-
expect(result).to.be.eql(testData);
66+
expect(result).to.be.eql(expected);
5467
done();
5568
});
5669
});

0 commit comments

Comments
 (0)