Class: Anemone::Storage::KyotoCabinet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/anemone/storage/kyoto_cabinet.rb

Instance Method Summary (collapse)

Constructor Details

- (KyotoCabinet) initialize(file)



18
19
20
21
22
23
# 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

Instance Method Details

- (Object) [](key)



25
26
27
28
29
# File 'lib/anemone/storage/kyoto_cabinet.rb', line 25

def [](key)
  if value = @db[key]
    load_value(value)
  end
end

- (Object) []=(key, value)



31
32
33
# File 'lib/anemone/storage/kyoto_cabinet.rb', line 31

def []=(key, value)
  @db[key] = [Marshal.dump(value)].pack("m")
end

- (Object) delete(key)



53
54
55
56
57
# File 'lib/anemone/storage/kyoto_cabinet.rb', line 53

def delete(key)
  value = self[key]
  @db.delete(key)
  value
end

- (Object) each



35
36
37
38
39
# File 'lib/anemone/storage/kyoto_cabinet.rb', line 35

def each
  @db.each do |k, v|
    yield(k, load_value(v))
  end
end

- (Boolean) has_key?(key)



41
42
43
44
45
# 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

- (Object) keys



47
48
49
50
51
# File 'lib/anemone/storage/kyoto_cabinet.rb', line 47

def keys
  acc = []
  @db.each_key { |key| acc << key.first }
  acc
end

- (Object) merge!(hash)



59
60
61
62
# File 'lib/anemone/storage/kyoto_cabinet.rb', line 59

def merge!(hash)
  hash.each { |key, value| self[key] = value }
  self
end