<commit_before>require 'tmpdir'
require 'fileutils'

module Paraboard
  class Registry
    attr_accessor :base_dir
    def initialize(base_dir: nil)
      @base_dir = base_dir or raise ArgumentError, "required base_dir"
      if !Dir.exist?(@base_dir)
        FileUtils.mkpath(@base_dir)
      end
    end

    def update(status)
      name = build_filename
      tmp  = "#{name}.tmp"
      f = open(tmp, "w+")
      f.flock(File::LOCK_EX)
      f.print(status)
      f.flock(File::LOCK_UN)
      f.close
      File.rename(tmp, name)
    end

    def read_all
      ret = {}
      files = Dir.glob("#{base_dir}/**")
      files.each do |file|
        next if file !~ /\/status_(.*)/
        id = $1
        f = open(file, "r+")
        ret[id] = f.read
      end
      ret
    end

    private
    def worker_id
      $$
    end

    def build_filename
      "#{base_dir}/status_#{worker_id}"
    end
  end
end<commit_msg>Changed negative if to unless<commit_after>require 'tmpdir'
require 'fileutils'

module Paraboard
  class Registry
    attr_accessor :base_dir
    def initialize(base_dir: nil)
      @base_dir = base_dir || raise ArgumentError, "required base_dir"
      FileUtils.mkpath(@base_dir) unless Dir.exist?(@base_dir)
    end

    def update(status)
      name = build_filename
      tmp  = "#{name}.tmp"
      f = open(tmp, "w+")
      f.flock(File::LOCK_EX)
      f.print(status)
      f.flock(File::LOCK_UN)
      f.close
      File.rename(tmp, name)
    end

    def read_all
      ret = {}
      files = Dir.glob("#{base_dir}/**")
      files.each do |file|
        next unless file =~ /\/status_(.*)/
        id = $1
        f = open(file, "r+")
        ret[id] = f.read
      end
      ret
    end

    private
    def worker_id
      $$
    end

    def build_filename
      "#{base_dir}/status_#{worker_id}"
    end
  end
end
{'fraction_non_alphanumeric': 0.12276906435911303, 'fraction_numerical': 0.001081665765278529, 'mean_word_length': 2.052805280528053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '781898', 'n_tokens_mistral': 651, 'n_tokens_neox': 644, 'n_words': 147}