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

- (TokyoCabinet) initialize(file)



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

- (Object) [](key)



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

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



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

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

- (Object) delete(key)



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

- (Object) each



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

- (Object) merge!(hash)



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