paper_trail_diff 0.15.0 → 0.17.0

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.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +724 -1
  3. data/README.md +43 -8
  4. data/docs/architecture.md +7 -5
  5. data/docs/performance.md +89 -32
  6. data/docs/reference.md +427 -96
  7. data/docs/testing.md +260 -32
  8. data/lib/paper_trail_diff/activity_belongs_to_event_applier.rb +2 -16
  9. data/lib/paper_trail_diff/activity_boundary.rb +55 -34
  10. data/lib/paper_trail_diff/activity_child_candidate_loader.rb +46 -57
  11. data/lib/paper_trail_diff/activity_collection_event_applier.rb +33 -11
  12. data/lib/paper_trail_diff/activity_collection_route_updater.rb +1 -1
  13. data/lib/paper_trail_diff/activity_event.rb +33 -21
  14. data/lib/paper_trail_diff/activity_event_record_resolver.rb +16 -116
  15. data/lib/paper_trail_diff/activity_event_route_finder.rb +49 -18
  16. data/lib/paper_trail_diff/activity_event_snapshot_refresher.rb +13 -7
  17. data/lib/paper_trail_diff/activity_history.rb +90 -49
  18. data/lib/paper_trail_diff/activity_range.rb +9 -35
  19. data/lib/paper_trail_diff/activity_root_snapshot_refresher.rb +104 -52
  20. data/lib/paper_trail_diff/activity_root_steps.rb +10 -13
  21. data/lib/paper_trail_diff/activity_scope_filter.rb +77 -0
  22. data/lib/paper_trail_diff/activity_snapshot_delta.rb +12 -15
  23. data/lib/paper_trail_diff/activity_snapshot_sequence.rb +37 -15
  24. data/lib/paper_trail_diff/activity_target_candidate_loader.rb +35 -43
  25. data/lib/paper_trail_diff/activity_timeline_builder.rb +200 -249
  26. data/lib/paper_trail_diff/activity_transaction_grouper.rb +3 -1
  27. data/lib/paper_trail_diff/activity_tree_filter.rb +54 -13
  28. data/lib/paper_trail_diff/activity_version_collector.rb +72 -138
  29. data/lib/paper_trail_diff/activity_view.rb +22 -11
  30. data/lib/paper_trail_diff/analysis.rb +39 -6
  31. data/lib/paper_trail_diff/analysis_batch.rb +16 -58
  32. data/lib/paper_trail_diff/approximate_paths.rb +4 -8
  33. data/lib/paper_trail_diff/association_activity_edge.rb +5 -24
  34. data/lib/paper_trail_diff/association_activity_selection.rb +10 -18
  35. data/lib/paper_trail_diff/association_discovery.rb +1 -6
  36. data/lib/paper_trail_diff/association_key.rb +9 -6
  37. data/lib/paper_trail_diff/association_traversal.rb +49 -6
  38. data/lib/paper_trail_diff/batch_boundary_resolver.rb +33 -42
  39. data/lib/paper_trail_diff/batch_preload.rb +2 -12
  40. data/lib/paper_trail_diff/batched_root_analyses.rb +72 -0
  41. data/lib/paper_trail_diff/batched_root_analyzer.rb +22 -14
  42. data/lib/paper_trail_diff/batched_root_versions.rb +25 -20
  43. data/lib/paper_trail_diff/branch_snapshot_refresher.rb +41 -42
  44. data/lib/paper_trail_diff/change_attribution.rb +314 -0
  45. data/lib/paper_trail_diff/change_row.rb +30 -10
  46. data/lib/paper_trail_diff/comparison_batch.rb +3 -4
  47. data/lib/paper_trail_diff/configuration.rb +1 -1
  48. data/lib/paper_trail_diff/deleted_analysis_batch.rb +22 -33
  49. data/lib/paper_trail_diff/deleted_root_filter.rb +29 -10
  50. data/lib/paper_trail_diff/deprecation.rb +4 -43
  51. data/lib/paper_trail_diff/diagnostics.rb +8 -7
  52. data/lib/paper_trail_diff/direct_record_state_loader.rb +8 -1
  53. data/lib/paper_trail_diff/endpoint.rb +78 -44
  54. data/lib/paper_trail_diff/errors.rb +2 -9
  55. data/lib/paper_trail_diff/fallback_paths.rb +2 -5
  56. data/lib/paper_trail_diff/habtm_transaction_owners.rb +5 -3
  57. data/lib/paper_trail_diff/historical_association_reifier.rb +3 -1
  58. data/lib/paper_trail_diff/historical_filter.rb +40 -15
  59. data/lib/paper_trail_diff/historical_snapshot_store.rb +80 -38
  60. data/lib/paper_trail_diff/instrumentation.rb +0 -8
  61. data/lib/paper_trail_diff/live_endpoint_batch_loader.rb +11 -7
  62. data/lib/paper_trail_diff/live_endpoint_provider.rb +7 -23
  63. data/lib/paper_trail_diff/live_graph_collector.rb +3 -3
  64. data/lib/paper_trail_diff/nested_comparator.rb +46 -19
  65. data/lib/paper_trail_diff/net_change.rb +276 -0
  66. data/lib/paper_trail_diff/own_window.rb +35 -0
  67. data/lib/paper_trail_diff/paper_trail_adapter.rb +119 -87
  68. data/lib/paper_trail_diff/prepared_activity_index.rb +182 -50
  69. data/lib/paper_trail_diff/prepared_child_candidates.rb +58 -0
  70. data/lib/paper_trail_diff/prepared_edge_loader.rb +43 -107
  71. data/lib/paper_trail_diff/prepared_history.rb +31 -36
  72. data/lib/paper_trail_diff/prepared_history_loader.rb +47 -12
  73. data/lib/paper_trail_diff/prepared_record_index.rb +143 -49
  74. data/lib/paper_trail_diff/prepared_snapshot_builder.rb +16 -0
  75. data/lib/paper_trail_diff/prepared_state_association_reader.rb +17 -19
  76. data/lib/paper_trail_diff/read_snapshot.rb +99 -0
  77. data/lib/paper_trail_diff/recorded_changes.rb +10 -11
  78. data/lib/paper_trail_diff/recorded_changes_reader.rb +60 -0
  79. data/lib/paper_trail_diff/recorded_class.rb +14 -19
  80. data/lib/paper_trail_diff/recorded_columns.rb +33 -0
  81. data/lib/paper_trail_diff/root_version_plan.rb +20 -22
  82. data/lib/paper_trail_diff/root_version_selection.rb +39 -29
  83. data/lib/paper_trail_diff/scope_condition.rb +92 -32
  84. data/lib/paper_trail_diff/scope_ranking.rb +27 -34
  85. data/lib/paper_trail_diff/scoped_analysis.rb +27 -4
  86. data/lib/paper_trail_diff/scoped_analysis_builder.rb +18 -7
  87. data/lib/paper_trail_diff/scoped_root_selection.rb +65 -39
  88. data/lib/paper_trail_diff/snapshot_normalizer.rb +18 -3
  89. data/lib/paper_trail_diff/stored_json_values.rb +52 -0
  90. data/lib/paper_trail_diff/stored_time_precision.rb +30 -18
  91. data/lib/paper_trail_diff/stored_values.rb +20 -0
  92. data/lib/paper_trail_diff/support.rb +185 -35
  93. data/lib/paper_trail_diff/time_of_day.rb +39 -0
  94. data/lib/paper_trail_diff/time_range.rb +40 -22
  95. data/lib/paper_trail_diff/time_version_range.rb +5 -10
  96. data/lib/paper_trail_diff/timeline_builder.rb +9 -3
  97. data/lib/paper_trail_diff/timeline_range.rb +18 -12
  98. data/lib/paper_trail_diff/timeline_snapshot_provider.rb +1 -1
  99. data/lib/paper_trail_diff/total_order.rb +55 -0
  100. data/lib/paper_trail_diff/transaction_cut.rb +235 -75
  101. data/lib/paper_trail_diff/traversal_preparer.rb +5 -8
  102. data/lib/paper_trail_diff/value_objects.rb +8 -4
  103. data/lib/paper_trail_diff/version.rb +1 -1
  104. data/lib/paper_trail_diff/version_association_candidate_scope.rb +1 -12
  105. data/lib/paper_trail_diff/version_boundary.rb +41 -23
  106. data/lib/paper_trail_diff/version_query.rb +25 -25
  107. data/lib/paper_trail_diff/version_range.rb +12 -16
  108. data/lib/paper_trail_diff/version_sequence_diagnostics.rb +36 -1
  109. data/lib/paper_trail_diff/version_table.rb +12 -8
  110. data/lib/paper_trail_diff.rb +32 -44
  111. data/sig/generated/paper_trail_diff/activity_belongs_to_event_applier.rbs +0 -8
  112. data/sig/generated/paper_trail_diff/activity_boundary.rbs +39 -17
  113. data/sig/generated/paper_trail_diff/activity_child_candidate_loader.rbs +19 -22
  114. data/sig/generated/paper_trail_diff/activity_collection_event_applier.rbs +13 -5
  115. data/sig/generated/paper_trail_diff/activity_event.rbs +25 -9
  116. data/sig/generated/paper_trail_diff/activity_event_record_resolver.rbs +6 -34
  117. data/sig/generated/paper_trail_diff/activity_event_route_finder.rbs +25 -4
  118. data/sig/generated/paper_trail_diff/activity_event_snapshot_refresher.rbs +6 -3
  119. data/sig/generated/paper_trail_diff/activity_history.rbs +54 -19
  120. data/sig/generated/paper_trail_diff/activity_range.rbs +6 -20
  121. data/sig/generated/paper_trail_diff/activity_root_snapshot_refresher.rbs +55 -10
  122. data/sig/generated/paper_trail_diff/activity_root_steps.rbs +6 -6
  123. data/sig/generated/paper_trail_diff/activity_scope_filter.rbs +51 -0
  124. data/sig/generated/paper_trail_diff/activity_snapshot_delta.rbs +5 -3
  125. data/sig/generated/paper_trail_diff/activity_snapshot_sequence.rbs +21 -3
  126. data/sig/generated/paper_trail_diff/activity_target_candidate_loader.rbs +19 -24
  127. data/sig/generated/paper_trail_diff/activity_timeline_builder.rbs +125 -127
  128. data/sig/generated/paper_trail_diff/activity_tree_filter.rbs +41 -8
  129. data/sig/generated/paper_trail_diff/activity_version_collector.rbs +42 -35
  130. data/sig/generated/paper_trail_diff/activity_view.rbs +16 -8
  131. data/sig/generated/paper_trail_diff/analysis.rbs +26 -5
  132. data/sig/generated/paper_trail_diff/analysis_batch.rbs +6 -24
  133. data/sig/generated/paper_trail_diff/association_activity_edge.rbs +0 -13
  134. data/sig/generated/paper_trail_diff/association_activity_selection.rbs +6 -9
  135. data/sig/generated/paper_trail_diff/association_discovery.rbs +0 -3
  136. data/sig/generated/paper_trail_diff/association_key.rbs +5 -0
  137. data/sig/generated/paper_trail_diff/association_traversal.rbs +26 -2
  138. data/sig/generated/paper_trail_diff/batch_boundary_resolver.rbs +17 -17
  139. data/sig/generated/paper_trail_diff/batch_preload.rbs +0 -4
  140. data/sig/generated/paper_trail_diff/batched_root_analyses.rbs +47 -0
  141. data/sig/generated/paper_trail_diff/batched_root_analyzer.rbs +13 -8
  142. data/sig/generated/paper_trail_diff/batched_root_versions.rbs +10 -6
  143. data/sig/generated/paper_trail_diff/branch_snapshot_refresher.rbs +16 -4
  144. data/sig/generated/paper_trail_diff/change_attribution.rbs +185 -0
  145. data/sig/generated/paper_trail_diff/change_row.rbs +22 -7
  146. data/sig/generated/paper_trail_diff/deleted_analysis_batch.rbs +11 -9
  147. data/sig/generated/paper_trail_diff/deleted_root_filter.rbs +9 -3
  148. data/sig/generated/paper_trail_diff/deprecation.rbs +4 -24
  149. data/sig/generated/paper_trail_diff/direct_record_state_loader.rbs +4 -0
  150. data/sig/generated/paper_trail_diff/endpoint.rbs +40 -16
  151. data/sig/generated/paper_trail_diff/errors.rbs +2 -11
  152. data/sig/generated/paper_trail_diff/habtm_transaction_owners.rbs +2 -1
  153. data/sig/generated/paper_trail_diff/historical_filter.rbs +20 -9
  154. data/sig/generated/paper_trail_diff/historical_snapshot_store.rbs +40 -10
  155. data/sig/generated/paper_trail_diff/instrumentation.rbs +0 -3
  156. data/sig/generated/paper_trail_diff/live_endpoint_batch_loader.rbs +3 -0
  157. data/sig/generated/paper_trail_diff/live_endpoint_provider.rbs +5 -8
  158. data/sig/generated/paper_trail_diff/live_graph_collector.rbs +2 -2
  159. data/sig/generated/paper_trail_diff/nested_comparator.rbs +20 -4
  160. data/sig/generated/paper_trail_diff/net_change.rbs +168 -0
  161. data/sig/generated/paper_trail_diff/own_window.rbs +22 -0
  162. data/sig/generated/paper_trail_diff/paper_trail_adapter.rbs +19 -6
  163. data/sig/generated/paper_trail_diff/prepared_activity_index.rbs +113 -17
  164. data/sig/generated/paper_trail_diff/prepared_child_candidates.rbs +39 -0
  165. data/sig/generated/paper_trail_diff/prepared_edge_loader.rbs +18 -35
  166. data/sig/generated/paper_trail_diff/prepared_history.rbs +15 -13
  167. data/sig/generated/paper_trail_diff/prepared_history_loader.rbs +28 -3
  168. data/sig/generated/paper_trail_diff/prepared_record_index.rbs +70 -15
  169. data/sig/generated/paper_trail_diff/prepared_snapshot_builder.rbs +12 -0
  170. data/sig/generated/paper_trail_diff/prepared_state_association_reader.rbs +6 -7
  171. data/sig/generated/paper_trail_diff/read_snapshot.rbs +62 -0
  172. data/sig/generated/paper_trail_diff/recorded_changes.rbs +4 -2
  173. data/sig/generated/paper_trail_diff/recorded_changes_reader.rbs +41 -0
  174. data/sig/generated/paper_trail_diff/recorded_class.rbs +7 -6
  175. data/sig/generated/paper_trail_diff/recorded_columns.rbs +21 -0
  176. data/sig/generated/paper_trail_diff/root_version_plan.rbs +8 -4
  177. data/sig/generated/paper_trail_diff/root_version_selection.rbs +27 -13
  178. data/sig/generated/paper_trail_diff/scope_condition.rbs +39 -9
  179. data/sig/generated/paper_trail_diff/scope_ranking.rbs +16 -16
  180. data/sig/generated/paper_trail_diff/scoped_analysis.rbs +19 -0
  181. data/sig/generated/paper_trail_diff/scoped_analysis_builder.rbs +9 -2
  182. data/sig/generated/paper_trail_diff/scoped_root_selection.rbs +40 -12
  183. data/sig/generated/paper_trail_diff/snapshot_normalizer.rbs +7 -0
  184. data/sig/generated/paper_trail_diff/stored_json_values.rbs +35 -0
  185. data/sig/generated/paper_trail_diff/stored_time_precision.rbs +19 -5
  186. data/sig/generated/paper_trail_diff/stored_values.rbs +13 -0
  187. data/sig/generated/paper_trail_diff/support.rbs +120 -23
  188. data/sig/generated/paper_trail_diff/time_of_day.rbs +21 -0
  189. data/sig/generated/paper_trail_diff/time_range.rbs +15 -7
  190. data/sig/generated/paper_trail_diff/time_version_range.rbs +2 -5
  191. data/sig/generated/paper_trail_diff/timeline_builder.rbs +6 -2
  192. data/sig/generated/paper_trail_diff/timeline_range.rbs +12 -7
  193. data/sig/generated/paper_trail_diff/total_order.rbs +32 -0
  194. data/sig/generated/paper_trail_diff/transaction_cut.rbs +131 -40
  195. data/sig/generated/paper_trail_diff/traversal_preparer.rbs +2 -2
  196. data/sig/generated/paper_trail_diff/value_objects.rbs +5 -1
  197. data/sig/generated/paper_trail_diff/version_association_candidate_scope.rbs +0 -7
  198. data/sig/generated/paper_trail_diff/version_boundary.rbs +17 -8
  199. data/sig/generated/paper_trail_diff/version_query.rbs +17 -10
  200. data/sig/generated/paper_trail_diff/version_range.rbs +6 -9
  201. data/sig/generated/paper_trail_diff/version_sequence_diagnostics.rbs +16 -0
  202. data/sig/generated/paper_trail_diff/version_table.rbs +3 -0
  203. data/sig/generated/paper_trail_diff.rbs +16 -16
  204. metadata +31 -7
  205. data/lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb +0 -60
  206. data/sig/generated/paper_trail_diff/preloaded_endpoint_batch_loader.rbs +0 -32
