Class: Amazon::Util::ProactiveResults

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/amazon/util/proactive_results.rb

Overview

ProactiveResults is not as lazy as LazyResults The constructor takes a block which should accept a pagenumber and return a page worth of results.

Constant Summary collapse

THREADPOOL_SIZE =
3

Instance Method Summary collapse

Constructor Details

#initialize(exception_handler = nil, &feeder) ⇒ ProactiveResults

Returns a new instance of ProactiveResults.



18
19
20
21
22
23
# File 'lib/amazon/util/proactive_results.rb', line 18

def initialize( exception_handler=nil, &feeder )
  @feeder = feeder
  @eh = exception_handler
  @tp = nil
  self.flush
end

Instance Method Details

#[](index) ⇒ Object

index into the result set. if we haven’t loaded enough, will wait until we have



55
56
57
58
# File 'lib/amazon/util/proactive_results.rb', line 55

def []( index )
  feedme while !@done and index >= @truth.size
  return @truth[index]
end

#each(&block) ⇒ Object

iterate over entire result set, waiting for threads to finish where necessary



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/amazon/util/proactive_results.rb', line 40

def each( &block ) # :yields: item
  index = 0
  while true
    if index >= @truth.size
      break if @done
      feedme
    else
      yield @truth[index]
      index += 1
    end
  end
end

#flushObject

clear the result set and start over again



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/amazon/util/proactive_results.rb', line 26

def flush
  @tp.finish unless @tp.nil?
  @tp = ThreadPool.new(THREADPOOL_SIZE, @eh)
  @done = false
  @inflight = [].extend(MonitorMixin)
  @current_page = 1
  @truth = []
  1.upto(THREADPOOL_SIZE) do |page|
    getPage(page)
  end
end

#inspectObject

:nodoc:



67
68
69
# File 'lib/amazon/util/proactive_results.rb', line 67

def inspect # :nodoc:
  "#<Amazon::Util::ProactiveResults truth_size=#{@truth.size} pending_pages=#{@pending.size}>"
end

#to_aObject

wait for the entire results set to be populated, then return an array of the results



62
63
64
65
# File 'lib/amazon/util/proactive_results.rb', line 62

def to_a
  feedme until @done
  return @truth.dup
end