Module: Mongoid::Threaded

Extended by:
Threaded
Included in:
Threaded
Defined in:
lib/mongoid/threaded.rb,
lib/mongoid/threaded/lifecycle.rb

Overview

This module contains logic for easy access to objects that have a lifecycle on the current thread.

Defined Under Namespace

Modules: Lifecycle

Constant Summary collapse

STORAGE_KEY =

The key for the shared thread- and fiber-local storage. It must be a symbol because keys for fiber-local storage must be symbols.

:'[mongoid]'
STORAGE_OWNER_KEY =

Tracks which fiber owns the storage hash, to detect when a fiber has inherited (rather than created) its storage from a parent fiber.

:'[mongoid]:owner'
DATABASE_OVERRIDE_KEY =
'db-override'
CLIENT_OVERRIDE_KEY =

The key to override the client.

'client-override'
CURRENT_SCOPE_KEY =

The key for the current thread's scope stack.

'current-scope'
AUTOSAVES_KEY =
'autosaves'
VALIDATIONS_KEY =
'validations'
TOUCH_MERGED_KEY =
'touch-merged'
STACK_KEYS =
Hash.new do |hash, key|
  hash[key] = "#{key}-stack"
end
SESSIONS_KEY =

The key for the current thread's sessions.

'sessions'
MODIFIED_DOCUMENTS_KEY =

The key for storing documents modified inside transactions.

'modified-documents'
EXECUTE_CALLBACKS =

The key storing the default value for whether or not callbacks are executed on documents.

'execute-callbacks'
BIND =
'bind'
ASSIGN =
'assign'
BUILD =
'build'
LOAD =
'load'
CREATE =
'create'

Instance Method Summary collapse

Instance Method Details

#add_modified_document(session, document) ⇒ Object

Store a reference to the document that was modified inside a transaction associated with the session.

Parameters:

  • session (Mongo::Session)

    Session in scope of which the document was modified.

  • document (Mongoid::Document)

    Mongoid document that was modified.



504
505
506
507
508
# File 'lib/mongoid/threaded.rb', line 504

def add_modified_document(session, document)
  return unless session&.in_transaction?

  modified_documents[session] << document
end

#autosaved?(document) ⇒ true | false

Is the document autosaved on the current thread?

Examples:

Is the document autosaved?

Threaded.autosaved?(doc)

Parameters:

  • document (Document)

    The document to check.

Returns:

  • (true | false)

    If the document is autosaved.



368
369
370
# File 'lib/mongoid/threaded.rb', line 368

def autosaved?(document)
  autosaves_for(document.class).include?(document._id)
end

#autosavesHash

Get all autosaves on the current thread.

Examples:

Get all autosaves.

Threaded.autosaves

Returns:

  • (Hash)

    The current autosaves.



403
404
405
# File 'lib/mongoid/threaded.rb', line 403

def autosaves
  get(AUTOSAVES_KEY) { {} }
end

#autosaves_for(klass) ⇒ Array

Get all autosaves on the current thread for the class.

Examples:

Get all autosaves.

Threaded.autosaves_for(Person)

Parameters:

  • klass (Class)

    The class to check.

Returns:

  • (Array)

    The current autosaves.



425
426
427
# File 'lib/mongoid/threaded.rb', line 425

def autosaves_for(klass)
  autosaves[klass] ||= []
end

#begin_autosave(document) ⇒ Object

Begin autosaving a document on the current thread.

Examples:

Begin autosave.

Threaded.begin_autosave(doc)

Parameters:

  • document (Document)

    The document to autosave.



198
199
200
# File 'lib/mongoid/threaded.rb', line 198

def begin_autosave(document)
  autosaves_for(document.class).push(document._id)
end

#begin_execution(name) ⇒ true

Begin entry into a named thread local stack.

Examples:

Begin entry into the stack.

Threaded.begin_execution(:create)

Parameters:

  • name (String)

    The name of the stack

Returns:

  • (true)

    True.



