Skip to content

Instantly share code, notes, and snippets.

@yhemanth
Created October 29, 2015 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhemanth/aff4b54d3f97d80873cc to your computer and use it in GitHub Desktop.
Save yhemanth/aff4b54d3f97d80873cc to your computer and use it in GitHub Desktop.
Lua script that can be embedded in Redis server for adding an element to a KMinHash set backed by a Redis sorted set
local count = redis.call('ZCARD', KEYS[1]);
if count == tonumber(ARGV[1]) then
local max_score = redis.call('ZRANGE', KEYS[1], -1, -1, 'WITHSCORES')[2]
if tonumber(ARGV[2]) >= tonumber(max_score) then
return
else
redis.call('ZREMRANGEBYRANK', KEYS[1], -1, -1)
end
end
redis.call('ZADD', KEYS[1], tonumber(ARGV[2]), tonumber(ARGV[3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment