Class: Redcord::Redis

Inherits:
Redis
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/redcord/redis.rb

Instance Method Summary collapse

Instance Method Details

#create_hash_returning_id(key, args, ttl:, index_attrs:, range_index_attrs:, custom_index_attrs:, hash_tag: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/redcord/redis.rb', line 20

def create_hash_returning_id(key, args, ttl:, index_attrs:, range_index_attrs:, custom_index_attrs:, hash_tag: nil)
  id = "#{SecureRandom.uuid}#{hash_tag}"
  custom_index_attrs_flat = custom_index_attrs.inject([]) do |result, (index_name, attrs)|
    result << index_name
    result << attrs.size
    result + attrs
  end
  run_script(
    :create_hash,
    keys: [id, hash_tag],
    argv: [key, ttl, index_attrs.size, range_index_attrs.size, custom_index_attrs_flat.size] +
      index_attrs + range_index_attrs + custom_index_attrs_flat + args.to_a.flatten,
  )
  id
end

#delete_hash(model, id, index_attrs:, range_index_attrs:, custom_index_attrs:) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/redcord/redis.rb', line 75

def delete_hash(model, id, index_attrs:, range_index_attrs:, custom_index_attrs:)
  custom_index_names = custom_index_attrs.keys
  run_script(
    :delete_hash,
    keys: [id, id.match(/\{.*\}$/)&.send(:[], 0)],
    argv: [model, index_attrs.size, range_index_attrs.size] + index_attrs + range_index_attrs + custom_index_names,
  )
end

#find_by_attr(model, query_conditions, select_attrs: Set.new, index_attrs:, range_index_attrs:, custom_index_attrs: Array.new, hash_tag: nil, custom_index_name: nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/redcord/redis.rb', line 96

def find_by_attr(
      model,
      query_conditions,
      select_attrs: Set.new,
      index_attrs:,
      range_index_attrs:,
      custom_index_attrs: Array.new,
      hash_tag: nil,
      custom_index_name: nil
    )
  conditions = flatten_with_partial_sort(query_conditions.clone, custom_index_attrs)
  res = run_script(
    :find_by_attr,
    keys: [hash_tag],
    argv: [model, custom_index_name, index_attrs.size, range_index_attrs.size, custom_index_attrs.size, conditions.size] + 
      index_attrs + range_index_attrs + custom_index_attrs + conditions + select_attrs.to_a.flatten
  )
  # The Lua script will return this as a flattened array.
  # Convert the result into a hash of {id -> model hash}
  res_hash = res.each_slice(2)
  res_hash.map { |key, val| [key, val.each_slice(2).to_h] }.to_h
end

#find_by_attr_count(model, query_conditions, index_attrs:, range_index_attrs:, custom_index_attrs: Array.new, hash_tag: nil, custom_index_name: nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/redcord/redis.rb', line 130

def find_by_attr_count(
      model,
      query_conditions,
      index_attrs:,
      range_index_attrs:,
      custom_index_attrs: Array.new,
      hash_tag: nil,
      custom_index_name: nil
    )
  conditions = flatten_with_partial_sort(query_conditions.clone, custom_index_attrs)
  run_script(
    :find_by_attr_count,
    keys: [hash_tag],
    argv: [model, custom_index_name, index_attrs.size, range_index_attrs.size, custom_index_attrs.size] +
      index_attrs + range_index_attrs + custom_index_attrs + conditions
  )
end

#scan_each_shard(key, count: 1000, &blk) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/redcord/redis.rb', line 148

def scan_each_shard(key, count: 1000, &blk)
  clients = instance_variable_get(:@client)
    &.instance_variable_get(:@node)
    &.instance_variable_get(:@clients)
    &.values

  if clients.nil?
    scan_each(match: key, count: count, &blk)
  else
    clients.each do |client|
      cursor = 0
      loop do
        cursor, keys = client.call([:scan, cursor, 'match', key, 'count', count])
        keys.each(&blk)
        break if cursor == "0"
      end
    end
  end
end

#update_hash(model, id, args, ttl:, index_attrs:, range_index_attrs:, custom_index_attrs:, hash_tag:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/redcord/redis.rb', line 48

def update_hash(model, id, args, ttl:, index_attrs:, range_index_attrs:, custom_index_attrs:, hash_tag:)
  custom_index_attrs_flat = custom_index_attrs.inject([]) do |result, (index_name, attrs)|
    if !(args.keys.to_set & attrs.to_set).empty?
      result << index_name
      result << attrs.size
      result + attrs
    else
      result
    end
  end
  run_script(
    :update_hash,
    keys: [id, hash_tag],
    argv: [model, ttl, index_attrs.size, range_index_attrs.size, custom_index_attrs_flat.size] +
      index_attrs + range_index_attrs + custom_index_attrs_flat + args.to_a.flatten,
  )
end