Skip to content

Instantly share code, notes, and snippets.

@fritzy
Last active March 15, 2016 18:24

Revisions

  1. fritzy revised this gist Nov 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generatelookup.lua
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    --EVAL "this script" lookup-hash pattern attribute
    --this script WILL NOT, because SCAN is not deterministic
    --this script WILL NOT RUN, because SCAN is not deterministic
    local lookup = KEYS[1];
    local pattern, attr = unpack(ARGV);

  2. fritzy created this gist Nov 12, 2013.
    60 changes: 60 additions & 0 deletions generatelookup.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/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();
    }
    );
    17 changes: 17 additions & 0 deletions generatelookup.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    --EVAL "this script" lookup-hash pattern attribute
    --this script WILL NOT, 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