123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Atomic

Relationships & Source Files
Namespace Children
Modules:
Classes:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/atomic.rb,
lib/mongoid/atomic/modifiers.rb,
lib/mongoid/atomic/paths/embedded.rb,
lib/mongoid/atomic/paths/root.rb,
lib/mongoid/atomic/paths/embedded/many.rb,
lib/mongoid/atomic/paths/embedded/one.rb

Overview

This module contains the logic for supporting atomic operations against the database.

Constant Summary

  • UPDATES =
    # File 'lib/mongoid/atomic.rb', line 12
    %i[
      atomic_array_pushes
      atomic_array_pulls
      atomic_array_add_to_sets
      atomic_pulls
      delayed_atomic_sets
      delayed_atomic_pulls
      delayed_atomic_unsets
    ].freeze

Instance Method Summary

DSL Calls

included

[ GitHub ]


22
23
24
25
26
# File 'lib/mongoid/atomic.rb', line 22

included do
  # When MongoDB finally fully implements the positional operator, we can
  # get rid of all indexing related code in Mongoid.
  attr_accessor :_index
end

Instance Method Details

#_updates(_use_indexes = false)

Alias for #atomic_updates.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 129

alias _updates atomic_updates

#add_atomic_pull(document)

Add the document as an atomic pull.

Examples:

Add the atomic pull.

person.add_atomic_pull(address)

Parameters:

  • document (Document)

    The embedded document to pull.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 34

def add_atomic_pull(document)
  document.flagged_for_destroy = true
  key = document.association_name.to_s
  delayed_atomic_pulls[key] ||= []
  delayed_atomic_pulls[key] << document
end

#add_atomic_unset(document) ⇒ Array<Document>

Add an atomic unset for the document.

Examples:

Add an atomic unset.

document.add_atomic_unset(doc)

Parameters:

  • document (Document)

    The child document.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 49

def add_atomic_unset(document)
  document.flagged_for_destroy = true
  key = document.association_name.to_s
  delayed_atomic_unsets[key] ||= []
  delayed_atomic_unsets[key] << document
end

#atomic_array_add_to_setsHash

For array fields these are the unique adds that need to happen.

Examples:

Get the array unique adds.

person.atomic_array_add_to_sets

Returns:

  • (Hash)

    The array add_to_sets.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 92

def atomic_array_add_to_sets
  @atomic_array_add_to_sets ||= {}
end

#atomic_array_pullsHash

For array fields these are the pulls that need to happen.

Examples:

Get the array pulls.

person.atomic_array_pulls

Returns:

  • (Hash)

    The array pulls.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 82

def atomic_array_pulls
  @atomic_array_pulls ||= {}
end

#atomic_array_pushesHash

For array fields these are the pushes that need to happen.

Examples:

Get the array pushes.

person.atomic_array_pushes

Returns:

  • (Hash)

    The array pushes.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 72

def atomic_array_pushes
  @atomic_array_pushes ||= {}
end

#atomic_attribute_name(name) ⇒ String

Returns path of the attribute for modification

Examples:

Get path of the attribute

address.atomic_attribute_name(:city)

Returns:

  • (String)

    The path to the document attribute in the database

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 62

def atomic_attribute_name(name)
  embedded? ? "#{atomic_position}.#{name}" : name
end

#atomic_delete_modifierString

Get the removal modifier for the document. Will be nil on root documents, $unset on embeds_one, $set on embeds_many.

Examples:

Get the removal operator.

name.atomic_delete_modifier

Returns:

  • (String)

    The pull or unset operation.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 139

def atomic_delete_modifier
  atomic_paths.delete_modifier
end

#atomic_insert_modifierString

Get the insertion modifier for the document. Will be nil on root documents, $set on embeds_one, $push on embeds_many.

Examples:

Get the insert operation.

name.atomic_insert_modifier

Returns:

  • (String)

    The pull or set operator.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 150

def atomic_insert_modifier
  atomic_paths.insert_modifier
end

#atomic_pathString

Return the path to this Document in JSON notation, used for atomic updates via $set in MongoDB.

Examples:

Get the path to this document.

address.atomic_path

Returns:

  • (String)

    The path to the document in the database.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 161

def atomic_path
  atomic_paths.path
end

#atomic_pathsObject

Get the atomic paths utility for this document.

Examples:

Get the atomic paths.

document.atomic_paths

Returns:

  • (Object)

    The associated path.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 181

def atomic_paths
  return @atomic_paths if @atomic_paths

  paths = if _association
            _association.path(self)
          else
            Atomic::Paths::Root.new(self)
          end

  paths.tap { @atomic_paths = paths unless new_record? }
end

#atomic_positionString

Returns the positional operator of this document for modification.

Examples:

Get the positional operator.

address.atomic_position

Returns:

  • (String)

    The positional operator with indexes.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 171

def atomic_position
  atomic_paths.position
end

#atomic_pullsArray<Hash>

Get all the attributes that need to be pulled.

Examples:

Get the pulls.

person.atomic_pulls

Returns:

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 199

def atomic_pulls
  pulls = {}
  delayed_atomic_pulls.each_pair do |_, docs|
    path = nil
    ids = docs.map do |doc|
      path ||= doc.flag_as_destroyed
      doc._id
    end
    pulls[path] = { '_id' => { '$in' => ids } } and path = nil
  end
  pulls
