Class: CassandraModel::Record

Inherits:
Object
  • Object
show all
Extended by:
QueryHelper, Forwardable
Includes:
DisplayableAttributes, MetaColumns
Defined in:
lib/cassandra_model/record.rb

Direct Known Subclasses

CounterRecord, TableDescriptor

Defined Under Namespace

Classes: Attributes, ConfigureableAttributes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueryHelper

after, all, before, def_query_helper, find_by

Methods included from DisplayableAttributes

#as_json, included

Methods included from MetaColumns

included

Constructor Details

#initialize(attributes = {}, options = {validate: true}) ⇒ Record

Returns a new instance of Record.



51
52
53
54
55
56
57
58
# File 'lib/cassandra_model/record.rb', line 51

def initialize(attributes = {}, options = {validate: true})
  ensure_attributes_accessible!
  validate_attributes!(attributes) if options[:validate]
  @execution_info = options[:execution_info]
  @valid = true
  @attributes = attributes.deep_dup
  after_initialize
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/cassandra_model/record.rb', line 10

def attributes
  @attributes
end

#execution_infoObject (readonly)

Returns the value of attribute execution_info.



10
11
12
# File 'lib/cassandra_model/record.rb', line 10

def execution_info
  @execution_info
end

#validObject (readonly)

Returns the value of attribute valid.



10
11
12
# File 'lib/cassandra_model/record.rb', line 10

def valid
  @valid
end

Class Method Details

.batch_typeObject



333
334
335
# File 'lib/cassandra_model/record.rb', line 333

def batch_type
  table_config.batch_type
end

.before_save(&block) ⇒ Object



428
429
430
# File 'lib/cassandra_model/record.rb', line 428

def before_save(&block)
  before_save_callbacks << block
end

.before_save_callbacksObject



432
433
434
# File 'lib/cassandra_model/record.rb', line 432

def before_save_callbacks
  table_config.before_save_callbacks ||= []
end

.columnsObject



337
338
339
340
341
# File 'lib/cassandra_model/record.rb', line 337

def columns
  table_data.columns ||= internal_columns.tap do |columns|
    columns.each { |column| define_attribute(column) }
  end
end

.connection_name=(value) ⇒ Object



314
315
316
# File 'lib/cassandra_model/record.rb', line 314

def connection_name=(value)
  table_config.connection_name = value
end

.create(attributes, options = {}) ⇒ Object Also known as: create!



366
367
368
# File 'lib/cassandra_model/record.rb', line 366

def create(attributes, options = {})
  create_async(attributes, options).get
end

.create_async(attributes, options = {}) ⇒ Object



362
363
364
# File 'lib/cassandra_model/record.rb', line 362

def create_async(attributes, options = {})
  self.new(attributes).save_async(options)
end

.first(clause = {}, options = {}) ⇒ Object



412
413
414
# File 'lib/cassandra_model/record.rb', line 412

def first(clause = {}, options = {})
  first_async(clause, options).get
end

.first_async(clause = {}, options = {}) ⇒ Object



404
405
406
# File 'lib/cassandra_model/record.rb', line 404

def first_async(clause = {}, options = {})
  request_async(clause, options.merge(limit: 1))
end

.order_by_clause(order_by) ⇒ Object



400
401
402
# File 'lib/cassandra_model/record.rb', line 400

def order_by_clause(order_by)
  " ORDER BY #{multi_csv_clause(order_by)}" if order_by
end

.query_for_deleteObject



351
352
353
354
# File 'lib/cassandra_model/record.rb', line 351

def query_for_delete
  where_clause = table.primary_key.map { |column| "#{column} = ?" }.join(' AND ')
  "DELETE FROM #{table_name} WHERE #{where_clause}"
end

.query_for_save(options = {}) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/cassandra_model/record.rb', line 343

def query_for_save(options = {})
  existence_clause = options[:check_exists] && ' IF NOT EXISTS'
  column_names = internal_columns.join(', ')
  column_sanitizers = (%w(?) * internal_columns.size).join(', ')
  save_query = "INSERT INTO #{table_name} (#{column_names}) VALUES (#{column_sanitizers})"
  "#{save_query}#{existence_clause}"
end

.query_for_update(new_attributes) ⇒ Object



356
357
358
359
360
# File 'lib/cassandra_model/record.rb', line 356

def query_for_update(new_attributes)
  where_clause = table.primary_key.map { |column| "#{column} = ?" }.join(' AND ')
  set_clause = new_attributes.keys.map { |column| "#{column} = ?" }.join(', ')
  "UPDATE #{table_name} SET #{set_clause} WHERE #{where_clause}"
end

.request(clause, options = {}) ⇒ Object



