Skip to content

Commit

Permalink
added unique key generation, and delete multi keys after completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Chan committed Feb 3, 2012
1 parent b94ec54 commit b037f39
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ exports.genKey = function(key, date) {
* example: 20120201_likes
**/
exports.genUniqueKey = function(keys) {
return 'myuniquekey';
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 16;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return 'multi:' + randomstring;
}

/**
Expand Down Expand Up @@ -249,5 +256,6 @@ Query.prototype.multirevrange = function(keys, start, end, callback) {
multi.zrevrange(multikey, s, e);
multi.exec(function(err, replies) {
callback(err, replies[1]);
db.del(multikey);
});
};
13 changes: 11 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ describe('scoreboard', function() {
keys.should.include('20120202_bar');
})
})

describe('#getUniqueKey', function() {
it('test multiple date range key generation', function(){
var key = scoreboard.genUniqueKey(['foo', 'bar']);
console.log(key);
key.should.have.length(22);
key.should.include('multi:');
})
})
})

describe('score', function(){
Expand Down Expand Up @@ -71,10 +80,10 @@ describe('score', function(){
})

describe('#leader', function() {
it('test mult index and get value in range', function(done) {
it('test multi index and get value in range', function(done) {
score.index('foo', 1, 'bar', function(err) {
score.index('poo', 1, 'par', function(err) {
score.leader({ keys: ['foo', 'poo'], date: { $start: new Date('1/31/2012'), $end: new Date('2/02/2012') } })
score.leader({ keys: ['foo', 'poo'], date: { $start: new Date('1/31/2012'), $end: new Date() } })
.skip(0).limit(-1)
.run(function(err, response) {
response.should.include('bar');
Expand Down

0 comments on commit b037f39

Please sign in to comment.