end

#atomic_pushesHash

Get all the push attributes that need to occur.

Examples:

Get the pushes.

person.atomic_pushes

Returns:

  • (Hash)

    The $push and $each operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 218

def atomic_pushes
  pushable? ? { atomic_position => as_attributes } : {}
end

#atomic_setsHash

Get all the attributes that need to be set.

Examples:

Get the sets.

person.atomic_sets

Returns:

  • (Hash)

    The $set operations.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 228

def atomic_sets
  if updateable?
    setters
  elsif settable?
    { atomic_path => as_attributes }
  else
    {}
  end
end

#atomic_unsetsArray<Hash>

Get all the attributes that need to be unset.

Examples:

Get the unsets.

person.atomic_unsets

Returns:

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 244

def atomic_unsets
  unsets = []
  delayed_atomic_unsets.each_pair do |name, docs|
    path = nil
    docs.each do |doc|
      path ||= doc.flag_as_destroyed
    end
    unsets.push(path || name)
  end
  unsets
end

#atomic_updates(_use_indexes = false) ⇒ Hash Also known as: #_updates

Note:

MongoDB does not allow “conflicting modifications” to be performed in a single operation. Conflicting modifications are detected by the ‘haveConflictingMod’ function in MongoDB. Examination of the code suggests that two modifications (a $set and a $push with $each, for example) conflict if:

(1) the key paths being modified are equal.
(2) one key path is a prefix of the other.

So a $set of ‘addresses.0.street’ will conflict with a $push and $each to ‘addresses’, and we will need to split our update into two pieces. We do not, however, attempt to match MongoDB’s logic exactly. Instead, we assume that two updates conflict if the first component of the two key paths matches.

Get all the atomic updates that need to happen for the current Document. This includes all changes that need to happen in the entire hierarchy that exists below where the save call was made.

Examples:

Get the updates that need to occur.

person.atomic_updates(children)

Returns:

  • (Hash)

    The updates and their modifiers.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 119

def atomic_updates(_use_indexes = false)
  process_flagged_destroys
  mods = Modifiers.new
  generate_atomic_updates(mods, self)
  _descendants.each do |child|
    child.process_flagged_destroys
    generate_atomic_updates(mods, child)
  end
  mods
end

#delayed_atomic_pullsHash

Get a hash of atomic pulls that are pending.

Examples:

Get the atomic pulls.

document.delayed_atomic_pulls

Returns:

  • (Hash)

    name/document pairs.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 272

def delayed_atomic_pulls
  @delayed_atomic_pulls ||= {}
end

#delayed_atomic_setsHash

Get all the atomic sets that have had their saves delayed.

Examples:

Get the delayed atomic sets.

person.delayed_atomic_sets

Returns:

  • (Hash)

    The delayed $sets.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 262

def delayed_atomic_sets
  @delayed_atomic_sets ||= {}
end

#delayed_atomic_unsetsHash

Get the delayed atomic unsets.

Examples:

Get the delayed atomic unsets.

document.delayed_atomic_unsets

Returns:

  • (Hash)

    The atomic unsets

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 282

def delayed_atomic_unsets
  @delayed_atomic_unsets ||= {}
end

#flag_as_destroyedString

Flag the document as destroyed and return the atomic path.

Examples:

Flag destroyed and return path.

document.flag_as_destroyed

Returns:

  • (String)

    The atomic path.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 292

def flag_as_destroyed
  self.destroyed = true
  self.flagged_for_destroy = false
  atomic_path
end

#flagged_destroysArray<Proc>

Get the flagged destroys.

Examples:

Get the flagged destroy.

document.flagged_destroys

Returns:

  • (Array<Proc>)

    The flagged destroys.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 304

def flagged_destroys
  @flagged_destroys ||= []
end

#generate_atomic_updates(mods, doc) (private)

Generates the atomic updates in the correct order.

Examples:

Generate the updates.

model.generate_atomic_updates(mods, doc)

Parameters:

  • mods (Modifiers)

    The atomic modifications.

  • doc (Document)

    The document to update for.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 337

def generate_atomic_updates(mods, doc)
  mods.unset(doc.atomic_unsets)
  mods.pull(doc.atomic_pulls)
  mods.set(doc.atomic_sets)
  mods.set(doc.delayed_atomic_sets)
  mods.push(doc.atomic_pushes)
  mods.push(doc.atomic_array_pushes)
  mods.add_to_set(doc.atomic_array_add_to_sets)
  mods.pull_all(doc.atomic_array_pulls)
end

#process_flagged_destroysArray

Process all the pending flagged destroys from nested attributes.

Examples:

Process all the pending flagged destroys.

document.process_flagged_destroys

Returns:

  • (Array)

    The cleared array.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 314

def process_flagged_destroys
  _assigning do
    flagged_destroys.each(&:call)
  end
  flagged_destroys.clear
end

#reset_atomic_updates! (private)

Clears all pending atomic updates.

[ GitHub ]

  
# File 'lib/mongoid/atomic.rb', line 324

def reset_atomic_updates!
  Atomic::UPDATES.each do |update|
    send(update).clear
  end
end