Module: DatastaxRails::Persistence::ClassMethods

Defined in:
lib/datastax_rails/persistence.rb

Overview

rubocop:disable Style/Documentation

Instance Method Summary collapse

Instance Method Details

#create(attributes = {}, options = {}, &block) ⇒ Object



58
59
60
61
62
# File 'lib/datastax_rails/persistence.rb', line 58

def create(attributes = {}, options = {}, &block)
  new(attributes, &block).tap do |object|
    object.save(options)
  end
end

#encode_attributes(record, cql) ⇒ Hash

Encodes the attributes in preparation for storing in cassandra. Calls the coders on the various type classes to do the heavy lifting.

Parameters:

  • record (DatastaxRails::Base)

    the record whose attributes we’re encoding

  • cql (Boolean)

    True if we’re formatting for CQL, otherwise False

Returns:

  • (Hash)

    a new hash with attributes encoded for storage



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/datastax_rails/persistence.rb', line 106

def encode_attributes(record, cql)
  encoded = {}
  Types::DirtyCollection.ignore_modifications do
    record.changed.each do |column_name|
      value = record.read_attribute(column_name)
      encoded[column_name.to_s] = cql ? attribute_definitions[column_name].type_cast_for_cql3(value) :
                                        attribute_definitions[column_name].type_cast_for_solr(value)
    end
  end
  encoded
end

#instantiate(_key, attributes, _selected_attributes = []) ⇒ DatastaxRails::Base

Instantiates a new object without calling initialize.

Parameters:

  • key (String)

    the primary key for the record

  • attributes (Hash)

    a hash containing the columns to set on the record

  • selected_attributes (Array)

    an array containing the attributes that were originally selected from cassandra to build this object. Used so that we can avoid lazy-loading attributes that don’t exist.

Returns:



96
97
98
# File 'lib/datastax_rails/persistence.rb', line 96

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

Overloads:

  • #remove(*keys, options) ⇒ Object

    Removes one or more keys with the given options

    Parameters:

    • keys (String)

      one or more keys to delete

    • options (Hash)

      generally the consistency level to set

  • #remove(*keys) ⇒ Object

    Removes one or more keys with the default options

    Parameters:

    • keys (String)

      one or more keys to delete



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/datastax_rails/persistence.rb', line 28

def remove(*keys)
  options = 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(v) }

    ActiveSupport::Notifications.instrument('remove.datastax_rails', column_family: column_family, key: key) do
      c = cql.delete(typecast_key)
      if options[:consistency]
        level = options[:consistency].to_s.upcase
        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

#truncateObject Also known as: delete_all

Truncates the column_family associated with this class



51
52
53
54
55
# File 'lib/datastax_rails/persistence.rb', line 51

def truncate
  ActiveSupport::Notifications.instrument('truncate.datastax_rails', column_family: column_family) do
    cql.truncate.execute
  end
end

#write(record, options = {}) ⇒ Object

Write a record to cassandra. Can be either an insert or an update (they are exactly the same to cassandra)

Parameters:

  • record (DatastaxRails::Base)

    the record that we are writing

  • options (Hash) (defaults to: {})

    a hash containing various options

Options Hash (options):

  • :consistency (Symbol)

    the consistency to set for the Cassandra operation (e.g., ALL)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/datastax_rails/persistence.rb', line 69

def write(record, options = {})
  level = (options[:consistency] || default_consistency).to_s.upcase
  if valid_consistency?(level)
    options[: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, options)
      else
        write_with_cql(record, options)
      end
    end
  end
end