[feature] include lua numeric table <=> php string array conversion #198

Closed
r043v opened this Issue May 31, 2012 · 4 comments

2 participants

@r043v

ok, i try a bit lua scripting into redis,

i'm forced to use 2 table entry for save key name and key data

exemple :

$lua = 'local el=redis.pcall("smembers",KEYS[1])
local out={}
local n=1
for k,v in pairs(el)
do
out[n]=v
out[n+1]=redis.pcall("hgetall",v)
n=n+2
end
return out';

who will return me :

Array
(
[0] => hashKey0name
[1] => Array
(
[0] => hashElem0
[1] => hashElem0value
[2] => hashElem1
[3] => hashElem1value
)

[2] => hashKey1name
[3] => Array
    (
        [0] => hashElem0
        [1] => hashElem0value
        [2] => hashElem1
        [3] => hashElem1value
    )

)

and i need to "decode" the output to made a classic php array like :

Array
(
[hashKey0name] => Array
(
[hashElem0] => hashElem0value
[hashElem1] => hashElem1value
)

[hashKey1name] => Array
    (
        [hashElem0] => hashElem0value
        [hashElem1] => hashElem1value
    )

)

i think the code already exist (for hgetall etc ...) can you set a hight level function (recursive) who will do the job ?

thanks in advance :- )

@r043v

here is a php version of the function

function lua2php($a)
{ if(!is_array($a)) return $a;
$nb = count($a);
$out = array();
for($n=0;$n<$nb;$n+=2)
{ $k = $a[$n];
$v = $a[$n+1];
if(is_array($v)) $v = lua2php($v);
$out[$k] = $v;
}
return $out;
}

@michael-grunder
phpredis member

Hey,

There is no real way for us to decode the data like this on the phpredis side, because the data that comes back from LUA could be literally anything. Consider this different example that also returns a very similar result:

local result={}
local sdata=redis.call('smembers','master')
local n=1
for k,v in pairs(sdata) do
        result[n]=v
        result[n+1]=redis.call('smembers','child-'..v)
        n=n+2
end
return result

And place this in the sets:

sadd master m1
sadd master m2
sadd child-m1 m1-c1 m2-c2
sadd child-m2 m2-c1 m2-c2

This nets us the following from redis-cli:

1) "m1"
2) 1) "m1-c1"
   2) "m1-c2"
3) "m2"
4) 1) "m2-c1"
   2) "m2-c2"

And this from phpredis:

Array
(
    [0] => m1
    [1] => Array
        (
            [0] => m1-c1
            [1] => m1-c2
        )

    [2] => m2
    [3] => Array
        (
            [0] => m2-c1
            [1] => m2-c2
        )

)

The structure is identical to your example, but in this case you wouldn't want the arrays transformed in this way. I think with LUA we need to leave the translation up to the caller, or you could do the translation inside of the LUA script itself.

I hope that makes sense!

Cheers,
Mike

@r043v

it make sense but we could get anyway a set of common tool function for work on the output for made general thing

it's easy to made this with php but php is slow i like speed :- )

the same function can be used for my exemple and your one, but for you just disabled recursion

(i really need to know how is parsed github comment ^^)

@michael-grunder
phpredis member

I think you're OK in terms of performance unless you're returning massive amounts of data. We could provide transformation hooks, but it would have to be in the form of a callback anyway and therefore still run in userland PHP code.

Here is general information on markdown (works in comments):
http://github.github.com/github-flavored-markdown/

Closing the issue for now.

Cheers,
Mike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment