Making a list uniq with a Lua script?

149 views
Skip to first unread message

MeSF

unread,
Mar 26, 2013, 6:59:23 PM3/26/13
to redi...@googlegroups.com
Hello everyone,

I was wondering if anyone already made a Lua script to make a list 'uniq', by removing all duplicates.

We have created this:


redis.call('del', KEYS[2])
local list = redis.call('lrange', KEYS[1] , 0, -1)
redis.log(redis.LOG_NOTICE, KEYS[1],  redis.call('llen', KEYS[1]))

local function table_count(tt, item)
  local count
  count = 0
  for ii,xx in pairs(tt) do
    if item == xx then count = count + 1 end
  end
  return count
end

local function table_unique(tt)
  local newtable
  newtable = {}
  for ii,xx in ipairs(tt) do
    if(table_count(newtable, xx) == 0) then
      newtable[#newtable+1] = xx
      redis.call('lpush', KEYS[2], xx)
    end
  end
  return newtable
end

table_unique(list)
redis.log(redis.LOG_NOTICE, KEYS[2],  redis.call('llen', KEYS[2]))

return redis.call('llen', KEYS[2])

But it's extremely slow. Anyone has a better idea?

Thanks

Josiah Carlson

unread,
Mar 27, 2013, 4:02:07 AM3/27/13
to redi...@googlegroups.com
Is your issue that you need the order to be in an order expected by adding to the left and right of the LIST?

If so, then you can use a Lua Script for insertion or removal from either end. Basically, you use a ZSET as a LIST, and use a bit of Lua to implement your LIST-like operations on ZSETs. About the only thing you can't do is "SORT" the "LIST" without performing a copy first - which can be done via:
 redis.call('rpush', key, unpack(redis.call('zrange', key2, 0, -1)))

 - Josiah


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages