class ScoutApm::ScoredItemSet
Constants
- ARBITRARILY_LARGE
-
A number larger than any score we will actually get.
- DEFAULT_MAX_SIZE
-
Without otherwise saying, default the size to this
Attributes
Public Class Methods
Source
# File lib/scout_apm/scored_item_set.rb, line 20 def initialize(max_size = DEFAULT_MAX_SIZE) @items = {} @max_size = max_size end
Public Instance Methods
Source
# 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
This function is a large if statement, with a few branches. See inline comments for each branch.
Source
# File lib/scout_apm/scored_item_set.rb, line 25 def each items.each do |(_, (_, item))| yield item end end
Source
# File lib/scout_apm/scored_item_set.rb, line 67 def eql?(other) items == other.items end
Equal to another set only if exactly the same set of items is inside
Also aliased as: ==
Private Instance Methods
Source
# File lib/scout_apm/scored_item_set.rb, line 75 def full? items.size >= max_size end
Source
# File lib/scout_apm/scored_item_set.rb, line 79 def store!(new_item) if !new_item.name.nil? # Never store a nil name. items[new_item.name] = [new_item.score, new_item.call] end end