Sha256: 3569141e9f8028e5519f53128515e5ac1894c68817f5b9aa2f56cb5c08c00f68

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

require 'tokyocabinet'

module Enygma
  module Adapters
    
    class TokyoCabinetAdapter < Enygma::Adapters::AbstractAdapter
      
      def connect!(db)
        @database = case db
        when TokyoCabinet::HDB
          db
        when TokyoCabinet::BDB
          db
        when TokyoCabinet::FDB
          db
        when TokyoCabinet::TDB
          db
        when String
          unless File.exist?(db)
            raise InvalidDatabase, "The Tokyo Cabinet database couldn't be found."
          end
          case db
          when /\.tch$/
            tkcab = TokyoCabinet::HDB.new
            tkcab.open(db, TokyoCabinet::HDB::OWRITER | TokyoCabinet::HDB::OCREAT)
            tkcab
          when /\.tcb$/
            tkcab = TokyoCabinet::BDB.new
            tkcab.open(db, TokyoCabinet::BDB::OWRITER | TokyoCabinet::BDB::OCREAT)
            tkcab
          when /\.tcf$/
            tkcab = TokyoCabinet::FDB.new
            tkcab.open(db, TokyoCabinet::FDB::OWRITER | TokyoCabinet::FDB::OCREAT)
            tkcab
          when /\.tct$/
            tkcab = TokyoCabinet::TDB.new
            tkcab.open(db, TokyoCabinet::TDB::OWRITER | TokyoCabinet::TDB::OCREAT)
            tkcab            
          else
            "The Tokyo Cabinet database type couldn't be inferred from the name given."
          end
        else
          raise InvalidDatabase, "The Tokyo Cabinet database couldn't be found."
        end
      end
      
      def query(args = {})
        prefix = args[:key_prefix] || ''
        args[:ids] ||= []
        args[:ids].collect do |i|
          value = @database.get("#{prefix}#{i}")
          begin
            Marshal.load(value)
          rescue TypeError
            value
          end
        end
      end
      
      def get_attribute(record, attribute)
        if record.respond_to?(attribute.to_sym)
          record.__send__(attribute.to_sym)
        elsif record.respond_to?(:[])
          record[attribute]
        else
          record
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sander6-enygma-0.1.0 lib/enygma/adapters/tokyo_cabinet.rb
sander6-enygma-0.1.1 lib/enygma/adapters/tokyo_cabinet.rb