Sha256: ea0c09586a6287ff91e9a86911359be8d4858f225ab15f0fcfb79a07e1df41f9

Contents?: true

Size: 1.12 KB

Versions: 12

Compression:

Stored size: 1.12 KB

Contents

begin
  require 'tokyocabinet'
rescue LoadError
  puts "You need the tokyocabinet gem to use Anemone::Storage::TokyoCabinet"
  exit
end

require 'forwardable'

module Anemone
  module Storage
    class TokyoCabinet
      extend Forwardable

      def_delegators :@db, :close, :size, :keys, :has_key?

      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

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

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

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

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

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

      private

      def load_value(value)
        Marshal.load(value.unpack("m")[0])
      end

    end
  end
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
arachni-0.2.4 lib/anemone/storage/tokyo_cabinet.rb
arachni-0.2.3 lib/anemone/storage/tokyo_cabinet.rb
arachni-0.2.2.2 lib/anemone/storage/tokyo_cabinet.rb
arachni-0.2.2.1 lib/anemone/storage/tokyo_cabinet.rb
anemone-0.5.0 lib/anemone/storage/tokyo_cabinet.rb
spk-anemone-0.4.0 lib/anemone/storage/tokyo_cabinet.rb
anemone-0.4.0 lib/anemone/storage/tokyo_cabinet.rb
anemone-0.3.2 lib/anemone/storage/tokyo_cabinet.rb
spk-anemone-0.3.1 lib/anemone/storage/tokyo_cabinet.rb
anemone-0.3.1 lib/anemone/storage/tokyo_cabinet.rb
spk-anemone-0.3.0 lib/anemone/storage/tokyo_cabinet.rb
anemone-0.3.0 lib/anemone/storage/tokyo_cabinet.rb