Class: FlatKit::LeafNode

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/flat_kit/leaf_node.rb

Overview

Private: The LeafNode is a wrapper around a Reader object to enable a consistent api for use in the MergeTree

The LeafNode keeps track of the head of the reader list and when its value is used up, it pulls the next value and then notifies the next level of the MergeTree that its value has changed and so should do another play.

If all the data is used up from the reader, it also notifies the next level of that so the next level can remove it from the tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader) ⇒ LeafNode

Returns a new instance of LeafNode.



20
21
22
23
24
25
26
# File 'lib/flat_kit/leaf_node.rb', line 20

def initialize(reader)
  @reader     = reader
  @enum       = @reader.to_enum
  @value      = @enum.next

  @next_level = nil
end

Instance Attribute Details

#next_levelObject

Returns the value of attribute next_level.



18
19
20
# File 'lib/flat_kit/leaf_node.rb', line 18

def next_level
  @next_level
end

#readerObject (readonly)

Returns the value of attribute reader.



16
17
18
# File 'lib/flat_kit/leaf_node.rb', line 16

def reader
  @reader
end

#valueObject (readonly)

Returns the value of attribute value.



16
17
18
# File 'lib/flat_kit/leaf_node.rb', line 16

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



66
67
68
69
70
# File 'lib/flat_kit/leaf_node.rb', line 66

def <=>(other)
  return -1 if other.sentinel?

  value <=> (other.value)
end

#finished?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/flat_kit/leaf_node.rb', line 62

def finished?
  @enum && @value.nil?
end

#leafObject



40
41
42
# File 'lib/flat_kit/leaf_node.rb', line 40

def leaf
  self
end

#leaf?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/flat_kit/leaf_node.rb', line 36

def leaf?
  true
end

#nextObject



53
54
55
56
57
58
59
60
# File 'lib/flat_kit/leaf_node.rb', line 53

def next
  begin
    @value = @enum.next
  rescue StopIteration
    @value = nil
  end
  @value
end

#sentinel?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/flat_kit/leaf_node.rb', line 32

def sentinel?
  false
end

#update_and_replayObject



44
45
46
47
48
49
50
51
# File 'lib/flat_kit/leaf_node.rb', line 44

def update_and_replay
  self.next
  if finished?
    ::FlatKit.logger.debug "#{reader.source} has finished reading #{reader.count} records"
    next_level.player_finished(self)
  end
  next_level.play
end

#winnerObject



28
29
30
# File 'lib/flat_kit/leaf_node.rb', line 28

def winner
  value
end