Class: RSpecTracer::Storage::LazySnapshot Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_tracer/storage/lazy_snapshot.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Lazy-reading view returned by JsonBackend#load_graph. Presents the same field surface as Storage::Snapshot but defers the disk read + deserialization for each field until the caller touches it.

Rationale: on a large cache (100 MB+), eager-reading every file at setup dominates warm-run startup (issue #17). Engine at setup touches 10/15 fields; reporters never touch the previous snapshot; third-party tooling often reads one or two. The 3-5 fields the Engine does NOT touch at setup (duplicate_examples, reverse_dependency, env_dependency; examples_coverage is deferred to finalize per the seed refactor) save their full disk+parse cost for every run.

Duck-types Snapshot: every Struct member becomes a method here, respond_to? returns true for each, to_h materializes the whole thing. Callers doing prev.send(field) or prev.field work unchanged.

Thread-safety: the memoization Hash is not guarded. Engine is single-threaded per run; parallel_tests workers each own their own LazySnapshot instance. If a future caller reads fields from multiple threads, wrap with a Monitor at construct time.

Constant Summary collapse

LAZY_FIELDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

(Snapshot.members - %i[schema_version run_id]).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_version:, run_id:, reader:) ⇒ LazySnapshot

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LazySnapshot.



34
35
36
37
38
39
# File 'lib/rspec_tracer/storage/lazy_snapshot.rb', line 34

def initialize(schema_version:, run_id:, reader:)
  @schema_version = schema_version
  @run_id = run_id
  @reader = reader
  @loaded = {}
end

Instance Attribute Details

#run_idObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/rspec_tracer/storage/lazy_snapshot.rb', line 32

def run_id
  @run_id
end

#schema_versionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/rspec_tracer/storage/lazy_snapshot.rb', line 32

def schema_version
  @schema_version
end

Instance Method Details

#to_hObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hash view matching Struct#to_h so callers composing snapshots (merge pipelines, reporter helpers) get the familiar shape. Forces every field to materialize, so only call this when the full cache is genuinely required.



53
54
55
# File 'lib/rspec_tracer/storage/lazy_snapshot.rb', line 53

def to_h
  Snapshot.members.to_h { |m| [m, public_send(m)] }
end

#to_snapshotObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Materialize a full eager Snapshot. Use when an API requires the Struct type (Merger input, backends that accept Snapshot on save). Reads every field.



60
61
62
# File 'lib/rspec_tracer/storage/lazy_snapshot.rb', line 60

def to_snapshot
  Snapshot.new(**to_h)
end