Permalink
Browse files

Improved find() to find in descending order when min>max; Add endpoin…

…t options.
  • Loading branch information...
1 parent cba6065 commit 976a6a4503be22e1ddc44cc9e9bf02816aecce94 Ju-yeong Park committed with Jan 2, 2013
Showing with 46 additions and 2 deletions.
  1. +46 −2 lib/retrieve.js
View
@@ -134,12 +134,56 @@ exports.find = function find(searches, callback) {
options.max = '+inf';
if ( ! options.offset && options.offset !== 0)
options.offset = '+inf';
+
+ var is_rev;
+ if( (options.min == '+inf' && options.max != '+inf') ||
+ (options.max == '-inf' && options.min != '-inf') ||
+ (options.min > options.max) )
+ is_rev = true;
+ else
+ is_rev = false;
+
+ if ( ! options.endpoint ) {
+ options.endpoint = '[]';
+ } else {
+ if ( options.endpoint.length == 1 ) {
+ if ( options.endpoint[0] == ')' || options.endpoint[1] == ']' ) {
+ options.endpoint = '[' + options.endpoint;
+ } else if ( options.endpoint[0] == '(' || options.endpoint[1] == '[' ) {
+ options.endpoint += ']';
+ } else {
+ return returnFunction('Invalid search parameters: endpoint expression is invalid.');
+ }
+ } else if ( options.endpoint.length != 2) {
+ return returnFunction('Invalid search parameters: endpoint expression is invalid.');
+ }
+ }
+ var endpoint_lt = options.endpoint[0] == '(';
+ var endpoint_gt = options.endpoint[1] == ')';
+
if (options.limit) {
- self.getClient().zrangebyscore(zSet.key, options.min, options.max,
+ if(is_rev)
+ self.getClient().zrevrangebyscore(zSet.key,
+ (endpoint_lt ? '(' : '') + options.min,
+ (endpoint_gt ? '(': '') + options.max,
+ 'LIMIT', options.offset, options.limit,
+ rangeCallback);
+ else
+ self.getClient().zrangebyscore(zSet.key,
+ (endpoint_lt ? '(' : '') + options.min,
+ (endpoint_gt ? '(': '') + options.max,
'LIMIT', options.offset, options.limit,
rangeCallback);
} else {
- self.getClient().zrangebyscore(zSet.key, options.min, options.max,
+ if(is_rev)
+ self.getClient().zrevrangebyscore(zSet.key,
+ (endpoint_lt ? '(' : '') + options.min,
+ (endpoint_gt ? '(': '') + options.max,
+ rangeCallback);
+ else
+ self.getClient().zrangebyscore(zSet.key,
+ (endpoint_lt ? '(' : '') + options.min,
+ (endpoint_gt ? '(': '') + options.max,
rangeCallback);
}
},

0 comments on commit 976a6a4

Please sign in to comment.