Sha256: d64a13cdca18b69fb566d5390fb431e797275885a892ccd3b0c17b110362039f

Contents?: true

Size: 1.19 KB

Versions: 20

Compression:

Stored size: 1.19 KB

Contents

require "dynport_tools"
require "redis"

class DynportTools::RedisDumper
  attr_accessor :redis
  
  def initialize(redis = nil)
    self.redis = redis
  end
  
  def zset_to_hash(key, window = 10_000)
    hash = {}
    offset = 0
    while true
      values = redis.zrevrange(key, offset, offset + (window - 1), :with_scores => true)
      current_key = nil
      values.each do |value|
        if current_key.nil?
          current_key = value
        else
          hash[current_key] = value
          current_key = nil
        end
      end
      offset += window
      break if values.length < window * 2
    end
    hash
  end
  
  def dump_hash(hash)
    hash.each do |key, value|
      puts [key, value].join("\t")
    end
  end
  
  def run_from_args(args)
    host, port, key = args
    if key
      self.redis = Redis.new(:host => host, :port => port)
      key_type = redis.type(key)
      if key_type == "zset"
        dump_hash(zset_to_hash(key))
      else
        $stderr.puts "only zsets are supported for now"
        exit(1)
      end
    else
      print_usage_and_die
    end
  end

  def print_usage_and_die
    $stderr.puts "USAGE: redis_dumper <redis_host> <redis_port> <key>"
    exit(1)
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
dynport_tools-0.2.22 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.21 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.20 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.19 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.18 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.17 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.16 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.15 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.14 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.13 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.12 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.11 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.10 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.9 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.8 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.6 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.5 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.4 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.3 lib/dynport_tools/redis_dumper.rb
dynport_tools-0.2.2 lib/dynport_tools/redis_dumper.rb