LUA Scripting help

32 views
Skip to first unread message

Kevin Burton

unread,
Feb 28, 2015, 6:07:19 PM2/28/15
to redi...@googlegroups.com
I have a script:

local count = 0
for k,v in ipairs(KEYS) do
   if redis.call('exists', v) then
       count = count + 1
   end
end
return count

When I run this script it always returns 20 (in my case that is the number if keys that were passed in). But when I run KEYS <pattern> I get an empty set. I seems that my LUA script is not detecting existence corrently. Ideas?


Itamar Haber

unread,
Feb 28, 2015, 7:02:53 PM2/28/15
to redi...@googlegroups.com
What <pattern> are you calling KEYS with? Use `KEYS *` to get all keys in a non-production environment.

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Jan-Erik Rediger

unread,
Mar 1, 2015, 6:44:41 AM3/1/15
to redi...@googlegroups.com
redis.call('exists', v) returns 0 if the key does not exist and 1 if it
does.
So you need to test if it returns 1 (0 is not a falsy value in Lua)

Kevin Burton

unread,
Mar 1, 2015, 10:10:08 AM3/1/15
to redi...@googlegroups.com
That is the point KEYS * returns an empty list so redis-cli connecting to my server says there are no keys but the LUA script seems to indicate there are.

Itamar Haber

unread,
Mar 1, 2015, 10:37:56 AM3/1/15
to redi...@googlegroups.com
Thanks for putting it differently - now I see the problem.

TL;DR - redis.call('exists') returns 0 or 1, depending on the key's existance. However, 'if 0' in Lua is true, i.e.:

foo@bar:~$ redis-cli eval 'if 0 then return 1 end' 0
(integer) 1

To solve do 'if redis.call('exists', v) == 1 then`.

