Class: Igniter::Extensions::Contracts::Incremental::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/extensions/contracts/incremental/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiled_graph:, profile:) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
# File 'lib/igniter/extensions/contracts/incremental/session.rb', line 10

def initialize(compiled_graph:, profile:)
  @compiled_graph = compiled_graph
  @profile = profile
  @node_states = {}
  @last_result = nil
end

Instance Attribute Details

#compiled_graphObject (readonly)

Returns the value of attribute compiled_graph.



8
9
10
# File 'lib/igniter/extensions/contracts/incremental/session.rb', line 8

def compiled_graph
  @compiled_graph
end

#profileObject (readonly)

Returns the value of attribute profile.



8
9
10
# File 'lib/igniter/extensions/contracts/incremental/session.rb', line 8

def profile
  @profile
end

Instance Method Details

#run(inputs:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/igniter/extensions/contracts/incremental/session.rb', line 17

def run(inputs:)
  normalized_inputs = Igniter::Contracts::NamedValues.new(inputs)
  current_values = Igniter::Contracts::MutableNamedValues.new
  current_outputs = Igniter::Contracts::MutableNamedValues.new
  current_states = {}

  changed_nodes = []
  skipped_nodes = []
  backdated_nodes = []
  recomputed_count = 0

  compiled_graph.operations.each do |operation|
    if operation.output?
      current_outputs.write(operation.name, current_values.fetch(operation.name))
      next
    end

    if operation.kind == :input
      state = resolve_input_state(operation, normalized_inputs)
      current_states[operation.name] = state
      current_values.write(operation.name, state.value)
      changed_nodes << operation.name if changed_value?(operation.name, state.value)
      next
    end

    dependency_versions = dependency_names_for(operation).each_with_object({}) do |dependency_name, memo|
      if current_states.key?(dependency_name)
        memo[dependency_name] =
          current_states.fetch(dependency_name).value_version
      end
    end

    previous = @node_states[operation.name]

    if previous && previous.dep_snapshot == dependency_versions
      current_states[operation.name] = previous
      current_values.write(operation.name, previous.value)
      skipped_nodes << operation.name
      next
    end

    handler = profile.runtime_handler(operation.kind)
    value = handler.call(
      operation: operation,
      state: current_values,
      outputs: current_outputs,
      inputs: normalized_inputs,
      profile: profile
    )

    value_version =
      if previous && previous.value == value
        backdated_nodes << operation.name
        previous.value_version
      else
        changed_nodes << operation.name if changed_value?(operation.name, value)
        previous ? previous.value_version + 1 : 1
      end

    current_states[operation.name] = NodeState.new(
      name: operation.name,
      value: value,
      value_version: value_version,
      dep_snapshot: dependency_versions
    )
    current_values.write(operation.name, value)
    recomputed_count += 1
  end

  execution_result = Igniter::Contracts::ExecutionResult.new(
    state: current_values.snapshot,
    outputs: current_outputs.snapshot,
    profile_fingerprint: profile.fingerprint,
    compiled_graph: compiled_graph
  )

  result = Result.new(
    execution_result: execution_result,
    changed_nodes: changed_nodes.uniq,
    skipped_nodes: skipped_nodes.uniq,
    backdated_nodes: backdated_nodes.uniq,
    changed_outputs: changed_outputs_for(execution_result),
    recomputed_count: recomputed_count
  )

  @node_states = current_states.freeze
  @last_result = result
end