Class: CassandraObject::Adapters::CassandraDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_object/adapters/cassandra_driver.rb

Defined Under Namespace

Classes: Client, SchemaCache

Constant Summary collapse

CLUSTER_CONFIG_OPTIONS =
[
  :credentials, :auth_provider, :compression, :hosts, :logger, :port,
  :load_balancing_policy, :reconnection_policy, :retry_policy, :listeners,
  :consistency, :trace, :page_size, :compressor, :username, :password,
  :ssl, :server_cert, :client_cert, :private_key, :passphrase,
  :connect_timeout, :futures_factory, :datacenter, :address_resolution,
  :address_resolution_policy, :idle_timeout, :heartbeat_interval, :timeout,
  :synchronize_schema, :schema_refresh_delay, :schema_refresh_timeout,
  :shuffle_replicas, :client_timestamps
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CassandraDriver

Returns a new instance of CassandraDriver.



17
18
19
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 17

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 15

def config
  @config
end

Instance Method Details

#clientObject



25
26
27
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 25

def client
  @client ||= self.new_client
end

#closeObject



33
34
35
36
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 33

def close
  @client.try(:close)
  @client = nil
end

#clusterObject



21
22
23
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 21

def cluster
  @cluster ||= Cassandra.cluster cluster_config
end

#cluster_configObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 38

def cluster_config
  {
    :hosts => config[:servers].map { |server| server.sub /:\d+/, '' },
    :port => config[:port] || 9042,
    :connect_timeout => config[:thrift][:connect_timeout] || 10,
    :timeout => config[:thrift][:timeout] || 10,
    :logger => config[:logger] || (defined?(Rails) && Rails.logger) || Logger.new(STDOUT),
    :consistency => (config[:consistency] || {})[:write_default].try(:to_sym) || :one,
  }
end

#cluster_config_newObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 49

def cluster_config_new
  config.slice(*CLUSTER_CONFIG_OPTIONS).reverse_merge(
    :hosts => config[:servers].map { |server| server.sub /:\d+/, '' },
    :port => 9042,
    :connect_timeout => config[:thrift].try(:[], :connect_timeout) || 10,
    :timeout => config[:thrift].try(:[], :timeout) || 10,
    :logger => config[:logger] || (defined?(Rails) && Rails.logger) || Logger.new(STDOUT)
  ).merge(
    :consistency => (config[:consistency] || {})[:write_default].try(:to_sym) || :one
  )
end

#new_clientObject



29
30
31
# File 'lib/cassandra_object/adapters/cassandra_driver.rb', line 29

def new_client
  Client.new(cluster.connect(config[:keyspace]), cluster)
end