@@ -3,17 +3,29 @@
3
3
 
4
4
  module PaperTrailDiff
5
5
  # Shares association event discovery across every root in a prepared graph.
6
+ #
7
+ # The candidates are the records the prepared snapshots can hold: those
8
+ # recorded under an owner from the start onward, and those under it now. A
9
+ # key changed without a version is recorded only by the record's next
10
+ # version, or by the current row, either of which can come after the range,
11
+ # and the snapshots read it from there. ActivityTreeFilter leaves out the
12
+ # events of a candidate that was outside the tree throughout.
6
13
  class PreparedActivityIndex
7
- #: (PreparedRecordIndex, Array[untyped], start_at: untyped, end_at: untyped) -> void
8
- def initialize(records, root_versions, start_at:, end_at:)
14
+ # The children the history's owners had, which its edge loader reads
15
+ # first for most of them.
16
+ attr_reader :children #: PreparedChildCandidates
17
+
18
+ #: (PreparedRecordIndex, Array[untyped], start_at: untyped) -> void
19
+ def initialize(records, root_versions, start_at:)
9
20
  @records = records
10
- @range = ActivityRange.new(start_at, end_at)
11
- @roots = root_versions.group_by { |version| [version.item_type, version.item_id.to_s] }
21
+ @start_at = start_at
22
+ @children = PreparedChildCandidates.new(start_at)
23
+ @roots = root_versions.group_by { |version| Endpoint.identity(version) }
12
24
  @nodes = {} #: Hash[String, Array[untyped]]
