모듈: Mongoid::Association::EagerLoadable
- 다음에 정의됨:
- lib/mongoid/association/eager_loadable.rb
개요
이 모듈은 기준에 대한 선행 로딩 동작을 정의합니다.
인스턴스 메서드 요약 접기
- #create_pipeline(current_assoc, mapping) ⇒ Object
-
#Huge_load(Docs) ⇒ Array<Mongoid::Document>
지정된 문서에 대한 연관 관계를 로드합니다.
-
#eager_load_with_lookup ⇒ Array<Mongoid::Document>
Load the associations for the given documents using $lookup.
-
#Huge_loadable? ⇒ true | false
기준에 즉시 로드해야 하는 연관 포함이 있는지 여부를 나타냅니다.
-
#사전 로드(associations, Docs) ⇒ 객체
지정된 문서에 대한 연관 관계를 로드합니다.
-
#preload_for_lookup(criteria) ⇒ Object
지정된 문서에 대한 연관 관계를 로드합니다.
- #switch_local_and_foreign_fields?(association) ⇒ Boolean
인스턴스 메서드 세부 정보
#create_pipeline(current_assoc, mapping) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# 파일 'lib/mongoid/association/eager_loadable.rb', 줄 147 def create_pipeline(current_assoc, 매핑) # Build nested pipeline for children and ordering pipeline_stages = [] # For belongs_to and has_and_belongs_to_many, the foreign key is on the current document # For has_many/has_one, the foreign key is on the related document 만약 switch_local_and_foreign_fields?(current_assoc) local_field = current_assoc.foreign_key foreign_field = current_assoc.Primary_key other local_field = current_assoc.Primary_key foreign_field = current_assoc.foreign_key end # Build the 'as' field with embedded path prefix if needed as_field = current_assoc.이름.to_s 단계 = { '$lookup' => { 'from' => current_assoc.class.컬렉션.이름, 'localField' => local_field, 'foreignField' => foreign_field, 'as' => as_field } } # Add ordering if defined on the association, or default to _id for consistent order 만약 current_assoc.주문 sort_spec = current_assoc.주문.is_a?(해시) ? current_assoc.주문 : { current_assoc.주문 => 1 } pipeline_stages << { '$sort' => sort_spec } other # Default to sorting by _id to maintain insertion order consistency pipeline_stages << { '$sort' => { '_id' => 1 } } end # Add nested lookups for child associations # Child associations don't need the embedded_path prefix since they're referenced from the looked-up document # Remove this class from the mapping to prevent infinite loops with circular references class_name = current_assoc.class.to_s 만약 child_assocs = 매핑.삭제(class_name) child_assocs.각 do |자식| pipeline_stages << create_pipeline(자식, 매핑) end end # Always add pipeline since we always have at least $sort 단계['$lookup']['pipeline'] = pipeline_stages 단계 end |
#Huge_load(Docs) ⇒ Array<Mongoid::Document>
지정된 문서에 대한 연관 관계를 로드합니다.
22 23 24 25 26 |
# 파일 'lib/mongoid/association/eager_loadable.rb', 줄 22 def Eager_load(참고자료) 참고자료.탭 do |d| 사전 로드(기준.포함, d) 만약 Eager_loadable? end end |
#eager_load_with_lookup ⇒ Array<Mongoid::Document>
Load the associations for the given documents using $lookup.
If any of the associated collections reside in a different cluster than the root class, falls back to the #includes behavior and logs a warning.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 파일 'lib/mongoid/association/eager_loadable.rb', 줄 34 def Eager_load_with_lookup 범죄자 = cross_cluster_inclusions 만약 범죄자.어떤? root_client = class.client_name offender_list = 범죄자.map { |a| "#{a.name} (#{a.klass.client_name})" }.가입(', ') 몽고이드.로거.경고( 'eager_load cannot use $lookup aggregation because the following associations ' \ "reside in a different cluster than #{klass} (client: #{root_client}): " \ "#{offender_list}. Falling back to #includes behavior." ) return eager_load(docs_for_lookup_fallback) end preload_for_lookup(criteria) end |
#Huge_loadable? ⇒ true | false
기준에 즉시 로드해야 하는 연관 포함이 있는지 여부를 나타냅니다.
13 14 15 |
# 파일 'lib/mongoid/association/eager_loadable.rb', 줄 13 def Eager_loadable? !기준.포함.비어 있나요? end |
#사전 로드(associations, Docs) ⇒ 객체
Load the associations for the given documents. This will be done recursively to load the associations of the given documents' associated documents.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 파일 'lib/mongoid/association/eager_loadable.rb', 줄 57 def 사전 로드(연관 관계, 참고자료) assoc_map = 연관 관계.group_by(&:inverse_class_name) docs_map = {} 대기열 = [ class.to_s ] # 단일 컬렉션 상속을 위한 계정 대기열.push(class.root_class.to_s) 만약 class != class.root_class 동안 class = 대기열.shift 다음 하지 않는 한 방식 = assoc_map.삭제(class) 방식.각 do |assoc| 대기열 << assoc.class_name # 이 클래스가 포함 트리에 중첩된 경우 문서만 로드합니다. # 위의 연관 관계에 대해. 상위 연관 관계가 없는 경우, # 이 메서드에 전달된 문서의 문서를 포함합니다. ds = 참고자료 ds = assoc.parent_inclusions.map { |p| docs_map[p].to_a }.flatten 만약 assoc.parent_inclusions.분량 > 0 res = assoc.관계.Eager_loader([ assoc ], ds).실행 docs_map[assoc.이름] ||= [].to_set docs_map[assoc.이름].merge(res) end end end |
#preload_for_lookup(criteria) ⇒ Object
Load the associations for the given documents. This will be done recursively to load the associations of the given documents' associated documents.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# 파일 'lib/mongoid/association/eager_loadable.rb', 줄 92 def preload_for_lookup(기준) assoc_map = 기준.포함.group_by(&:inverse_class_name) # match first 파이프라인 = 기준.선택기.to_pipeline # then sort, skip, limit 파이프라인.concat(기준..to_pipeline_for_lookup) # 단일 컬렉션 상속을 위한 계정 root_class = class.root_class 만약 assoc_map[class.to_s] assoc_map[class.to_s].각 do |assoc| # Create a copy of the mapping for each top-level association to avoid mutation issues 파이프라인 << create_pipeline(assoc, assoc_map.dup) end end 만약 class != root_class && assoc_map[root_class.to_s] assoc_map[root_class.to_s].각 do |assoc| # Create a copy of the mapping for each top-level association to avoid mutation issues 파이프라인 << create_pipeline(assoc, assoc_map.dup) end end 열망.신규(기준.포함, [], true, 파이프라인).실행 end |
#switch_local_and_foreign_fields?(association) ⇒ Boolean
142 143 144 145 |
# 파일 'lib/mongoid/association/eager_loadable.rb', 줄 142 def switch_local_and_foreign_fields?(연관 관계) 연관 관계.is_a?(몽고이드::연관 관계::참조됨::Belongs To) || 연관 관계.is_a?(몽고이드::연관 관계::참조됨::Has And Belongs To Many) end |