class Aerospike::Unpacker

Constants

MsgPackExt

Public Class Methods

new() click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 46
def initialize
  @unpacker = MessagePack::Unpacker.new
  MsgPackExt::TYPES.each do |type|
    @unpacker.register_type(type) { |data| MsgPackExt.new(type, data) }
  end
end
use() { |unpacker| ... } click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 29
def self.use
  unpacker = @@pool.poll
  unpacker.reset
  yield unpacker
ensure
  @@pool.offer(unpacker)
end

Public Instance Methods

reset() click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 62
def reset
  @unpacker.reset
end
unpack(bytes) click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 53
def unpack(bytes)
  obj = @unpacker.feed(bytes).read
  case obj
  when Array then unpack_list(obj)
  when Hash  then unpack_map(obj)
  else obj
  end
end

Private Instance Methods

normalize_elem(elem) click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 85
def normalize_elem(elem)
  case elem
  when String
    ptype = elem.ord
    value = elem[1..-1]
    if (ptype == ParticleType::STRING)
      value.encode!(Aerospike.encoding)
    end
    value
  when Array
    unpack_list(elem)
  when Hash
    unpack_map(elem)
  else
    elem
  end
end
normalize_strings_in_array(arr) click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 103
def normalize_strings_in_array(arr)
  arr.map! { |elem| normalize_elem(elem) }
end
normalize_strings_in_map(hash) click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 107
def normalize_strings_in_map(hash)
  hash.inject({}) do |h, (k,v)|
    h.update({ normalize_elem(k) => normalize_elem(v) })
  end
end
unpack_list(array) click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 68
def unpack_list(array)
  list = normalize_strings_in_array(array)
  unless list.empty?
    list.shift if MsgPackExt === list.first
  end
  list
end
unpack_map(hash) click to toggle source
# File lib/aerospike/utils/unpacker.rb, line 76
def unpack_map(hash)
  hash = normalize_strings_in_map(hash)
  unless hash.empty?
    (key, _) = hash.first
    hash.shift if MsgPackExt === key
  end
  hash
end