lib/logstash/plugin_mixins/jdbc/value_tracking.rb in logstash-input-jdbc-4.3.13 vs lib/logstash/plugin_mixins/jdbc/value_tracking.rb in logstash-input-jdbc-4.3.14
- old
+ new
@@ -31,41 +31,52 @@
attr_reader :value
def initialize(handler)
@file_handler = handler
- set_value(get_initial)
+ set_initial
end
- def get_initial
+ def set_initial
# override in subclass
end
def set_value(value)
# override in subclass
end
def write
@file_handler.write(@value)
end
+
+ private
+ def common_set_initial(method_symbol, default)
+ persisted = @file_handler.read
+ if persisted && persisted.respond_to?(method_symbol)
+ @value = persisted
+ else
+ @file_handler.clean
+ @value = default
+ end
+ end
end
class NumericValueTracker < ValueTracking
- def get_initial
- @file_handler.read || 0
+ def set_initial
+ common_set_initial(:gcd, 0)
end
def set_value(value)
return unless value.is_a?(Numeric)
@value = value
end
end
class DateTimeValueTracker < ValueTracking
- def get_initial
- @file_handler.read || DateTime.new(1970)
+ def set_initial
+ common_set_initial(:to_datetime, DateTime.new(1970))
end
def set_value(value)
if value.respond_to?(:to_datetime)
@value = value.to_datetime
@@ -74,12 +85,12 @@
end
end
end
class TimeValueTracker < ValueTracking
- def get_initial
- @file_handler.read || Time.at(0).utc
+ def set_initial
+ common_set_initial(:to_time, Time.at(0).utc)
end
def set_value(value)
if value.respond_to?(:to_time)
@value = value.to_time
@@ -88,9 +99,11 @@
end
end
end
class FileHandler
+ attr_reader :path
+
def initialize(path)
@path = path
@exists = ::File.exist?(@path)
end