Class: Ruptr::GoldenMaster::Store::FS

Inherits:
Ruptr::GoldenMaster::Store show all
Defined in:
lib/ruptr/golden_master.rb

Instance Method Summary collapse

Methods inherited from Ruptr::GoldenMaster::Store

#get_golden, #set_trial

Constructor Details

#initialize(golden_path:, trial_path:) ⇒ FS

The same store must be usable from multiple forked process. For each process, trial data is accumulated in @trial and appended to the @trial_tmp_path file before the process exits.



96
97
98
99
100
101
102
103
# File 'lib/ruptr/golden_master.rb', line 96

def initialize(golden_path:, trial_path:)
  super()
  @golden_path = golden_path
  @golden_tmp_path = Pathname("#{golden_path}.new")
  @trial_path = trial_path
  @trial_tmp_path = Pathname("#{trial_path}.new")
  @trial_tmp_path.truncate(0) if @trial_tmp_path.exist?
end

Instance Method Details

#accept_trial(preserve: nil) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/ruptr/golden_master.rb', line 133

def accept_trial(preserve: nil)
  load_trial
  super
  @golden = @golden.to_a.sort.to_h # keep golden file identical if possible
  @golden_tmp_path.open('w') { |io| Marshal.dump(@golden, io) }
  @golden_tmp_path.rename(@golden_path)
end

#dump_trialObject



128
129
130
131
# File 'lib/ruptr/golden_master.rb', line 128

def dump_trial
  flush_trial
  @trial_tmp_path.rename(@trial_path) if @trial_tmp_path.exist?
end

#flush_trialObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/ruptr/golden_master.rb', line 117

def flush_trial
  return if @trial.empty?
  @trial_tmp_path.open('a') do |io|
    # Ensuring (hopefully) that the Marshal chunk is appended with a single write(2) call.
    io.sync = true
    # XXX Would be better if Marshal errors were caught earlier.
    io.write(Marshal.dump(@trial))
  end
  @trial.clear
end

#load_goldenObject



105
106
107
108
# File 'lib/ruptr/golden_master.rb', line 105

def load_golden
  # NOTE: The golden file is always a single Marshal chunk.
  @golden = @golden_path.exist? ? @golden_path.open { |io| Marshal.load(io) } : {}
end

#load_trialObject



110
111
112
113
114
115
# File 'lib/ruptr/golden_master.rb', line 110

def load_trial
  # NOTE: The trial file may be made up of multiple Marshal chunks.
  @trial = {}.tap do |h|
    @trial_path.open { |io| h.merge!(Marshal.load(io)) until io.eof? } if @trial_path.exist?
  end
end