13
25
  # Rows are cached by reflection, which a subclass shares with its base.
14
- @children = {} #: Hash[untyped, untyped]
15
26
  @targets = {} #: Hash[untyped, untyped]
16
27
  @rows = {} #: Hash[untyped, Hash[String, Array[untyped]]]
28
+ @conditions = ScopeCondition::Cache.new
17
29
  end
18
30
 
19
31
  #: (untyped, Array[untyped]) -> void
@@ -25,7 +37,7 @@ module PaperTrailDiff
25
37
  #: (untyped, Array[untyped]) -> Array[untyped]
26
38
  def versions(model, ids)
27
39
  ids.flat_map do |id|
28
- @roots[[model.base_class.name, id.to_s]] || @records.versions_for_id(model, id)
40
+ @roots[Support.record_identity(model, id)] || @records.versions_for_id(model, id)
29
41
  end
30
42
  end
31
43
 
@@ -50,40 +62,48 @@ module PaperTrailDiff
50
62
  end
51
63
  end
52
64
 
53
- # The versions of a through's join rows that are on its path. Under
54
- # `source_type:`, a row is on it only while its type column names that
55
- # class, so only the versions it had that type before or after count. A
56
- # version saved before the type column existed has no type to read.
57
- #: (Hash[Symbol, untyped], untyped) -> Array[untyped]
58
- def join_versions(group, reflection)
65
+ # The versions of a through's join rows that are on its path (see
66
+ # ThroughPath). A row's versions in one transaction are judged together, by
67
+ # its states before the transaction and after it, as ActivityTreeFilter
68
+ # judges a record; a version saved outside a transaction is judged alone.
69
+ # A row created under another owner and moved here later, one that left,
70
+ # or one created and destroyed inside one transaction, saved versions that
71
+ # change nothing these `owners` show. As events they would open steps of
72
+ # their own, and inside a transaction, where a change surfaces one event
73
+ # late, take the credit for another record's change.
74
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped], untyped) -> Array[untyped]
75
+ def join_versions(owners, group, reflection)
76
+ path = ThroughPath.new(owners, reflection, @conditions)
59
77
  versions = group.fetch(:versions)
