Class: Anemone::Storage::TokyoCabinet

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

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ TokyoCabinet

Returns a new instance of TokyoCabinet



18
19
20
21
22
23
# File 'lib/anemone/storage/tokyo_cabinet.rb', line 18

def initialize(file)
  raise "TokyoCabinet filename must have .tch extension" if File.extname(file) != '.tch'
  @db = ::TokyoCabinet::HDB::new
  @db.open(file, ::TokyoCabinet::HDB::OWRITER | ::TokyoCabinet::HDB::OCREAT)
  @db.clear
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#delete(key) ⇒ Object



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

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

#eachObject



41
42
43
44
45
# File 'lib/anemone/storage/tokyo_cabinet.rb', line 41

def each
  @db.keys.each do |k|
    yield(k, self[k])
  end
end

#merge!(hash) ⇒ Object



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

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