Class: MapReduce::Queue

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/scrappy/agent/map_reduce.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



11
12
13
14
15
# File 'lib/scrappy/agent/map_reduce.rb', line 11

def initialize
  super
  @items   = []
  @history = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



9
10
11
# File 'lib/scrappy/agent/map_reduce.rb', line 9

def history
  @history
end

#itemsObject (readonly)

Returns the value of attribute items.



9
10
11
# File 'lib/scrappy/agent/map_reduce.rb', line 9

def items
  @items
end

Instance Method Details

#<<(value) ⇒ Object



32
33
34
# File 'lib/scrappy/agent/map_reduce.rb', line 32

def << value
  push value
end

#empty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/scrappy/agent/map_reduce.rb', line 44

def empty?
  synchronize { @items.empty? }
end

#pop {|item| ... } ⇒ Object

Yields:

  • (item)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/scrappy/agent/map_reduce.rb', line 17

def pop
  yielded = false
  item = nil
  synchronize do
    item = @items.shift
    @history << item if item
    if @items.empty?
      yield item if (block_given? and item)
      yielded = true
    end
  end
  yield item if (block_given? and not yielded)
  item
end

#push(value) ⇒ Object



36
37
38
# File 'lib/scrappy/agent/map_reduce.rb', line 36

def push value
  synchronize { @items << value }
end

#push_unless_done(value) ⇒ Object



40
41
42
# File 'lib/scrappy/agent/map_reduce.rb', line 40

def push_unless_done value
  synchronize { @items << value if !@history.include?(value) and !@items.include?(value) }
end