Skip to content

Commit

Permalink
Start implementing sorted set functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Wilke committed Feb 24, 2015
1 parent 5e47e68 commit 126112e
Show file tree
Hide file tree
Showing 3 changed files with 419 additions and 5 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,28 @@ javascript.
## Installation

## Usage

## Roadmap

* 0.1.0
- Support for most redis commands.
- Mocha tests for all supported commands.
- Browser testing and compatibility determination.
* 1.0.0
- Support for multiple mock redis instances.
- Support for migrating data between mock redis instances.
* 2.0.0
- Support for migrating data from a mock redis instance to a real
redis instance.
- ?

## Versions

* 0.0.2
- Transaction support that works.
- Bug squashes.
- Enhanced commands.
* 0.0.1
- DEPRECATED
- Initial implementation.
- Numerous bugs.
139 changes: 134 additions & 5 deletions redis-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@
};
};

var gather = function (f) {
var gather = function (f, e) {
var end = f.length;
if (e) {
end = e;
}
return function () {
var idx, len = arguments.length;
var callback;
Expand All @@ -67,7 +70,7 @@
if (typeof arguments[idx] === "function") {
callback = arguments[idx];
}
else if (arguments[idx]) {
else if (arguments[idx] !== null && arguments[idx] !== undefined) {
list.push(arguments[idx]);
}
}
Expand Down Expand Up @@ -602,12 +605,13 @@
};

redismock.zadd = function (key, score, member, callback) {
var g = gather(this.zadd).apply(this, arguments);
var g = gather(this.zadd, 3).apply(this, arguments);
callback = g.callback;
return this
.ifType(key, 'zset', callback)
.thennx(function () { cache[zsets][key] = {}; })
.then(function () {
var count = 0;
g
.list
.map(function (elem, index) {
Expand All @@ -622,19 +626,144 @@
.forEach(function (sm) {
var score = sm[0];
var m = sm[1];
var noop = false;
if (!(score in cache[zsets][key])) {
cache[zsets][key][score] = [];
}
cache[zsets][key][score].push(m);
Object
.keys(cache[zsets][key])
.map(function (score) {
return [parseInt(score, 10), cache[zsets][key][score].indexOf(m)];
})
.filter(function (si) {
return si[1] !== -1;
})
.forEach(function (si) {
if (si[0] !== score) {
cache[zsets][key][si[0]].splice(si[1], 1);
}
else {
noop = true;
}
});
if (!noop) {
cache[zsets][key][score].push(m);
count += 1;
}
});
return cb(callback)(null, 1);
return cb(callback)(null, count);
})
.end();
};

redismock.zcard = function (key, callback) {
return this
.ifType(key, 'zset', callback)
.thenex(function () {
var count = Object.keys(cache[zsets][key]).reduce(function (cnt, score) {
return cnt + cache[zsets][key][score].length;
}, 0);
return cb(callback)(null, count);
})
.thennx(function () { return cb(callback)(null, 0); })
.end();
};

redismock.zcount = function (key, min, max, callback) {
return this
.ifType(key, 'zset', callback)
.thenex(function () {
var count = Object
.keys(cache[zsets][key])
.map(function (score) {
return parseInt(score, 10);
})
.filter(function (score) {
return min <= score && score <= max;
})
.reduce(function (cnt, score) {
return cnt + cache[zsets][key][score].length;
}, 0);
return cb(callback)(null, count);
})
.thennx(function () { return cb(callback)(null, 0); })
.end();
};

redismock.zrange = function (key, start, stop, callback) {
var withscores = false;
if (typeof callback !== "function" && callback === "withscores") {
withscores = true;
callback = arguments[4];
}
if (start < 0) {
start = this.zcard(key) + start;
}
if (stop < 0) {
stop = this.zcard(key) + stop;
}
return this
.ifType(key, 'zset', callback)
.thenex(function () {
var index = 0;
var arr = [];
Object
.keys(cache[zsets][key])
.map(function (score) {
return parseInt(score, 10);
})
.sort()
.some(function (score) {
cache[zsets][key][score].forEach(function (member) {
if (start <= index && index <= stop) {
arr.push(member);
if (withscores) {
arr.push(score);
}
}
index += 1;
});
if (index > stop) {
return true;
}
return false;
});
return cb(callback)(null, arr);
})
.thennx(function () { return cb(callback)(null, []); })
.end();
};

/*redismock.zrangebyscore = function (key, min, max, callback) {
};*/

redismock.zrank = function (key, member, callback) {
return this
.ifType(key, 'zset', callback)
.thenex(function () {
var idx = 0;
var found = Object
.keys(cache[zsets][key])
.map(function (score) {
return parseInt(score, 10);
})
.sort()
.some(function (score) {
if (cache[zsets][key][score].indexOf(member) !== -1) {
return true;
}
idx += 1;
return false;
});
if (!found) {
return cb(callback)(null, null);
}
return cb(callback)(null, idx);
})
.thennx(function () { return cb(callback)(null, null); })
.end();
};

redismock.hdel = function (key, field, callback) {
var count = 0;
var g = gather(this.hdel).apply(this, arguments);
Expand Down
Loading

0 comments on commit 126112e

Please sign in to comment.