code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def uuid?(object) return true if uuid_in_string?(object) object.is_a?(Cassandra::Uuid) end
Determine if an object is a UUID @param object an object to check @return [Boolean] true if the object is recognized by Cequel as a UUID
uuid?
ruby
cequel/cequel
lib/cequel/uuids.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/uuids.rb
MIT
def initialize(keyspace, options = {}) options.assert_valid_keys(:auto_apply, :unlogged, :consistency) @keyspace = keyspace @auto_apply = options[:auto_apply] @unlogged = options.fetch(:unlogged, false) @consistency = options.fetch(:consistency, keyspace.default_consistency) reset end
@param keyspace [Keyspace] the keyspace that this batch will be executed on @param options [Hash] @option options [Integer] :auto_apply If specified, flush the batch after this many statements have been added. @option options [Boolean] :unlogged (false) Whether to use an [unlogged batch]( http://www.datastax.com/dev/blog/atomic-batches-in-cassandra-1-2). Logged batches guarantee atomicity (but not isolation) at the cost of a performance penalty; unlogged batches are useful for bulk write operations but behave the same as discrete writes. @see Keyspace#batch
initialize
ruby
cequel/cequel
lib/cequel/metal/batch.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/batch.rb
MIT
def execute(statement) @statements << statement if @auto_apply && @statements.size >= @auto_apply apply reset end end
Add a statement to the batch. @param (see Keyspace#execute)
execute
ruby
cequel/cequel
lib/cequel/metal/batch.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/batch.rb
MIT
def initialize(keyspace) @keyspace = keyspace end
@param keyspace [Keyspace] keyspace to make writes to @api private
initialize
ruby
cequel/cequel
lib/cequel/metal/batch_manager.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/batch_manager.rb
MIT
def batch(options = {}) new_batch = Batch.new(keyspace, options) if current_batch if current_batch.unlogged? && new_batch.logged? fail ArgumentError, "Already in an unlogged batch; can't start a logged batch." end return yield(current_batch) end begin self.current_batch = new_batch yield(new_batch).tap { new_batch.apply } ensure self.current_batch = nil end end
Execute write operations in a batch. Any inserts, updates, and deletes inside this method's block will be executed inside a CQL BATCH operation. @param options [Hash] @option (see Batch#initialize) @yield context within which all write operations will be batched @return return value of block @raise [ArgumentError] if attempting to start a logged batch while already in an unlogged batch, or vice versa. @example Perform inserts in a batch DB.batch do DB[:posts].insert(:id => 1, :title => 'One') DB[:posts].insert(:id => 2, :title => 'Two') end @note If this method is created while already in a batch of the same type (logged or unlogged), this method is a no-op.
batch
ruby
cequel/cequel
lib/cequel/metal/batch_manager.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/batch_manager.rb
MIT
def initialize(condition, bind_vars) @condition, @bind_vars = condition, bind_vars end
Create a new row specification @param [String] condition CQL string representing condition @param [Array] bind_vars Bind variables
initialize
ruby
cequel/cequel
lib/cequel/metal/cql_row_specification.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/cql_row_specification.rb
MIT
def cql [@condition, *@bind_vars] end
CQL and bind variables for this condition @return [Array] CQL string followed by zero or more bind variables
cql
ruby
cequel/cequel
lib/cequel/metal/cql_row_specification.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/cql_row_specification.rb
MIT
def initialize(table_name, keyspace) @table_name, @keyspace = table_name, keyspace @select_columns, @ttl_columns, @writetime_columns, @row_specifications, @sort_order = [], [], [], [], {} end
@param table_name [Symbol] column family for this data set @param keyspace [Keyspace] keyspace this data set's table lives in @see Keyspace#[] @api private
initialize
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def list_prepend(column, elements, options = {}) updater { list_prepend(column, elements) }.execute(options) end
Prepend element(s) to a list in the row(s) matched by this data set. @param column [Symbol] name of list column to prepend to @param elements [Object,Array] one element or an array of elements to prepend @param options [Options] options for persisting the column data @option (see Writer#initialize) @return [void] @example posts.list_prepend(:categories, ['CQL', 'ORMs']) @note A bug (CASSANDRA-8733) exists in Cassandra versions 0.3.0-2.0.12 and 2.1.0-2.1.2 which will make elements appear in REVERSE ORDER in the list. @note If a enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #list_append @see #update
list_prepend
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def list_append(column, elements, options = {}) updater { list_append(column, elements) }.execute(options) end
Append element(s) to a list in the row(s) matched by this data set. @param column [Symbol] name of list column to append to @param elements [Object,Array] one element or an array of elements to append @param options [Options] options for persisting the column data @option (see Writer#initialize) @return [void] @example posts.list_append(:categories, ['CQL', 'ORMs']) @note If a enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #list_append @see #update @since 1.0.0
list_append
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def list_replace(column, index, value, options = {}) updater { list_replace(column, index, value) }.execute(options) end
Replace a list element at a specified index with a new value @param column [Symbol] name of list column @param index [Integer] which element to replace @param value [Object] new value at this index @param options [Options] options for persisting the data @option (see Writer#initialize) @return [void] @example posts.list_replace(:categories, 2, 'Object-Relational Mapper') @note if a enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #update @since 1.0.0
list_replace
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def list_remove(column, value, options = {}) updater { list_remove(column, value) }.execute(options) end
Remove all occurrences of a given value from a list column @param column [Symbol] name of list column @param value [Object] value to remove @param options [Options] options for persisting the data @option (see Writer#initialize) @return [void] @example posts.list_remove(:categories, 'CQL3') @note If enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #list_remove_at @see #update @since 1.0.0
list_remove
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def list_remove_at(column, *positions) options = positions.extract_options! sorted_positions = positions.sort.reverse deleter { list_remove_at(column, *sorted_positions) }.execute(options) end
@overload list_remove_at(column, *positions, options = {}) Remove the value from a given position or positions in a list column @param column [Symbol] name of list column @param positions [Integer] position(s) in list to remove value from @param options [Options] options for persisting the data @option (see Writer#initialize) @return [void] @example posts.list_remove_at(:categories, 2) @note If enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #list_remove @see #update @since 1.0.0
list_remove_at
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def map_remove(column, *keys) options = keys.extract_options! deleter { map_remove(column, *keys) }.execute(options) end
@overload map_remove(column, *keys, options = {}) Remove a given key from a map column @param column [Symbol] name of map column @param keys [Object] map key to remove @param options [Options] options for persisting the data @option (see Writer#initialize) @return [void] @example posts.map_remove(:credits, 'editor') @note If enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #update @since 1.0.0
map_remove
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def set_add(column, values, options = {}) updater { set_add(column, values) }.execute(options) end
Add one or more elements to a set column @param column [Symbol] name of set column @param values [Object,Set] value or values to add @param options [Options] options for persisting the data @option (see Writer#initialize) @return [void] @example posts.set_add(:tags, 'cql3') @note If enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #update @since 1.0.0
set_add
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def set_remove(column, value, options = {}) updater { set_remove(column, value) }.execute(options) end
Remove an element from a set @param column [Symbol] name of set column @param value [Object] value to remove @param options [Options] options for persisting the data @option (see Writer#initialize) @return [void] @example posts.set_remove(:tags, 'cql3') @note If enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #update @since 1.0.0
set_remove
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def map_update(column, updates, options = {}) updater { map_update(column, updates) }.execute(options) end
Update one or more keys in a map column @param column [Symbol] name of set column @param updates [Hash] map of map keys to new values @param options [Options] options for persisting the data @option (see Writer#initialize) @return [void] @example posts.map_update(:credits, 'editor' => 34) @note If enclosed in a Keyspace#batch block, this method will be executed as part of the batch. @see #update @since 1.0.0
map_update
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def select(*columns) clone.tap do |data_set| data_set.select_columns.concat(columns.flatten) end end
Select specified columns from this data set. @param columns [Symbol] columns columns to select @return [DataSet] new data set scoped to specified columns
select
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def select_ttl(*columns) clone.tap do |data_set| data_set.ttl_columns.concat(columns.flatten) end end
Return the remaining TTL for the specified columns from this data set. @param columns [Symbol] columns to select @return [DataSet] new data set scoped to specified columns @since 1.0.0
select_ttl
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def select_writetime(*columns) clone.tap do |data_set| data_set.writetime_columns.concat(columns.flatten) end end
Return the write time for the specified columns in the data set @param columns [Symbol] columns to select @return [DataSet] new data set scoped to specified columns @since 1.0.0
select_writetime
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def select!(*columns) clone.tap do |data_set| data_set.select_columns.replace(columns.flatten) end end
Select specified columns from this data set, overriding chained scope. @param columns [Symbol,Array] columns to select @return [DataSet] new data set scoped to specified columns
select!
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def where(row_specification, *bind_vars) clone.tap do |data_set| data_set.row_specifications .concat(build_row_specifications(row_specification, bind_vars)) end end
Filter this data set with a row specification @overload where(column_values) @param column_values [Hash] Map of column name to values to match @example database[:posts].where(title: 'Hey') @overload where(cql, *bind_vars) @param cql [String] CQL fragment representing `WHERE` statement @param bind_vars [Object] Bind variables for the CQL fragment @example DB[:posts].where('title = ?', 'Hey') @return [DataSet] New data set scoped to the row specification
where
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def where!(row_specification, *bind_vars) clone.tap do |data_set| data_set.row_specifications .replace(build_row_specifications(row_specification, bind_vars)) end end
Replace existing row specifications @see #where @return [DataSet] New data set with only row specifications given
where!
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def order(pairs) clone.tap do |data_set| data_set.sort_order.merge!(pairs.symbolize_keys) end end
Control how the result rows are sorted @param pairs [Hash] Map of column name to sort direction @return [DataSet] new data set with the specified ordering @note The only valid ordering column is the first clustering column @since 1.0.0
order
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def consistency(consistency) clone.tap do |data_set| data_set.query_consistency = consistency end end
rubocop:disable LineLength Change the consistency for queries performed by this data set @param consistency [Symbol] a consistency level @return [DataSet] new data set tuned to the given consistency @see http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_config_consistency_c.html @since 1.1.0
consistency
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def each return enum_for(:each) unless block_given? results.each { |row| yield Row.from_result_row(row) } end
rubocop:enable LineLength Enumerate over rows in this data set. Along with #each, all other Enumerable methods are implemented. @overload each @return [Enumerator] enumerator for rows, if no block given @overload each(&block) @yield [Hash] result rows @return [void] @return [Enumerator,void]
each
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def first row = execute_cql(*limit(1).cql).first Row.from_result_row(row) end
@return [Hash] the first row in this data set
first
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def count raise Cequel::Record::DangerousQueryError.new end
@raise [DangerousQueryError] to prevent loading the entire record set to be counted
count
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def cql statement = Statement.new .append(select_cql) .append(" FROM #{table_name}") .append(*row_specifications_cql) .append(sort_order_cql) .append(limit_cql) .append(allow_filtering_cql) end
@return [Statement] CQL `SELECT` statement encoding this data set's scope.
cql
ruby
cequel/cequel
lib/cequel/metal/data_set.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/data_set.rb
MIT
def delete_row @delete_row = true end
Delete the entire row or rows matched by the data set @return [void]
delete_row
ruby
cequel/cequel
lib/cequel/metal/deleter.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/deleter.rb
MIT
def list_remove_at(column, *positions) statements .concat(positions.map { |position| "#{column}[#{position}]" }) end
Remove elements from a list by position @param column [Symbol] name of list column @param positions [Integer] positions in list from which to delete elements @return [void]
list_remove_at
ruby
cequel/cequel
lib/cequel/metal/deleter.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/deleter.rb
MIT
def map_remove(column, *keys) statements.concat(keys.length.times.map { "#{column}[?]" }) bind_vars.concat(keys) end
Remote elements from a map by key @param column [Symbol] name of map column @param keys [Object] keys to delete from map @return [void]
map_remove
ruby
cequel/cequel
lib/cequel/metal/deleter.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/deleter.rb
MIT
def increment(data) data.each_pair do |column_name, delta| operator = delta < 0 ? '-' : '+' statements << "#{column_name} = #{column_name} #{operator} ?" bind_vars << delta.abs end end
Increment one or more columns by given deltas @param data [Hash<Symbol,Integer>] map of column names to deltas @return [void]
increment
ruby
cequel/cequel
lib/cequel/metal/incrementer.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/incrementer.rb
MIT
def decrement(data) increment(Hash[data.map { |column, count| [column, -count] }]) end
Decrement one or more columns by given deltas @param data [Hash<Symbol,Integer>] map of column names to deltas @return [void]
decrement
ruby
cequel/cequel
lib/cequel/metal/incrementer.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/incrementer.rb
MIT
def initialize(configuration={}) @lock = Monitor.new configure(configuration) end
@api private @param configuration [Options] @option (see #configure) @see Cequel.connect
initialize
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def configure(configuration = {}) if configuration.key?(:thrift) warn "Cequel no longer uses the Thrift transport to communicate " \ "with Cassandra. The :thrift option is deprecated and ignored." end @configuration = configuration @error_policy = extract_cassandra_error_policy(configuration) @cassandra_options = extract_cassandra_options(configuration) @hosts, @port = extract_hosts_and_port(configuration) @credentials = extract_credentials(configuration) @ssl_config = extract_ssl_config(configuration) @name = configuration[:keyspace] @default_consistency = configuration[:default_consistency].try(:to_sym) @client_compression = configuration[:client_compression].try(:to_sym) # reset the connections clear_active_connections! end
Configure this keyspace from a hash of options @param configuration [Options] configuration options @option configuration [String] :host ('127.0.0.1') hostname of single Cassandra instance to connect to @option configuration [Integer] :port (9042) port on which to connect to all specified hosts @option configuration [Integer] :max_retries maximum number of retries on connection failure @option configuration [Array<String>] :hosts list of Cassandra instances to connect to (hostnames only) @option configuration [String] :username user to auth with (leave blank for no auth) @option configuration [String] :password password to auth with (leave blank for no auth) @option configuration [String] :keyspace name of keyspace to connect to @option configuration [Boolean] :ssl enable/disable ssl/tls support @option configuration [String] :server_cert path to ssl server certificate @option configuration [String] :client_cert path to ssl client certificate @option configuration [String] :private_key path to ssl client private key @option configuration [String] :passphrase the passphrase for client private key @option configuration [String] :cassandra_error_policy A mixin for handling errors from Cassandra @option configuration [Hash] :cassandra_options A hash of arbitrary options to pass to Cassandra @return [void]
configure
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def client synchronize do @client ||= cluster.connect(name) end end
@return [Cassandra::Session] the low-level client session provided by the adapter @api private
client
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def execute(statement, *bind_vars) execute_with_options(Statement.new(statement, bind_vars), { consistency: default_consistency }) end
Execute a CQL query in this keyspace If a connection error occurs, will retry a maximum number of time (default 3) before re-raising the original connection error. @param statement [String] CQL string @param bind_vars [Object] values for bind variables @return [Enumerable] the results of the query @see #execute_with_options
execute
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def execute_with_options(statement, options={}) options[:consistency] ||= default_consistency cql, options = *case statement when Statement [prepare_statement(statement), {arguments: statement.bind_vars}.merge(options)] when Cassandra::Statements::Batch [statement, options] end log('CQL', statement) do error_policy.execute_stmt(self) do client.execute(cql, options) end end end
Execute a CQL query in this keyspace with the given options @param statement [String,Statement,Batch] statement to execute @param options [Options] options for statement execution @return [Enumerable] the results of the query @since 1.1.0
execute_with_options
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def prepare_statement(statement) cql = case statement when Statement statement.cql else statement end error_policy.execute_stmt(self) do client.prepare(cql) end end
Wraps the prepare statement in the default retry strategy @param statement [String,Statement] statement to prepare @return [Cassandra::Statement::Prepared] the prepared statement
prepare_statement
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def clear_active_connections! synchronize do if defined? @client remove_instance_variable(:@client) end if defined? @client_without_keyspace remove_instance_variable(:@client_without_keyspace) end if defined? @cluster @cluster.close remove_instance_variable(:@cluster) end end end
Clears all active connections @return [void]
clear_active_connections!
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def default_consistency @default_consistency || :quorum end
@return [Symbol] the default consistency for queries in this keyspace @since 1.1.0
default_consistency
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def bug8733_version? version_file = File.expand_path('../../../../.cassandra-versions', __FILE__) @all_versions ||= File.read(version_file).split("\n").map(&:strip) # bug exists in versions 0.3.0-2.0.12 and 2.1.0-2.1.2 @bug8733_versions ||= @all_versions[0..@all_versions.index('2.0.12')] + @all_versions[@all_versions.index('2.1.0')..@all_versions.index('2.1.2')] @bug8733_versions.include?(cassandra_version) end
return true if Cassandra server version is known to include bug CASSANDRA-8733
bug8733_version?
ruby
cequel/cequel
lib/cequel/metal/keyspace.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/keyspace.rb
MIT
def log(label, statement) return yield if logger.nil? response = nil begin time = Benchmark.ms { response = yield } generate_message = lambda do format_for_log(label, "#{time.round.to_i}ms", statement) end if time >= slowlog_threshold logger.warn(&generate_message) else logger.debug(&generate_message) end rescue Exception => e logger.error { format_for_log(label, 'ERROR', statement) } raise end response end
Log a CQL statement @param label [String] a logical label for this statement @param statement [String,Statement,Batch] the CQL statement to log @return [void]
log
ruby
cequel/cequel
lib/cequel/metal/request_logger.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/request_logger.rb
MIT
def initialize(column, value) @column, @value = column, value end
@param column [Symbol] column name @param value [Object,Array] value or values to match
initialize
ruby
cequel/cequel
lib/cequel/metal/row_specification.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/row_specification.rb
MIT
def cql value = if Enumerable === @value && @value.count == 1 @value.first else @value end if Array === value ["#{@column} IN ?", value] else ["#{@column} = ?", value] end end
@return [String] row specification as CQL fragment
cql
ruby
cequel/cequel
lib/cequel/metal/row_specification.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/row_specification.rb
MIT
def initialize(cql_or_prepared='', bind_vars=[]) cql, prepared = *case cql_or_prepared when Cassandra::Statements::Prepared [cql_or_prepared.cql, cql_or_prepared] else [cql_or_prepared.to_s, nil] end @cql, @prepared, @bind_vars = cql, prepared, bind_vars end
@return [Array] cassandra type hints for bind variables
initialize
ruby
cequel/cequel
lib/cequel/metal/statement.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/statement.rb
MIT
def prepare(keyspace) @prepared ||= keyspace.client.prepare(cql) end
@return [Cassandra::Statements::Prepared] prepared version of this statement
prepare
ruby
cequel/cequel
lib/cequel/metal/statement.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/statement.rb
MIT
def prepend(cql, *bind_vars) @cql.prepend(cql) @bind_vars.unshift(*bind_vars) end
Add a CQL fragment with optional bind variables to the beginning of the statement @param (see #append) @return [void]
prepend
ruby
cequel/cequel
lib/cequel/metal/statement.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/statement.rb
MIT
def append(cql, *bind_vars) unless cql.nil? @cql << cql @bind_vars.concat(bind_vars) end self end
Add a CQL fragment with optional bind variables to the end of the statement @param cql [String] CQL fragment @param bind_vars [Object] zero or more bind variables @return [void]
append
ruby
cequel/cequel
lib/cequel/metal/statement.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/statement.rb
MIT
def args [cql, *bind_vars] end
@return [Array] this statement as an array of arguments to Keyspace#execute (CQL string followed by bind variables)
args
ruby
cequel/cequel
lib/cequel/metal/statement.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/statement.rb
MIT
def set(data) data.each_pair do |column, value| column_updates[column.to_sym] = value end end
Directly set column values @param data [Hash] map of column names to values @return [void] @see DataSet#update
set
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def list_prepend(column, elements) elements = Array(elements) statements << "#{column} = ? + #{column}" bind_vars << elements end
Prepend elements to a list column @param column [Symbol] column name @param elements [Array<Object>] elements to prepend @return [void] @see DataSet#list_prepend
list_prepend
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def list_append(column, elements) elements = Array(elements) statements << "#{column} = #{column} + ?" bind_vars << elements end
Append elements to a list column @param column [Symbol] column name @param elements [Array] elements to append @return [void] @see DataSet#list_append
list_append
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def list_remove(column, value) value = Array(value) statements << "#{column} = #{column} - ?" bind_vars << value end
Remove all occurrences of an element from a list @param column [Symbol] column name @param value value to remove @return [void] @see DataSet#list_remove
list_remove
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def list_replace(column, index, value) statements << "#{column}[#{index}] = ?" bind_vars << value end
Replace a list item at a given position @param column [Symbol] column name @param index [Integer] index at which to replace value @param value new value for position @return [void] @see DataSet#list_replace
list_replace
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def set_add(column, values) statements << "#{column} = #{column} + ?" bind_vars << Set.new(::Kernel.Array(values)) end
Add elements to a set @param column [Symbol] column name @param values [Set] elements to add to set @return [void] @see DataSet#set_add
set_add
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def set_remove(column, values) statements << "#{column} = #{column} - ?" bind_vars << Set.new(::Kernel.Array(values)) end
Remove elements from a set @param column [Symbol] column name @param values [Set] elements to remove from set @return [void] @see DataSet#set_remove
set_remove
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def map_update(column, updates) statements << "#{column} = #{column} + ?" bind_vars << updates end
Add or update elements in a map @param column [Symbol] column name @param updates [Hash] map of keys to values to update in map @return [void] @see DataSet#map_update
map_update
ruby
cequel/cequel
lib/cequel/metal/updater.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/updater.rb
MIT
def initialize(data_set, &block) @data_set, @options, @block = data_set, options, block @statements, @bind_vars = [], [] SimpleDelegator.new(self).instance_eval(&block) if block end
@param data_set [DataSet] data set to write to
initialize
ruby
cequel/cequel
lib/cequel/metal/writer.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/writer.rb
MIT
def execute(options = {}) options.assert_valid_keys(:timestamp, :ttl, :consistency) return if empty? statement = Statement.new consistency = options.fetch(:consistency, data_set.query_consistency) write_to_statement(statement, options) statement.append(*data_set.row_specifications_cql) data_set.write_with_options(statement, consistency: consistency) end
Execute the statement as a write operation @param options [Options] options @option options [Symbol] :consistency what consistency level to use for the operation @option options [Integer] :ttl time-to-live in seconds for the written data @option options [Time,Integer] :timestamp the timestamp associated with the column values @return [void]
execute
ruby
cequel/cequel
lib/cequel/metal/writer.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/writer.rb
MIT
def generate_upsert_options(options) upsert_options = options.slice(:timestamp, :ttl) if upsert_options.empty? '' else ' USING ' << upsert_options.map do |key, value| serialized_value = case key when :timestamp then (value.to_f * 1_000_000).to_i else value end "#{key.to_s.upcase} #{serialized_value}" end.join(' AND ') end end
Generate CQL option statement for inserts and updates
generate_upsert_options
ruby
cequel/cequel
lib/cequel/metal/writer.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/metal/writer.rb
MIT
def belongs_to(name, options = {}) if parent_association fail InvalidRecordConfiguration, "Can't declare more than one belongs_to association" end if table_schema.key_columns.any? fail InvalidRecordConfiguration, "belongs_to association must be declared before declaring " \ "key(s)" end key_options = options.extract!(:partition) self.parent_association = BelongsToAssociation.new(self, name.to_sym, options) parent_association.association_key_columns.each_with_index do |column, i| foreign_key_parts = self.parent_association.foreign_keys foreign_key = foreign_key_parts.any? ? foreign_key_parts[i] : "#{name}_#{column.name}" key foreign_key.to_sym, column.type, key_options end def_parent_association_accessors end
@!attribute parent_association @return [BelongsToAssociation] association declared by {#belongs_to} @!attribute child_associations @return [Hash<Symbol,HasManyAssociation>] associations declared by {#has_many} Declare the parent association for this record. The name of the class is inferred from the name of the association. The `belongs_to` declaration also serves to define key columns, which are derived from the key columns of the parent class. So, if the parent class `Blog` has a primary key `(subdomain)`, this will declare a key column `blog_subdomain` of the same type. If the parent class has multiple keys, e.g. it belongs to a parent class, defining a `partition: true` option will declare all of the parent's keys as partition key columns for this class. Parent associations are read/write, so declaring `belongs_to :blog` will define a `blog` getter and `blog=` setter, which will update the underlying key column. Note that a record's parent cannot be changed once the record has been saved. @param name [Symbol] name of the parent association @param options [Options] options for association @option (see BelongsToAssociation#initialize) @return [void] @see Associations
belongs_to
ruby
cequel/cequel
lib/cequel/record/associations.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/associations.rb
MIT
def has_many(name, options = {}) association = HasManyAssociation.new(self, name.to_sym, options) self.child_associations = child_associations.merge(name => association) def_child_association_reader(association) end
Declare a child association. The child association should have a `belongs_to` referencing this class or, at a minimum, must have a primary key whose first N columns have the same types as the N columns in this class's primary key. `has_many` associations are read-only, so `has_many :posts` will define a `posts` reader but not a `posts=` writer; and the collection returned by `posts` will be immutable. @param name [Symbol] plural name of association @param options [Options] options for association @option (see HasManyAssociation#initialize) @return [void] @see Associations
has_many
ruby
cequel/cequel
lib/cequel/record/associations.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/associations.rb
MIT
def count raise Cequel::Record::DangerousQueryError.new end
@raise [DangerousQueryError] to prevent loading the entire record set to be counted
count
ruby
cequel/cequel
lib/cequel/record/association_collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/association_collection.rb
MIT
def initialize(owner_class, name, options = {}) options.assert_valid_keys(:class_name, :foreign_key) @foreign_keys = Array(options.fetch(:foreign_key, [])).map { |x| x.to_sym } @owner_class, @name = owner_class, name.to_sym @association_class_name = options.fetch(:class_name, @name.to_s.classify) end
@param owner_class [Class] child class that declared `belongs_to` @param name [Symbol] name of the association @param options [Options] options for association @option options [String] :class_name name of parent class @api private
initialize
ruby
cequel/cequel
lib/cequel/record/belongs_to_association.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/belongs_to_association.rb
MIT
def association_class @association_class ||= association_class_name.constantize end
@return [Class] parent class declared by `belongs_to`
association_class
ruby
cequel/cequel
lib/cequel/record/belongs_to_association.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/belongs_to_association.rb
MIT
def instance_variable_name @instance_variable_name ||= :"@#{name}" end
@return [Symbol] instance variable name to use for storing the parent instance in a record @api private
instance_variable_name
ruby
cequel/cequel
lib/cequel/record/belongs_to_association.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/belongs_to_association.rb
MIT
def initialize(column, gt, inclusive, value) @column, @gt, @inclusive, @value = column, gt, inclusive, value end
@param column [Schema::Column] column bound applies to @param gt [Boolean] `true` if this is a lower bound @param inclusive [Boolean] `true` if this is an inclusive bound @param value value for bound
initialize
ruby
cequel/cequel
lib/cequel/record/bound.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/bound.rb
MIT
def to_cql_with_bind_variables [to_cql, bind_value] end
@return [Array] pair containing CQL string and bind value
to_cql_with_bind_variables
ruby
cequel/cequel
lib/cequel/record/bound.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/bound.rb
MIT
def update_all(attributes) each_data_set { |data_set| data_set.update(attributes) } end
Update all matched records with the given column values, without executing callbacks. @param attributes [Hash] map of column names to values @return [void]
update_all
ruby
cequel/cequel
lib/cequel/record/bulk_writes.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/bulk_writes.rb
MIT
def delete_all each_data_set { |data_set| data_set.delete } end
Delete all matched records without executing callbacks @return [void]
delete_all
ruby
cequel/cequel
lib/cequel/record/bulk_writes.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/bulk_writes.rb
MIT
def destroy_all each { |record| record.destroy } end
Destroy all matched records, executing destroy callbacks for each record. @return [void]
destroy_all
ruby
cequel/cequel
lib/cequel/record/bulk_writes.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/bulk_writes.rb
MIT
def initialize(model, column) @model, @column = model, column end
@param model [Record] record that contains this collection @param column [Schema::Column] column this collection's data belongs to @return [Collection] a new collection
initialize
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def loaded! modifications.each { |modification| modification.call() }.clear end
Notify the collection that its underlying data is loaded in memory. @return [void] @api private
loaded!
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def clear to_update { deleter.delete_columns(column_name) } to_modify { super } end
Remove all elements from the list. This will propagate to the database as a DELETE of the list column. @return [List] self
clear
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def concat(array) array = cast_collection(array) to_update { updater.list_append(column_name, array) } to_modify { super } end
Concatenate another collection onto this list. @param array [Array] elements to concatenate @return [List] self
concat
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def delete(object) object = cast_element(object) to_update { updater.list_remove(column_name, object) } to_modify { super } end
Remove all instances of a given value from the list. @param object value to remove @return [List] self
delete
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def delete_at(index) to_update { deleter.list_remove_at(column_name, index) } to_modify { super } end
Remove the element at a given position from the list. @param index [Integer] position from which to remove the element @return [List] self
delete_at
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def push(*objects) objects.map! { |object| cast_element(object) } to_update { updater.list_append(column_name, objects) } to_modify { super } end
Push (append) one or more elements to the end of the list. @param objects value(s) to add to the end of the list @return [List] self
push
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def replace(array) array = cast_collection(array) to_update { updater.set(column_name => array) } to_modify { super } end
Replace the entire contents of this list with a new collection @param array [Array] new elements for this list @return [List] self
replace
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def unshift(*objects) objects.map!(&method(:cast_element)) prepared = @model.class.connection.bug8733_version? ? objects.reverse : objects to_update { updater.list_prepend(column_name, prepared) } to_modify { super } end
Prepend one or more values to the beginning of this list @param objects value(s) to add to the beginning of the list @return [List] self
unshift
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def add(object) object = cast_element(object) to_update { updater.set_add(column_name, object) } to_modify { super } end
Add an element to the set @param object element to add @return [Set] self
add
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def clear to_update { deleter.delete_columns(column_name) } to_modify { super } end
Remove everything from the set. Equivalent to deleting the collection column from the record's row. @return [Set] self
clear
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def delete(object) object = cast_element(object) to_update { updater.set_remove(column_name, object) } to_modify { super } end
Remove a single element from the set @param object element to remove @return [Set] self
delete
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def replace(set) set = cast_collection(set) to_update { updater.set(column_name => set) } to_modify { super } end
Replace the entire contents of this set with another set @param set [::Set] set containing new elements @return [Set] self
replace
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def clear to_update { deleter.delete_columns(column_name) } to_modify { super } end
Remove all elements from this map. Equivalent to deleting the column value from the row in CQL @return [Map] self
clear
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def delete(key) key = cast_key(key) to_update { deleter.map_remove(column_name, key) } to_modify { super } end
Delete one key from the map @param key the key to delete @return [Map] self
delete
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def merge!(hash) hash = cast_collection(hash) to_update { updater.map_update(column_name, hash) } to_modify { super } end
Update a collection of keys and values given by a hash @param hash [Hash] hash containing keys and values to set @return [Map] self
merge!
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def replace(hash) hash = cast_collection(hash) to_update { updater.set(column_name => hash) } to_modify { super } end
Replace the entire contents of this map with a new one @param hash [Hash] hash containing new keys and values @return [Map] self
replace
ruby
cequel/cequel
lib/cequel/record/collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/collection.rb
MIT
def initialize(record_set) @record_set = record_set @data_set = record_set.connection[record_set.target_class.table_name] end
@param record_set [RecordSet] record set for which to construct data set
initialize
ruby
cequel/cequel
lib/cequel/record/data_set_builder.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/data_set_builder.rb
MIT
def build add_limit add_select_columns add_where_statement add_bounds add_order set_consistency set_allow_filtering set_page_size set_paging_state data_set end
@return [Metal::DataSet] a DataSet exposing the rows for the record set
build
ruby
cequel/cequel
lib/cequel/record/data_set_builder.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/data_set_builder.rb
MIT
def initialize(owner_class, name, options = {}) options.assert_valid_keys(:class_name, :dependent) @owner_class, @name = owner_class, name @association_class_name = options.fetch(:class_name, name.to_s.classify) case options[:dependent] when :destroy, :delete, nil @dependent = options[:dependent] else fail ArgumentError, "Invalid :dependent option #{options[:dependent].inspect}. " \ "Valid values are :destroy, :delete" end end
@param owner_class [Class] Record class that declares this association @param name [Symbol] name of the association @param options [Options] options for the association @option options [Symbol] :class_name name of the child class @option options [Boolean] :dependent propagation behavior for destroy @api private
initialize
ruby
cequel/cequel
lib/cequel/record/has_many_association.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/has_many_association.rb
MIT
def association_class @association_class ||= association_class_name.constantize end
@return [Class] class of child association
association_class
ruby
cequel/cequel
lib/cequel/record/has_many_association.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/has_many_association.rb
MIT
def initialize(record_set) fail ArgumentError if record_set.nil? @record_set = record_set exploded_key_attributes = [{}].tap do |all_key_attributes| key_columns.zip(scoped_key_values) do |column, values| all_key_attributes.replace([values].flatten.compact.flat_map do |value| all_key_attributes.map do |key_attributes| key_attributes.merge(column.name => value) end end) end end unloaded_records = exploded_key_attributes.map do |key_attributes| record_set.target_class.new_empty(key_attributes, self) end super(unloaded_records) end
@param record_set [RecordSet] record set representing the records in this collection @api private
initialize
ruby
cequel/cequel
lib/cequel/record/lazy_record_collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/lazy_record_collection.rb
MIT
def load! records_by_identity = index_by { |record| record.key_values } record_set.find_each_row do |row| identity = row.values_at(*record_set.key_column_names) records_by_identity[identity].hydrate(row) end loaded_count = count { |record| record.loaded? } if loaded_count < count fail Cequel::Record::RecordNotFound, "Expected #{count} results; got #{loaded_count}" end self end
Hydrate all the records in this collection from a database query @return [LazyRecordCollection] self
load!
ruby
cequel/cequel
lib/cequel/record/lazy_record_collection.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/lazy_record_collection.rb
MIT
def create(attributes = {}, &block) new(attributes, &block).tap { |record| record.save } end
Initialize a new record instance, assign attributes, and immediately save it. @param attributes [Hash] attributes to assign to the new record @yieldparam record [Record] record to make modifications before saving @return [Record] self @example Create a new record with attribute assignment Post.create( blog_subdomain: 'cassandra', permalink: 'cequel', title: 'Cequel: The Next Generation' ) @example Create a new record with a block Post.create do |post| post.blog = blog post.permalink = 'cequel' post.title = 'Cequel: The Next Generation' end
create
ruby
cequel/cequel
lib/cequel/record/persistence.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/persistence.rb
MIT
def exists? load! true rescue RecordNotFound false end
Check if an unloaded record exists in the database @return `true` if the record has a corresponding row in the database @since 1.0.0
exists?
ruby
cequel/cequel
lib/cequel/record/persistence.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/persistence.rb
MIT
def load assert_keys_present! record_collection.load! unless loaded? self end
Load an unloaded record's row from the database and hydrate the record's attributes @return [Record] self @since 1.0.0
load
ruby
cequel/cequel
lib/cequel/record/persistence.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/persistence.rb
MIT
def load! load.tap do if transient? fail RecordNotFound, "Couldn't find #{self.class.name} with " \ "#{key_attributes.inspect}" end end end
Attempt to load an unloaded record and raise an error if the record does not correspond to a row in the database @return [Record] self @raise [RecordNotFound] if row does not exist in the database @see #load @since 1.0.0
load!
ruby
cequel/cequel
lib/cequel/record/persistence.rb
https://github.com/cequel/cequel/blob/master/lib/cequel/record/persistence.rb
MIT