Class: Anemone::Storage::KyotoCabinet
- Inherits:
-
Object
- Object
- Anemone::Storage::KyotoCabinet
- Extended by:
- Forwardable
- Defined in:
- lib/anemone/storage/kyoto_cabinet.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(file) ⇒ KyotoCabinet
constructor
A new instance of KyotoCabinet.
- #keys ⇒ Object
- #merge!(hash) ⇒ Object
Constructor Details
#initialize(file) ⇒ KyotoCabinet
Returns a new instance of KyotoCabinet.
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
#[](key) ⇒ Object
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 |
#[]=(key, value) ⇒ Object
31 32 33 |
# File 'lib/anemone/storage/kyoto_cabinet.rb', line 31 def []=(key, value) @db[key] = [Marshal.dump(value)].pack("m") end |
#delete(key) ⇒ Object
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 |
#each ⇒ Object
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 |
#has_key?(key) ⇒ Boolean
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 |
#keys ⇒ Object
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 |
#merge!(hash) ⇒ Object
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 |