Class: Mongoid::Contextual::Mongo::DocumentsLoader Private
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Forwardable
|
|
Instance Chain:
|
|
Inherits: | Object |
Defined in: | lib/mongoid/contextual/mongo/documents_loader.rb |
Overview
Loads documents for the provided criteria.
Class Method Summary
-
.executor(name = Mongoid.async_query_executor) ⇒ Concurrent::ImmediateExecutor | Concurrent::ThreadPoolExecutor
Internal use only
Returns suitable executor according to
::Mongoid
config options. -
.global_thread_pool_async_query_executor ⇒ Concurrent::ThreadPoolExecutor
Internal use only
Returns asynchronous executor to be used when async_query_executor config option is set to
:global_thread_pool
. -
.immediate_executor ⇒ Concurrent::ImmediateExecutor
Internal use only
Returns synchronous executor to be used when async_query_executor config option is set to
:immediate
. -
.new(view, klass, criteria, executor: self.class.executor) ⇒ DocumentsLoader
constructor
Internal use only
Instantiates the document loader instance and immediately schedules its execution using the provided executor.
Instance Attribute Summary
- #criteria ⇒ Mongoid::Criteria rw Internal use only
-
#pending? ⇒ true | false
readonly
Internal use only
Returns false or true whether the loader is in pending state.
-
#started? ⇒ true | false
readonly
Internal use only
Returns false or true whether the loader is in started state.
::Mongoid::Association::EagerLoadable
- Included
#eager_loadable? | Indicates whether the criteria has association inclusions which should be eager loaded. |
Instance Method Summary
-
#execute ⇒ Array<Mongoid::Document>
Internal use only
Loads records specified by
@criteria
from the database, and convert them to::Mongoid
documents of@klass
type. -
#unschedule
Internal use only
Mark the loader as unscheduled.
-
#start ⇒ true | false
private
Internal use only
Mark the loader as started if possible.
::Mongoid::Association::EagerLoadable
- Included
#eager_load | Load the associations for the given documents. |
#preload | Load the associations for the given documents. |
Class Method Details
.executor(name = Mongoid.async_query_executor) ⇒ Concurrent::ImmediateExecutor
| Concurrent::ThreadPoolExecutor
Returns suitable executor according to ::Mongoid
config options.
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 61
def self.executor(name = Mongoid.async_query_executor) case name.to_sym when :immediate immediate_executor when :global_thread_pool global_thread_pool_async_query_executor else raise Errors::InvalidQueryExecutor.new(name) end end
.global_thread_pool_async_query_executor ⇒ Concurrent::ThreadPoolExecutor
Returns asynchronous executor to be used when async_query_executor config option is set to :global_thread_pool
. This executor runs operations on background threads using a thread pool.
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 32
def self.global_thread_pool_async_query_executor create_pool = Proc.new do |concurrency| Concurrent::ThreadPoolExecutor.new( min_threads: 0, max_threads: concurrency, max_queue: concurrency * 4, fallback_policy: :caller_runs ) end concurrency = Mongoid.global_executor_concurrency || 4 @@global_thread_pool_async_query_executor ||= create_pool.call(concurrency) if @@global_thread_pool_async_query_executor.max_length != concurrency old_pool = @@global_thread_pool_async_query_executor @@global_thread_pool_async_query_executor = create_pool.call(concurrency) old_pool.shutdown end @@global_thread_pool_async_query_executor end
.immediate_executor ⇒ Concurrent::ImmediateExecutor
Returns synchronous executor to be used when async_query_executor config option is set to :immediate
. This executor runs all operations on the current thread, blocking as necessary.
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 22
def self.immediate_executor @@immediate_executor ||= Concurrent::ImmediateExecutor.new end
Instance Attribute Details
#criteria ⇒ Mongoid::Criteria (rw)
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 75
attr_accessor :criteria
#pending? ⇒ true
| false
(readonly)
Returns false or true whether the loader is in pending state.
Pending state means that the loader execution has been scheduled, but has not been started yet.
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 107
def pending? @mutex.synchronize do @state == :pending end end
#started? ⇒ true
| false
(readonly)
Returns false or true whether the loader is in started state.
Started state means that the loader execution has been started. Note that the loader stays in this state even after the execution completed (successfully or failed).
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 121
def started? @mutex.synchronize do @state == :started end end
Instance Method Details
#execute ⇒ Array<Mongoid::Document>
Loads records specified by @criteria
from the database, and convert them to ::Mongoid
documents of @klass
type.
This method is called by the task (possibly asynchronous) scheduled when creating an instance of the loader. However, this method can be called directly, if it is desired to execute loading on the caller thread immediately.
Calling this method does not change the state of the loader.
#start ⇒ true
| false
(private)
Mark the loader as started if possible.
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 165
def start @mutex.synchronize do if @state == :pending @state = :started true else false end end end
#unschedule
Mark the loader as unscheduled.
If the loader is marked unscheduled, it will not be executed. The only option to load the documents is to call #execute method directly.
Please note that if execution of a task has been already started, unscheduling does not have any effect.
# File 'lib/mongoid/contextual/mongo/documents_loader.rb', line 134
def unschedule @mutex.synchronize do @state = :cancelled unless @state == :started end end