Class: LogStash::Filters::Dictionary::File

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/filters/dictionary/file.rb

Direct Known Subclasses

CsvFile, JsonFile, YamlFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, refresh_interval, exact, regex, **file_type_args) ⇒ File

Returns a new instance of File.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/logstash/filters/dictionary/file.rb', line 34

def initialize(path, refresh_interval, exact, regex, **file_type_args)
  @dictionary_path = path
  @refresh_interval = refresh_interval
  @short_refresh = @refresh_interval <= 300
  rw_lock = java.util.concurrent.locks.ReentrantReadWriteLock.new
  @write_lock = rw_lock.writeLock
  @dictionary = Hash.new
  @update_method = method(:merge_dictionary)
  initialize_for_file_type(**file_type_args)
  args = [@dictionary, rw_lock]
  klass = case
          when exact && regex then FetchStrategy::File::ExactRegex
          when exact          then FetchStrategy::File::Exact
          else                     FetchStrategy::File::RegexUnion
          end
  @fetch_strategy = klass.new(*args)
  load_dictionary(raise_exception = true)
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



32
33
34
# File 'lib/logstash/filters/dictionary/file.rb', line 32

def dictionary
  @dictionary
end

#fetch_strategyObject (readonly)

Returns the value of attribute fetch_strategy.



32
33
34
# File 'lib/logstash/filters/dictionary/file.rb', line 32

def fetch_strategy
  @fetch_strategy
end

Class Method Details

.create(path, refresh_interval, refresh_behaviour, exact, regex, **file_type_args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/logstash/filters/dictionary/file.rb', line 12

def self.create(path, refresh_interval, refresh_behaviour, exact, regex, **file_type_args)
  if /\.y[a]?ml$/.match(path)
    instance = YamlFile.new(path, refresh_interval, exact, regex, **file_type_args)
  elsif path.end_with?(".json")
    instance = JsonFile.new(path, refresh_interval, exact, regex)
  elsif path.end_with?(".csv")
    instance = CsvFile.new(path, refresh_interval, exact, regex)
  else
    raise "Translate: Dictionary #{path} has a non valid format"
  end
  if refresh_behaviour == 'merge'
    instance.set_update_strategy(:merge_dictionary)
  elsif refresh_behaviour == 'replace'
    instance.set_update_strategy(:replace_dictionary)
  else
    # we really should never get here
    raise(LogStash::ConfigurationError, "Unknown value for refresh_behaviour=#{refresh_behaviour.to_s}")
  end
end

Instance Method Details

#load_dictionary(raise_exception = false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/logstash/filters/dictionary/file.rb', line 53

def load_dictionary(raise_exception=false)
  begin
    @dictionary_mtime = ::File.mtime(@dictionary_path).to_f
    @update_method.call
  rescue Errno::ENOENT
    logger.warn("dictionary file read failure, continuing with old dictionary", :path => @dictionary_path)
  rescue => e
    loading_exception(e, raise_exception)
  end
end

#reload_dictionaryObject

scheduler executes this method, periodically



103
104
105
106
107
108
109
# File 'lib/logstash/filters/dictionary/file.rb', line 103

def reload_dictionary
  if @short_refresh
    load_dictionary if needs_refresh?
  else
    load_dictionary
  end
end

#set_update_strategy(method_sym) ⇒ Object



64
65
66
67
# File 'lib/logstash/filters/dictionary/file.rb', line 64

def set_update_strategy(method_sym)
  @update_method = method(method_sym)
  self
end