Sha256: ef43fd3dd39a7b74d0730600a677df87005261c0348fde850b737e014f9c88e1

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

class Cassandra::Mapper
  BATCH_SIZE = 500

  def insert(hash)
    data = Data::Insert.new config, hash
    keyspace.insert table, data.packed_keys, data.columns
    data.return!
  end

  def remove(hash)
    data = Data::Remove.new config, hash
    keyspace.remove table, data.packed_keys, data.columns
    data.return!
  end

  def get(query)
    request  = Data::Request.new config, query
    columns  = columns_for request
    response = Data::Response.new config, request.keys, columns
    response.unpack
  end

  def one(keys)
    get(keys).first
  end

  def each(&block)
    keyspace.each_key table do |packed_keys|
      keys = unpack_keys packed_keys
      get(keys).each &block
    end
  end

  def all
    to_enum.to_a
  end

  private

  def columns_for(request, offset=nil)
    columns = keyspace.get table, request.packed_keys, request.query(offset)
    columns ||= {}
    if columns.size == BATCH_SIZE
      columns.merge! columns_for(request, columns.keys.last)
    end
    columns
  end

  def unpack_keys(packed_keys)
    keys = packed_keys.split Data::Request::KEY_SEPARATOR
    keys = Hash[config.key.zip(keys)]
    keys.each do |field, value|
      keys[field] = Convert.from config.types[field], value
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cassandra-mapper-0.2 lib/cassandra/mapper/extend/queries.rb
cassandra-mapper-0.1 lib/cassandra/mapper/extend/queries.rb