class Ccp::Kvs::Tokyo::Cabinet

Public Class Methods

new(source) click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 7
def initialize(source)
  @source = source
  @db     = HDB.new
end

Public Instance Methods

clear() click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 83
def clear
  tryW("clear")
  @db.clear or tokyo_error!("clear: ")
end
count() click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 55
def count
  tryR("count")
  return @db.rnum
end
del(k) click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 36
def del(k)
  tryW("del")
  v = @db[k.to_s]
  if v
    if @db.delete(k.to_s)
      return decode(v)
    else
      tokyo_error!("del(%s): " % k)
    end
  else
    return nil
  end
end
each(&block) click to toggle source

iterator

# File lib/ccp/kvs/tokyo/cabinet.rb, line 91
def each(&block)
  each_pair(&block)
end
each_key(&block) click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 101
def each_key(&block)
  tryR("each_key")
  @db.iterinit or tokyo_error!("each_key: ")
  while key = @db.iternext
    block.call(key)
  end
end
each_pair(&block) click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 95
def each_pair(&block)
  each_key do |key|
    block.call(key, get(key))
  end
end
exist?(k) click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 50
def exist?(k)
  tryR("exist?")
  return @db.has_key?(k.to_s)
end
first() click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 123
def first
  key = first_key
  if key
    return [key, get(key)]
  else
    return nil
  end
end
first_key() click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 117
def first_key
  tryR("first_key")
  @db.iterinit or tokyo_error!("first_key: ")
  return @db.iternext
end
get(k) click to toggle source

kvs

# File lib/ccp/kvs/tokyo/cabinet.rb, line 15
def get(k)
  tryR("get")
  v = @db[k.to_s]
  if v
    return decode(v)
  else
    if @db.ecode == HDB::ENOREC
      return nil
    else
      tokyo_error!("get(%s): " % k)
    end
  end
end
keys() click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 109
def keys
  array = []
  each_key do |key|
    array << key
  end
  return array
end
read() click to toggle source

bulk operations (not DRY but fast)

# File lib/ccp/kvs/tokyo/cabinet.rb, line 63
def read
  tryR("read")
  hash = {}
  @db.iterinit or tokyo_error!("read: ")
  while k = @db.iternext
    v = @db.get(k) or tokyo_error!("get(%s): " % k)
    hash[k] = decode(v)
  end
  return hash
end
set(k,v) click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 29
def set(k,v)
  tryW("set")
  val = encode(v)
  @db[k.to_s] = val or
    tokyo_error!("set(%s): " % k)
end
write(h) click to toggle source
# File lib/ccp/kvs/tokyo/cabinet.rb, line 74
def write(h)
  tryW("write")
  h.each_pair do |k,v|
    val = encode(v)
    @db[k.to_s] = val or tokyo_error!("write(%s): " % k)
  end
  return h
end