Class: CassandraModel::Record

Inherits:
Object
  • Object
show all
Extended by:
QueryHelper, Forwardable
Includes:
DisplayableAttributes, MetaColumns, RecordDebug
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 RecordDebug

#debug

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.



54
55
56
57
58
59
60
61
# File 'lib/cassandra_model/record.rb', line 54

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.



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

def attributes
  @attributes
end

#execution_infoObject (readonly)

Returns the value of attribute execution_info.



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

def execution_info
  @execution_info
end

#validObject (readonly)

Returns the value of attribute valid.



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

def valid
  @valid
end

Class Method Details

.batch_typeObject



387
388
389
# File 'lib/cassandra_model/record.rb', line 387

def batch_type
  table_config.batch_type
end

.before_save(&block) ⇒ Object



519
520
521
# File 'lib/cassandra_model/record.rb', line 519

def before_save(&block)
  before_save_callbacks << block
end

.before_save_callbacksObject



523
524
525
# File 'lib/cassandra_model/record.rb', line 523

def before_save_callbacks
  table_config.before_save_callbacks ||= []
end

.cassandra_columnsObject



446
447
448
449
450
# File 'lib/cassandra_model/record.rb', line 446

def cassandra_columns
  table_data.cassandra_columns ||= table.connection.keyspace.table(table_name).columns.inject({}) do |memo, column|
    memo.merge!(column.name.to_sym => column.type)
  end
end

.columnsObject



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

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

.connection_name=(value) ⇒ Object



368
369
370
# File 'lib/cassandra_model/record.rb', line 368

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

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



420
421
422
# File 'lib/cassandra_model/record.rb', line 420

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

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



416
417
418
# File 'lib/cassandra_model/record.rb', line 416

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

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



503
504
505
# File 'lib/cassandra_model/record.rb', line 503

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

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



495
496
497
# File 'lib/cassandra_model/record.rb', line 495

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

.normalized_attributes(attributes) ⇒ Object



434
435
436
# File 'lib/cassandra_model/record.rb', line 434

def normalized_attributes(attributes)
  attributes.symbolize_keys
end

.normalized_column(column) ⇒ Object



430
431
432
# File 'lib/cassandra_model/record.rb', line 430

def normalized_column(column)
  column.to_sym
end

.order_by_clause(order_by) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/cassandra_model/record.rb', line 480

def order_by_clause(order_by)
  if order_by
    order_by = [order_by] unless order_by.is_a?(Array)
    ordering_columns = order_by.map do |column|
      if column.is_a?(Hash)
        column, direction = column.first
        "#{column} #{direction.upcase}"
      else
        column
      end
    end
    " ORDER BY #{multi_csv_clause(ordering_columns)}"
  end
end

.query_for_deleteObject



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

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



397
398
399
400
401
402
403
# File 'lib/cassandra_model/record.rb', line 397

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



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

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



499
500
501
# File 'lib/cassandra_model/record.rb', line 499

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

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



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/cassandra_model/record.rb', line 452

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



471
472
473
474
475
476
477
478
# File 'lib/cassandra_model/record.rb', line 471

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

.restriction_attributes(restriction) ⇒ Object



426
427
428
# File 'lib/cassandra_model/record.rb', line 426

def restriction_attributes(restriction)
  restriction
end

.save_in_batch(type) ⇒ Object



383
384
385
# File 'lib/cassandra_model/record.rb', line 383

def save_in_batch(type)
  table_config.batch_type = type
end

.select_column(column) ⇒ Object



442
443
444
# File 'lib/cassandra_model/record.rb', line 442

def select_column(column)
  column
end

.select_columns(columns) ⇒ Object



438
439
440
# File 'lib/cassandra_model/record.rb', line 438

def select_columns(columns)
  columns
end

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



507
508
509
510
511
512
513
514
515
516
517
# File 'lib/cassandra_model/record.rb', line 507

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



527
528
529
# File 'lib/cassandra_model/record.rb', line 527

def shard_key
  partition_key.last
end

.tableObject



376
377
378
379
380
381
# File 'lib/cassandra_model/record.rb', line 376

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



372
373
374
# File 'lib/cassandra_model/record.rb', line 372

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

.table_name=(value) ⇒ Object



364
365
366
# File 'lib/cassandra_model/record.rb', line 364

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

Instance Method Details

#==(rhs) ⇒ Object



111
112
113
# File 'lib/cassandra_model/record.rb', line 111

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

#clustering_columnsObject



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

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

#deleteObject



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

def delete
  delete_async.get
end

#delete_asyncObject



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

def delete_async
  internal_delete_async
end

#inspectObject Also known as: to_s



105
106
107
# File 'lib/cassandra_model/record.rb', line 105

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

#invalidate!Object



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

def invalidate!
  @valid = false
end

#partition_keyObject



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

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

#primary_keyObject



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

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

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



79
80
81
# File 'lib/cassandra_model/record.rb', line 79

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

#save_async(options = {}) ⇒ Object



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

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

#update(new_attributes) ⇒ Object



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

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

#update_async(new_attributes) ⇒ Object



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

def update_async(new_attributes)
  internal_update_async(new_attributes)
end