60
- source_type = reflection.options[:source_type]
61
- return versions unless source_type
78
+ return versions if path.open?
62
79
 
63
- column = reflection.foreign_type
64
- versions.select do |version|
65
- @records.states_around(group.fetch(:model), version.item_id, version).any? do |state|
66
- state.attributes[column] == source_type.to_s
67
- end
68
- end
80
+ runs = versions.group_by { |version| join_run(version) }.values
81
+ runs.select { |run| on_path?(group.fetch(:model), run, path) }.flat_map(&:itself)
69
82
  end
70
83
 
84
+ # Only the children the history loaded. Snapshots read children only under
85
+ # an owner the history prepared, and it loaded every child of those. An
86
+ # owner reached another way, such as a belongs_to target loaded for a
87
+ # different path, can have children the history never loaded, which no
88
+ # snapshot holds.
71
89
  #: (untyped, Array[untyped], untyped, ActivityRange) -> Array[untyped]
72
90
  def child_ids(model, ids, reflection, range)
73
- historical, live = @children[reflection] ||= child_rows(model, reflection)
74
- empty = [] #: Array[untyped]
75
- selected = historical_ids(reflection.klass, ids, historical, range)
76
- selected |= ids.flat_map { |id| live.fetch(id.to_s, empty) } if range.current_end?
77
- selected.uniq
91
+ # An owner the edges did not read is read with every other one this
92
+ # history holds, so that the next needs nothing more.
93
+ historical, live = @children.call(model, ids, reflection) { node_ids(model) }
94
+ child = reflection.klass
95
+ # An owner with no live children reads as nil, which names no record.
96
+ current = ids.flat_map { |id| live[id.to_s] }.select { |id| loaded?(child, id) }
97
+ historical_ids(child, ids, historical, range) | current
78
98
  end
79
99
 
80
- # The `belongs_to` targets each owner pointed at in the range: at its start
81
- # and after each change inside it, plus the live row at a current end.
100
+ # The `belongs_to` targets each owner pointed at from the range's start
101
+ # onward, plus the live row and those its recorded states name.
82
102
  #: (untyped, Array[untyped], untyped, ActivityRange) -> Array[ActivityTargetCandidateLoader::Row]
83
103
  def target_rows(model, ids, reflection, range)
84
104
  historical, live = @targets[reflection] ||= target_candidates(model, reflection)
85
- rows = eligible_rows(model, rows_for(historical, ids), range)
86
- range.current_end? ? rows + rows_for(live, ids) : rows
105
+ eligible_rows(model, rows_for(historical, ids), range) + rows_for(live, ids) +
106
+ recorded_target_rows(model, ids, reflection)
87
107
  end
88
108
 
