module MeRedis::ZipValues

todo warn in development when gzipped size iz bigger than strict use prepend for classes

Public Class Methods

prepended(base) click to toggle source
# File lib/me_redis/zip_values.rb, line 40
def self.prepended(base)
  base::Future.prepend(FutureUnzip)

  base.extend(MeRedis::ClassMethods)

  base.me_config.default_compressor = ::MeRedis::ZipValues::ZlibCompressor
end

Public Instance Methods

_patch_futures(client) click to toggle source
# File lib/me_redis/zip_values.rb, line 62
def _patch_futures(client)
  client.futures.each do |ftr|

    ftr.set_transformation do |vl|
      if vl && FutureUnzip::COMMANDS[ftr._command[0]]
        # we only dealing here with GET methods, so it could be hash getters or get/mget
        keys = ftr._command[0][0] == 'h' ? ftr._command[1, 1] : ftr._command[1..-1]
        if ftr._command[0] == :mget
          vl.each_with_index.map{ |v, i| zip?(keys[i]) ? self.class.get_compressor_for_key(keys[i]).decompress( v ) : v }
        elsif zip?(keys[0])
          compressor = self.class.get_compressor_for_key(keys[0])
          # on hash commands it could be an array
          vl.is_a?(Array) ? vl.map!{ |v| compressor.decompress(v) } : compressor.decompress(vl)
        else
          vl
        end
      else
        vl
      end
    end

  end
end
get( key ) click to toggle source

Redis prepended methods

Calls superclass method
# File lib/me_redis/zip_values.rb, line 100
def get( key ); unzip_value( super( key ), key) end
getset( key, value ) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 106
def getset( key, value ); unzip_value( super( key, zip_value(value, key) ), key ) end
hget( key, h_key ) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 108
def hget( key, h_key ); unzip_value( super( key, h_key ), key ) end
hmget( key, *args ) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 114
def hmget( key, *args ); unzip_arr_or_future( super(key, *args), key ) end
hmset( key, *args ) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 112
def hmset( key, *args ); super( key, map_hmsets_arr(key, *args) ) end
hset( key, h_key, value ) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 109
def hset( key, h_key, value );  super( key, h_key, zip_value(value, key) ) end
hsetnx( key, h_key, value ) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 110
def hsetnx( key, h_key, value ); super( key, h_key, zip_value(value, key) ) end
mget(*args) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 103
def mget(*args); unzip_arr_or_future(super(*args), args ) end
mset(*args) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 104
def mset(*args); super( *map_msets_arr(args) ) end
multi(&block) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 55
def multi(&block)
  super do |redis|
    block.call(redis)
    _patch_futures(@client)
  end
end
pipelined(&block) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 48
def pipelined(&block)
  super do |redis|
    block.call(redis)
    _patch_futures(@client)
  end
end
set( key, value, options = {} ) click to toggle source
Calls superclass method
# File lib/me_redis/zip_values.rb, line 101
def set( key, value, options = {} ); super( key, zip_value(value, key), options ) end
unzip_value(value, key) click to toggle source
# File lib/me_redis/zip_values.rb, line 91
def unzip_value(value, key)
  return value if value.is_a?( FutureUnzip )

  value.is_a?(String) && zip?(key) ? self.class.get_compressor_for_key(key).decompress( value ) : value
end
zip?(key) click to toggle source
# File lib/me_redis/zip_values.rb, line 97
def zip?(key); self.class.zip?(key) end
zip_value(value, key ) click to toggle source
# File lib/me_redis/zip_values.rb, line 87
def zip_value(value, key )
  zip?(key) ? self.class.get_compressor_for_key(key).compress( value ) : value
end

Private Instance Methods

map_hmsets_arr( key, *args ) click to toggle source
# File lib/me_redis/zip_values.rb, line 124
def map_hmsets_arr( key, *args )
  return args unless zip?(key)
  counter = 0
  args.map!{ |kv| (counter +=1).odd? ? kv : zip_value(kv, key ) }
end
map_msets_arr( args ) click to toggle source
# File lib/me_redis/zip_values.rb, line 130
def map_msets_arr( args )
  args.tap { (args.length/2).times{ |i| args[2*i+1] = zip_value(args[2*i+1], args[2*i] ) } }
end
unzip_arr_or_future( arr, keys ) click to toggle source
# File lib/me_redis/zip_values.rb, line 118
def unzip_arr_or_future( arr, keys )
  return arr if arr.is_a?(FutureUnzip)

  arr.tap { arr.each_with_index { |val, i| arr[i] = unzip_value(val,keys.is_a?(Array) ? keys[i] : keys)} }
end