class Fluent::ConsistentArrayStorage
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.
Public Class Methods
new(filename)
click to toggle source
# File lib/fluent/plugin/out_router.rb, line 158 def initialize(filename) @store_file = filename @temp_file = filename + ".tmp" end
Public Instance Methods
load()
click to toggle source
Returns the array stored on disc. Returns an empty array if the file does not exist
# File lib/fluent/plugin/out_router.rb, line 172 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)
click to toggle source
# File lib/fluent/plugin/out_router.rb, line 163 def store(array) file = File.open(@temp_file, 'w') file << array.to_json file.close File.rename(@temp_file, @store_file) end