class Moneta::Adapters::TokyoTyrant

TokyoTyrant backend @api public

Constants

ENOREC

error code: no record found

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :host (‘127.0.0.1’) Server host name @option options [Integer] :port (1978) Server port @option options [::TokyoTyrant::RDB] :backend Use existing backend instance

Calls superclass method Moneta::Adapter::new
# File lib/moneta/adapters/tokyotyrant.rb, line 37
def initialize(options = {})
  super
  @native = backend.class.name != 'TokyoTyrant::RDB'
  probe = '__tokyotyrant_endianness_probe'
  backend.delete(probe)
  backend.addint(probe, 1)
  @pack = backend.delete(probe) == [1].pack('l>') ? 'l>' : 'l<'
end

Public Instance Methods

close() click to toggle source

(see Proxy#close)

# File lib/moneta/adapters/tokyotyrant.rb, line 89
def close
  backend.close
  nil
end
create(key, value, options = {}) click to toggle source

(see Proxy#create)

# File lib/moneta/adapters/tokyotyrant.rb, line 75
def create(key, value, options = {})
  if @native
    begin
      # Native client throws an exception
      backend.putkeep(key, pack(value))
    rescue TokyoTyrantError
      false
    end
  else
    backend.putkeep(key, pack(value))
  end
end
delete(key, options = {}) click to toggle source

(see Proxy#delete)

# File lib/moneta/adapters/tokyotyrant.rb, line 61
def delete(key, options = {})
  value = load(key, options)
  if value
    backend.delete(key) or error
    value
  end
end
increment(key, amount = 1, options = {}) click to toggle source

(see Proxy#increment)

# File lib/moneta/adapters/tokyotyrant.rb, line 70
def increment(key, amount = 1, options = {})
  backend.addint(key, amount) or error
end
load(key, options = {}) click to toggle source

(see Proxy#load)

# File lib/moneta/adapters/tokyotyrant.rb, line 47
def load(key, options = {})
  value = backend[key]
  # raise if there is an error and the error is not "no record"
  error if value == nil && backend.ecode != ENOREC
  value && unpack(value)
end
slice(*keys, **options) click to toggle source

(see Proxy#slice)

# File lib/moneta/adapters/tokyotyrant.rb, line 95
def slice(*keys, **options)
  hash =
    if @native
      backend.mget(*keys)
    else
      hash = Hash[keys.map { |key| [key] }]
      raise unless backend.mget(hash) >= 0
      hash
    end

  hash.each do |key, value|
    hash[key] = unpack(value)
  end
end
store(key, value, options = {}) click to toggle source

(see Proxy#store)

# File lib/moneta/adapters/tokyotyrant.rb, line 55
def store(key, value, options = {})
  backend.put(key, pack(value)) or error
  value
end
values_at(*keys, **options) click to toggle source

(see Proxy#values_at)

# File lib/moneta/adapters/tokyotyrant.rb, line 111
def values_at(*keys, **options)
  hash = slice(*keys, **options)
  keys.map { |key| hash[key] }
end

Private Instance Methods

error() click to toggle source
# File lib/moneta/adapters/tokyotyrant.rb, line 143
def error
  raise "#{backend.class.name} error: #{backend.errmsg}"
end
pack(value) click to toggle source
# File lib/moneta/adapters/tokyotyrant.rb, line 118
def pack(value)
  intvalue = value.to_i
  if intvalue >= 0 && intvalue <= 0xFFFFFFFF && intvalue.to_s == value
    # Pack as 4 byte integer
    [intvalue].pack(@pack)
  elsif value.bytesize >= 4
    # Add nul character to make value distinguishable from integer
    value + "\0"
  else
    value
  end
end
unpack(value) click to toggle source
# File lib/moneta/adapters/tokyotyrant.rb, line 131
def unpack(value)
  if value.bytesize == 4
    # Unpack 4 byte integer
    value.unpack1(@pack).to_s
  elsif value.bytesize >= 5 && value[-1] == ?\0
    # Remove nul character
    value[0..-2]
  else
    value
  end
end