Skip to content

Instantly share code, notes, and snippets.

@fritzy
Last active March 15, 2016 18:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fritzy/7436527 to your computer and use it in GitHub Desktop.
Save fritzy/7436527 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// ./generatelookup -l lookupkey -p somekey* -a attribute
var redis = require('redis').createClient();
var async = require('async');
var opt = require('optimist');
opt.usage('$0 -l lookup -p pattern -a attribute');
opt.demand(['l', 'p', 'a']);
var argv = opt.argv;
var iter = '0';
async.doWhilst(
function (acb) {
console.log('SCAN', iter);
//scan with the current iterator, matching the given pattern
redis.send_command('SCAN', [iter, 'MATCH', argv.p], function (err, result) {
var keys;
if (err) {
acb(err);
} else {
//update the iterator
iter = result[0];
async.each(result[1],
//for each key
function (key, ecb) {
console.log(key);
redis.get(key, function (err, result) {
var value;
if (err) {
ecb(err);
} else {
//find the attribute in the result and set the lookup
value = JSON.parse(result);
console.log("Setting lookup:", value[argv.a], key);
redis.hset(argv.l, value[argv.a], key, function (err) {
ecb(err);
});
}
});
},
function (err) {
//done with this scan iterator; on to the next
acb(err);
}
)
}
});
},
//test to see if iterator is done
function () { return iter != '0'; },
//done
function (err) {
if(err) {
console.log("Error:", err);
} else {
console.log("Done.");
}
redis.end();
}
);
--EVAL "this script" lookup-hash pattern attribute
--this script WILL NOT RUN, because SCAN is not deterministic
local lookup = KEYS[1];
local pattern, attr = unpack(ARGV);
local iter = '0';
while true do
local scan = redis.call('SCAN', iter, 'MATCH', pattern);
iter = scan[1];
for idx, keyname in ipairs(scan[2]) do
local value = cjson.decode(redis.call('GET', keyname));
redis.call('HSET', lookup, value[attr], keyname);
end
if (iter == '0') then
break;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment