Class: Redis::Populator
- Inherits:
-
Object
- Object
- Redis::Populator
- Includes:
- Helper
- Defined in:
- lib/redis_migrator/redis_populator.rb
Instance Attribute Summary collapse
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
-
#generate_keys(num) ⇒ Object
Generate sets’ keys to populate redis cluster.
-
#initialize(redis_hosts) ⇒ Populator
constructor
A new instance of Populator.
-
#populate_cluster(keys_num, num) ⇒ Object
Populates redis cluster.
-
#populate_keys(node, keys, size) ⇒ Object
Populates sets with the given amount of members.
Methods included from Helper
#parse_redis_url, #to_redis_proto
Constructor Details
Instance Attribute Details
#redis ⇒ Object
Returns the value of attribute redis.
8 9 10 |
# File 'lib/redis_migrator/redis_populator.rb', line 8 def redis @redis end |
Instance Method Details
#generate_keys(num) ⇒ Object
Generate sets’ keys to populate redis cluster
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/redis_migrator/redis_populator.rb', line 17 def generate_keys(num) num.times.inject({}) do |acc, i| key = ::Digest::MD5.hexdigest(i.to_s) node = redis.node_for(key).client hash_key = "redis://#{node.host}:#{node.port}/#{node.db}" acc[hash_key] = [] if acc[hash_key].nil? acc[hash_key] << key acc end end |
#populate_cluster(keys_num, num) ⇒ Object
Populates redis cluster
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/redis_migrator/redis_populator.rb', line 46 def populate_cluster(keys_num, num) redis.flushdb nodes = generate_keys(keys_num) threads = [] nodes.keys.each do |node_url| node = parse_redis_url(node_url) threads << Thread.new(node, nodes[node_url], num) {|node, keys, size| populate_keys(node, keys, size) } end threads.each{|t| t.join} end |
#populate_keys(node, keys, size) ⇒ Object
Populates sets with the given amount of members
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/redis_migrator/redis_populator.rb', line 31 def populate_keys(node, keys, size) f = IO.popen("redis-cli -h #{node[:host]} -p #{node[:port]} -n #{node[:db]} --pipe", IO::RDWR) keys.each do |key| size.times.map do |x| f << to_redis_proto('SADD', key, ::Digest::MD5.hexdigest('f' + x.to_s)) end end f.close end |