class Anemone::Storage::KyotoCabinet

Public Class Methods

new(file) click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 18
def initialize(file)
  raise "KyotoCabinet filename must have .kch extension" if File.extname(file) != '.kch'
  @db = ::KyotoCabinet::DB::new
  @db.open(file, ::KyotoCabinet::DB::OWRITER | ::KyotoCabinet::DB::OCREATE)
  @db.clear
end

Public Instance Methods

[](key) click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 25
def [](key)
  if value = @db[key]
    load_value(value)
  end
end
[]=(key, value) click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 31
def []=(key, value)
  @db[key] = [Marshal.dump(value)].pack("m")
end
delete(key) click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 53
def delete(key)
  value = self[key]
  @db.delete(key)
  value
end
each() { |k, load_value(v)| ... } click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 35
def each
  @db.each do |k, v|
    yield(k, load_value(v))
  end
end
has_key?(key) click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 41
def has_key?(key)
  # Kyoto Cabinet doesn't have a way to query whether a key exists, so hack it
  keys = @db.match_prefix(key)
  !!keys && keys.include?(key)
end
keys() click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 47
def keys
  acc = []
  @db.each_key { |key| acc << key.first }
  acc
end
merge!(hash) click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 59
def merge!(hash)
  hash.each { |key, value| self[key] = value }
  self
end

Private Instance Methods

load_value(value) click to toggle source
# File lib/anemone/storage/kyoto_cabinet.rb, line 66
def load_value(value)
  Marshal.load(value.unpack("m")[0])
end