Class: Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_migrations/cql-rb-wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster, keyspace = nil) ⇒ Client

Returns a new instance of Client.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 56

def initialize(cluster, keyspace = nil)
  @cluster = cluster
  @sessions = {}
  if keyspace
    Rails.logger.try(:info, "Creating Cassandra session: #{keyspace.inspect}")
    @session = cluster.connect(keyspace)
    @sessions[keyspace] = @session
  else
    Rails.logger.try(:info, "Creating Cassandra session: [no keyspace]")
    @session = @cluster.connect()
    @sessions[:default] = @session
  end
end

Class Method Details

.connect(options) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 36

def self.connect(options)
  Rails.logger.try(:info, "Connecting to Cassandra cluster: #{options}")

  unless @cluster = Cassandra.cluster(options)
    raise CassandraMigrations::Errors::ClusterError.new(options)
  end

  self.new(@cluster)
end

Instance Method Details

#batch(type = :logged, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 79

def batch(type = :logged, options = {})
  batch = BatchStatement.new(self, @session.send(:"#{type}_batch"))
  if block_given?
    yield(batch)
    batch.execute(options)
  else
    batch
  end
end

#closeObject



89
90
91
92
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 89

def close
  Rails.logger.try(:info, "Closing Cassandra session: #{@session.inspect}")
  @session.close
end

#connected?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 98

def connected?
  @session.instance_variable_get('@state') == :connected
end

#execute(*args) ⇒ Object



70
71
72
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 70

def execute(*args)
  @session.execute(*args)
end

#keyspaceObject



94
95
96
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 94

def keyspace
  @session.keyspace
end

#prepare(statement, options = {}) ⇒ Object



74
75
76
77
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 74

def prepare(statement, options = {})
  s = @session.prepare(statement, options)
  PreparedStatement.new(self, s)
end

#use(keyspace) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/cassandra_migrations/cql-rb-wrapper.rb', line 46

def use(keyspace)
  if @sessions[keyspace]
    @session = @sessions[keyspace]
  else
    Rails.logger.try(:info, "Creating Cassandra session: #{keyspace.inspect}")
    @session = @cluster.connect(keyspace)
    @sessions[keyspace] = @session
  end
end