Class: IgniterLang::TemporalAccessRuntime::MemoryBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter_lang/temporal_access_runtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemoryBackend

Returns a new instance of MemoryBackend.



305
306
307
308
309
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 305

def initialize
  @append_observations = []
  @events = Hash.new { |hash, key| hash[key] = [] }
  @access_observations = []
end

Instance Attribute Details

#access_observationsObject (readonly)

Returns the value of attribute access_observations.



303
304
305
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 303

def access_observations
  @access_observations
end

#append_observationsObject (readonly)

Returns the value of attribute append_observations.



303
304
305
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 303

def append_observations
  @append_observations
end

#eventsObject (readonly)

Returns the value of attribute events.



303
304
305
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 303

def events
  @events
end

Instance Method Details

#append(subject, valid_from, value, value_type:) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 318

def append(subject, valid_from, value, value_type:)
  payload = {
    "kind" => "history_append_observation",
    "subject" => subject,
    "valid_from" => valid_from,
    "value" => value,
    "value_type" => value_type
  }
  observation = payload.merge(
    "observation_id" => "obs/history_append/#{Canonical.short_hash(payload)}",
    "observed_at" => valid_from,
    "temporal" => {
      "axis" => "valid_time",
      "as_of" => valid_from,
      "lifecycle" => "durable"
    }
  )
  @append_observations << observation
  observation
end

#append_bihistory_event(event) ⇒ Object



358
359
360
361
362
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 358

def append_bihistory_event(event)
  history_ref = event.fetch("history_ref")
  @events[history_ref] << event
  @events[history_ref].sort_by! { |entry| [entry.fetch("valid_from"), entry.fetch("tx_from"), entry.fetch("event_id")] }
end

#bihistory_at(history_ref, vt:, tt:, node_name:) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 364

def bihistory_at(history_ref, vt:, tt:, node_name:)
  vt_time = parse_axis!("vt", vt)
  tt_time = parse_axis!("tt", tt)
  selected = @events.fetch(history_ref, [])
    .select { |event| covers_valid_time?(event, vt_time) && Time.iso8601(event.fetch("tx_from")) <= tt_time }
    .max_by { |event| [Time.iso8601(event.fetch("tx_from")), event.fetch("event_id")] }
  result = selected ? Option.some(selected.fetch("value")) : Option.none
  observation = bihistory_access_observation(history_ref, vt, tt, node_name, selected, result)
  @access_observations << observation
  [result, observation]
end

#history_at(subject, as_of) ⇒ Object



339
340
341
342
343
344
345
346
347
348
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 339

def history_at(subject, as_of)
  as_of_time = parse_axis!("as_of", as_of)
  selected = @append_observations
    .select { |obs| obs.fetch("subject") == subject && Time.iso8601(obs.fetch("valid_from")) <= as_of_time }
    .max_by { |obs| Time.iso8601(obs.fetch("valid_from")) }
  result = selected ? Option.some(selected.fetch("value")) : Option.none
  observation = history_access_observation(subject, as_of, selected, result)
  @access_observations << observation
  [result, observation]
end

#read_as_of(subject, as_of) ⇒ Object



350
351
352
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 350

def read_as_of(subject, as_of)
  history_at(subject, as_of)
end

#seed(events) ⇒ Object



354
355
356
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 354

def seed(events)
  events.each { |event| append_bihistory_event(event) }
end

#seed_append_observations(observations) ⇒ Object



311
312
313
314
315
316
# File 'lib/igniter_lang/temporal_access_runtime.rb', line 311

def seed_append_observations(observations)
  observations.each do |observation|
    append(observation.fetch("subject"), observation.fetch("valid_from"), observation.fetch("value"),
           value_type: observation.fetch("value_type"))
  end
end