408
409
410
# File 'lib/cassandra_model/record.rb', line 408

def request(clause, options = {})
  request_async(clause, options).get
end

.request_async(clause, options = {}) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/cassandra_model/record.rb', line 372

def request_async(clause, options = {})
  page_size = options[:page_size]
  trace = options[:trace]
  request_query, invalidated_result, where_values = request_meta(clause, options)
  statement = statement(request_query)

  query_options = {}
  query_options[:page_size] = page_size if page_size
  query_options[:consistency] = read_consistency if read_consistency
  query_options[:trace] = trace if trace

  future = session.execute_async(statement, *where_values, query_options)
  if options[:limit] == 1
    single_result_row_future(future, invalidated_result)
  else
    paginator_result_future(future, invalidated_result)
  end
end

.request_meta(clause, options) ⇒ Object



391
392
393
394
395
396
397
398
# File 'lib/cassandra_model/record.rb', line 391

def request_meta(clause, options)
  where_clause, where_values = where_params(clause)
  select_clause, use_query_result = select_params(options)
  order_by_clause = order_by_clause(options[:order_by])
  limit_clause = limit_clause(options)
  request_query = "SELECT #{select_clause} FROM #{table_name}#{where_clause}#{order_by_clause}#{limit_clause}"
  [request_query, use_query_result, where_values]
end

.save_in_batch(type) ⇒ Object



329
330
331
# File 'lib/cassandra_model/record.rb', line 329

def save_in_batch(type)
  table_config.batch_type = type
end

.shard(hashing_column = nil, max_shard = nil, &block) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
# File 'lib/cassandra_model/record.rb', line 416

def shard(hashing_column = nil, max_shard = nil, &block)
  if hashing_column
    if block_given?
      hashing_shard(hashing_column, &block)
    else
      modulo_shard(hashing_column, max_shard)
    end
  else
    manual_shard(&block)
  end
end

.shard_keyObject



436
437
438
# File 'lib/cassandra_model/record.rb', line 436

def shard_key
  partition_key.last
end

.tableObject



322
323
324
325
326
327
# File 'lib/cassandra_model/record.rb', line 322

def table
  table_data.table ||= begin
    table_name = table_config.table_name || generate_table_name
    TableRedux.new(table_config.connection_name, table_name)
  end
end

.table=(value) ⇒ Object



318
319
320
# File 'lib/cassandra_model/record.rb', line 318

def table=(value)
  table_data.table = value
end

.table_name=(value) ⇒ Object



310
311
312
# File 'lib/cassandra_model/record.rb', line 310

def table_name=(value)
  table_config.table_name = value
end

Instance Method Details

#==(rhs) ⇒ Object



108
109
110
# File 'lib/cassandra_model/record.rb', line 108

def ==(rhs)
  rhs.respond_to?(:attributes) && @attributes == rhs.attributes
end

#clustering_columnsObject



94
95
96
# File 'lib/cassandra_model/record.rb', line 94

def clustering_columns
  attributes.slice(*self.class.clustering_columns)
end

#deleteObject



82
83
84
# File 'lib/cassandra_model/record.rb', line 82

def delete
  delete_async.get
end

#delete_asyncObject



64
65
66
# File 'lib/cassandra_model/record.rb', line 64

def delete_async
  internal_delete_async
end

#inspectObject Also known as: to_s



102
103
104
# File 'lib/cassandra_model/record.rb', line 102

def inspect
  %Q{#<#{self.class.to_s}#{inspected_validation} #{inspected_attributes}>}
end

#invalidate!Object



72
73
74
# File 'lib/cassandra_model/record.rb', line 72

def invalidate!
  @valid = false
end

#partition_keyObject



90
91
92
# File 'lib/cassandra_model/record.rb', line 90

def partition_key
  attributes.slice(*self.class.partition_key)
end

#primary_keyObject



98
99
100
# File 'lib/cassandra_model/record.rb', line 98

def primary_key
  attributes.slice(*self.class.primary_key)
end

#save(options = {}) ⇒ Object Also known as: save!



76
77
78
# File 'lib/cassandra_model/record.rb', line 76

def save(options = {})
  save_async(options).get
end

#save_async(options = {}) ⇒ Object



60
61
62
# File 'lib/cassandra_model/record.rb', line 60

def save_async(options = {})
  internal_save_async(options)
end

#update(new_attributes) ⇒ Object



86
87
88
# File 'lib/cassandra_model/record.rb', line 86

def update(new_attributes)
  update_async(new_attributes).get
end

#update_async(new_attributes) ⇒ Object



68
69
70
# File 'lib/cassandra_model/record.rb', line 68

def update_async(new_attributes)
  internal_update_async(new_attributes)
end