Class: ScoutApm::ScoredItemSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scout_apm/scored_item_set.rb

Constant Summary collapse

ARBITRARILY_LARGE =

A number larger than any score we will actually get.

100000000
DEFAULT_MAX_SIZE =

Without otherwise saying, default the size to this

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = DEFAULT_MAX_SIZE) ⇒ ScoredItemSet

Returns a new instance of ScoredItemSet.



20
21
22
23
# File 'lib/scout_apm/scored_item_set.rb', line 20

def initialize(max_size = DEFAULT_MAX_SIZE)
  @items = {}
  @max_size = max_size
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



18
19
20
# File 'lib/scout_apm/scored_item_set.rb', line 18

def items
  @items
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



17
18
19
# File 'lib/scout_apm/scored_item_set.rb', line 17

def max_size
  @max_size
end

Instance Method Details

#<<(new_item) ⇒ Object

This function is a large if statement, with a few branches. See inline comments for each branch.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scout_apm/scored_item_set.rb', line 32

def <<(new_item)
  return if new_item.name == :unknown

  # If we have this item in the hash already, compare the new & old ones, and store
  # the new one only if it's higher score.
  if items.has_key?(new_item.name)
    if new_item.score > items[new_item.name].first
      store!(new_item)
    end


  # If the set is full, then we have to see if we evict anything to store
  # this one
  elsif full?
    smallest_name, smallest_score = items.inject([nil, ARBITRARILY_LARGE]) do |(memo_name, memo_score), (name, (stored_score, _))|
      if stored_score < memo_score
        [name, stored_score]
      else
        [memo_name, memo_score]
      end
    end

    if smallest_score < new_item.score
      items.delete(smallest_name)
      store!(new_item)
    end


  # Set isn't full, and we've not seen this new_item, so go ahead and store it.
  else
    store!(new_item)
  end
end

#eachObject



25
26
27
28
29
# File 'lib/scout_apm/scored_item_set.rb', line 25

def each
  items.each do |(_, (_, item))|
    yield item
  end
end