lib/prometheus/client/helper/mmaped_file.rb in prometheus-client-mmap-0.7.0.beta19 vs lib/prometheus/client/helper/mmaped_file.rb in prometheus-client-mmap-0.7.0.beta20

- old
+ new

@@ -5,30 +5,10 @@ module Prometheus module Client module Helper class MmapedFile < Mmap include EntryParser - - class << self - def open(filepath) - MmapedFile.new(filepath, 'rw', Mmap::MAP_SHARED) - end - - def ensure_exclusive_file(file_prefix = 'mmaped_file') - (0..Float::INFINITY).lazy - .map { |f_num| "#{file_prefix}_#{Prometheus::Client.pid}-#{f_num}.db" } - .map { |filename| File.join(Prometheus::Client.configuration.multiprocess_files_dir, filename) } - .find { |path| Helper::FileLocker.lock_to_process(path) } - end - - def open_exclusive_file(file_prefix = 'mmaped_file') - filename = Helper::MmapedFile.ensure_exclusive_file(file_prefix) - open(filename) - end - end - - MINIMUM_SIZE = 8 attr_reader :filepath, :size def initialize(filepath, mode = 'r', protection = Mmap::MAP_SHARED, options = {}) @filepath = filepath @@ -39,15 +19,15 @@ super(filepath, mode, protection, options) end def used=(value) - self[4..7] = [value].pack('l') + self[0..3] = [value].pack('l') end def add_entry(data, value) - self.used = 12 if used.zero? + self.used = START_POSITION if used.zero? # Pad to be 8-byte aligned. padded = data + (' ' * (8 - (data.length + 4) % 8)) entry = [data.length, padded, value].pack("lA#{padded.length}d") used_ = used @@ -68,9 +48,29 @@ private def initial_mmap_file_size Prometheus::Client.configuration.initial_mmap_file_size + end + + public + + class << self + def open(filepath) + MmapedFile.new(filepath, 'rw', Mmap::MAP_SHARED) + end + + def ensure_exclusive_file(file_prefix = 'mmaped_file') + (0..Float::INFINITY).lazy + .map { |f_num| "#{file_prefix}_#{Prometheus::Client.pid}-#{f_num}.db" } + .map { |filename| File.join(Prometheus::Client.configuration.multiprocess_files_dir, filename) } + .find { |path| Helper::FileLocker.lock_to_process(path) } + end + + def open_exclusive_file(file_prefix = 'mmaped_file') + filename = Helper::MmapedFile.ensure_exclusive_file(file_prefix) + open(filename) + end end end end end end