89
109
  #: (untyped, untyped) -> bool
@@ -102,12 +122,48 @@ module PaperTrailDiff
102
122
  private
103
123
 
104
124
  # @rbs @records: PreparedRecordIndex
105
- # @rbs @range: ActivityRange
125
+ # @rbs @start_at: untyped
106
126
  # @rbs @roots: Hash[Array[String], Array[untyped]]
107
127
  # @rbs @nodes: Hash[String, Array[untyped]]
108
- # @rbs @children: Hash[untyped, untyped]
109
128
  # @rbs @targets: Hash[untyped, untyped]
110
129
  # @rbs @rows: Hash[untyped, Hash[String, Array[untyped]]]
130
+ # @rbs @conditions: ScopeCondition::Cache
131
+
132
+ # The versions one row saved in one transaction, or alone.
133
+ #: (untyped) -> Array[untyped]
134
+ def join_run(version)
135
+ [version.item_id, ActivityTransactionGroups.transaction_key(version) || version]
136
+ end
137
+
138
+ # A run's versions are in the order they were saved, as the row's series
139
+ # holds them.
140
+ #: (untyped, Array[untyped], ThroughPath) -> bool
141
+ def on_path?(model, run, path)
142
+ first = run.fetch(0)
143
+ @records.typed_states_across(model, first.item_id, first, run.fetch(-1))
144
+ .any? { |attributes| path.include?(attributes) }
145
+ end
146
+
147
+ # The targets the owners' recorded states point at. PT-AT names a target
148
+ # only in the rows its owner's versions wrote, so a key changed without a
149
+ # version, such as one `has_paper_trail only:` leaves out, names its target
150
+ # only in the state the owner's next version recorded. Prepared history
151
+ # reads that state, so its snapshots show the target, and the target's
152
+ # versions are events too. A key of two columns is read per record instead.
153
+ #: (untyped, Array[untyped], untyped) -> Array[ActivityTargetCandidateLoader::Row]
154
+ def recorded_target_rows(model, ids, reflection)
155
+ key = reflection.foreign_key
156
+ return [] if key.is_a?(Array)
157
+
158
+ ids.flat_map do |id|
159
+ targets = @records.attribute_values_for(model, [id], key)
160
+ types = (@records.attribute_values_for(model, [id], reflection.foreign_type) if
161
+ reflection.polymorphic?) || []
162
+ targets.zip(types).filter_map do |target, type|
163
+ ActivityTargetCandidateLoader::Row.new(id, target, type) unless target.nil?
164
+ end
165
+ end
166
+ end
111
167
 
112
168
  #: (untyped, Array[untyped]) -> bool
113
169
  def member_class?(model, classes)
@@ -120,6 +176,8 @@ module PaperTrailDiff
120
176
  ids.flat_map do |id|
121
177
  historical.fetch(id.to_s, empty).filter_map do |row|
122
178
  _owner, child_id, timestamp, event = row
179
+ next unless loaded?(model, child_id)
180
+
123
181
  child_id if eligible_version?(model, child_id, timestamp, event, range)
124
182
  end
125
183
  end
@@ -135,16 +193,6 @@ module PaperTrailDiff
135
193
  end
136
194
  end
137
195
 
138
- #: (untyped, untyped) -> untyped
139
- def child_rows(model, reflection)
140
- loader = ActivityChildCandidateLoader.new(
141
- model, node_ids(model), reflection, @range
142
- )
143
- [loader.historical_rows.group_by { |row| row.first.to_s },
144
- loader.current_pairs.group_by { |row| row.first.to_s }
145
- .transform_values { |rows| rows.map(&:last) }]
146
- end
147
-
148
196
  #: (Hash[String, Array[ActivityTargetCandidateLoader::Row]], Array[untyped]) -> Array[ActivityTargetCandidateLoader::Row]
149
197
  def rows_for(grouped, ids)
150
198
  ids.flat_map { |id| grouped[id.to_s] || [] }
@@ -153,14 +201,13 @@ module PaperTrailDiff
153
201
  #: (untyped, Array[ActivityTargetCandidateLoader::Row], ActivityRange) -> Array[ActivityTargetCandidateLoader::Row]
154
202
  def eligible_rows(model, rows, range)
155
203
  rows.select do |row|
156
- eligible_version?(model, row.owner_id, row.created_at, row.event, range) &&
157
- range.recorded_before_end?(row.created_at, row.version_id)
204
+ eligible_version?(model, row.owner_id, row.created_at, row.event, range)
158
205
  end
159
206
  end
160
207
 
161
208
  #: (untyped, untyped) -> untyped
162
209
  def target_candidates(model, reflection)
163
- loader = ActivityTargetCandidateLoader.new(model, node_ids(model), reflection, @range)
210
+ loader = ActivityTargetCandidateLoader.new(model, node_ids(model), reflection, @start_at)
164
211
  [loader.historical_rows.group_by { |row| row.owner_id.to_s },
165
212
  loader.current_rows.group_by { |row| row.owner_id.to_s }]
166
213
  end
@@ -168,22 +215,27 @@ module PaperTrailDiff
168
215
  # Mirrors VersionAssociationCandidateScope's before-window rule. The loaded
169
216
  # series includes changes to other owners too, so an old membership cannot
170
217
  # survive a later pre-window reassignment just because that owner is absent.
218
+ # A range starting at a version starts before that version's transaction,
219
+ # as the scope's start does (see VersionBoundary.transaction_start), so a
220
+ # membership that ended earlier in that transaction still held there.
171
221
  #: (untyped, untyped, untyped, String?, ActivityRange) -> bool
172
222
  def eligible_version?(model, id, timestamp, event, range)
173
- return timestamp <= range.end_time if timestamp >= range.start_time
223
+ return true if timestamp >= range.start_time
174
224
  return false if event == 'destroy'
175
225
 
176
226
  later = @records.versions_for_id(model, id)
177
227
  .bsearch { |version| version.created_at > timestamp }
