Skip to content

Commit

Permalink
start of variadic support
Browse files Browse the repository at this point in the history
  • Loading branch information
tkaemming committed Dec 9, 2015
1 parent 6e60635 commit 1b434d1
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions scripts/push.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
local topic = KEYS[1]

-- TODO: Support variadic arguments for publishing multiple items.
local item = ARGV[1]
local items = ARGV

local configuration = redis.call('GET', topic)
assert(configuration, 'topic does not exist')
configuration = cmsgpack.unpack(configuration)

local number = 0
local offset = 0
local length = 0 -- length of current page

local function start_page ()
redis.log(redis.LOG_DEBUG, string.format('Starting new page %s#%s.', topic, number))
Expand All @@ -23,21 +23,25 @@ local function close_page ()
number = number + 1
end

local last = redis.call('ZREVRANGE', topic .. '/pages', '0', '0', 'WITHSCORES')
if #last > 0 then
number = tonumber(last[1])
local length = redis.call('LLEN', topic .. '/pages/' .. number)
offset = tonumber(last[2]) + length
local function check_page ()
if length >= configuration['size'] then
close_page()
start_page()
end
end

local last = redis.call('ZREVRANGE', topic .. '/pages', '0', '0', 'WITHSCORES')
if #last > 0 then
number = tonumber(last[1])
length = redis.call('LLEN', topic .. '/pages/' .. number)
offset = tonumber(last[2]) + length
else
start_page()
end

local page = topic .. '/pages/' .. number

redis.call('RPUSH', page, item)
for i=1,#items do
check_page()
redis.call('RPUSH', topic .. '/pages/' .. number, items[i])
end

return offset

0 comments on commit 1b434d1

Please sign in to comment.