模块:Mongoid::Threaded

扩展方式:
线程化
包含在:
线程化
定义于:
lib/mongoid/threaded.rb ,
lib/mongoid/threaded/lifecycle.rb

Overview

此模块包含可轻松访问在当前线程上具有生命周期的对象的逻辑。

在命名空间下定义

模块: 生命周期

常量摘要折叠

STORAGE_KEY =

共享线程和纤程本地存储的键。它必须是一个符号,因为纤程本地存储的键必须是符号。

:'[mongoid]'
STORAGE_OWNER_KEY =

追踪哪个纤程拥有存储哈希,以检测纤程何时从父纤程继承(而不是创建)其存储。

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

用于覆盖客户端的密钥。

'客户端覆盖'
CURRENT_SCOPE_KEY =

当前线程作用域堆栈的键。

'current-scope'
AUTOSAVES_KEY =
'自动保存'
VALIDATIONS_KEY =
'验证'
TOUCH_MERGED_KEY =
'touch-merged'
STACK_KEYS =
哈希.new do |哈希, key|
  哈希[key] = "#{key}-堆栈"
end
SESSIONS_KEY =

当前线程会话的键。

'会话'
MODIFIED_DOCUMENTS_KEY =

用于存储在事务内部修改的文档的密钥。

'Modified-Documents'
EXECUTE_CALLBACKS =

该键存储是否对文档执行回调的默认值。

'执行回调'
BIND =
'bind'
ASSIGN =
'分配'
BUILD =
'构建'
LOAD =
'负载'
CREATE =
'创建'

实例方法摘要折叠

实例方法详细信息

#add_modified_document(session, document) ⇒ Object

存储对在与会话关联的ACID 事务中修改的文档的引用。

参数:

  • 会话 ( Mongo::Session )

    修改文档的会话范围。

  • 文档 ( Mongoid::Document )

    已修改的 Mongoid文档。



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

def add_modified_document(会话, 文档)
  return 除非 会话&。in_transaction?

  Modified_documents[会话] << 文档
end

#自动保存? (文档) ⇒ true | false

文档是否在当前线程上自动保存?

例子:

文档是否自动保存?

Threaded.autosaved?(doc)

参数:

  • 文档 (文档)

    要检查的文档。

返回:

  • ( true | false )

    如果文档自动保存。



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

def 自动保存?(文档)
  autosaves_for(文档.class).包括?(文档._id)
end

# autosaves哈希

获取当前线程上的所有自动保存。

例子:

获取所有自动保存。

Threaded.autosaves

返回:

  • (哈希)

    当前自动保存。



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

def 自动保存
  获取(AUTOSAVES_KEY) { {} }
end

#autosaves_for(klass) ⇒ Array

获取该类的当前线程上的所有自动保存。

例子:

获取所有自动保存。

Threaded.autosaves_for(Person)

参数:

  • klass ( class )

    要检查的类。

返回:

  • ( Array )

    当前自动保存。



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

def autosaves_for(klass)
  自动保存[klass] ||= []
end

#begin_autosave(document) ⇒ Object

开始在当前线程上自动保存文档。

例子:

Begin autosave.

Threaded.begin_autosave(doc)

参数:

  • 文档 (文档)

    要自动保存的文档。



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

def begin_autosave(文档)
  autosaves_for(文档.class).推动(文档._id)
end

# begin_execution (name) ⇒ true

开始进入已命名的线程本地堆栈。

例子:

开始堆栈。

Threaded.begin_execution(:create)

参数:

  • 名称 ( string )

    堆栈的名称

返回:

  • ( true )

    是的。



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

def begin_execution(名称)
  堆栈(名称).推动(true)
end

#begin_touch_merged(document) ⇒ 对象

标记 document 的接触更新已合并到当前线程上的原子插入中。

例子:

开始触摸合并。

Threaded.begin_touch_merged(doc)

参数:

  • 文档 (文档)

    正在插入的嵌入式文档。



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

def begin_touch_merged(文档)
  touch_merged_for(文档.class).推动(文档._id)
end

# begin_validate (文档) ⇒对象

开始在当前线程上验证文档。

例子:

Begin validation.

Threaded.begin_validate(doc)

参数:

  • 文档 (文档)

    要验证的文档。



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

def begin_validate(文档)
  validations_for(文档.class).推动(文档._id)
