Module: Familia::Tools

Extended by:
Tools
Included in:
Tools
Defined in:
lib/familia/tools.rb

Instance Method Summary collapse

Instance Method Details

#get_any(keyname, uri = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/familia/tools.rb', line 50

def get_any(keyname, uri = nil)
  type = Familia.redis(uri).type keyname
  case type
  when 'string'
    Familia.redis(uri).get keyname
  when 'list'
    Familia.redis(uri).lrange(keyname, 0, -1) || []
  when 'set'
    Familia.redis(uri).smembers(keyname) || []
  when 'zset'
    Familia.redis(uri).zrange(keyname, 0, -1) || []
  when 'hash'
    Familia.redis(uri).hgetall(keyname) || {}
  else
    nil
  end
end

#move_keys(filter, source_uri, target_uri, &each_key) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/familia/tools.rb', line 6

def move_keys(filter, source_uri, target_uri, &each_key)
  raise "Source and target are the same (#{target_uri})" if target_uri == source_uri

  Familia.connect target_uri
  source_keys = Familia.redis(source_uri).keys(filter)
  puts "Moving #{source_keys.size} keys from #{source_uri} to #{target_uri} (filter: #{filter})"
  source_keys.each_with_index do |key, idx|
    type = Familia.redis(source_uri).type key
    ttl = Familia.redis(source_uri).ttl key
    if source_uri.host == target_uri.host && source_uri.port == target_uri.port
      Familia.redis(source_uri).move key, target_uri.db
    else
      case type
      when 'string'
        Familia.redis(source_uri).get key
      when 'list'
        Familia.redis(source_uri).lrange key, 0, -1
      when 'set'
        Familia.redis(source_uri).smembers key
      else
        raise Familia::Problem, "unknown key type: #{type}"
      end
      raise 'Not implemented'
    end
    yield(idx, type, key, ttl) unless each_key.nil?
  end
end

#rename(filter, source_uri, target_uri = nil, &each_key) ⇒ Object

Use the return value from each_key as the new key name



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/familia/tools.rb', line 35

def rename(filter, source_uri, target_uri = nil, &each_key)
  target_uri ||= source_uri
  move_keys filter, source_uri, target_uri if source_uri != target_uri
  source_keys = Familia.redis(source_uri).keys(filter)
  puts "Renaming #{source_keys.size} keys from #{source_uri} (filter: #{filter})"
  source_keys.each_with_index do |key, idx|
    Familia.trace :RENAME1, Familia.redis(source_uri), "#{key}", ''
    type = Familia.redis(source_uri).type key
    ttl = Familia.redis(source_uri).ttl key
    newkey = yield(idx, type, key, ttl) unless each_key.nil?
    Familia.trace :RENAME2, Familia.redis(source_uri), "#{key} -> #{newkey}", caller(1..1).first
    Familia.redis(source_uri).renamenx key, newkey
  end
end