Skip to content

Commit

Permalink
Support for tracking the top-used tags at any one time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Lecocq committed Apr 24, 2012
1 parent 09bc1dc commit 867a503
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions cancel.lua
Expand Up @@ -52,6 +52,7 @@ else
local tags = cjson.decode(redis.call('hget', 'ql:j:' .. jid, 'tags') or '{}')
for i, tag in ipairs(tags) do
redis.call('zrem', 'ql:t:' .. tag, jid)
redis.call('zincrby', 'ql:tags', -1, tag)
end

-- Just go ahead and delete our data
Expand Down
2 changes: 2 additions & 0 deletions complete.lua
Expand Up @@ -174,6 +174,7 @@ else
local tags = cjson.decode(redis.call('hget', 'ql:j:' .. jid, 'tags') or '{}')
for i, tag in ipairs(tags) do
redis.call('zrem', 'ql:t:' .. tag, jid)
redis.call('zincrby', 'ql:tags', -1, tag)
end
redis.call('del', 'ql:j:' .. jid)
end
Expand All @@ -186,6 +187,7 @@ else
local tags = cjson.decode(redis.call('hget', 'ql:j:' .. jid, 'tags') or '{}')
for i, tag in ipairs(tags) do
redis.call('zrem', 'ql:t:' .. tag, jid)
redis.call('zincrby', 'ql:tags', -1, tag)
end
redis.call('del', 'ql:j:' .. jid)
end
Expand Down
1 change: 1 addition & 0 deletions put.lua
Expand Up @@ -83,6 +83,7 @@ end
-- Add this job to the list of jobs tagged with whatever tags were supplied
for i, tag in ipairs(tags) do
redis.call('zadd', 'ql:t:' .. tag, now, jid)
redis.call('zincrby', 'ql:tags', 1, tag)
end

-- If we're in the failed state, remove all of our data
Expand Down
15 changes: 13 additions & 2 deletions tag.lua
@@ -1,5 +1,7 @@
-- tag(0, (('add' | 'remove'), jid, now, tag, [tag, ...]) | 'get', tag, [offset, [count]])
-- ----------------------------------------------------------------------------------
-- tag(0, ('add' | 'remove'), jid, now, tag, [tag, ...])
-- tag(0, 'get', tag, [offset, [count]])
-- tag(0, 'top', [offset, [count]])
-- ------------------------------------------------------------------------------------------------------------------
-- Accepts a jid, 'add' or 'remove', and then a list of tags
-- to either add or remove from the job. Alternatively, 'get',
-- a tag to get jobs associated with that tag, and offset and
Expand All @@ -16,6 +18,9 @@
-- ...
-- ]
-- }
--
-- If 'top' is supplied, it returns the most commonly-used tags
-- in a paginated fashion.

if #KEYS ~= 0 then
error('Tag(): Got ' .. #KEYS .. ', expected 0')
Expand Down Expand Up @@ -43,6 +48,7 @@ if command == 'add' then
table.insert(tags, tag)
end
redis.call('zadd', 'ql:t:' .. tag, now, jid)
redis.call('zincrby', 'ql:tags', 1, tag)
end

tags = cjson.encode(tags)
Expand All @@ -69,6 +75,7 @@ elseif command == 'remove' then
local tag = ARGV[i]
_tags[tag] = nil
redis.call('zrem', 'ql:t:' .. tag, jid)
redis.call('zincrby', 'ql:tags', -1, tag)
end

local results = {}
Expand All @@ -92,6 +99,10 @@ elseif command == 'get' then
total = redis.call('zcard', 'ql:t:' .. tag),
jobs = redis.call('zrange', 'ql:t:' .. tag, offset, count)
})
elseif command == 'top' then
local offset = assert(tonumber(ARGV[2] or 0) , 'Tag(): Arg "offset" not a number: ' .. tostring(ARGV[2]))
local count = assert(tonumber(ARGV[3] or 25), 'Tag(): Arg "count" not a number: ' .. tostring(ARGV[3]))
return cjson.encode(redis.call('zrevrangebyscore', 'ql:tags', '+inf', 2, 'limit', offset, count))
else
error('Tag(): First argument must be "add", "remove" or "get"')
end

0 comments on commit 867a503

Please sign in to comment.