ar_lazy_preload 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ar_lazy_preload/active_record/association_relation.rb +10 -5
- data/lib/ar_lazy_preload/associated_context_builder.rb +13 -8
- data/lib/ar_lazy_preload/association_tree_builder.rb +2 -6
- data/lib/ar_lazy_preload/context.rb +12 -65
- data/lib/ar_lazy_preload/contexts/auto_preload_context.rb +14 -0
- data/lib/ar_lazy_preload/contexts/base_context.rb +61 -0
- data/lib/ar_lazy_preload/contexts/lazy_preload_context.rb +33 -0
- data/lib/ar_lazy_preload/version.rb +1 -1
- metadata +33 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a265f11a7c21888ebe4ac60ccce4ab42bf56ff8527217fa8946013df0c2b0324
|
4
|
+
data.tar.gz: ce5ed84890259cff5d994a6618fc8017af7e46dc6acb974ef44ec5cc183e494f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e7220676078a6a8fb0ed8b93e7eeb81260d20ce692177466dfa97e6ee8915fd56bd524fbac81b039f48ef6ba43023d9838235d6540f0518de73053395ca7ae1
|
7
|
+
data.tar.gz: 35280832594c531085e7d51047282852615029d4b019532cf00e098c069f85f98a4bfa9cc85f54866baa039c9124346420aa989919161a465d53dc2a873db473
|
@@ -6,16 +6,21 @@ module ArLazyPreload
|
|
6
6
|
module AssociationRelation
|
7
7
|
def initialize(*args)
|
8
8
|
super(*args)
|
9
|
+
setup_preloading_context unless ArLazyPreload.config.auto_preload?
|
10
|
+
end
|
11
|
+
|
12
|
+
delegate :owner, :reflection, to: :proxy_association
|
13
|
+
delegate :lazy_preload_context, to: :owner
|
9
14
|
|
10
|
-
|
11
|
-
return if context.nil?
|
15
|
+
private
|
12
16
|
|
13
|
-
|
17
|
+
def setup_preloading_context
|
18
|
+
return if lazy_preload_context.nil?
|
19
|
+
|
20
|
+
association_tree_builder = AssociationTreeBuilder.new(lazy_preload_context.association_tree)
|
14
21
|
subtree = association_tree_builder.subtree_for(reflection.name)
|
15
22
|
|
16
23
|
lazy_preload!(subtree)
|
17
24
|
end
|
18
|
-
|
19
|
-
delegate :owner, :reflection, to: :proxy_association
|
20
25
|
end
|
21
26
|
end
|
@@ -24,11 +24,13 @@ module ArLazyPreload
|
|
24
24
|
# Takes all the associated records for the records, attached to the :parent_context and creates
|
25
25
|
# a preloading context for them
|
26
26
|
def perform
|
27
|
-
|
27
|
+
associated_records = parent_context.records.flat_map do |record|
|
28
|
+
next if record.nil?
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
record_association = record.public_send(association_name)
|
31
|
+
reflection = reflection_cache[record.class]
|
32
|
+
reflection.collection? ? record_association.target : record_association
|
33
|
+
end
|
32
34
|
|
33
35
|
Context.register(records: associated_records, association_tree: child_association_tree)
|
34
36
|
end
|
@@ -36,13 +38,16 @@ module ArLazyPreload
|
|
36
38
|
private
|
37
39
|
|
38
40
|
def child_association_tree
|
41
|
+
# `association_tree` is unnecessary when auto preload is enabled
|
42
|
+
return nil if ArLazyPreload.config.auto_preload?
|
43
|
+
|
39
44
|
AssociationTreeBuilder.new(parent_context.association_tree).subtree_for(association_name)
|
40
45
|
end
|
41
46
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
47
|
+
def reflection_cache
|
48
|
+
@reflection_cache ||= Hash.new do |hash, klass|
|
49
|
+
hash[klass] = klass.reflect_on_association(association_name)
|
50
|
+
end
|
46
51
|
end
|
47
52
|
end
|
48
53
|
end
|
@@ -8,9 +8,6 @@ module ArLazyPreload
|
|
8
8
|
attr_reader :association_tree
|
9
9
|
|
10
10
|
def initialize(association_tree)
|
11
|
-
# Since `association_tree` can be an array or a single hash
|
12
|
-
# Converting it to an array is easier for processing
|
13
|
-
# like jquery
|
14
11
|
@association_tree =
|
15
12
|
case association_tree
|
16
13
|
when Array
|
@@ -18,8 +15,7 @@ module ArLazyPreload
|
|
18
15
|
when Hash
|
19
16
|
[association_tree]
|
20
17
|
else
|
21
|
-
raise
|
22
|
-
"unexpected association_tree with class #{association_tree.class}"
|
18
|
+
raise ArgumentError, "unexpected association_tree with class #{association_tree.class}"
|
23
19
|
end.select { |node| node.is_a?(Hash) }
|
24
20
|
end
|
25
21
|
|
@@ -31,7 +27,7 @@ module ArLazyPreload
|
|
31
27
|
|
32
28
|
def subtree_cache
|
33
29
|
@subtree_cache ||= Hash.new do |hash, association|
|
34
|
-
hash[association] = association_tree.
|
30
|
+
hash[association] = association_tree.flat_map { |node| node[association] }
|
35
31
|
end
|
36
32
|
end
|
37
33
|
end
|
@@ -1,76 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
4
|
-
require "ar_lazy_preload/
|
3
|
+
require "ar_lazy_preload/contexts/base_context"
|
4
|
+
require "ar_lazy_preload/contexts/auto_preload_context"
|
5
|
+
require "ar_lazy_preload/contexts/lazy_preload_context"
|
5
6
|
|
6
7
|
module ArLazyPreload
|
7
|
-
# This class is responsible for holding a connection between a list of ActiveRecord::Base objects
|
8
|
-
# which have been loaded by the same instance of ActiveRecord::Relation. It also contains a tree
|
9
|
-
# of associations, which were requested to be loaded lazily.
|
10
|
-
# Calling #preload_association method will cause loading of ALL associated objects for EACH
|
11
|
-
# ecord when requested association is found in the association tree.
|
12
8
|
class Context
|
13
9
|
# Initiates lazy preload context for given records
|
14
10
|
def self.register(records:, association_tree:)
|
15
|
-
return if records.empty?
|
16
|
-
|
17
|
-
ArLazyPreload
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
def initialize(records:, association_tree:)
|
25
|
-
@records = records.compact
|
26
|
-
@association_tree = association_tree
|
27
|
-
|
28
|
-
@records.each { |record| record.lazy_preload_context = self }
|
29
|
-
end
|
30
|
-
|
31
|
-
# This method checks if the association is present in the association_tree and preloads for all
|
32
|
-
# objects in the context it if needed.
|
33
|
-
def try_preload_lazily(association_name)
|
34
|
-
return unless association_needs_preload?(association_name)
|
35
|
-
|
36
|
-
preloader.preload(records, association_name)
|
37
|
-
AssociatedContextBuilder.prepare(parent_context: self, association_name: association_name)
|
38
|
-
# Our tracking of loading state
|
39
|
-
# Otherwise `#preload` will be called many times even when association is loaded
|
40
|
-
mark_association_as_loaded(association_name)
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def association_needs_preload?(association_name, node_tree = association_tree)
|
46
|
-
# Check whether association loading state
|
47
|
-
# to avoid calling preload unnecessarily
|
48
|
-
return false if association_loaded?(association_name)
|
49
|
-
return true if ArLazyPreload.config.auto_preload?
|
50
|
-
|
51
|
-
node_tree.any? do |node|
|
52
|
-
if node.is_a?(Symbol)
|
53
|
-
node == association_name
|
54
|
-
elsif node.is_a?(Hash)
|
55
|
-
node.key?(association_name)
|
56
|
-
end
|
11
|
+
return if records.empty?
|
12
|
+
|
13
|
+
if ArLazyPreload.config.auto_preload?
|
14
|
+
Contexts::AutoPreloadContext.new(records: records)
|
15
|
+
elsif association_tree.any?
|
16
|
+
Contexts::LazyPreloadContext.new(
|
17
|
+
records: records,
|
18
|
+
association_tree: association_tree
|
19
|
+
)
|
57
20
|
end
|
58
21
|
end
|
59
|
-
|
60
|
-
def mark_association_as_loaded(association_name)
|
61
|
-
loaded_association_names.add(association_name)
|
62
|
-
end
|
63
|
-
|
64
|
-
def association_loaded?(association_name)
|
65
|
-
loaded_association_names.include?(association_name)
|
66
|
-
end
|
67
|
-
|
68
|
-
def loaded_association_names
|
69
|
-
@loaded_association_names ||= Set.new
|
70
|
-
end
|
71
|
-
|
72
|
-
def preloader
|
73
|
-
@preloader ||= ActiveRecord::Associations::Preloader.new
|
74
|
-
end
|
75
22
|
end
|
76
23
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ArLazyPreload
|
4
|
+
module Contexts
|
5
|
+
# This class is responsible for automatic association preloading
|
6
|
+
class AutoPreloadContext < BaseContext
|
7
|
+
protected
|
8
|
+
|
9
|
+
def association_needs_preload?(_association_name)
|
10
|
+
true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "set"
|
4
|
+
require "ar_lazy_preload/associated_context_builder"
|
5
|
+
|
6
|
+
module ArLazyPreload
|
7
|
+
module Contexts
|
8
|
+
# This is a base context class, which is responsible for holding a connection between a list of
|
9
|
+
# ActiveRecord::Base objects which have been loaded by the same instance of
|
10
|
+
# ActiveRecord::Relation.
|
11
|
+
class BaseContext
|
12
|
+
attr_reader :records
|
13
|
+
|
14
|
+
# :records - array of ActiveRecord instances
|
15
|
+
def initialize(records:)
|
16
|
+
@records = records.dup
|
17
|
+
@records.compact!
|
18
|
+
@records.each { |record| record.lazy_preload_context = self }
|
19
|
+
end
|
20
|
+
|
21
|
+
# This method checks if the association should be loaded and preloads it for all
|
22
|
+
# objects in the context it if needed.
|
23
|
+
def try_preload_lazily(association_name)
|
24
|
+
return if association_loaded?(association_name) ||
|
25
|
+
!association_needs_preload?(association_name)
|
26
|
+
|
27
|
+
perform_preloading(association_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def association_needs_preload?(_association_name)
|
33
|
+
raise NotImplementedError
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def perform_preloading(association_name)
|
39
|
+
preloader.preload(records, association_name)
|
40
|
+
loaded_association_names.add(association_name)
|
41
|
+
|
42
|
+
AssociatedContextBuilder.prepare(
|
43
|
+
parent_context: self,
|
44
|
+
association_name: association_name
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def association_loaded?(association_name)
|
49
|
+
loaded_association_names.include?(association_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
def loaded_association_names
|
53
|
+
@loaded_association_names ||= Set.new
|
54
|
+
end
|
55
|
+
|
56
|
+
def preloader
|
57
|
+
@preloader ||= ActiveRecord::Associations::Preloader.new
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ArLazyPreload
|
4
|
+
module Contexts
|
5
|
+
# This class is responsible for lazy preloading. It contains a tree of associations, which were
|
6
|
+
# requested to be loaded lazily.
|
7
|
+
class LazyPreloadContext < BaseContext
|
8
|
+
attr_reader :association_tree
|
9
|
+
|
10
|
+
# :records - array of ActiveRecord instances
|
11
|
+
# :association_tree - list of symbols or hashes representing a tree of preloadable
|
12
|
+
# associations
|
13
|
+
def initialize(records:, association_tree:)
|
14
|
+
@association_tree = association_tree
|
15
|
+
|
16
|
+
super(records: records)
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def association_needs_preload?(association_name)
|
22
|
+
association_tree.any? do |node|
|
23
|
+
case node
|
24
|
+
when Symbol
|
25
|
+
node == association_name
|
26
|
+
when Hash
|
27
|
+
node.key?(association_name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_lazy_preload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,6 +136,20 @@ dependencies:
|
|
122
136
|
- - ">="
|
123
137
|
- !ruby/object:Gem::Version
|
124
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: memory_profiler
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
125
153
|
description: lazy_preload implementation for ActiveRecord models
|
126
154
|
email:
|
127
155
|
- dmitry.a.tsepelev@gmail.com
|
@@ -143,6 +171,9 @@ files:
|
|
143
171
|
- lib/ar_lazy_preload/association_tree_builder.rb
|
144
172
|
- lib/ar_lazy_preload/configuration.rb
|
145
173
|
- lib/ar_lazy_preload/context.rb
|
174
|
+
- lib/ar_lazy_preload/contexts/auto_preload_context.rb
|
175
|
+
- lib/ar_lazy_preload/contexts/base_context.rb
|
176
|
+
- lib/ar_lazy_preload/contexts/lazy_preload_context.rb
|
146
177
|
- lib/ar_lazy_preload/railtie.rb
|
147
178
|
- lib/ar_lazy_preload/version.rb
|
148
179
|
homepage: https://github.com/DmitryTsepelev/ar_lazy_preload
|