Class: Fluent::Plugin::Buffer::Metadata

Inherits:
Struct
  • Object
show all
Defined in:
lib/fluent/plugin/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timekey, tag, variables) ⇒ Metadata

Returns a new instance of Metadata.



73
74
75
# File 'lib/fluent/plugin/buffer.rb', line 73

def initialize(timekey, tag, variables)
  super(timekey, tag, variables, 0)
end

Instance Attribute Details

#seqObject

Returns the value of attribute seq

Returns:

  • (Object)

    the current value of seq



72
73
74
# File 'lib/fluent/plugin/buffer.rb', line 72

def seq
  @seq
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



72
73
74
# File 'lib/fluent/plugin/buffer.rb', line 72

def tag
  @tag
end

#timekeyObject

Returns the value of attribute timekey

Returns:

  • (Object)

    the current value of timekey



72
73
74
# File 'lib/fluent/plugin/buffer.rb', line 72

def timekey
  @timekey
end

#variablesObject

Returns the value of attribute variables

Returns:

  • (Object)

    the current value of variables



72
73
74
# File 'lib/fluent/plugin/buffer.rb', line 72

def variables
  @variables
end

Instance Method Details

#<=>(o) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fluent/plugin/buffer.rb', line 123

def <=>(o)
  timekey2 = o.timekey
  tag2 = o.tag
  variables2 = o.variables
  if (!!timekey ^ !!timekey2) || (!!tag ^ !!tag2) || (!!variables ^ !!variables2)
    # One has value in a field, but another doesn't have value in same field
    # This case occurs very rarely
    if timekey == timekey2 # including the case of nil == nil
      if tag == tag2
        cmp_variables(variables, variables2)
      elsif tag.nil?
        -1
      elsif tag2.nil?
        1
      else
        tag <=> tag2
      end
    elsif timekey.nil?
      -1
    elsif timekey2.nil?
      1
    else
      timekey <=> timekey2
    end
  else
    # objects have values in same field pairs (comparison with non-nil and nil doesn't occur here)
    (timekey <=> timekey2 || 0).nonzero? || # if `a <=> b` is nil, then both are nil
      (tag <=> tag2 || 0).nonzero? ||
      cmp_variables(variables, variables2)
  end
end

#cmp_variables(v1, v2) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fluent/plugin/buffer.rb', line 87

def cmp_variables(v1, v2)
  if v1.nil? && v2.nil?
    return 0
  elsif v1.nil? # v2 is non-nil
    return -1
  elsif v2.nil? # v1 is non-nil
    return 1
  end
  # both of v1 and v2 are non-nil
  v1_sorted_keys = v1.keys.sort
  v2_sorted_keys = v2.keys.sort
  if v1_sorted_keys != v2_sorted_keys
    if v1_sorted_keys.size == v2_sorted_keys.size
      v1_sorted_keys <=> v2_sorted_keys
    else
      v1_sorted_keys.size <=> v2_sorted_keys.size
    end
  else
    v1_sorted_keys.each do |k|
      a = v1[k]
      b = v2[k]
      if a && b && a != b
        return a <=> b
      elsif a && b || (!a && !b) # same value (including both are nil)
        next
      elsif a # b is nil
        return 1
      else # a is nil (but b is non-nil)
        return -1
      end
    end

    0
  end
end

#dup_nextObject



77
78
79
80
81
# File 'lib/fluent/plugin/buffer.rb', line 77

def dup_next
  m = dup
  m.seq = seq + 1
  m
end

#empty?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/fluent/plugin/buffer.rb', line 83

def empty?
  timekey.nil? && tag.nil? && variables.nil?
end

#hashObject

This is an optimization code. Current Struct’s implementation is comparing all data. github.com/ruby/ruby/blob/0623e2b7cc621b1733a760b72af246b06c30cf96/struct.c#L1200-L1203 Actually this overhead is very small but this class is generated *per chunk* (and used in hash object). This means that this class is one of the most called object in Fluentd. See github.com/fluent/fluentd/pull/2560



160
161
162
# File 'lib/fluent/plugin/buffer.rb', line 160

def hash
  timekey.hash
end