Module: LogStash::Util::PathCache

Extended by:
PathCache
Included in:
PathCache
Defined in:
lib/logstash/util/accessors.rb

Overview

PathCache is a singleton which globally caches the relation between a field reference and its decomposition into a [key, path array] tuple. For example the field reference [foo][baz] is decomposed into [“baz”, [“foo”, “bar”]].

Constant Summary collapse

CACHE =

requiring libraries and defining constants is thread safe in JRuby so PathCache::CACHE will be corretly initialized, once, when accessors.rb will be first required

ThreadSafe::Cache.new

Instance Method Summary collapse

Instance Method Details

#get(field_reference) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/logstash/util/accessors.rb', line 20

def get(field_reference)
  # the "get_or_default(x, nil) || put(x, parse(x))" is ~2x faster than "get || put" because the get call is
  # proxied through the JRuby JavaProxy op_aref method. the correct idiom here would be to use
  # "compute_if_absent(x){parse(x)}" but because of the closure creation, it is ~1.5x slower than
  # "get_or_default || put".
  # this "get_or_default || put" is obviously non-atomic which is not really important here
  # since all threads will set the same value and this cache will stabilize very quickly after the first
  # few events.
  CACHE.get_or_default(field_reference, nil) || CACHE.put(field_reference, parse(field_reference))
end

#parse(field_reference) ⇒ Object



31
32
33
34
# File 'lib/logstash/util/accessors.rb', line 31

def parse(field_reference)
  path = field_reference.split(/[\[\]]/).select{|s| !s.empty?}
  [path.pop, path]
end