Class: Fluent::Counter::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/counter/store.rb

Defined Under Namespace

Classes: DummyParent

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Store

Returns a new instance of Store.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/counter/store.rb', line 29

def initialize(opt = {})
  @log = opt[:log] || $log

  # Notice: This storage is not be implemented auto save.
  @storage = Plugin.new_storage('local', parent: DummyParent.new(@log))
  conf = if opt[:path]
           {'persistent' => true, 'path' => opt[:path] }
         else
           {'persistent' => false }
         end
  @storage.configure(Fluent::Config::Element.new('storage', {}, conf, []))
end

Class Method Details

.gen_key(scope, key) ⇒ Object



25
26
27
# File 'lib/fluent/counter/store.rb', line 25

def self.gen_key(scope, key)
  "#{scope}\t#{key}"
end

Instance Method Details

#delete(key) ⇒ Object



102
103
104
105
# File 'lib/fluent/counter/store.rb', line 102

def delete(key)
  ret = @storage.delete(key) or raise UnknownKey.new("`#{key}` doesn't exist in counter")
  build_response(ret)
end

#get(key, raise_error: false, raw: false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fluent/counter/store.rb', line 85

def get(key, raise_error: false, raw: false)
  ret = if raise_error
          @storage.get(key) or raise UnknownKey.new("`#{key}` doesn't exist in counter")
        else
          @storage.get(key)
        end
  if raw
    ret
  else
    ret && build_response(ret)
  end
end

#inc(key, data, force: false) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fluent/counter/store.rb', line 107

def inc(key, data, force: false)
  value = data.delete('value')
  init(key, data) if !key?(key) && force
  v = get(key, raise_error: true, raw: true)
  valid_type!(v, value)

  v['total'] += value
  v['current'] += value
  t = EventTime.now
  v['last_modified_at'] = [t.sec, t.nsec]
  @storage.put(key, v)

  build_response(v)
end

#init(key, data, ignore: false) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/counter/store.rb', line 74

def init(key, data, ignore: false)
  ret = if v = get(key)
          raise InvalidParams.new("#{key} already exists in counter") unless ignore
          v
        else
          @storage.put(key, build_value(data))
        end

  build_response(ret)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/fluent/counter/store.rb', line 98

def key?(key)
  !!@storage.get(key)
end

#reset(key) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fluent/counter/store.rb', line 122

def reset(key)
  v = get(key, raise_error: true, raw: true)
  success = false
  old_data = v.dup
  now = EventTime.now
  last_reset_at = EventTime.new(*v['last_reset_at'])

  #  Does it need reset?
  if (last_reset_at + v['reset_interval']) <= now
    success = true
    v['current'] = initial_value(v['type'])
    t = [now.sec, now.nsec]
    v['last_reset_at'] = t
    v['last_modified_at'] = t
    @storage.put(key, v)
  end

  {
    'elapsed_time' => now - last_reset_at,
    'success' => success,
    'counter_data' => build_response(old_data)
  }
end

#startObject



66
67
68
# File 'lib/fluent/counter/store.rb', line 66

def start
  @storage.load
end

#stopObject



70
71
72
# File 'lib/fluent/counter/store.rb', line 70

def stop
  @storage.save
end