178
- !later || later.created_at >= range.start_time
228
+ !later || later.created_at >= range.start_time || range.opening?(later)
179
229
  end
180
230
 
231
+ # Each statement reads the rows of 500 transactions.
181
232
  #: (untyped, untyped) -> Hash[String, Array[untyped]]
182
233
  def load_habtm_rows(model, reflection)
183
- model.paper_trail.version_association_class.where(
184
- version_id: transaction_ids(versions(model, node_ids(model))),
185
- foreign_key_name: reflection.name
186
- ).group_by { |row| row.version_id.to_s }
234
+ rows = model.paper_trail.version_association_class.where(foreign_key_name: reflection.name)
235
+ transaction_ids(versions(model, node_ids(model)))
236
+ .each_slice(Support::IDS_PER_STATEMENT)
237
+ .flat_map { |slice| rows.where(version_id: slice) }
238
+ .group_by { |row| row.version_id.to_s }
187
239
  end
188
240
 
189
241
  # A version without a transaction id names no rows, so it can stay in.
@@ -191,5 +243,85 @@ module PaperTrailDiff
191
243
  def transaction_ids(versions)
192
244
  versions.map { |version| VersionBoundary.transaction_id(version) }.uniq
193
245
  end
246
+
247
+ # Whether one state of a through's join row puts it on the through's path.
248
+ # The row must join one of the owners as the join association reads it: by
249
+ # its key, by the owner's type under `as:`, by its class when the
250
+ # association is to a subclass, and by the association's scope when the
251
+ # gem can read it, unranked, as a through reads its parts. A through that
252
+ # goes through a belongs_to, or through another through, is joined by the
253
+ # owner's key or by rows further down, which a row's own state cannot
254
+ # show. Under `source_type:`, the row's type column must name that class;
255
+ # a version saved before the column existed has no type to read.
256
+ class ThroughPath
257
+ #: (Hash[Symbol, untyped], untyped, ScopeCondition::Cache) -> void
258
+ def initialize(owners, reflection, conditions)
259
+ @reflection = reflection
260
+ @through = reflection.through_reflection
261
+ @keyed = keyed?
262
+ @ids = owners.fetch(:ids).to_set(&:to_s)
263
+ @owner_types = owner_types(owners.fetch(:model))
264
+ @conditions = conditions
265
+ end
266
+
267
+ # Whether every state of a row is on the path, so that none need be read.
268
+ #: () -> bool
269
+ def open?
270
+ !@keyed && @reflection.options[:source_type].nil?
271
+ end
272
+
273
+ #: (Hash[untyped, untyped]) -> bool
274
+ def include?(attributes)
275
+ (!@keyed || joined?(attributes)) && typed?(attributes)
276
+ end
277
+
278
+ private
279
+
280
+ # @rbs @reflection: untyped
281
+ # @rbs @through: untyped
282
+ # @rbs @keyed: bool
283
+ # @rbs @ids: Set[String]
284
+ # @rbs @owner_types: Array[String]?
285
+ # @rbs @conditions: ScopeCondition::Cache
286
+
287
+ #: () -> bool
288
+ def keyed?
289
+ !@through.through_reflection? && !@through.belongs_to? &&
290
+ !@through.foreign_key.is_a?(Array)
291
+ end
292
+
293
+ #: (Hash[untyped, untyped]) -> bool
294
+ def joined?(attributes)
295
+ @ids.include?(attributes.fetch(@through.foreign_key).to_s) && owner_type?(attributes) &&
296
+ AssociationTraversal.sti_member?(@through.klass, attributes) && in_scope?(attributes)
297
+ end
298
+
299
+ # The names an `as:` row's type column may hold for an owner.
300
+ #: (untyped) -> Array[String]?
301
+ def owner_types(model)
302
+ return unless @through.type
303
+
304
+ [model.name, model.base_class.name, @through.active_record.name]
305
+ end
306
+
307
+ #: (Hash[untyped, untyped]) -> bool
308
+ def owner_type?(attributes)
309
+ types = @owner_types
310
+ !types || types.include?(attributes.fetch(@through.type))
311
+ end
312
+
313
+ #: (Hash[untyped, untyped]) -> bool
314
+ def in_scope?(attributes)
315
+ condition = @conditions.call(@through, ranked: false)
316
+ !condition || condition.matches?(attributes)
317
+ end
318
+
319
+ # As the through's snapshots read a join row (see
320
+ # PreparedStateAssociationReader#typed_join?).
321
+ #: (Hash[untyped, untyped]) -> bool
322
+ def typed?(attributes)
323
+ AssociationTraversal.source_type_member?(@reflection, attributes[@reflection.foreign_type])
324
+ end
325
+ end
194
326
  end
