paginate-1.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. --[[
  2. Paginate a set or hash
  3. Input:
  4. KEYS[1] key pointing to the set or hash to be paginated.
  5. ARGV[1] page start offset
  6. ARGV[2] page end offset (-1 for all the elements)
  7. ARGV[3] cursor
  8. ARGV[4] offset
  9. ARGV[5] max iterations
  10. ARGV[6] fetch jobs?
  11. Output:
  12. [cursor, offset, items, numItems]
  13. ]]
  14. local rcall = redis.call
  15. -- Includes
  16. --- @include "includes/findPage"
  17. local key = KEYS[1]
  18. local scanCommand = "SSCAN"
  19. local countCommand = "SCARD"
  20. local type = rcall("TYPE", key)["ok"]
  21. if type == "none" then
  22. return {0, 0, {}, 0}
  23. elseif type == "hash" then
  24. scanCommand = "HSCAN"
  25. countCommand = "HLEN"
  26. elseif type ~= "set" then
  27. return
  28. redis.error_reply("Pagination is only supported for sets and hashes.")
  29. end
  30. local numItems = rcall(countCommand, key)
  31. local startOffset = tonumber(ARGV[1])
  32. local endOffset = tonumber(ARGV[2])
  33. if endOffset == -1 then
  34. endOffset = numItems
  35. end
  36. local pageSize = (endOffset - startOffset) + 1
  37. local cursor, offset, items, jobs = findPage(key, scanCommand, startOffset,
  38. pageSize, ARGV[3], tonumber(ARGV[4]),
  39. tonumber(ARGV[5]), ARGV[6])
  40. return {cursor, offset, items, numItems, jobs}