Module: DatastaxRails::Persistence::ClassMethods
- Defined in:
- lib/datastax_rails/persistence.rb
Overview
rubocop:disable Style/Documentation
Instance Method Summary collapse
- #create(attributes = {}, options = {}, &block) ⇒ Object
-
#encode_attributes(record, cql, attributes = {}) ⇒ Hash
Encodes the attributes in preparation for storing in cassandra.
-
#instantiate(_key, attributes, _selected_attributes = []) ⇒ DatastaxRails::Base
Instantiates a new object without calling
initialize
. -
#remove(*keys) ⇒ Object
Removes one or more records with corresponding keys.
-
#truncate ⇒ Object
(also: #delete_all)
Truncates the column_family associated with this class.
-
#write(record, options = {}) ⇒ Object
Write a full record to cassandra.
-
#write_attribute(record, attributes, options = {}) ⇒ Object
Write one or more attributes directly to cassandra.
Instance Method Details
#create(attributes = {}, options = {}, &block) ⇒ Object
57 58 59 60 61 |
# File 'lib/datastax_rails/persistence.rb', line 57 def create(attributes = {}, = {}, &block) new(attributes, &block).tap do |object| object.save() end end |
#encode_attributes(record, cql, attributes = {}) ⇒ Hash
Encodes the attributes in preparation for storing in cassandra. Calls the coders on the various type classes to do the heavy lifting.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/datastax_rails/persistence.rb', line 136 def encode_attributes(record, cql, attributes = {}) encoded = {} Types::DirtyCollection.ignore_modifications do if attributes.empty? record.send(cql ? 'changed' : 'column_names').each do |column_name| attributes[column_name] = record.read_attribute(column_name) end end attributes.each do |k, v| encoded[k.to_s] = cql ? attribute_definitions[k].type_cast_for_cql3(v) : attribute_definitions[k].type_cast_for_solr(v) end end encoded end |
#instantiate(_key, attributes, _selected_attributes = []) ⇒ DatastaxRails::Base
Instantiates a new object without calling initialize
.
126 127 128 |
# File 'lib/datastax_rails/persistence.rb', line 126 def instantiate(_key, attributes, _selected_attributes = []) allocate.init_with('attributes' => attributes) end |
#remove(*keys, options) ⇒ Object #remove(*keys) ⇒ Object
Removes one or more records with corresponding keys. Last parameter can be a hash specifying the consistency level. The keys should be in the form returned by #id_for_update
Model.remove({id: '12345'},{id: '67890'}, :consistency => 'LOCAL_QUORUM)
TODO: Submit multiple deletes as a batch
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/datastax_rails/persistence.rb', line 27 def remove(*keys) = keys.last.is_a?(Hash) ? keys.pop : {} # keys = keys.flat_map { |k| attribute_definitions[primary_key].type_cast(k) } keys.each do |key| typecast_key = {} key.each { |k, v| typecast_key[k] = attribute_definitions[k].type_cast_for_cql3(v) } ActiveSupport::Notifications.instrument('remove.datastax_rails', column_family: column_family, key: key) do c = cql.delete(typecast_key) if [:consistency] level = [:consistency] if valid_consistency?(level) c.using(level) else fail ArgumentError, "'#{level}' is not a valid Cassandra consistency level" end end c.execute end end end |
#truncate ⇒ Object Also known as: delete_all
Truncates the column_family associated with this class
50 51 52 53 54 |
# File 'lib/datastax_rails/persistence.rb', line 50 def truncate ActiveSupport::Notifications.instrument('truncate.datastax_rails', column_family: column_family) do cql.truncate.execute end end |
#write(record, options = {}) ⇒ Object
Write a full record to cassandra.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/datastax_rails/persistence.rb', line 71 def write(record, = {}) level = ([:consistency] || default_consistency) if valid_consistency?(level) [:consistency] = level else fail ArgumentError, "'#{level}' is not a valid Cassandra consistency level" end record.id.tap do |key| ActiveSupport::Notifications.instrument('insert.datastax_rails', column_family: column_family, key: key.to_s, attributes: record.attributes) do if (storage_method == :solr) write_with_solr(record.id_for_update, encode_attributes(record, false), ) else write_with_cql(record.id_for_update, encode_attributes(record, true), ) end end end end |
#write_attribute(record, attributes, options = {}) ⇒ Object
Write one or more attributes directly to cassandra. Bypasses all normal validation and callbacks. Record must be already persisted.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/datastax_rails/persistence.rb', line 98 def write_attribute(record, attributes, = {}) fail NotPersistedError if record.new_record? level = ([:consistency] || default_consistency).to_s.upcase if valid_consistency?(level) [:consistency] = level else fail ArgumentError, "'#{level}' is not a valid Cassandra consistency level" end record.id.tap do |key| ActiveSupport::Notifications.instrument('update.datastax_rails', column_family: column_family, key: key.to_s, attributes: record.attributes) do if (storage_method == :solr) write_with_solr(record.id_for_update, encode_attributes(record, false, attributes), ) else write_with_cql(record.id_for_update, encode_attributes(record, true, attributes), ) end end end end |