Module: Mongoid::Atomic

Extended by:
ActiveSupport::Concern
Included in:
Composable, Contextual::Mongo
Defined in:
lib/mongoid/atomic.rb,
lib/mongoid/atomic/modifiers.rb,
lib/mongoid/atomic/paths/root.rb,
lib/mongoid/atomic/paths/embedded.rb,
lib/mongoid/atomic/paths/embedded/one.rb,
lib/mongoid/atomic/paths/embedded/many.rb

Overview

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

Defined Under Namespace

Modules: Paths Classes: Modifiers

Constant Summary

UPDATES =
[
  :atomic_array_pushes,
  :atomic_array_pulls,
  :atomic_array_add_to_sets,
  :atomic_pulls,
  :delayed_atomic_sets,
  :delayed_atomic_pulls,
  :delayed_atomic_unsets
]

Instance Method Summary (collapse)

Instance Method Details

- (Object) add_atomic_pull(document)

Add the document as an atomic pull.

Examples:

Add the atomic pull.

person.add_atomic_pull(address)

Parameters:

  • The (Document)

    embedded document to pull.

Since:

  • 2.2.0



37
38
39
40
# File 'lib/mongoid/atomic.rb', line 37

def add_atomic_pull(document)
  document.flagged_for_destroy = true
  (delayed_atomic_pulls[document..to_s] ||= []).push(document)
end

- (Array<Document>) add_atomic_unset(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:

Since:

  • 3.0.0



52
53
54
55
# File 'lib/mongoid/atomic.rb', line 52

def add_atomic_unset(document)
  document.flagged_for_destroy = true
  (delayed_atomic_unsets[document..to_s] ||= []).push(document)
end

- (Hash) atomic_array_add_to_sets

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.

Since:

  • 2.4.0



101
102
103
# File 'lib/mongoid/atomic.rb', line 101

def atomic_array_add_to_sets
  @atomic_array_add_to_sets ||= {}
end

- (Hash) atomic_array_pulls

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.

Since:

  • 2.4.0



89
90
91
# File 'lib/mongoid/atomic.rb', line 89

def atomic_array_pulls
  @atomic_array_pulls ||= {}
end

- (Hash) atomic_array_pushes

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.

Since:

  • 2.4.0



77
78
79
# File 'lib/mongoid/atomic.rb', line 77

def atomic_array_pushes
  @atomic_array_pushes ||= {}
end

- (String) atomic_attribute_name(name)

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

Since:

  • 3.0.0



65
66
67
# File 'lib/mongoid/atomic.rb', line 65

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

- (String) atomic_delete_modifier

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.



147
148
149
# File 'lib/mongoid/atomic.rb', line 147

def atomic_delete_modifier
  atomic_paths.delete_modifier
end

- (String) atomic_insert_modifier

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.



158
159
160
# File 'lib/mongoid/atomic.rb', line 158

def atomic_insert_modifier
  atomic_paths.insert_modifier
end

- (String) atomic_path

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.



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

def atomic_path
  atomic_paths.path
end

- (Object) atomic_paths

Get the atomic paths utility for this document.

Examples:

Get the atomic paths.

document.atomic_paths

Returns:

  • (Object)

    The associated path.

Since:

  • 2.1.0



191
192
193
# File 'lib/mongoid/atomic.rb', line 191

def atomic_paths
  @atomic_paths ||=  ? .path(self) : Atomic::Paths::Root.new(self)
end

- (String) atomic_position

Returns the positional operator of this document for modification.

Examples:

Get the positional operator.

address.atomic_position

Returns:

  • (String)

    The positional operator with indexes.



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

def atomic_position
  atomic_paths.position
end

- (Array<Hash>) atomic_pulls

Get all the attributes that need to be pulled.

Examples:

Get the pulls.

person.atomic_pulls

Returns:

  • (Array<Hash>)

    The $pullAll operations.

Since:

  • 2.2.0



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/mongoid/atomic.rb', line 203

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

- (Hash) atomic_pushes

Get all the push attributes that need to occur.

Examples:

Get the pushes.

person.atomic_pushes

Returns:

  • (Hash)

    The $pushAll operations.

Since:

  • 2.1.0



224
225
226
# File 'lib/mongoid/atomic.rb', line 224

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

- (Hash) atomic_sets

Get all the attributes that need to be set.

Examples:

Get the sets.

person.atomic_sets

Returns:

  • (Hash)

    The $set operations.

Since:

  • 2.1.0



236
237
238
# File 'lib/mongoid/atomic.rb', line 236

def atomic_sets
  updateable? ? setters : settable? ? { atomic_path => as_document } : {}
end

- (Array<Hash>) atomic_unsets

Get all the attributes that need to be unset.

Examples:

Get the unsets.

person.atomic_unsets

Returns:

  • (Array<Hash>)

    The $unset operations.

Since:

  • 2.2.0



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/mongoid/atomic.rb', line 248

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

- (Hash) atomic_updates(use_indexes = false) 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 $pushAll, 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 $pushAll 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.

Since:

  • 2.1.0



128
129
130
131
132
133
134
135
136
137
# File 'lib/mongoid/atomic.rb', line 128

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

- (Hash) delayed_atomic_pulls

Get a hash of atomic pulls that are pending.

Examples:

Get the atomic pulls.

document.delayed_atomic_pulls

Returns:

  • (Hash)

    name/document pairs.

Since:

  • 2.3.2



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

def delayed_atomic_pulls
  @delayed_atomic_pulls ||= {}
end

- (Hash) delayed_atomic_sets

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.

Since:

  • 2.3.0



268
269
270
# File 'lib/mongoid/atomic.rb', line 268

def delayed_atomic_sets
  @delayed_atomic_sets ||= {}
end

- (Hash) delayed_atomic_unsets

Get the delayed atomic unsets.

Examples:

Get the delayed atomic unsets.

document.delayed_atomic_unsets

Returns:

  • (Hash)

    The atomic unsets

Since:

  • 3.0.0



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

def delayed_atomic_unsets
  @delayed_atomic_unsets ||= {}
end

- (String) flag_as_destroyed

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.

Since:

  • 3.0.0



304
305
306
307
308
# File 'lib/mongoid/atomic.rb', line 304

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

- (Array<Proc>) flagged_destroys

Get the flagged destroys.

Examples:

Get the flagged destroy.

document.flagged_destroys

Returns:

  • (Array<Proc>)

    The flagged destroys.

Since:

  • 3.0.10



318
319
320
# File 'lib/mongoid/atomic.rb', line 318

def flagged_destroys
  @flagged_destroys ||= []
end

- (Array) process_flagged_destroys

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.

Since:

  • 3.0.10



330
331
332
333
334
335
336
337
# File 'lib/mongoid/atomic.rb', line 330

def process_flagged_destroys
  _assigning do
    flagged_destroys.each do |block|
      block.call
    end
  end
  flagged_destroys.clear
end