195
327
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module PaperTrailDiff
5
+ # The children each owner of a has_many or has_one had from a history's
6
+ # start onward, and has now, read once for each owner across the history
7
+ # (see ActivityChildCandidateLoader). Preparation reads them for the owners
8
+ # it loads, and activity then finds them here.
9
+ class PreparedChildCandidates
10
+ # What one class's association has read: the rows recorded under each
11
+ # owner and its live children's ids, by the owner's id.
12
+ class Read
13
+ attr_reader :historical #: Hash[String, Array[Array[untyped]]]
14
+ attr_reader :live #: Hash[String, Array[untyped]]
15
+
16
+ #: () -> void
17
+ def initialize
18
+ @historical = {}
19
+ @live = {}
20
+ @owners = Set.new #: Set[String]
21
+ end
22
+
23
+ #: (Array[untyped]) -> Array[untyped]
24
+ def unread(ids)
25
+ ids.reject { |id| @owners.include?(id.to_s) }
26
+ end
27
+
28
+ #: (Array[untyped], ActivityChildCandidateLoader) -> void
29
+ def add(owners, loader)
30
+ loader.historical_rows.each { |row| (@historical[row.first.to_s] ||= []) << row }
31
+ loader.current_pairs.each { |owner, child| (@live[owner.to_s] ||= []) << child }
32
+ owners.each { |id| @owners << id.to_s }
33
+ end
34
+ end
35
+
36
+ #: (untyped) -> void
37
+ def initialize(start_at)
38
+ @start_at = start_at
39
+ @read = {} #: Hash[Array[untyped], Read]
40
+ end
41
+
42
+ # The owners not read yet are read together, with the others the block
43
+ # names, so that owners asked about later need none. Each owner class
44
+ # reads its own, as a polymorphic row can store the class's name.
45
+ #: (untyped, Array[untyped], untyped) ?{ () -> Array[untyped] } -> [Hash[String, Array[Array[untyped]]], Hash[String, Array[untyped]]]
46
+ def call(model, ids, reflection)
47
+ read = @read[[model, reflection]] ||= Read.new
48
+ unless read.unread(ids).empty?
49
+ owners = read.unread(block_given? ? yield | ids : ids)
50
+ read.add(owners, ActivityChildCandidateLoader.new(model, owners, reflection, @start_at))
51
+ end
52
+ [read.historical, read.live]
53
+ end
54
+
55
+ # @rbs @start_at: untyped
56
+ # @rbs @read: Hash[Array[untyped], Read]
57
+ end
58
+ end
@@ -4,11 +4,14 @@
4
4
  module PaperTrailDiff
5
5
  # Loads every historical identity that can participate in one association edge.
6
6
  class PreparedEdgeLoader
7
- #: (PreparedRecordIndex, Array[untyped], ?start_at: untyped) -> void
8
- def initialize(records, root_versions, start_at: root_versions.first&.created_at)
7
+ # A history shares `children` with its activity index, which reads the
8
+ # children this finds again.
9
+ #: (PreparedRecordIndex, Array[untyped], ?start_at: untyped, ?children: PreparedChildCandidates) -> void
10
+ def initialize(records, root_versions, start_at: root_versions.first&.created_at,
11
+ children: PreparedChildCandidates.new(start_at))
9
12
  @records = records
10
13
  @transaction_ids = root_versions.filter_map(&:transaction_id)
11
- @start_at = start_at
14
+ @children = children
12
15
  end
13
16
 
14
17
  # HABTM alone also returns each root transaction's members.
@@ -25,37 +28,40 @@ module PaperTrailDiff
25
28
 
26
29
  # @rbs @records: PreparedRecordIndex
27
30
  # @rbs @transaction_ids: Array[untyped]
28
- # @rbs @start_at: untyped
31
+ # @rbs @children: PreparedChildCandidates
29
32
 
30
33
  #: (untyped, Array[untyped], untyped) -> Hash[String, Hash[Symbol, untyped]]
31
34
  def belongs_to_groups(owner_class, owner_ids, reflection)
32
- key = scalar_foreign_key(reflection)
35
+ key = AssociationKey.scalar_foreign_key(reflection)
33
36
  return {} unless key
34
- unless reflection.polymorphic?
35
- return group_for(reflection.klass,
36
- @records.attribute_values_for(owner_class, owner_ids, key))
37
- end
37
+ return polymorphic_groups(owner_class, owner_ids, reflection, key) if reflection.polymorphic?
38
+
39
+ group_for(reflection.klass, @records.attribute_values_for(owner_class, owner_ids, key))
40
+ end
38
41
 
42
+ # Grouping only decides what to preload, so a type that names no class is
43
+ # left to raise where that state is read.
44
+ #: (untyped, Array[untyped], untyped, String) -> Hash[String, Hash[Symbol, untyped]]
45
+ def polymorphic_groups(owner_class, owner_ids, reflection, key)
39
46
  grouped = {} #: Hash[String, Hash[Symbol, untyped]]
40
- # Grouping only decides what to preload, so a type that names no class is
41
- # left to raise where that state is read.
42
47
  @records.records_for(owner_class, owner_ids).each do |owner|
43
48
  id = owner.public_send(key)
44
49
  type = owner.public_send(reflection.foreign_type).to_s
45
50
  model_class = AssociationTraversal.named_class(type)
46
51
  add_identity(grouped, model_class, id) if model_class && id
47
52
  end
48
- grouped
53
+ grouped.transform_values { |group| group.merge(ids: group.fetch(:ids).uniq) }
49
54
  end
50
55
 
56
+ # Each owner's children, recorded or live, each once.
51
57
  #: (untyped, Array[untyped], untyped) -> Hash[String, Hash[Symbol, untyped]]
52
58
  def child_groups(owner_class, owner_ids, reflection)
53
- key = scalar_foreign_key(reflection)
54
- return {} unless key
55
-
56
- historical = historical_child_ids(owner_class, owner_ids, reflection, key)
57
- live = live_child_ids(owner_class, owner_ids, reflection, key)
58
- owners = merge_owners(historical, live)
59
+ historical, live = @children.call(owner_class, owner_ids, reflection)
60
+ empty = [] #: Array[untyped]
61
+ owners = owner_ids.to_h do |id|
62
+ key = id.to_s
63
+ [key, historical.fetch(key, empty).map { |row| row.fetch(1) } | live.fetch(key, empty)]
64
+ end
59
65
  group_for(reflection.klass, owners.values.flatten, owners: owners)
60
66
  end
61
67
 
@@ -64,82 +70,36 @@ module PaperTrailDiff
64
70
  #: (untyped, untyped) -> [Hash[String, Hash[Symbol, untyped]], Hash[String, Array[untyped]]]
65
71
  def habtm_groups(owner_class, reflection)
66
72
  rows = habtm_rows(owner_class, reflection)
