lua-redis-parser
lua-redis-parser copied to clipboard
zrange withscores not parsed well
Parsed reply bodies from zrange with "withscore" option do not have any delimiter between the member and score, thus making the reply almost impossible to interpret.
Some test code below. The /redis location is a raw_response redis interpretor that is used in many of your examples. The output is 1 999888, which is confusing.
local parser = require "redis.parser"
local req = parser.build_query( { "zadd", "test", 888, 999 } )
req = req .. parser.build_query( { "zrange", "test", 0, -1, "withscores" })
local res = ngx.location.capture("/redis?2",
{ body = req })
local replies = parser.parse_replies(res.body, 2)
-- formatted reply
for i, reply in ipairs(replies) do
local res, typ = reply[1], reply[2]
ngx.say(res)
end
(Awesome work on open_resty btw!)
On Fri, May 4, 2012 at 1:02 PM, Brian [email protected] wrote:
-- formatted reply for i, reply in ipairs(replies) do local res, typ = reply[1], reply[2] ngx.say(res) end
Actually, you're using redis pipeling here and the "replies" variable is a Lua table holding all the replies for all your redis commands (including one zadd command and one zrange command), therefore the "res" variable that you feed into ngx.say() could be of type "table". Because ngx.say() will automatically concatenate the string elements in the Lua table-typed arguments, it gives you a false impression that no delimiter exists between the scores and the values. Try the following:
for i, reply in ipairs(replies) do
local res, typ = reply[1], reply[2]
if type(res) == "table" then
ngx.say("table: ", table.concat(res, " "))
else
ngx.say("scalar: ", table.concat(res, " "))
end
end
That is, you should check table-typed "res" variable yourself, just as indicated in the official documentation for the redis zadd command:
http://redis.io/commands/zadd
The table elements at odd-number indexes are "values" while those at even-number indexes are "scores".
Regards, -agentzh
BTW, you may also want to take a look at the lua-resty-redis library: https://github.com/agentzh/lua-resty-redis This library is more flexible and more convenient to use than ngx_redis2 + lua-redis-parser. Also lua-resty-redis can be more efficient.
Any updates on this issue?