Class: CassandraModel::Record
Defined Under Namespace
Classes: Attributes, ConfigureableAttributes
Instance Attribute Summary collapse
Attributes included from Scopes
#scopes
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Scopes
scope
after, all, before, def_query_helper, find_by
#debug
#as_json, included
included
Constructor Details
#initialize(attributes = {}, options = {validate: true}) ⇒ Record
Returns a new instance of Record.
56
57
58
59
60
61
62
63
|
# File 'lib/cassandra_model/record.rb', line 56
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
#attributes ⇒ Object
Returns the value of attribute attributes.
12
13
14
|
# File 'lib/cassandra_model/record.rb', line 12
def attributes
@attributes
end
|
#execution_info ⇒ Object
Returns the value of attribute execution_info.
12
13
14
|
# File 'lib/cassandra_model/record.rb', line 12
def execution_info
@execution_info
end
|
#valid ⇒ Object
Returns the value of attribute valid.
12
13
14
|
# File 'lib/cassandra_model/record.rb', line 12
def valid
@valid
end
|
Class Method Details
.batch_type ⇒ Object
396
397
398
|
# File 'lib/cassandra_model/record.rb', line 396
def batch_type
table_config.batch_type
end
|
.before_save(&block) ⇒ Object
537
538
539
|
# File 'lib/cassandra_model/record.rb', line 537
def before_save(&block)
before_save_callbacks << block
end
|
.before_save_callbacks ⇒ Object
541
542
543
|
# File 'lib/cassandra_model/record.rb', line 541
def before_save_callbacks
table_config.before_save_callbacks ||= []
end
|
.cassandra_columns ⇒ Object
464
465
466
467
468
|
# File 'lib/cassandra_model/record.rb', line 464
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
|
.columns ⇒ Object
400
401
402
403
404
|
# File 'lib/cassandra_model/record.rb', line 400
def columns
table_data.columns ||= internal_columns.tap do |columns|
columns.each { |column| define_attribute(column) }
end
end
|
.composite_defaults ⇒ Object
410
411
412
|
# File 'lib/cassandra_model/record.rb', line 410
def composite_defaults
[]
end
|
.connection_name=(value) ⇒ Object
377
378
379
|
# File 'lib/cassandra_model/record.rb', line 377
def connection_name=(value)
table_config.connection_name = value
end
|
.create(attributes, options = {}) ⇒ Object
Also known as:
create!
438
439
440
|
# File 'lib/cassandra_model/record.rb', line 438
def create(attributes, options = {})
create_async(attributes, options).get
end
|
.create_async(attributes, options = {}) ⇒ Object
434
435
436
|
# File 'lib/cassandra_model/record.rb', line 434
def create_async(attributes, options = {})
self.new(attributes).save_async(options)
end
|
.denormalized_column_map(input_columns) ⇒ Object
406
407
408
|
# File 'lib/cassandra_model/record.rb', line 406
def denormalized_column_map(input_columns)
(columns & input_columns).inject({}) { |memo, column| memo.merge!(column => column) }
end
|
.first(clause = {}, options = {}) ⇒ Object
521
522
523
|
# File 'lib/cassandra_model/record.rb', line 521
def first(clause = {}, options = {})
first_async(clause, options).get
end
|
.first_async(clause = {}, options = {}) ⇒ Object
513
514
515
|
# File 'lib/cassandra_model/record.rb', line 513
def first_async(clause = {}, options = {})
request_async(clause, options.merge(limit: 1))
end
|
.normalized_attributes(attributes) ⇒ Object
452
453
454
|
# File 'lib/cassandra_model/record.rb', line 452
def normalized_attributes(attributes)
attributes.symbolize_keys
end
|
.normalized_column(column) ⇒ Object
448
449
450
|
# File 'lib/cassandra_model/record.rb', line 448
def normalized_column(column)
column.to_sym
end
|
.order_by_clause(order_by) ⇒ Object
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
# File 'lib/cassandra_model/record.rb', line 498
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_delete ⇒ Object
423
424
425
426
|
# File 'lib/cassandra_model/record.rb', line 423
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
414
415
416
417
418
419
420
421
|
# File 'lib/cassandra_model/record.rb', line 414
def query_for_save(options = {})
existence_clause = options[:check_exists] && ' IF NOT EXISTS'
ttl_clause = options[:ttl] && " USING TTL #{options[:ttl]}"
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}#{ttl_clause}"
end
|
.query_for_update(new_attributes) ⇒ Object
428
429
430
431
432
|
# File 'lib/cassandra_model/record.rb', line 428
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
517
518
519
|
# File 'lib/cassandra_model/record.rb', line 517
def request(clause, options = {})
request_async(clause, options).get
end
|
.request_async(clause, options = {}) ⇒ Object
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
# File 'lib/cassandra_model/record.rb', line 470
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
|
489
490
491
492
493
494
495
496
|
# File 'lib/cassandra_model/record.rb', line 489
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
444
445
446
|
# File 'lib/cassandra_model/record.rb', line 444
def restriction_attributes(restriction)
restriction
end
|
.save_in_batch(type) ⇒ Object
392
393
394
|
# File 'lib/cassandra_model/record.rb', line 392
def save_in_batch(type)
table_config.batch_type = type
end
|
.select_column(column) ⇒ Object
460
461
462
|
# File 'lib/cassandra_model/record.rb', line 460
def select_column(column)
column
end
|
.select_columns(columns) ⇒ Object
456
457
458
|
# File 'lib/cassandra_model/record.rb', line 456
def select_columns(columns)
columns
end
|
.shard(hashing_column = nil, max_shard = nil, &block) ⇒ Object
525
526
527
528
529
530
531
532
533
534
535
|
# File 'lib/cassandra_model/record.rb', line 525
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_key ⇒ Object
545
546
547
|
# File 'lib/cassandra_model/record.rb', line 545
def shard_key
partition_key.last
end
|
.table ⇒ Object
385
386
387
388
389
390
|
# File 'lib/cassandra_model/record.rb', line 385
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
381
382
383
|
# File 'lib/cassandra_model/record.rb', line 381
def table=(value)
table_data.table = value
end
|
.table_name=(value) ⇒ Object
373
374
375
|
# File 'lib/cassandra_model/record.rb', line 373
def table_name=(value)
table_config.table_name = value
end
|
Instance Method Details
#==(rhs) ⇒ Object
113
114
115
116
117
|
# File 'lib/cassandra_model/record.rb', line 113
def ==(rhs)
rhs.respond_to?(:attributes) && columns.all? do |column|
attributes[column] == rhs.attributes[column]
end
end
|
#clustering_columns ⇒ Object
99
100
101
|
# File 'lib/cassandra_model/record.rb', line 99
def clustering_columns
attributes.slice(*self.class.clustering_columns)
end
|
#delete ⇒ Object
87
88
89
|
# File 'lib/cassandra_model/record.rb', line 87
def delete
delete_async.get
end
|
#delete_async ⇒ Object
69
70
71
|
# File 'lib/cassandra_model/record.rb', line 69
def delete_async
internal_delete_async
end
|
#inspect ⇒ Object
Also known as:
to_s
107
108
109
|
# File 'lib/cassandra_model/record.rb', line 107
def inspect
%Q{#<#{self.class.to_s}#{inspected_validation} #{inspected_attributes}>}
end
|
#invalidate! ⇒ Object
77
78
79
|
# File 'lib/cassandra_model/record.rb', line 77
def invalidate!
@valid = false
end
|
#partition_key ⇒ Object
95
96
97
|
# File 'lib/cassandra_model/record.rb', line 95
def partition_key
attributes.slice(*self.class.partition_key)
end
|
#primary_key ⇒ Object
103
104
105
|
# File 'lib/cassandra_model/record.rb', line 103
def primary_key
attributes.slice(*self.class.primary_key)
end
|
#save(options = {}) ⇒ Object
Also known as:
save!
81
82
83
|
# File 'lib/cassandra_model/record.rb', line 81
def save(options = {})
save_async(options).get
end
|
#save_async(options = {}) ⇒ Object
65
66
67
|
# File 'lib/cassandra_model/record.rb', line 65
def save_async(options = {})
internal_save_async(options)
end
|
#update(new_attributes) ⇒ Object
91
92
93
|
# File 'lib/cassandra_model/record.rb', line 91
def update(new_attributes)
update_async(new_attributes).get
end
|
#update_async(new_attributes) ⇒ Object
73
74
75
|
# File 'lib/cassandra_model/record.rb', line 73
def update_async(new_attributes)
internal_update_async(new_attributes)
end
|