Skip to content

Commit

Permalink
fixup! Add DataStores::DirectFileStore
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Magliola committed Dec 4, 2018
1 parent a0074d3 commit f26accf
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions lib/prometheus/client/data_stores/direct_file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,18 @@ def all_values
stores_for_metric.each do |file_path|
begin
store = FileMappedDict.new(file_path, true)
store.with_file_lock do
store.all_values.each do |(labelset_qs, v)|
# Labels come as a query string, and CGI::parse returns arrays for each key
# "foo=bar&x=y" => { "foo" => ["bar"], "x" => ["y"] }
# Turn the keys back into symbols, and remove the arrays
label_set = CGI::parse(labelset_qs).map do |k, vs|
[k.to_sym, vs.first]
end.to_h

stores_data[label_set] << v
end
store.all_values.each do |(labelset_qs, v)|
# Labels come as a query string, and CGI::parse returns arrays for each key
# "foo=bar&x=y" => { "foo" => ["bar"], "x" => ["y"] }
# Turn the keys back into symbols, and remove the arrays
label_set = CGI::parse(labelset_qs).map do |k, vs|
[k.to_sym, vs.first]
end.to_h

stores_data[label_set] << v
end
ensure
store.close
store.close if store
end
end

Expand Down Expand Up @@ -192,25 +190,34 @@ class FileMappedDict
attr_reader :capacity, :used, :positions

def initialize(filename, readonly = false)
@positions = {}
@used = 0

open_file(filename, readonly)
@capacity = @f.size
@used = @f.read(4).unpack('l')[0] if @capacity > 0

@positions = {}
@used = @f.read(4).unpack('l')[0]
if @used == 0
@used = 8
@f.seek(0)
@f.write([@used].pack('l'))
if @used > 0
# File already has data. Read the existing values
with_file_lock do
read_all_values.each do |key, _, pos|
@positions[key] = pos
end
end
else
read_all_values.each do |key, _, pos|
@positions[key] = pos
# File is empty. Init the `used` counter, if we're in write mode
if !readonly
@used = 8
@f.seek(0)
@f.write([@used].pack('l'))
end
end
end

# Yield (key, value, pos). No locking is performed.
def all_values
read_all_values.map { |k, v, p| [k, v] }
with_file_lock do
read_all_values.map { |k, v, p| [k, v] }
end
end

def read_value(key)
Expand Down Expand Up @@ -260,6 +267,7 @@ def open_file(filename, readonly)
if @f.size == 0 && !readonly
resize_file(INITIAL_FILE_SIZE)
end
@capacity = @f.size
end

def resize_file(new_capacity)
Expand Down

0 comments on commit f26accf

Please sign in to comment.