130
131
132
# File 'lib/mongoid/threaded.rb', line 130

def begin_execution(name)
  stack(name).push(true)
end

#begin_touch_merged(document) ⇒ Object

Mark that a document's touch updates have been merged into an atomic insert on the current thread.

Examples:

Begin touch merged.

Threaded.begin_touch_merged(doc)

Parameters:

  • document (Document)

    The embedded document being inserted.



219
220
221
# File 'lib/mongoid/threaded.rb', line 219

def begin_touch_merged(document)
  touch_merged_for(document.class).push(document._id)
end

#begin_validate(document) ⇒ Object

Begin validating a document on the current thread.

Examples:

Begin validation.

Threaded.begin_validate(doc)

Parameters:

  • document (Document)

    The document to validate.



208
209
210
# File 'lib/mongoid/threaded.rb', line 208

def begin_validate(document)
  validations_for(document.class).push(document._id)
end

#begin_without_default_scope(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Begin suppressing default scopes for given model on the current thread.

Examples:

Begin without default scope stack.

Threaded.begin_without_default_scope(klass)

Parameters:

  • klass (Class)

    The model to suppress default scoping on.



261
262
263
# File 'lib/mongoid/threaded.rb', line 261

def begin_without_default_scope(klass)
  stack(:without_default_scope).push(klass)
end

#clear_modified_documents(session) ⇒ Set<Mongoid::Document>

Clears the set of modified documents for the given session, and return the content of the set before the clearance.

Parameters:

  • session (Mongo::Session)

    Session for which the modified documents set should be cleared.

Returns:

  • (Set<Mongoid::Document>)

    Collection of modified documents before it was cleared.



517
518
519
# File 'lib/mongoid/threaded.rb', line 517

def clear_modified_documents(session)
  modified_documents.delete(session) || []
end

#clear_session(client: nil) ⇒ nil

Note:

For backward compatibility it is allowed to call this method without

Clear the cached session for this thread for a client.

specifying client parameter.

Parameters:

  • client (Mongo::Client | nil) (defaults to: nil)

    The client to clear the session for.

Returns:

  • (nil)


494
495
496
# File 'lib/mongoid/threaded.rb', line 494

def clear_session(client: nil)
  sessions.delete(client)&.end_session
end

#client_overrideString | Symbol

Get the global client override.

Examples:

Get the global client override.

Threaded.client_override

Returns:

  • (String | Symbol)

    The override.



283
284
285
# File 'lib/mongoid/threaded.rb', line 283

def client_override
  get(CLIENT_OVERRIDE_KEY)
end

#client_override=(name) ⇒ String | Symbol

Set the global client override.

Examples:

Set the global client override.

Threaded.client_override = :testing

Parameters:

  • name (String | Symbol)

    The global override name.

Returns:

  • (String | Symbol)

    The override.



295
296
297
# File 'lib/mongoid/threaded.rb', line 295

def client_override=(name)
  set(CLIENT_OVERRIDE_KEY, name)
end

#current_scope(klass = nil) ⇒ Criteria

Get the current Mongoid scope.

Examples:

Get the scope.

Threaded.current_scope(klass)
Threaded.current_scope

Parameters:

  • klass (Klass) (defaults to: nil)

    The class type of the scope.

Returns:



308
309
310
311
312
313
314
315
316
# File 'lib/mongoid/threaded.rb', line 308

def current_scope(klass = nil)
  current_scope = get(CURRENT_SCOPE_KEY)

  if klass && current_scope.respond_to?(:keys)
    current_scope[current_scope.keys.find { |k| k <= klass }]
  else
    current_scope
  end
end

#current_scope=(scope) ⇒ Criteria

Set the current Mongoid scope.

Examples:

Set the scope.

Threaded.current_scope = scope

Parameters:

  • scope (Criteria)

    The current scope.

Returns:



326
327
328
# File 'lib/mongoid/threaded.rb', line 326

def current_scope=(scope)
  set(CURRENT_SCOPE_KEY, scope)
end

#database_overrideString | Symbol

