-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
batch.rb
59 lines (49 loc) · 1.78 KB
/
batch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
require "active_record/batched_methods/type_mismatch"
module ActiveRecord::BatchedMethods
# Represents a batch of things that will be preloaded together
class Batch # :nodoc:
def initialize(klass)
@klass = klass
@entries = Set.new
@result_sets = {}
end
def add(entry)
unless entry.is_a?(@klass)
raise TypeMismatch.new("Cannot add object of type #{entry.class} to batch of #{@klass}")
end
@entries << entry
end
def result_for(name, args, entry)
results = result_set_for(name, args, entry)
results = perform_for(name, args, entry) unless results
results[entry]
end
private
# Get the hash that contains the result for a given entry.
#
# Note: It's important to maintain seprate hashes here instead of merging
# since the hash _may_ be defined using Hash#default_proc
def result_set_for(name, args, entry)
@result_sets.dig(name, args, entry)
end
# Perform the batched method for the given name & entry
def perform_for(name, args, entry)
# Determine the slice to run which is either all, or an appropriate
# slice containing the given entry
method = @klass.batched_methods.fetch(name)
batch_size = method.batch_size
slice = batch_size ?
@entries.each_slice(batch_size).detect { |b| b.include?(entry) } :
@entries
# Call the method with the slice and add the appropriate references to @result_sets
slice_results = method.call(slice, *args)
slice.each do |object|
@result_sets[name] ||= {}
@result_sets[name][args] ||= {}
@result_sets[name][args][object] = slice_results
end
slice_results
end
end
end