模块:Mongoid::Threaded

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

Overview

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

在命名空间下定义

模块: 生命周期

常量摘要折叠

DATABASE_OVERRIDE_KEY =
'[mongoid]:db-override'
CLIENTS_KEY =

用于存储客户端的密钥常量。

'[mongoid]:clients'
CLIENT_OVERRIDE_KEY =

用于覆盖客户端的密钥。

'[mongoid]:client-override'
CURRENT_SCOPE_KEY =

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

'[mongoid]:current-scope'
AUTOSAVES_KEY =
'[mongoid]:autosaves'
VALIDATIONS_KEY =
'[mongoid]:validations'
STACK_KEYS =
哈希.new do |哈希, key|
  哈希[key] = " [mongoid]: #{ key } - 堆栈 "
end
SESSIONS_KEY =

当前线程会话的键。

'[mongoid]:sessions'
MODIFIED_DOCUMENTS_KEY =

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

'[mongoid]:modified-documents'
EXECUTE_CALLBACKS =

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

'[mongoid]:execute-callbacks'
BIND =
' bind '.冻结
ASSIGN =
' assign '.冻结
BUILD =
' 构建 '.冻结
LOAD =
' load '.冻结
CREATE =
' create '.冻结

实例方法摘要折叠

实例方法详细信息

#add_modified_document(session, document) ⇒ Object

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

参数:

  • 会话 ( Mongo::Session )

    修改文档的会话范围。

  • 文档 ( Mongoid::Document )

    已修改的 Mongoid文档。

[查看源代码]

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

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

  Modified_documents[会话] << 文档
end

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

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

例子:

文档是否自动保存?

Threaded.autosaved?(doc)

参数:

  • 文档 (文档)

    要检查的文档。

返回:

  • ( true | false )

    如果文档自动保存。

[查看源代码]

334
335
336
# File 'lib/mongoid/threaded.rb', 第334行

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

# autosaves哈希

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

例子:

获取所有自动保存。

Threaded.autosaves

返回:

  • (哈希)

    当前自动保存。

[查看源代码]

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

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

#autosaves_for(klass) ⇒ Array

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

例子:

获取所有自动保存。

Threaded.autosaves_for(Person)

参数:

  • klass ( class )

    要检查的类。

返回:

  • ( Array )

    当前自动保存。

[查看源代码]

378
379
380
# File 'lib/mongoid/threaded.rb', 第378行

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

#begin_autosave(document) ⇒ Object

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

例子:

Begin autosave.

Threaded.begin_autosave(doc)

参数:

  • 文档 (文档)

    要自动保存的文档。

[查看源代码]

185
186
187
# File 'lib/mongoid/threaded.rb', 第185行

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

# begin_execution (name) ⇒ true

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

例子:

开始堆栈。

Threaded.begin_execution(:create)

参数:

  • 名称 ( string )

    堆栈的名称

返回:

  • ( true )

    是的。

[查看源代码]

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

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

# begin_validate (文档) ⇒对象

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

例子:

Begin validation.

Threaded.begin_validate(doc)

参数:

  • 文档 (文档)

    要验证的文档。

[查看源代码]

195
196
197
# File 'lib/mongoid/threaded.rb', 第195行

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

# begin_ without_default_scope (klass) ⇒对象

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

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

例子:

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

Threaded.begin_without_default_scope(klass)

参数:

  • klass ( class )

    要抑制默认范围的模型。

[查看源代码]

227
228
229
# File 'lib/mongoid/threaded.rb', 第227行

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

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

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

参数:

  • 会话 ( Mongo::Session )

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

返回:

[查看源代码]

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

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

#clear_session(client: nil) ⇒ nil

注意:

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

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

指定“客户端”参数。

参数:

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

    要清除会话的客户端。

返回:

  • (nil)
[查看源代码]

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

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

# client_overridestring |符号

获取全局客户端覆盖。

例子:

获取全局客户端覆盖。

Threaded.client_override

返回:

  • ( string | Symbol )

    覆盖。

[查看源代码]

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

def client_override
  获取(CLIENT_OVERRIDE_KEY)
end

# client_override= (name) ⇒ String |符号

设置全局客户端覆盖。

例子:

设置全局客户端覆盖。

Threaded.client_override = :testing

参数:

  • 名称 ( string | Symbol )

    全局覆盖名称。

返回:

  • ( string | Symbol )

    覆盖。

[查看源代码]

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

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

# current_scope (klass = nil) ⇒条件

获取当前 Mongoid 作用域。

例子:

获取范围。

Threaded.current_scope(klass)
Threaded.current_scope

参数:

  • klass (Klass) (默认为: nil

    范围的类类型。

返回:

[查看源代码]

274
275
276
277
278
279
280
281
282
# File 'lib/mongoid/threaded.rb', 第274行

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 )

    当前范围。

返回:

[查看源代码]

292
293
294
# File 'lib/mongoid/threaded.rb', 第292行

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

# database_overridestring |符号

获取全局数据库覆盖。

例子:

获取全局数据库覆盖。

Threaded.database_override

返回:

  • ( string | Symbol )

    覆盖。

[查看源代码]

127
128
129
# File 'lib/mongoid/threaded.rb', 第127行

def database_override
  获取(DATABASE_OVERRIDE_KEY)
end

# database_override= (name) ⇒ string |符号

设置全局数据库覆盖。

例子:

设置全局数据库覆盖。

Threaded.database_override = :testing

参数:

  • 名称 ( string | Symbol )

    全局覆盖名称。

返回:

  • ( string | Symbol )

    覆盖。

[查看源代码]

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

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

# 删除 (key) ⇒ 对象

从线程局部存储中删除已命名的变量。

参数:

  • key ( string | Symbol )

    要删除的变量的名称。