Get the global database override.

Examples:

Get the global database override.

Threaded.database_override

Returns:

  • (String | Symbol)

    The override.



140
141
142
# File 'lib/mongoid/threaded.rb', line 140

def database_override
  get(DATABASE_OVERRIDE_KEY)
end

#database_override=(name) ⇒ String | Symbol

Set the global database override.

Examples:

Set the global database override.

Threaded.database_override = :testing

Parameters:

  • name (String | Symbol)

    The global override name.

Returns:

  • (String | Symbol)

    The override.



152
153
154
# File 'lib/mongoid/threaded.rb', line 152

def database_override=(name)
  set(DATABASE_OVERRIDE_KEY, name)
end

#delete(key) ⇒ Object

Removes the named variable from local storage.

Parameters:

  • key (String | Symbol)

    the name of the variable to remove.



109
110
111
# File 'lib/mongoid/threaded.rb', line 109

def delete(key)
  storage.delete(key)
end

#execute_callbacks=(flag) ⇒ Object

Indicates whether document callbacks should be invoked by default for the current thread. Individual documents may further override the callback behavior, but this will be used for the default behavior.

Parameters:

  • flag (true | false)

    Whether or not document callbacks should be executed by default.



543
544
545
# File 'lib/mongoid/threaded.rb', line 543

def execute_callbacks=(flag)
  set(EXECUTE_CALLBACKS, flag)
end

#execute_callbacks?true | false

Queries whether document callbacks should be executed by default for the current thread.

