Module: LogStash::Util

Defined in:
lib/logstash/util.rb,
lib/logstash/namespace.rb,
lib/logstash/util/accessors.rb,
lib/logstash/util/decorators.rb,
lib/logstash/util/plugin_version.rb

Defined Under Namespace

Modules: Decorators, FileTools, JavaVersion, PathCache, Require, SocketPeer Classes: Accessors, Charset, Password, PluginVersion

Constant Summary collapse

UNAME =
case RbConfig::CONFIG["host_os"]
  when /^linux/; "linux"
  else; RbConfig::CONFIG["host_os"]
end
PR_SET_NAME =
15

Class Method Summary collapse

Class Method Details

.hash_merge(dst, src) ⇒ Object

Merge hash ‘src’ into ‘dst’ nondestructively

Duplicate keys will become array values

src, dst[“foo”

]



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logstash/util.rb', line 32

def self.hash_merge(dst, src)
  src.each do |name, svalue|
    if dst.include?(name)
      dvalue = dst[name]
      if dvalue.is_a?(Hash) && svalue.is_a?(Hash)
        dvalue = hash_merge(dvalue, svalue)
      elsif svalue.is_a?(Array)
        if dvalue.is_a?(Array)
          # merge arrays without duplicates.
          dvalue |= svalue
        else
          dvalue = [dvalue] | svalue
        end
      else
        if dvalue.is_a?(Array)
          dvalue << svalue unless dvalue.include?(svalue)
        else
          dvalue = [dvalue, svalue] unless dvalue == svalue
        end
      end

      dst[name] = dvalue
    else
      # dst doesn't have this key, just set it.
      dst[name] = svalue
    end
  end

  return dst
end

.hash_merge_many(*hashes) ⇒ Object

def self.hash_merge



100
101
102
103
104
105
106
# File 'lib/logstash/util.rb', line 100

def self.hash_merge_many(*hashes)
  dst = {}
  hashes.each do |hash|
    hash_merge_with_dups(dst, hash)
  end
  return dst
end

.hash_merge_with_dups(dst, src) ⇒ Object

Merge hash ‘src’ into ‘dst’ nondestructively

Duplicate keys will become array values Arrays merged will simply be appended.

src, dst[“foo”

]



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/logstash/util.rb', line 69

def self.hash_merge_with_dups(dst, src)
  src.each do |name, svalue|
    if dst.include?(name)
      dvalue = dst[name]
      if dvalue.is_a?(Hash) && svalue.is_a?(Hash)
        dvalue = hash_merge(dvalue, svalue)
      elsif svalue.is_a?(Array)
        if dvalue.is_a?(Array)
          # merge arrays without duplicates.
          dvalue += svalue
        else
          dvalue = [dvalue] + svalue
        end
      else
        if dvalue.is_a?(Array)
          dvalue << svalue unless dvalue.include?(svalue)
        else
          dvalue = [dvalue, svalue] unless dvalue == svalue
        end
      end

      dst[name] = dvalue
    else
      # dst doesn't have this key, just set it.
      dst[name] = svalue
    end
  end

  return dst
end

.normalize(o) ⇒ Object

identity function, pure Ruby object don’t need normalization.



122
123
124
125
126
127
128
129
130
131
# File 'lib/logstash/util.rb', line 122

def self.normalize(o)
  case o
  when Java::JavaUtil::LinkedHashMap
    o.inject({}){|r, (k, v)| r[k] = normalize(v); r}
  when Java::JavaUtil::ArrayList
    o.map{|i| normalize(i)}
  else
    o
  end
end

.set_thread_name(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/logstash/util.rb', line 12

def self.set_thread_name(name)
  if RUBY_ENGINE == "jruby"
    # Keep java and ruby thread names in sync.
    Java::java.lang.Thread.currentThread.setName(name)
  end
  Thread.current[:name] = name

  if UNAME == "linux"
    require "logstash/util/prctl"
    # prctl PR_SET_NAME allows up to 16 bytes for a process name
    # since MRI 1.9, JRuby, and Rubinius use system threads for this.
    LibC.prctl(PR_SET_NAME, name[0..16], 0, 0, 0)
  end
end

.stringify_symbols(o) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/logstash/util.rb', line 139

def self.stringify_symbols(o)
  case o
  when Hash
    o.inject({}){|r, (k, v)| r[k.is_a?(Symbol) ? k.to_s : k] = stringify_symbols(v); r}
  when Array
    o.map{|i| stringify_symbols(i)}
  when Symbol
    o.to_s
  else
    o
  end
end