67
- shared = HabtmTransactionOwners.new(owner_class, reflection).shared(rows.map(&:version_id))
68
- memberships = @transaction_ids.to_h do |transaction_id|
69
- [transaction_id.to_s, membership_ids(rows, transaction_id)]
73
+ recorded = rows.group_by { |row| row.version_id.to_s }
74
+ shared = HabtmTransactionOwners.new(owner_class, reflection).shared(recorded.keys)
75
+ [group_for(reflection.klass, rows.map(&:foreign_key_id)),
76
+ memberships(recorded).except(*shared)]
77
+ end
78
+
79
+ # Each root transaction's members, from its rows grouped by transaction.
80
+ #: (Hash[String, Array[untyped]]) -> Hash[String, Array[untyped]]
81
+ def memberships(recorded)
82
+ empty = [] #: Array[untyped]
83
+ @transaction_ids.to_h do |transaction_id|
84
+ key = transaction_id.to_s
85
+ [key, recorded.fetch(key, empty).map(&:foreign_key_id)]
70
86
  end
71
- [group_for(reflection.klass, rows.map(&:foreign_key_id)), memberships.except(*shared)]
72
87
  end
73
88
 
89
+ # Each statement reads up to 500 of the root transactions: SQL Server
90
+ # compiles a long list of ids slowly.
74
91
  #: (untyped, untyped) -> Array[untyped]
75
92
  def habtm_rows(owner_class, reflection)
76
- owner_class.paper_trail.version_association_class.where(
77
- version_id: @transaction_ids,
78
- foreign_key_name: reflection.name
79
- )
80
- end
81
-
82
- #: (Array[untyped], untyped) -> Array[untyped]
83
- def membership_ids(rows, transaction_id)
84
- rows.filter_map do |row|
85
- row.foreign_key_id if row.version_id.to_s == transaction_id.to_s
86
- end
87
- end
88
-
89
- #: (untyped, Array[untyped], untyped, String) -> Hash[String, Array[untyped]]
90
- def historical_child_ids(
91
- owner_class,
92
- owner_ids,
93
- reflection,
94
- foreign_key
95
- )
96
- version_class = owner_class.paper_trail.version_class
97
- association_class = owner_class.paper_trail.version_association_class
98
- relation = historical_child_relation(owner_class, owner_ids, reflection, foreign_key)
99
- pairs = child_candidate_scope(version_class).call(relation).pluck(
100
- association_class.arel_table[:foreign_key_id],
101
- version_class.arel_table[:item_id]
102
- )
103
- group_ids_by_owner(typed_child_ids(reflection.klass, pairs))
104
- end
105
-
106
- #: (untyped, Array[Array[untyped]]) -> Array[Array[untyped]]
107
- def typed_child_ids(model_class, pairs)
108
- pairs.map { |owner_id, child_id| [owner_id, Support.record_id(model_class, child_id)] }
109
- end
110
-
111
- #: (untyped, Array[untyped], untyped, String) -> untyped
112
- def historical_child_relation(owner_class, owner_ids, reflection, foreign_key)
113
- version_class = owner_class.paper_trail.version_class
114
93
  associations = owner_class.paper_trail.version_association_class
115
- VersionTable.joined(associations, version_class).where(
116
- foreign_key_name: foreign_key,
117
- foreign_key_id: owner_ids,
118
- foreign_type: parent_types(owner_class)
119
- ).where(version_class.table_name => { item_type: reflection.klass.base_class.name })
120
- end
121
-
122
- #: (untyped) -> VersionAssociationCandidateScope
123
- def child_candidate_scope(version_class)
124
- VersionAssociationCandidateScope.new(
125
- version_class,
126
- @start_at
127
- )
128
- end
129
-
130
- #: (untyped, Array[untyped], untyped, String) -> Hash[String, Array[untyped]]
131
- def live_child_ids(owner_class, owner_ids, reflection, foreign_key)
132
- # Every row of the table, as history reads them: an STI subclass's
133
- # siblings are left out where children are resolved.
134
- relation = reflection.klass.base_class.unscoped.where(foreign_key => owner_ids)
135
- relation = relation.where(reflection.type => parent_types(owner_class)) if reflection.type
136
- group_ids_by_owner(relation.pluck(foreign_key, reflection.klass.primary_key))
94
+ @transaction_ids.uniq.each_slice(Support::IDS_PER_STATEMENT).flat_map do |slice|
95
+ associations.where(version_id: slice, foreign_key_name: reflection.name)
96
+ end
137
97
  end
138
98
 
139
99
  #: (Hash[String, Hash[Symbol, untyped]], untyped, untyped) -> void
140
100
  def add_identity(groups, model_class, id)
141
101
  group = groups[model_class.name.to_s] ||= { model: model_class, ids: [] }
142
- group.fetch(:ids) << id unless group.fetch(:ids).include?(id)
102
+ group.fetch(:ids) << id
143
103
  end
144
104
 
145
105
  #: (untyped, Array[untyped], ?owners: Hash[String, Array[untyped]]?) -> Hash[String, Hash[Symbol, untyped]]
@@ -151,29 +111,5 @@ module PaperTrailDiff
151
111
  group[:owners] = owners if owners
152
112
  { model_class.name => group }
153
113
  end
154
-
155
- # A set rather than a scan, so that a wide edge stays linear in its width.
156
- #: (Array[Array[untyped]]) -> Hash[String, Array[untyped]]
157
- def group_ids_by_owner(pairs)
158
- grouped = {} #: Hash[String, Set[untyped]]
159
- pairs.each { |owner_id, child_id| (grouped[owner_id.to_s] ||= Set.new) << child_id }
160
- grouped.transform_values(&:to_a)
161
- end
162
-
163
- #: (Hash[String, Array[untyped]], Hash[String, Array[untyped]]) -> Hash[String, Array[untyped]]
164
- def merge_owners(left, right)
165
- left.merge(right) { |_owner, first, second| first | second }
166
- end
167
-
168
- #: (untyped) -> String?
169
- def scalar_foreign_key(reflection)
170
- key = reflection.foreign_key
171
- key unless key.is_a?(Array)
172
- end
173
-
174
- #: (untyped) -> Array[String?]
175
- def parent_types(owner_class)
176
- [nil, '', owner_class.name, owner_class.base_class.name]
177
- end
178
114
  end
179
115
  end