Class: Cassandra::Session

Inherits:
Object
  • Object
show all
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

Instance Method Details

#closeself

Synchronously closes current session

Returns:

  • (self)

    this session

See Also:



284
285
286
# File 'lib/cassandra/session.rb', line 284

def close
  close_async.get
end

#close_asyncCassandra::Future<Cassandra::Session>

Asynchronously closes current session

Returns:



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/cassandra/session.rb', line 265

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

Yield Parameters:

Returns:



255
256
257
258
259
# File 'lib/cassandra/session.rb', line 255

def counter_batch
  statement = Statements::Batch::Counter.new
  yield(statement) if block_given?
  statement
end

#execute(statement, *args, options = nil) ⇒ Cassandra::Result

A blocking wrapper around #execute_async

Deprecated.

This style will soon be removed, use the :arguments option to provide positional arguments instead.

Note:

Last argument will be treated as options if it is a Hash. Therefore, make sure to pass empty options when executing a statement with the last parameter required to be a map datatype.

Executes a statement using the deprecated splat-style way of passing positional arguments/

Parameters:

Parameters:

Returns:

Raises:

See Also:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cassandra/session.rb', line 169

def execute(statement, *args)
  options = nil
  options = args.pop if args.last.is_a?(::Hash)

  unless args.empty?
    ::Kernel.warn "[WARNING] Splat style (*args) positional arguments " \
                  "are deprecated, use the :arguments option instead, " \
                  "called from #{caller.first}"

    options ||= {}
    options[:arguments] = args
  end

  if options
    execute_async(statement, options).get
  else
    execute_async(statement).get
  end
end

#execute_async(statement, *args, options = nil) ⇒ Cassandra::Future<Cassandra::Result>

Note:

Positional arguments for simple statements are only supported on starting with Apache Cassandra 2.0 and above.

Executes a given statement and returns a future result

Deprecated.

This style will soon be removed, use the :arguments option to provide positional arguments instead.

Note:

Last argument will be treated as options if it is a Hash. Therefore, make sure to pass empty options when executing a statement with the last parameter required to be a map datatype.

Executes a statement using the deprecated splat-style way of passing positional arguments/

Parameters:

Parameters:

Options Hash (options):

  • :consistency (Symbol)

    consistency level for the request. Must be one of CONSISTENCIES

  • :page_size (Integer)

    size of results page. You can page through results using Result#next_page or Result#next_page_async

  • :trace (Boolean) — default: false

    whether to enable request tracing

  • :timeout (Numeric) — default: nil

    if specified, it is a number of seconds after which to time out the request if it hasn't completed

  • :serial_consistency (Symbol) — default: nil

    this option is only relevant for conditional updates and specifies a serial consistency to be used, one of Cassandra::SERIAL_CONSISTENCIES

  • :paging_state (String) — default: nil

    this option is used for stateless paging, where result paging is resumed some time after the initial request.

  • :arguments (Array) — default: nil

    positional arguments for the statement.

Returns:

See Also:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cassandra/session.rb', line 93

def execute_async(statement, *args)
  options = nil
  options = args.pop if args.last.is_a?(::Hash)

  unless args.empty?
    ::Kernel.warn "[WARNING] Splat style (*args) positional arguments " \
                  "are deprecated, use the :arguments option instead, " \
                  "called from #{caller.first}"

    options ||= {}
    options[:arguments] = args
  end

  if options
    options = @options.override(options)
  else
    options = @options
  end

  case statement
  when ::String
    @client.query(Statements::Simple.new(statement, options.arguments), options)
  when Statements::Simple
    @client.query(statement, options)
  when Statements::Prepared
    @client.execute(statement.bind(options.arguments), options)
  when Statements::Bound
    @client.execute(statement, options)
  when Statements::Batch
    @client.batch(statement, options)
  else
    @futures.error(::ArgumentError.new("unsupported statement #{statement.inspect}"))
  end
rescue => e
  @futures.error(e)
end

#inspectString

Returns a CLI-friendly session representation.

Returns:

  • (String)

    a CLI-friendly session representation



289
290
291
# File 'lib/cassandra/session.rb', line 289

def inspect
  "#<#{self.class.name}:0x#{self.object_id.to_s(16)}>"
end

#keyspaceString

Returns current keyspace

Returns:

  • (String)

    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

Yield Parameters:

Returns:



234
235
236
237
238
# File 'lib/cassandra/session.rb', line 234

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

Returns:

Raises:

See Also:



226
227
228
# File 'lib/cassandra/session.rb', line 226

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

Parameters:

  • statement (String, Cassandra::Statements::Simple)

    a statement to prepare

  • options (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (options):

  • :trace (Boolean) — default: false

    whether to enable request tracing

  • :timeout (Numeric) — default: nil

    if specified, it is a number of seconds after which to time out the request if it hasn't completed

Returns:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cassandra/session.rb', line 200

def prepare_async(statement, options = nil)
  if options.is_a?(::Hash)
    options = @options.override(options)
  else
    options = @options
  end

  case statement
  when ::String
    @client.prepare(statement, options)
  when Statements::Simple
    @client.prepare(statement.cql, options)
  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

Yield Parameters:

Returns:



245
246
247
248
249
# File 'lib/cassandra/session.rb', line 245

def unlogged_batch
  statement = Statements::Batch::Unlogged.new
  yield(statement) if block_given?
  statement
end