end

# begin_ without_default_scope (klass) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

开始在当前线程上抑制给定模型的默认作用域。

例子:

开始时没有默认作用域堆栈。

Threaded.begin_without_default_scope(klass)

参数:

  • klass ( class )

    要抑制默认范围的模型。



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

def begin_ without_default_scope(klass)
  堆栈(: without_default_scope).推动(klass)
end

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

清除给设立会话的已修改文档集,并返回清除之前的文档设立内容。

参数:

  • 会话 ( Mongo::Session )

    应清除已修改文档设立的会话。

返回:



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

def clear_modified_documents(会话)
  Modified_documents.删除(会话) || []
end

#clear_session(client: nil) ⇒ nil

注意:

为了向后兼容,允许调用此方法而无需

清除客户端为此线程缓存的会话。

指定 client 参数。

参数:

  • 客户端 ( Mongo::Client | nil ) (默认为: nil

    要清除会话的客户端。

返回:

  • (nil)


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

def clear_session(客户端: nil)
  会话.删除(客户端)&。end_session
end

# client_overridestring |符号

获取全局客户端覆盖。

例子:

获取全局客户端覆盖。

Threaded.client_override

返回:

  • ( string | Symbol )

    覆盖。



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

def client_override
  获取(CLIENT_OVERRIDE_KEY)
end

# client_override= (name) ⇒ String |符号

设置全局客户端覆盖。

例子:

设置全局客户端覆盖。

Threaded.client_override = :testing

参数:

  • 名称 ( string | Symbol )

    全局覆盖名称。

返回:

  • ( string | Symbol )

    覆盖。



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

def client_override=(名称)
  (CLIENT_OVERRIDE_KEY, 名称)
end

# current_scope (klass = nil) ⇒条件

获取当前 Mongoid 作用域。

例子:

获取范围。

Threaded.current_scope(klass)
Threaded.current_scope

参数:

  • klass (Klass) (默认为: nil

    范围的类类型。

返回:



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

def current_scope(klass = nil)
  current_scope = 获取(CURRENT_SCOPE_KEY)

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

# current_scope= (scope) ⇒ Criteria

设置当前 Mongoid 作用域。

例子:

设置范围。

Threaded.current_scope = scope

参数:

  • 范围 ( Criteria )

    当前范围。

返回:



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

def current_scope=(范围)
  (CURRENT_SCOPE_KEY, 范围)
end

# database_overridestring |符号

获取全局数据库覆盖。

例子:

获取全局数据库覆盖。

Threaded.database_override

返回:

  • ( string | Symbol )

    覆盖。



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

def database_override
  获取(DATABASE_OVERRIDE_KEY)
end

# database_override= (name) ⇒ string |符号

设置全局数据库覆盖。

例子:

设置全局数据库覆盖。

Threaded.database_override = :testing

参数:

  • 名称 ( string | Symbol )

    全局覆盖名称。

返回:

  • ( string | Symbol )

    覆盖。



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

def database_override=(名称)
  (DATABASE_OVERRIDE_KEY, 名称)
end

# 删除 (key) ⇒ 对象

从本地存储中删除已命名的变量。

参数:

  • key ( string | Symbol )

    要删除的变量的名称。



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

def 删除(key)
  存储空间:.删除(key)
end

# execute_callbacks= (flag) ⇒对象

指示默认情况下是否应为当前线程调用文档回调。 单个文档可以进一步覆盖回调行为,但这将用于默认行为。

参数:

  • flag ( true | false )

    默认是否应执行文档回调。



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

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

#execute_callbacks?true | false

查询是否应默认为当前线程执行文档回调。

除非另有说明(通过 #execute_callbacks=),否则返回 true。

返回:

  • ( true | false )

    默认是否应执行文档回调。



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

def execute_callbacks?
  if 有?(EXECUTE_CALLBACKS)
    获取(EXECUTE_CALLBACKS)
  else
    true
  end
end

#正在执行? (name) ⇒ true

正在执行指定的堆栈

例子:

我们是否处于堆栈执行中?

Threaded.executing?(:create)

参数:

  • 名称 (符号)

    堆栈的名称

返回:

  • ( true )

    如果正在执行堆栈。



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

def 正在执行?(名称)
  !堆栈(名称).空?
end

# exit_autosave (document) ⇒对象

退出当前线程上的文档自动保存功能。

例子:

退出自动保存。

Threaded.exit_autosave(doc)

参数:

  • 文档 (文档)

    要自动保存的文档。



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

def exit_autosave(文档)
  autosaves_for(文档.class).delete_one(文档._id)
end

# exit_execution (name) ⇒ true

从已命名的线程本地堆栈中退出。

例子:

从堆栈中退出。

Threaded.exit_execution(:create)

参数:

  • 名称 (符号)

    堆栈的名称

返回:

  • ( true )

    是的。



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

def exit_execution(名称)
  堆栈(名称).Pop
end

#exit_touch_merged(document) ⇒ 对象

清除当前线程上 document 的 touch-merged 标志。

例子:

退出触摸合并。

Threaded.exit_touch_merged(doc)

参数:

  • 文档 (文档)

    要清除的document。



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

def exit_touch_merged(文档)
  touch_merged_for(文档.class).delete_one(文档._id)
end

# exit_validate (文档) ⇒对象

退出验证当前线程上的文档。

例子:

退出验证。

Threaded.exit_validate(doc)

参数:

  • 文档 (文档)

    要验证的文档。



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

def exit_validate(文档)
  validations_for(文档.class).delete_one(文档._id)
end

# exit_Without_default_scope (klass) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

退出,抑制当前线程上给定模型的默认作用域。

例子:

退出时不使用默认作用域堆栈。

Threaded.exit_without_default_scope(klass)

参数:

  • klass ( class )

    要取消抑制默认范围的模型。



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

def exit_Without_default_scope(klass)
  堆栈(: without_default_scope).删除(klass)
end

# get (key, &default) ⇒ 对象 | nil

查询具有给定名称的线程或纤程局部变量。如果给定了一个区块,并且该变量尚不存在,则在返回该变量之前,该区块的返回值将被设立为该变量的值。

应用程序(尤其是 Mongoid)使用此方法而不是 Thread#[] 非常重要,因为 Thread#[] 实际上用于纤程局部变量,而 Mongoid 在某些回调中使用纤程作为实施细节。将线程本地状态放入纤程本地存储中将导致当相关回调在不同纤程中运行时,状态不可见。

受影响的回调是针对嵌入式子项的级联回调。

参数:

  • key ( string | Symbol )

    要查询的变量名称

  • 访问 ( Proc )

    一个可选区块,必须返回该变量的默认(初始)值。

返回:

  • ( Object | nil )

    所查询变量的值,如果未设立且未给出默认值,则为 nil。



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

def 获取(key, 访问)
  结果 = 存储空间:[key]

  if 结果.nil? && 访问
    结果 = 产量
    (key, 结果)
  end

  结果
end

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

注意:

为了向后兼容,允许调用此方法而无需

获取客户端此线程的缓存会话。

指定 client 参数。

参数:

  • 客户端 ( Mongo::Client | nil ) (默认为: nil

    要缓存会话的客户端。

返回:

  • ( Mongo::Session | nil )

    在此线程上缓存的会话,或者为零。



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

def get_session(客户端: nil)
  会话[客户端]
end

# 有? (key) ⇒ true | false

查询本地存储中是否存在命名变量。

参数:

  • key ( string | Symbol )

    要查询的变量名称。

返回:

  • ( true | false )

    给定变量是否存在。



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

def 有?(key)
  存储空间:.键?(key)
end

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

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回已修改文档的线程存储。

返回:

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

    按会话索引的已修改文档。



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

def Modified_documents
  获取(MODIFIED_DOCUMENTS_KEY) { 哈希.new { |h, k| h[k] = .new } }
end

# 重置!对象

将当前线程或纤程本地存储重置为其初始状态。这对于在启动新线程或纤程时确保状态干净非常有用。

Mongoid::Config.real_isolation_level 的值用于确定是否重置当前线程或纤程的存储。



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

def reset!
  案例 配置.real_isolation_level
  when :thread
    线程.Current.thread_variable_set(STORAGE_KEY, nil)
  when :Fiber
    光纤[STORAGE_KEY] = {}
    光纤[STORAGE_OWNER_KEY] = 光纤.Current.object_id
  else
    提高 " 隔离性 : #{Config.real_isolation_level.inspect} "
  end
end

#个会话Hash<Integer, Set>

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回会话的线程存储。

返回:

  • ( Hash<Integer, Set> )

    按客户端对象ID进行索引的会话。



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

def 会话
  获取(SESSIONS_KEY) { {}.compare_by_identity }
end

# 设立 (key, value) ⇒ 对象

将本地存储中具有给定名称的变量设置为给定值。有关为什么需要此方法的讨论,以及为什么在嵌入式子项的级联回调中应避免使用 Thread#[]=,请参阅 #get。

参数:

  • key ( string | Symbol )

    要设立的变量名称。

  • ( Object | nil )

    要设立的变量值(如果要取消设置变量,则为 nil



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

def (key, )
  存储空间:[key] = 
end

# set_current_scope (scope, klass) ⇒ Criteria

设置当前 Mongoid 作用域。 可安全用于多模型作用域链。

例子:

设置范围。

Threaded.current_scope(scope, klass)

参数:

  • 范围 ( Criteria )

    当前范围。

  • klass ( class )

    当前模型类。

返回:



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

def set_current_scope(范围, klass)
  if 范围.nil?
    unset_current_scope(klass)
  else
    current_scope = 获取(CURRENT_SCOPE_KEY) { {} }
    current_scope[klass] = 范围
  end
end

# set_session (session, 客户端: nil) ⇒对象

注意:

为了向后兼容,允许调用此方法而无需

为客户端的该线程缓存会话。

指定 client 参数。

参数:

  • 会话 ( Mongo::Session )

    要保存的会话。

  • 客户端 ( Mongo::Client | nil ) (默认为: nil

    要缓存会话的客户端。



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

def set_session(会话, 客户端: nil)
  会话[客户端] = 会话
end

# stack (name) ⇒数组

获取命名的堆栈。

例子:

按名称获取堆栈

Threaded.stack(:create)

参数:

  • 名称 (符号)

    堆栈的名称

返回:

  • ( Array )

    堆栈。



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

def 堆栈(名称)
  获取(STACK_KEYS[名称]) { [] }
end

# touch_merged哈希

GET 所有触摸合并跟踪。

例子:

获取所有触摸合并。

Threaded.touch_merged

返回:

  • (哈希)

    当前接触合并的跟踪哈希值。



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

def touch_merged
  获取(TOUCH_MERGED_KEY) { {} }
end

#touch_merged?(document) ⇒ true | false

Is the document flagged as having had its touch 更新s merged into an atomic insert?

例子:

Is the document touch-merged?

Threaded.touch_merged?(doc)

参数:

  • 文档 (文档)

    要检查的文档。

返回:

  • ( true | false )

    如果 document 的触摸已合并。



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

def touch_merged?(文档)
  touch_merged_for(文档.class).包括?(文档._id)
end

# touch_merged_for (klass) ⇒ 数组

获取该类的当前线程上的所有触摸合并document ID。

例子:

获取所有触摸合并。

Threaded.touch_merged_for(Sofa)

参数:

  • klass ( class )

    要检查的类。

返回:

  • ( Array )

    当前的触摸合并document ID。



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

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

#已验证? (document) ⇒ true | false

文档是否在当前线程上进行了验证?

例子:

文档是否经过验证?

Threaded.validated?(doc)

参数:

  • 文档 (文档)

    要检查的文档。

返回:

  • ( true | false )

    如果文档已通过验证。



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

def 已验证?(文档)
  validations_for(文档.class).包括?(文档._id)
end

# validations哈希

获取当前线程上的所有验证。

例子:

获取所有验证。

Threaded.validations

返回:

  • (哈希)

    当前验证。



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

def 验证
  获取(VALIDATIONS_KEY) { {} }
end

#validations_for(klass) ⇒ Array

获取类的当前线程上的所有验证。

例子:

获取所有验证。

Threaded.validations_for(Person)

参数:

  • klass ( class )

    要检查的类。

返回:

  • ( Array )

    当前验证。



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

def validations_for(klass)
  验证[klass] ||= []
end

# without_default_scope? (klass) ⇒布尔值

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

是否在当前线程上抑制给定 klass 的默认作用域?

例子:

给定类的默认作用域是否被抑制?

Threaded.without_default_scope?(klass)

参数:

  • klass ( class )

    要检查默认范围抑制的模型。

返回:



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

def without_default_scope?(klass)
  堆栈(: without_default_scope).包括?(klass)
end