Unless otherwise indicated (by #execute_callbacks=), this will return true.

Returns:

  • (true | false)

    Whether or not document callbacks should be executed by default.



529
530
531
532
533
534
535
# File 'lib/mongoid/threaded.rb', line 529

def execute_callbacks?
  if has?(EXECUTE_CALLBACKS)
    get(EXECUTE_CALLBACKS)
  else
    true
  end
end

#executing?(name) ⇒ true

Are in the middle of executing the named stack

Examples:

Are we in the stack execution?

Threaded.executing?(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (true)

    If the stack is being executed.



164
165
166
# File 'lib/mongoid/threaded.rb', line 164

def executing?(name)
  !stack(name).empty?
end

#exit_autosave(document) ⇒ Object

Exit autosaving a document on the current thread.

Examples:

Exit autosave.

Threaded.exit_autosave(doc)

Parameters:

  • document (Document)

    The document to autosave.



229
230
231
# File 'lib/mongoid/threaded.rb', line 229

def exit_autosave(document)
  autosaves_for(document.class).delete_one(document._id)
end

#exit_execution(name) ⇒ true

Exit from a named thread local stack.

Examples:

Exit from the stack.

Threaded.exit_execution(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (true)

    True.



176
177
178
# File 'lib/mongoid/threaded.rb', line 176

def exit_execution(name)
  stack(name).pop
end

#exit_touch_merged(document) ⇒ Object

Clear the touch-merged flag for a document on the current thread.

Examples:

Exit touch merged.

Threaded.exit_touch_merged(doc)

Parameters:

  • document (Document)

    The document to clear.



249
250
251
# File 'lib/mongoid/threaded.rb', line 249

def exit_touch_merged(document)
  touch_merged_for(document.class).delete_one(document._id)
end

#exit_validate(document) ⇒ Object

Exit validating a document on the current thread.

Examples:

Exit validation.

Threaded.exit_validate(doc)

Parameters:

  • document (Document)

    The document to validate.



239
240
241
# File 'lib/mongoid/threaded.rb', line 239

def exit_validate(document)
  validations_for(document.class).delete_one(document._id)
end

#exit_without_default_scope(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Exit suppressing default scopes for given model on the current thread.

Examples:

Exit without default scope stack.

Threaded.exit_without_default_scope(klass)

Parameters:

  • klass (Class)

    The model to unsuppress default scoping on.



273
274
275
# File 'lib/mongoid/threaded.rb', line 273

def exit_without_default_scope(klass)
  stack(:without_default_scope).delete(klass)
end

#get(key, &default) ⇒ Object | nil

Queries the thread- or fiber-local variable with the given name. If a block is given, and the variable does not already exist, the return value of the block will be set as the value of the variable before returning it.

It is very important that applications (and especially Mongoid) use this method instead of Thread#[], since Thread#[] is actually for fiber-local variables, and Mongoid uses Fibers as an implementation detail in some callbacks. Putting thread-local state in a fiber-local store will result in the state being invisible when relevant callbacks are run in a different fiber.

Affected callbacks are cascading callbacks on embedded children.

Parameters:

  • key (String | Symbol)

    the name of the variable to query

  • default (Proc)

    an optional block that must return the default (initial) value of this variable.

Returns:

  • (Object | nil)

    the value of the queried variable, or nil if it is not set and no default was given.



84
85
86
87
88
89
90
91
92
93
# File 'lib/mongoid/threaded.rb', line 84

def get(key, &default)
  result = storage[key]

  if result.nil? && default
    result = yield
    set(key, result)
  end

  result
end

#get_session(client: nil) ⇒ Mongo::Session | nil

Note:

For backward compatibility it is allowed to call this method without

Get the cached session for this thread for a client.

specifying client parameter.

Parameters:

  • client (Mongo::Client | nil) (defaults to: nil)

    The client to cache the session for.

Returns:

  • (Mongo::Session | nil)

    The session cached on this thread or nil.



482
483
484
# File 'lib/mongoid/threaded.rb', line 482

def get_session(client: nil)
  sessions[client]
end

#has?(key) ⇒ true | false

Queries the presence of a named variable in local storage.

Parameters:

  • key (String | Symbol)

    the name of the variable to query.

Returns:

  • (true | false)

    whether the given variable is present or not.



118
119
120
# File 'lib/mongoid/threaded.rb', line 118

def has?(key)
  storage.key?(key)
end

#modified_documentsHash<Mongo::Session, Set<Mongoid::Document>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the thread store of modified documents.

Returns:

  • (Hash<Mongo::Session, Set<Mongoid::Document>>)

    The modified documents indexed by session.



562
563
564
# File 'lib/mongoid/threaded.rb', line 562

def modified_documents
  get(MODIFIED_DOCUMENTS_KEY) { Hash.new { |h, k| h[k] = Set.new } }
end

#reset!Object

Resets the current thread- or fiber-local storage to its initial state. This is useful for making sure the state is clean when starting a new thread or fiber.

The value of Mongoid::Config.real_isolation_level is used to determine whether to reset the storage for the current thread or fiber.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mongoid/threaded.rb', line 53

def reset!
  case Config.real_isolation_level
  when :thread
    Thread.current.thread_variable_set(STORAGE_KEY, nil)
  when :fiber
    Fiber[STORAGE_KEY] = {}
    Fiber[STORAGE_OWNER_KEY] = Fiber.current.object_id
  else
    raise "Unknown isolation level: #{Config.real_isolation_level.inspect}"
  end
end

#sessionsHash<Integer, Set>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the thread store of sessions.

Returns:

  • (Hash<Integer, Set>)

    The sessions indexed by client object ID.



552
553
554
# File 'lib/mongoid/threaded.rb', line 552

def sessions
  get(SESSIONS_KEY) { {}.compare_by_identity }
end

#set(key, value) ⇒ Object

Sets a variable in local storage with the given name to the given value. See #get for a discussion of why this method is necessary, and why Thread#[]= should be avoided in cascading callbacks on embedded children.

Parameters:

  • key (String | Symbol)

    the name of the variable to set.

  • value (Object | nil)

    the value of the variable to set (or nil if you wish to unset the variable)



102
103
104
# File 'lib/mongoid/threaded.rb', line 102

def set(key, value)
  storage[key] = value
end

#set_current_scope(scope, klass) ⇒ Criteria

Set the current Mongoid scope. Safe for multi-model scope chaining.

Examples:

Set the scope.

Threaded.current_scope(scope, klass)

Parameters:

  • scope (Criteria)

    The current scope.

  • klass (Class)

    The current model class.

Returns:



339
340
341
342
343
344
345
346
# File 'lib/mongoid/threaded.rb', line 339

def set_current_scope(scope, klass)
  if scope.nil?
    unset_current_scope(klass)
  else
    current_scope = get(CURRENT_SCOPE_KEY) { {} }
    current_scope[klass] = scope
  end
end

#set_session(session, client: nil) ⇒ Object

Note:

For backward compatibility it is allowed to call this method without

Cache a session for this thread for a client.

specifying client parameter.

Parameters:

  • session (Mongo::Session)

    The session to save.

  • client (Mongo::Client | nil) (defaults to: nil)

    The client to cache the session for.



470
471
472
# File 'lib/mongoid/threaded.rb', line 470

def set_session(session, client: nil)
  sessions[client] = session
end

#stack(name) ⇒ Array

Get the named stack.

Examples:

Get a stack by name

Threaded.stack(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (Array)

    The stack.



188
189
190
# File 'lib/mongoid/threaded.rb', line 188

def stack(name)
  get(STACK_KEYS[name]) { [] }
end

#touch_mergedHash

Get all touch-merged tracking on the current thread.

Examples:

Get all touch-merged.

Threaded.touch_merged

Returns:

  • (Hash)

    The current touch-merged tracking hash.



447
448
449
# File 'lib/mongoid/threaded.rb', line 447

def touch_merged
  get(TOUCH_MERGED_KEY) { {} }
end

#touch_merged?(document) ⇒ true | false

Is the document flagged as having had its touch updates merged into an atomic insert?

Examples:

Is the document touch-merged?

Threaded.touch_merged?(doc)

Parameters:

  • document (Document)

    The document to check.

Returns:

  • (true | false)

    If the document's touch was merged.



393
394
395
# File 'lib/mongoid/threaded.rb', line 393

def touch_merged?(document)
  touch_merged_for(document.class).include?(document._id)
end

#touch_merged_for(klass) ⇒ Array

Get all touch-merged document IDs on the current thread for the class.

Examples:

Get all touch-merged.

Threaded.touch_merged_for(Sofa)

Parameters:

  • klass (Class)

    The class to check.

Returns:

  • (Array)

    The current touch-merged document IDs.



459
460
461
# File 'lib/mongoid/threaded.rb', line 459

def touch_merged_for(klass)
  touch_merged[klass] ||= []
end

#validated?(document) ⇒ true | false

Is the document validated on the current thread?

Examples:

Is the document validated?

Threaded.validated?(doc)

Parameters:

  • document (Document)

    The document to check.

Returns:

  • (true | false)

    If the document is validated.



380
381
382
# File 'lib/mongoid/threaded.rb', line 380

def validated?(document)
  validations_for(document.class).include?(document._id)
end

#validationsHash

Get all validations on the current thread.

Examples:

Get all validations.

Threaded.validations

Returns:

  • (Hash)

    The current validations.



413
414
415
# File 'lib/mongoid/threaded.rb', line 413

def validations
  get(VALIDATIONS_KEY) { {} }
end

#validations_for(klass) ⇒ Array

Get all validations on the current thread for the class.

Examples:

Get all validations.

Threaded.validations_for(Person)

Parameters:

  • klass (Class)

    The class to check.

Returns:

  • (Array)

    The current validations.



437
438
439
# File 'lib/mongoid/threaded.rb', line 437

def validations_for(klass)
  validations[klass] ||= []
end

#without_default_scope?(klass) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is the given klass' default scope suppressed on the current thread?

Examples:

Is the given klass' default scope suppressed?

Threaded.without_default_scope?(klass)

Parameters:

  • klass (Class)

    The model to check for default scope suppression.

Returns:



356
357
358
# File 'lib/mongoid/threaded.rb', line 356

def without_default_scope?(klass)
  stack(:without_default_scope).include?(klass)
end