Longer version: I changed the script slightly and used my very own redis-lua-debugger (https://github.com/redislabs/redis-lua-debugger) to verify that behavior. Below is the modified script and the debugger's output - note how e is set to 0 and doesn't change until the end:

foo@bar:~$ cat t.lua 
rld.start()
local count = 0
local e

for k,v in ipairs(KEYS) do
   e = redis.call('exists', v)
   if e then 
       count = count + 1
   end
end
return count
foo@bar:~$ redis-cli flushall
OK
foo@bar:~$ redis-cli --eval t.lua foo bar baz qaz
(integer) 4

foo@bar:~$ cat /var/log/redis_6379
...
[970] 01 Mar 17:34:38.564 # [rld] -- rld v0.1.1 started
[970] 01 Mar 17:34:38.564 # [rld] KEYS[1] = 'foo'
[970] 01 Mar 17:34:38.564 # [rld] KEYS[2] = 'bar'
[970] 01 Mar 17:34:38.564 # [rld] KEYS[3] = 'baz'
[970] 01 Mar 17:34:38.564 # [rld] KEYS[4] = 'qaz'
[970] 01 Mar 17:34:38.564 # [rld] at Lua:@user_script (line: 2)
[970] 01 Mar 17:34:38.565 # [rld] at Lua:@user_script (line: 3)
[970] 01 Mar 17:34:38.565 # [rld] new [count] = 0
[970] 01 Mar 17:34:38.565 # [rld] at Lua:@user_script (line: 5)
[970] 01 Mar 17:34:38.565 # [rld] call to function C:ipairs
[970] 01 Mar 17:34:38.565 # [rld] return from function C:ipairs
[970] 01 Mar 17:34:38.565 # [rld] call to function C:(for generator)
[970] 01 Mar 17:34:38.565 # [rld] return from function C:(for generator)
[970] 01 Mar 17:34:38.565 # [rld] at Lua:@user_script (line: 6)
[970] 01 Mar 17:34:38.565 # [rld] new [k] = 1
[970] 01 Mar 17:34:38.565 # [rld] new [v] = 'foo'
[970] 01 Mar 17:34:38.565 # [rld] call to function C:call
[970] 01 Mar 17:34:38.565 # [rld] return from function C:call
[970] 01 Mar 17:34:38.566 # [rld] at Lua:@user_script (line: 7)
[970] 01 Mar 17:34:38.566 # [rld] new [e] = 0
[970] 01 Mar 17:34:38.566 # [rld] at Lua:@user_script (line: 8)
[970] 01 Mar 17:34:38.566 # [rld] at Lua:@user_script (line: 5)
[970] 01 Mar 17:34:38.566 # [rld] changed [count] = 1 (was 0)
[970] 01 Mar 17:34:38.566 # [rld] call to function C:(for generator)
[970] 01 Mar 17:34:38.566 # [rld] return from function C:(for generator)
[970] 01 Mar 17:34:38.566 # [rld] at Lua:@user_script (line: 6)
[970] 01 Mar 17:34:38.566 # [rld] changed [k] = 2 (was 1)
[970] 01 Mar 17:34:38.566 # [rld] changed [v] = 'bar' (was foo)
[970] 01 Mar 17:34:38.566 # [rld] call to function C:call
[970] 01 Mar 17:34:38.567 # [rld] return from function C:call
[970] 01 Mar 17:34:38.567 # [rld] at Lua:@user_script (line: 7)
[970] 01 Mar 17:34:38.567 # [rld] at Lua:@user_script (line: 8)
[970] 01 Mar 17:34:38.567 # [rld] at Lua:@user_script (line: 5)
[970] 01 Mar 17:34:38.567 # [rld] changed [count] = 2 (was 1)
[970] 01 Mar 17:34:38.567 # [rld] call to function C:(for generator)
[970] 01 Mar 17:34:38.567 # [rld] return from function C:(for generator)
[970] 01 Mar 17:34:38.567 # [rld] at Lua:@user_script (line: 6)
[970] 01 Mar 17:34:38.567 # [rld] changed [k] = 3 (was 2)
[970] 01 Mar 17:34:38.567 # [rld] changed [v] = 'baz' (was bar)
[970] 01 Mar 17:34:38.567 # [rld] call to function C:call
[970] 01 Mar 17:34:38.568 # [rld] return from function C:call
[970] 01 Mar 17:34:38.568 # [rld] at Lua:@user_script (line: 7)
[970] 01 Mar 17:34:38.568 # [rld] at Lua:@user_script (line: 8)
[970] 01 Mar 17:34:38.569 # [rld] at Lua:@user_script (line: 5)
[970] 01 Mar 17:34:38.569 # [rld] changed [count] = 3 (was 2)
[970] 01 Mar 17:34:38.569 # [rld] call to function C:(for generator)
[970] 01 Mar 17:34:38.569 # [rld] return from function C:(for generator)
[970] 01 Mar 17:34:38.569 # [rld] at Lua:@user_script (line: 6)
[970] 01 Mar 17:34:38.569 # [rld] changed [k] = 4 (was 3)
[970] 01 Mar 17:34:38.569 # [rld] changed [v] = 'qaz' (was baz)
[970] 01 Mar 17:34:38.569 # [rld] call to function C:call
[970] 01 Mar 17:34:38.569 # [rld] return from function C:call
[970] 01 Mar 17:34:38.569 # [rld] at Lua:@user_script (line: 7)
[970] 01 Mar 17:34:38.569 # [rld] at Lua:@user_script (line: 8)
[970] 01 Mar 17:34:38.569 # [rld] at Lua:@user_script (line: 5)
[970] 01 Mar 17:34:38.570 # [rld] changed [count] = 4 (was 3)
[970] 01 Mar 17:34:38.570 # [rld] call to function C:(for generator)
[970] 01 Mar 17:34:38.570 # [rld] return from function C:(for generator)
[970] 01 Mar 17:34:38.570 # [rld] at Lua:@user_script (line: 11)
[970] 01 Mar 17:34:38.570 # [rld] return from function Lua:@user_script
[970] 01 Mar 17:34:38.570 # [rld] -- profiler summary:
[970] 01 Mar 17:34:38.570 # [rld] --   C:(for generator) called x5 times
[970] 01 Mar 17:34:38.570 # [rld] --   C:ipairs called x1 times
[970] 01 Mar 17:34:38.570 # [rld] --   C:call called x4 times
[970] 01 Mar 17:34:38.570 # [rld] -- rld exited: end of script.

--

Itamar Haber | Chief Developers Advocate
Redis Watch Newsletter - Curator and Janitor
Redis Labs - Enterprise-Class Redis for Developers

Mobile: +1 (415) 688 2443
Mobile (IL): +972 (54) 567 9692
Email: ita...@redislabs.com
Skype: itamar.haber

Blog  |  Twitter  |  LinkedIn


Reply all
Reply to author
Forward
0 new messages