Module: Sidekiq::Paginator

Defined in:
lib/sidekiq/paginator.rb

Constant Summary collapse

TYPE_CACHE =
{
  "dead" => "zset",
  "retry" => "zset",
  "schedule" => "zset"
}

Instance Method Summary collapse

Instance Method Details

#page(key, pageidx = 1, page_size = 25, opts = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sidekiq/paginator.rb', line 11

def page(key, pageidx = 1, page_size = 25, opts = nil)
  current_page = (pageidx.to_i < 1) ? 1 : pageidx.to_i
  pageidx = current_page - 1
  total_size = 0
  items = []
  starting = pageidx * page_size
  ending = starting + page_size - 1

  Sidekiq.redis do |conn|
    # horrible, think you can make this cleaner?
    type = TYPE_CACHE[key]
    if type
    elsif key.start_with?("queue:")
      type = TYPE_CACHE[key] = "list"
    else
      type = TYPE_CACHE[key] = conn.type(key)
    end
    rev = opts && opts[:reverse]

    case type
    when "zset"
      total_size, items = conn.multi { |transaction|
        transaction.zcard(key)
        if rev
          transaction.zrange(key, starting, ending, "REV", "withscores")
        else
          transaction.zrange(key, starting, ending, "withscores")
        end
      }
      [current_page, total_size, items]
    when "list"
      total_size, items = conn.multi { |transaction|
        transaction.llen(key)
        if rev
          transaction.lrange(key, -ending - 1, -starting - 1)
        else
          transaction.lrange(key, starting, ending)
        end
      }
      items.reverse! if rev
      [current_page, total_size, items]
    when "none"
      [1, 0, []]
    else
      raise "can't page a #{type}"
    end
  end
end

#page_items(items, pageidx = 1, page_size = 25) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/sidekiq/paginator.rb', line 60

def page_items(items, pageidx = 1, page_size = 25)
  current_page = (pageidx.to_i < 1) ? 1 : pageidx.to_i
  pageidx = current_page - 1
  starting = pageidx * page_size
  items = items.to_a
  [current_page, items.size, items[starting, page_size]]
end