Class: Cassandra::Session
- Inherits:
-
Object
- Object
- Cassandra::Session
- Extended by:
- Forwardable
- Defined in:
- lib/cassandra/session.rb
Overview
Sessions are used for query execution. Each session tracks its current keyspace. A session should be reused as much as possible, however it is ok to create several independent session for interacting with different keyspaces in the same application.
Instance Method Summary collapse
-
#close ⇒ self
Synchronously closes current session.
-
#close_async ⇒ Cassandra::Future<Cassandra::Session>
Asynchronously closes current session.
-
#counter_batch {|batch| ... } ⇒ Statements::Batch
Returns a counter Cassandra::Statements::Batch instance and optionally yields it to a given block.
-
#execute(statement, options = nil) ⇒ Cassandra::Result
A blocking wrapper around #execute_async.
-
#execute_async(statement, options = nil) ⇒ Cassandra::Future<Cassandra::Result>
Executes a given statement and returns a future result.
-
#inspect ⇒ String
A CLI-friendly session representation.
-
#keyspace ⇒ String
Returns current keyspace.
-
#logged_batch {|batch| ... } ⇒ Statements::Batch
(also: #batch)
Returns a logged Cassandra::Statements::Batch instance and optionally yields it to a given block.
-
#prepare(*args) ⇒ Cassandra::Statements::Prepared
A blocking wrapper around #prepare_async.
-
#prepare_async(statement, options = nil) ⇒ Cassandra::Future<Cassandra::Statements::Prepared>
Prepares a given statement and returns a future prepared statement.
-
#unlogged_batch {|batch| ... } ⇒ Statements::Batch
Returns a unlogged Cassandra::Statements::Batch instance and optionally yields it to a given block.
Instance Method Details
#close ⇒ self
Synchronously closes current session
221 222 223 |
# File 'lib/cassandra/session.rb', line 221 def close close_async.get end |
#close_async ⇒ Cassandra::Future<Cassandra::Session>
Asynchronously closes current session
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/cassandra/session.rb', line 202 def close_async promise = @futures.promise @client.close.on_complete do |f| if f.resolved? promise.fulfill(self) else f.on_failure {|e| promise.break(e)} end end promise.future end |
#counter_batch {|batch| ... } ⇒ Statements::Batch
Returns a counter Cassandra::Statements::Batch instance and optionally yields it to a given block
192 193 194 195 196 |
# File 'lib/cassandra/session.rb', line 192 def counter_batch statement = Statements::Batch::Counter.new yield(statement) if block_given? statement end |
#execute(statement, options = nil) ⇒ Cassandra::Result
A blocking wrapper around #execute_async
120 121 122 |
# File 'lib/cassandra/session.rb', line 120 def execute(statement, = nil) execute_async(statement, ).get end |
#execute_async(statement, options = nil) ⇒ Cassandra::Future<Cassandra::Result>
Positional arguments for simple statements are only supported starting with Apache Cassandra 2.0 and above.
Named arguments for simple statements are only supported starting with Apache Cassandra 2.1 and above.
Executes a given statement and returns a future result
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cassandra/session.rb', line 77 def execute_async(statement, = nil) if = .override() else = end case statement when ::String @client.query(Statements::Simple.new(statement, .arguments, .type_hints), ) when Statements::Simple @client.query(statement, ) when Statements::Prepared @client.execute(statement.bind(.arguments), ) when Statements::Bound @client.execute(statement, ) when Statements::Batch @client.batch(statement, ) else @futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}")) end rescue => e @futures.error(e) end |
#inspect ⇒ String
Returns a CLI-friendly session representation.
226 227 228 |
# File 'lib/cassandra/session.rb', line 226 def inspect "#<#{self.class.name}:0x#{self.object_id.to_s(16)}>" end |
#keyspace ⇒ String
Returns current keyspace
27 |
# File 'lib/cassandra/session.rb', line 27 def_delegators :@client, :keyspace |
#logged_batch {|batch| ... } ⇒ Statements::Batch Also known as: batch
Returns a logged Cassandra::Statements::Batch instance and optionally yields it to a given block
171 172 173 174 175 |
# File 'lib/cassandra/session.rb', line 171 def logged_batch(&block) statement = Statements::Batch::Logged.new yield(statement) if block_given? statement end |
#prepare(*args) ⇒ Cassandra::Statements::Prepared
A blocking wrapper around #prepare_async
163 164 165 |
# File 'lib/cassandra/session.rb', line 163 def prepare(*args) prepare_async(*args).get end |
#prepare_async(statement, options = nil) ⇒ Cassandra::Future<Cassandra::Statements::Prepared>
Prepares a given statement and returns a future prepared statement
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/cassandra/session.rb', line 137 def prepare_async(statement, = nil) if .is_a?(::Hash) = .override() else = end case statement when ::String @client.prepare(statement, ) when Statements::Simple @client.prepare(statement.cql, ) else @futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}")) end rescue => e @futures.error(e) end |
#unlogged_batch {|batch| ... } ⇒ Statements::Batch
Returns a unlogged Cassandra::Statements::Batch instance and optionally yields it to a given block
182 183 184 185 186 |
# File 'lib/cassandra/session.rb', line 182 def unlogged_batch statement = Statements::Batch::Unlogged.new yield(statement) if block_given? statement end |