Sha256: 19d391ef85e9fad02b8bc665d94ee6c033898144766d9b999373c338934fb20d

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require "ruby-progressbar"

module RedisScanner
  class Engine
    def initialize(redis, options)
      @redis = redis
      @options = options
      @rule = Rule.new
    end

    def run
      result = scan
      result.values.sort
    end

    private

    def create_progress_bar
      total = @redis.total_keys
      bar = ProgressBar.create(
        title: "Keys",
        format: '%a %bᗧ%i %p%% %t',
        progress_mark: ' ',
        remainder_mark: '・',
        total: total)
      bar.log "total keys is #{total}"
      bar
    end

    def scan
      cursor = 0
      stat = Hash.new {|hash, key| hash[key] = Pattern.new(key) }

      bar = create_progress_bar
      while true
        if @options[:match]
          cursor, result = @redis.scan cursor, match: @options[:match]
        else
          cursor, result = @redis.scan cursor
        end
        result.each do |key|
          pattern = @rule.extract_pattern(key)
          if @options[:detail]
            type, size = @redis.get_type_and_size(key)
            stat[pattern].increment type, size
          else
            stat[pattern].increment
          end
          bar.increment
        end
        cursor = cursor.to_i
        break if cursor == 0
      end
      bar.finish

      stat
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_scanner-0.1.3 lib/redis_scanner/engine.rb