module Quandl::Cassandra::Base::Connection::ClassMethods

Public Instance Methods

connection() click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 71
def connection
  @@connection ||= establish_connection
end
consistency() click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 75
def consistency
  Quandl::Cassandra.configuration.consistency
end
delete(*ids) click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 11
def delete(*ids)
  statement = prepare("DELETE FROM #{table_name} WHERE id = ?")
  Array(ids).flatten.
    collect{|id| statement.async.execute(id, consistency) }.
    collect(&:value)
end
establish_connection() click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 85
def establish_connection
  c = Cql::Client.connect(
    hosts:        Quandl::Cassandra.configuration.hosts, 
    consistency:  Quandl::Cassandra.configuration.consistency )
  # switch keyspace
  c.use( Quandl::Cassandra.configuration.keyspace ) if Quandl::Cassandra.configuration.keyspace.present?
  @@connection = c
  c
end
execute(statement, consist = nil) click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 35
def execute(statement, consist = nil)
  consist = consistency unless consist.present?
  with_connection do |c|
    c.execute( statement, consist )
  end
rescue Cql::QueryError
  raise $!, "#{$!}\n  statement: #{statement}", $!.backtrace
end
execute_async(statement, consist = nil) click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 26
def execute_async(statement, consist = nil)
  consist = consistency unless consist.present?
  with_connection do |c|
    c.async.execute( statement, consist )
  end
rescue Cql::QueryError
  raise $!, "#{$!}\n  statement: #{statement}", $!.backtrace
end
prepare(statement) click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 18
def prepare(statement)
  with_connection do |c|
    c.prepare(statement)
  end
rescue Cql::QueryError
  raise $!, "#{$!}\n  statement: #{statement}", $!.backtrace
end
rescue_and_retry(times, &block) click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 50
def rescue_and_retry(times, &block)

  block.call(connection)
  
rescue Cql::QueryError => err
  #puts "ERROR: #{err.code} #{err}\n  cql: '#{err.cql}'"
  Quandl::Logger.error("#{err.code} '#{err.cql}' #{$!}")
  # raise error if it's failed three times
  raise( $!, "#{err.code} '#{err.cql}' #{$!}", $!.backtrace ) if times <= 1
  # otherwise rescue and retry
  rescue_and_retry(times - 1, &block)
  
rescue Cql::Io::ConnectionError, Cql::NotConnectedError => err
  #puts "ERROR: #{err}"
  Quandl::Logger.error(err)
  reset_connection
  raise if times <= 1
  rescue_and_retry(times - 1, &block)
  
end
reset_connection() click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 79
def reset_connection
  connection.close
  @@connection = establish_connection
  true
end
truncate() click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 7
def truncate
  execute("TRUNCATE #{table_name}")
end
with_connection(&block) click to toggle source
# File lib/quandl/cassandra/base/connection.rb, line 44
def with_connection(&block)
  rescue_and_retry(2) do
    block.call(connection)
  end
end