This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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(); | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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