[查看源代码]

84
85
86
# File 'lib/mongoid/threaded.rb', 第84行

def 删除(key)
  (key, nil)
end

# execute_callbacks= (flag) ⇒对象

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

参数:

  • flag ( true | false )

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

[查看源代码]

474
475
476
# File 'lib/mongoid/threaded.rb', 第474行

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

#execute_callbacks?true | false

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

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

返回:

  • ( true | false )

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

[查看源代码]

460
461
462
463
464
465
466
# File 'lib/mongoid/threaded.rb', 第460行

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

#正在执行? (name) ⇒ true

正在执行指定的堆栈

例子:

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

Threaded.executing?(:create)

参数:

  • 名称 (符号)

    堆栈的名称

返回:

  • ( true )

    如果正在执行堆栈。

[查看源代码]

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

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

# exit_autosave (document) ⇒对象

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

例子:

退出自动保存。

Threaded.exit_autosave(doc)

参数:

  • 文档 (文档)

    要自动保存的文档。

[查看源代码]

205
206
207
# File 'lib/mongoid/threaded.rb', 第205行

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

# exit_execution (name) ⇒ true

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

例子:

从堆栈中退出。

Threaded.exit_execution(:create)

参数:

  • 名称 (符号)

    堆栈的名称

返回:

  • ( true )

    是的。

[查看源代码]

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

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

# exit_validate (文档) ⇒对象

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

例子:

退出验证。

Threaded.exit_validate(doc)

参数:

  • 文档 (文档)

    要验证的文档。

[查看源代码]

215
216
217
# File 'lib/mongoid/threaded.rb', 第215行

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

# exit_Without_default_scope (klass) ⇒对象

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

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

例子:

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

Threaded.exit_without_default_scope(klass)

参数:

  • klass ( class )

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

[查看源代码]

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

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。

[查看源代码]

59
60
61
62
63
64
65
66
67
68
# File 'lib/mongoid/threaded.rb', 第59行

def 获取(key, 访问)
  结果 = 线程.Current.thread_variable_get(key)

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

  结果
end

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

注意:

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

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

指定“客户端”参数。

参数:

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

    要缓存会话的客户端。

返回:

  • ( Mongo::Session | nil )

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

[查看源代码]

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

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

# 有? (key) ⇒ true | false

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

参数:

  • key ( string | Symbol )

    要查询的变量名称。

返回:

  • ( true | false )

    给定变量是否存在。

[查看源代码]

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mongoid/threaded.rb', 第93行

def 有?(key)
  # 这里有一个 JRuby 行为与 MRI 不同的经典示例。 在
  # MRI,如果将线程变量设立为零,则会将其从列表中删除
  # 以及对thread_variable?(key) 的后续调用将返回 false。 不
  # JRuby 也是如此。 一旦设立,就无法取消设置该线程变量。
  #
  # 但是,由于将变量设置为 nil 应该将其删除,
  # 我们可以假设 nil 值变量实际上并不存在。

  # 所以,不要这样:
  # Thread.current.thread_variable?(key)

  # 我们必须这样做:
  !获取(key).nil?
end

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

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

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

返回:

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

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

[查看源代码]

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

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

#个会话Hash<Integer, Set>

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

返回会话的线程存储。

返回:

  • ( Hash<Integer, Set> )

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

[查看源代码]

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

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

# 设立 (key, value) ⇒ 对象

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

参数:

  • key ( string | Symbol )

    要设立的变量名称。

  • ( Object | nil )

    要设立的变量值(如果您希望取消设置变量,则为“nil”)

[查看源代码]

77
78
79
# File 'lib/mongoid/threaded.rb', 第77行

def (key, )
  线程.Current.thread_variable_set(key, )
end

# set_current_scope (scope, klass) ⇒ Criteria

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

例子:

设置范围。

Threaded.current_scope(scope, klass)

参数:

  • 范围 ( Criteria )

    当前范围。

  • klass ( class )

    当前模型类。

返回:

[查看源代码]

305
306
307
308
309
310
311
312
# File 'lib/mongoid/threaded.rb', 第305行

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) ⇒对象

注意:

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

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

指定“客户端”参数。

参数:

  • 会话 ( Mongo::Session )

    要保存的会话。

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

    要缓存会话的客户端。

[查看源代码]

401
402
403
# File 'lib/mongoid/threaded.rb', 第401行

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

# stack (name) ⇒数组

获取命名的堆栈。

例子:

按名称获取堆栈

Threaded.stack(:create)

参数:

  • 名称 (符号)

    堆栈的名称

返回:

  • ( Array )

    堆栈。

[查看源代码]

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

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

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

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

例子:

文档是否经过验证?

Threaded.validated?(doc)

参数:

  • 文档 (文档)

    要检查的文档。

返回:

  • ( true | false )

    如果文档已通过验证。

[查看源代码]

346
347
348
# File 'lib/mongoid/threaded.rb', 第346行

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

# validations哈希

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

例子:

获取所有验证。

Threaded.validations

返回:

  • (哈希)

    当前验证。

[查看源代码]

366
367
368
# File 'lib/mongoid/threaded.rb', 第366行

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

#validations_for(klass) ⇒ Array

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

例子:

获取所有验证。

Threaded.validations_for(Person)

参数:

  • klass ( class )

    要检查的类。

返回:

  • ( Array )

    当前验证。

[查看源代码]

390
391
392
# File 'lib/mongoid/threaded.rb', 第390行

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

# without_default_scope? (klass) ⇒布尔值

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

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

例子:

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

Threaded.without_default_scope?(klass)

参数:

  • klass ( class )

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

返回:

[查看源代码]

322
323
324
# File 'lib/mongoid/threaded.rb', 第322行

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