Class: RSpec::Core::FilterableItemRepository::QueryOptimized

Inherits:
UpdateOptimized
  • Object
show all
Defined in:
lib/rspec/core/metadata_filter.rb

Overview

This implementation is much more complex, and is optimized for rare (or hopefully no) updates once the queries start. Updates incur a cost as it has to clear the memoization and keep track of applicable keys. Queries will be O(N) the first time an item is provided with a given set of applicable metadata; subsequent queries with items with the same set of applicable metadata will be O(1) due to internal memoization.

This is ideal for use by config, where filterable items (e.g. hooks) are typically added at the start of the process (e.g. in `spec_helper`) and then repeatedly queried as example groups and examples are defined.

Instance Attribute Summary

Attributes inherited from UpdateOptimized

#items_and_filters

Instance Method Summary collapse

Constructor Details

#initialize(applies_predicate) ⇒ QueryOptimized



155
156
157
158
159
160
161
162
# File 'lib/rspec/core/metadata_filter.rb', line 155

def initialize(applies_predicate)
  super
  @applicable_keys   = Set.new
  @proc_keys         = Set.new
  @memoized_lookups  = Hash.new do |hash, |
    hash[] = find_items_for()
  end
end

Instance Method Details

#append(item, metadata) ⇒ Object



164
165
166
167
# File 'lib/rspec/core/metadata_filter.rb', line 164

def append(item, )
  super
  handle_mutation()
end

#delete(item, metadata) ⇒ Object



174
175
176
177
# File 'lib/rspec/core/metadata_filter.rb', line 174

def delete(item, )
  super
  reconstruct_caches
end

#items_for(metadata) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rspec/core/metadata_filter.rb', line 179

def items_for()
  # The filtering of `metadata` to `applicable_metadata` is the key thing
  # that makes the memoization actually useful in practice, since each
  # example and example group have different metadata (e.g. location and
  # description). By filtering to the metadata keys our items care about,
  # we can ignore extra metadata keys that differ for each example/group.
  # For example, given `config.include DBHelpers, :db`, example groups
  # can be split into these two sets: those that are tagged with `:db` and those
  # that are not. For each set, this method for the first group in the set is
  # still an `O(N)` calculation, but all subsequent groups in the set will be
  # constant time lookups when they call this method.
   = ()

  if .any? { |k, _| @proc_keys.include?(k) }
    # It's unsafe to memoize lookups involving procs (since they can
    # be non-deterministic), so we skip the memoization in this case.
    find_items_for()
  else
    @memoized_lookups[]
  end
end

#prepend(item, metadata) ⇒ Object



169
170
171
172
# File 'lib/rspec/core/metadata_filter.rb', line 169

def prepend(item, )
  super
  handle_mutation()
end