Permalink
Browse files

Merge branch 'feature/zlist-trim' into develop

  • Loading branch information...
2 parents 6df96ff + 6f27187 commit 70b5defb64a2dccc3270d3daf13877b882bae3bd @FGRibreau committed Mar 12, 2012
Showing with 100 additions and 7 deletions.
  1. +36 −4 README.md
  2. +1 −2 bin/redis-zlist-size
  3. +59 −0 bin/redis-ztrim
  4. +4 −1 package.json
View
40 README.md
@@ -6,17 +6,51 @@
## Documentation
+* [redis-ztrim](#ztrim)
* [redis-zlist-size](#zlist-size)
* [redis-rename](#rename)
* [redis-delall](#delall)
+<a name="ztrim" />
+### Redis-ztrim
+
+```
+Usage: node ./bin/redis-ztrim [options] pattern start stop
+
+Trim a group of zlists matching `pattern` so that they'll contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list with the highest score (the head), 1 the next element and so on.
+
+For example: redis-ztrim foobar* 0 2 will modify the zsets matching the pattern foobar* so that only the first three elements of each zsets will remain.
+
+Options:
+ -p, --preview preview what is going to be deleted [boolean] [default: false]
+ -v, --verbose verbose mode [boolean] [default: false]
+ -h, --help this message. [boolean]
+```
+
+Trim all zsets matching the pattern `bringr:*:*:ua:*" so that only their members ranked from 0 to 99 will remain.
+```shell
+$ redis-ztrim
+✔ bringr:1:121:ua:m trimmed
+✔ bringr:1:121:ua:y trimmed
+✔ bringr:1:123:ua:y trimmed
+✔ bringr:1:123:ua:m trimmed
+✔ bringr:1:121:ua:d trimmed
+✔ bringr:1:123:ua:d trimmed
+✔ bringr:6:93:ua:y trimmed
+✔ bringr:6:93:ua:m trimmed
+✔ bringr:1:123:ua:h trimmed
+✔ bringr:6:93:ua:d trimmed
+[... truncated ... ]
+```
+
+
<a name="zlist-size" />
### Redis-zlist-size
```
Retrieve a list of zset keys via `pattern` and order them by size
-Usage: node ./redis-zlist-size [options] pattern
+Usage: redis-zlist-size [options] pattern
Options:
--json print results in JSON format [default: false]
@@ -26,7 +60,7 @@ Options:
```
```shell
-$ redis-zlist-size "bringr:*:*:tc:*"
+$ redis-zlist-size "bringr:*:*:ua:*"
bringr:1:121:ua:m Members 15821544 Size 81660
bringr:1:121:ua:y Members 15821544 Size 81660
bringr:1:123:ua:y Members 13782356 Size 66280
@@ -37,8 +71,6 @@ bringr:6:93:ua:y Members 10138538 Size 52129
bringr:6:93:ua:m Members 10138538 Size 52129
bringr:1:123:ua:h Members 9192799 Size 44231
bringr:6:93:ua:d Members 6720535 Size 34702
-bringr:1:123:ua:h Members 3112684 Size 14940
-bringr:1:121:ua:d Members 2248963 Size 12071
[... truncated ... ]
```
View
3 bin/redis-zlist-size
@@ -18,7 +18,6 @@ if(argv.h || argv._.length != 1){
return arg.showHelp(console.error);
}
-
// First check type
function checkKeysType(keys, fnNext){
var i = keys.length - 1;
@@ -60,6 +59,7 @@ function calcKeysSize(keys, fnNext){
keysWithSize.push(
[
key
+ , members.length
, members.reduce(function(m, v){
m += v.length || 0;
@@ -70,7 +70,6 @@ function calcKeysSize(keys, fnNext){
return m;
}, 0)
- , members.length
, largestKey
]);
View
59 bin/redis-ztrim
@@ -0,0 +1,59 @@
+#!/usr/bin/env node
+
+var spawn = require('child_process').spawn
+, r = require(__dirname+'/../lib/')
+, arg = require('optimist').usage('\
+Usage: $0 [options] pattern start stop \n\n\
+\
+Trim a group of zlists matching `pattern` so that they\'ll contain only the specified range of elements specified. \
+Both start and stop are zero-based indexes, where 0 is the first element of the list with the highest score (the head), 1 the next element and so on. \n\n\
+For example: redis-ztrim foobar* 0 2 will modify the zsets matching the pattern foobar* so that only the first three elements of each zsets will remain.')
+ .describe('p', 'preview what is going to be deleted').alias('p', 'preview').boolean('p')
+ .describe('v', 'verbose mode').alias('v', 'verbose').boolean('v')
+ .describe('h', 'this message.').alias('h','help').boolean('h')
+ .default({ v : false, p : false })
+, argv = arg.argv
+, pattern = argv._[0]
+, start = argv._[1]
+, stop = argv._[2];
+
+if(argv.h || argv._.length < 1 || argv._.length > 1 && (stop == undefined || start != undefined && start != 0)){
+ if(argv._.length > 1 && start != 0){console.error('Unsupported: start should be 0.')}
+ return arg.showHelp(console.error);
+}
+
+function trimKeys(keys){
+ var i = keys.length - 1;
+
+ function fnIter(){
+
+ var args = ['zremrangebyrank', keys[i], 0, -stop-1];
+
+ r.redisCli(args, function(res){
+ if(!isNaN(parseInt(res, 10))){
+ argv.verbose && console.log('', keys[i], ,"\t", 'trimmed');
+ return fnDone();
+ }
+
+ console.error('>', args.join(' '));
+ console.error(res.trim());
+ });
+ }
+
+ function fnDone(){
+ if(--i == -1) return;
+ fnIter();
+ }
+
+ fnIter();
+}
+
+function printKeys(keys){
+ console.log(keys.join('\n'));
+ console.log("%s key(s) found", keys.length);
+}
+
+
+function fnIf(cond, fnCondValid, fnCondFalse){return cond ? fnCondValid : fnCondFalse;}
+
+r.findKeys(pattern, fnIf(argv.preview, printKeys, trimKeys));
View
5 package.json
@@ -14,7 +14,10 @@
},
"bin":{
- "redis-rename": "./bin/redis-rename"
+ "redis-rename": "./bin/redis-rename",
+ "redis-delall": "./bin/redis-delall",
+ "redis-zlist-size": "./bin/redis-zlist-size",
+ "redis-ztrim": "./bin/redis-ztrim"
},
"author" : "Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com)",

0 comments on commit 70b5def

Please sign in to comment.