Class: Exwiw::Adapter::MongodbAdapter::StreamingResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/exwiw/adapter/mongodb_adapter.rb

Overview

A lazy, streaming stand-in for the materialized result array #execute used to return. Wrapping the Mongo cursor (instead of ‘.to_a`) keeps the dump’s dominant memory cost — the full result set — off the heap: the Runner pulls documents through ‘each_slice`, so at most one chunk of documents (plus the small propagation-key arrays) is resident at a time, even for large or embed-heavy collections.

It satisfies the two things the Runner asks of an execute result:

- #size: the record count, used to skip empty collections and to log.
  Answered with a `count_documents` query (which only walks index
  entries, far cheaper than fetching every document) rather than by
  draining the cursor.
- #each (via Enumerable / each_slice): a single streaming pass over the
  cursor. While streaming it captures — per propagation key, BEFORE
  handing the document to the caller's masking — the values downstream
  children will `$in`-match against, publishing them into @state once
  the pass completes.

Contract note: unlike the old ‘.to_a` execute, which populated @state eagerly, this defers state capture until the result is consumed. The Runner always fully consumes a non-empty result before any child collection is processed, so propagation is unaffected; a caller that only needs @state must iterate the result (e.g. `.to_a`).

Instance Method Summary collapse

Constructor Details

#initialize(view:, collection:, keys:, state:) ⇒ StreamingResult

Returns a new instance of StreamingResult.



43
44
45
46
47
48
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 43

def initialize(view:, collection:, keys:, state:)
  @view = view
  @collection = collection
  @keys = keys
  @state = state
end

Instance Method Details

#eachObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 55

def each
  return enum_for(:each) { size } unless block_given?

  captured = @keys.each_with_object({}) { |key, acc| acc[key] = [] }
  @view.each do |doc|
    @keys.each { |key| captured[key] << doc[key] }
    yield doc
  end
  @state[@collection] = captured
  self
end

#sizeObject Also known as: length



50
51
52
# File 'lib/exwiw/adapter/mongodb_adapter.rb', line 50

def size
  @size ||= @view.count_documents
end