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.



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

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



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

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

#[]=(key, value) ⇒ Object



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

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

#delete(key) ⇒ Object



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

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

#eachObject



40
41
42
# File 'lib/anemone/storage/tokyo_cabinet.rb', line 40

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

#merge!(hash) ⇒ Object



44
45
46
47
# File 'lib/anemone/storage/tokyo_cabinet.rb', line 44

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