Class: RubyReactor::Map::ResultEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby_reactor/map/result_enumerator.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_id, reactor_class_name, strict_ordering: true, batch_size: DEFAULT_BATCH_SIZE) ⇒ ResultEnumerator

Returns a new instance of ResultEnumerator.



12
13
14
15
16
17
18
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 12

def initialize(map_id, reactor_class_name, strict_ordering: true, batch_size: DEFAULT_BATCH_SIZE)
  @map_id = map_id
  @reactor_class_name = reactor_class_name
  @strict_ordering = strict_ordering
  @batch_size = batch_size
  @storage = RubyReactor.configuration.storage_adapter
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



10
11
12
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 10

def batch_size
  @batch_size
end

#map_idObject (readonly)

Returns the value of attribute map_id.



10
11
12
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 10

def map_id
  @map_id
end

#reactor_class_nameObject (readonly)

Returns the value of attribute reactor_class_name.



10
11
12
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 10

def reactor_class_name
  @reactor_class_name
end

#strict_orderingObject (readonly)

Returns the value of attribute strict_ordering.



10
11
12
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 10

def strict_ordering
  @strict_ordering
end

Instance Method Details

#[](index) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 62

def [](index)
  return nil if index.negative? || index >= count

  results = @storage.retrieve_map_results_batch(
    @map_id,
    @reactor_class_name,
    offset: index,
    limit: 1,
    strict_ordering: @strict_ordering
  )

  return nil if results.empty?

  wrap_result(results.first)
end

#any?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 58

def any?
  !empty?
end

#countObject Also known as: size, length



48
49
50
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 48

def count
  @count ||= @storage.count_map_results(@map_id, @reactor_class_name)
end

#eachObject



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
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 20

def each
  return enum_for(:each) unless block_given?

  if @strict_ordering
    count.times do |i|
      yield self[i]
    end
  else
    offset = 0
    loop do
      results = @storage.retrieve_map_results_batch(
        @map_id,
        @reactor_class_name,
        offset: offset,
        limit: @batch_size,
        strict_ordering: @strict_ordering
      )

      break if results.empty?

      results.each { |result| yield wrap_result(result) }

      offset += results.size
      break if results.size < @batch_size
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 54

def empty?
  count.zero?
end

#failuresObject



90
91
92
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 90

def failures
  lazy.select { |result| result.is_a?(RubyReactor::Failure) }.map(&:error)
end

#firstObject



78
79
80
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 78

def first
  self[0]
end

#lastObject



82
83
84
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 82

def last
  self[count - 1]
end

#successesObject



86
87
88
# File 'lib/ruby_reactor/map/result_enumerator.rb', line 86

def successes
  lazy.select { |result| result.is_a?(RubyReactor::Success) }.map(&:value)
end