Class: Wimp::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/wimp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader_block, name: nil, max_batch_size: Float::INFINITY) ⇒ Batch

Returns a new instance of Batch.



47
48
49
50
51
52
53
54
55
# File 'lib/wimp.rb', line 47

def initialize(loader_block, name: nil, max_batch_size: Float::INFINITY)
  @name = name
  @queue = Concurrent::Array.new
  @lock = Concurrent::ReadWriteLock.new
  @loader_block = loader_block
  @max_batch_size = max_batch_size
  @fulfilled = false
  @results = nil
end

Instance Attribute Details

#fulfilledObject

Returns the value of attribute fulfilled.



45
46
47
# File 'lib/wimp.rb', line 45

def fulfilled
  @fulfilled
end

#nameObject

Returns the value of attribute name.



44
45
46
# File 'lib/wimp.rb', line 44

def name
  @name
end

Instance Method Details

#fulfilled?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/wimp.rb', line 89

def fulfilled?
  @fulfilled
end

#queue(key) ⇒ Object



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
84
85
86
87
# File 'lib/wimp.rb', line 57

def queue(key)
  @queue << key

  DelayedResult.new do
    results = if @fulfilled
                @lock.with_read_lock do
                  @results
                end
              else
                @lock.with_write_lock do
                  if @fulfilled
                    @results
                  else
                    @fulfilled = true
                    r = @loader_block.(@queue)
                    @results = if r.is_a?(DelayedResult)
                      normalize_results(r.value!)
                    else
                      normalize_results(r)
                    end
                  end
                end
              end

    unless results.key?(key)
      raise StandardError, "Batch loader didn't resolve a key: #{key}. Resolved keys: #{results.keys}"
    end

    results[key]
  end
end