class Counter::LossyCounter

Public Class Methods

new(config) click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 125
def initialize(config)
    @gamma = config.has_key?(:gamma) ? config[:gamma].to_f : 0.005
    @epsilon = config.has_key?(:epsilon) ? config[:epsilon].to_f : 0.001
    @current = 1
    @freq_counter = {}
    @delta_counter = {}
    @num = 0
    @max_size = -1
end

Public Instance Methods

add(key) click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 135
def add(key)
    if @freq_counter.has_key?(key)
        @freq_counter[key] += 1
    else
        @freq_counter[key] = 1
        @delta_counter[key] = @current - 1
    end
    @num += 1
    if @num % (1 / @epsilon).to_i == 0
        sweep()
    end
end
get() click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 163
def get()
    buf = {}
    @freq_counter.each_pair { |key, value|
        if value > (@num * (@gamma - @epsilon) ).to_i
            buf[key] = value
        end
    }
    return buf
end
get_current_max_size() click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 177
def get_current_max_size()
    return @max_size
end
get_metrics() click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 189
def get_metrics()
    return {'num' => get_num(), 'max_size' => get_current_max_size(), 'current_size' => @freq_counter.size() , 'reduced_size' => get().size(), "gamma" => @gamma , "epsilon" => @epsilon , "n_x_gamma" => get_num_x_gamma(), "n_x_gamma-epsilon" => get_num_x_gamma_d_epsilon() }
end
get_num() click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 173
def get_num()
    return @num
end
get_num_x_gamma() click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 181
def get_num_x_gamma()
    return @num.to_f * @gamma
end
get_num_x_gamma_d_epsilon() click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 185
def get_num_x_gamma_d_epsilon()
    return @num.to_f * (@gamma - @epsilon)
end
sweep() click to toggle source
# File lib/fluent/plugin/out_lossycount.rb, line 148
def sweep()
    length = @freq_counter.length
    if @max_size < length
        @max_size = length
    end

    @freq_counter.each_pair { |key, value|
        if value <= (@current - @delta_counter[key])
            @freq_counter.delete(key)
            @delta_counter.delete(key)
        end
    }
    @current += 1
end