lua-redis-parser icon indicating copy to clipboard operation
lua-redis-parser copied to clipboard

zrange withscores not parsed well

Open beedub opened this issue 13 years ago • 3 comments
trafficstars

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!)

beedub avatar May 04 '12 05:05 beedub

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

agentzh avatar May 04 '12 06:05 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.

agentzh avatar May 04 '12 06:05 agentzh

Any updates on this issue?

agentzh avatar May 12 '12 12:05 agentzh