Class: Fluent::ConsistentArrayStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/out_router.rb

Overview

ConsistentArrayStorage serializes/deserializes an array to disc. It uses two files. First it writes the serialized array into a tmp file and then renames the tmp file to the correct filename. The rename action is fast and Linux gives a guarantee that the rename operation is atomic (on local discs). This removes the risk of any kind of crash leaving the @store_file file in a bad state.

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ ConsistentArrayStorage

Returns a new instance of ConsistentArrayStorage.



142
143
144
145
# File 'lib/fluent/plugin/out_router.rb', line 142

def initialize(filename)
  @store_file = filename
  @temp_file = filename + ".tmp"
end

Instance Method Details

#loadObject

Returns the array stored on disc. Returns an empty array if the file does not exist



156
157
158
159
160
161
162
163
164
165
# File 'lib/fluent/plugin/out_router.rb', line 156

def load
  if File.exists?(@store_file) then
    file = File.open(@store_file, 'r')
    json_string = file.read
    file.close
    JSON.parse(json_string)
  else
    []
  end
end

#store(array) ⇒ Object



147
148
149
150
151
152
# File 'lib/fluent/plugin/out_router.rb', line 147

def store(array)
  file = File.open(@temp_file, 'w')
  file << array.to_json
  file.close
  File.rename(@temp_file, @store_file)
end