<commit_before>require 'fileutils'
require 'yaml/store'
require_relative 'mapper'
require_relative 'settings'
require_relative 'sig_finder'
require_relative 'signature'
require_relative 'tags'

module Signore
  class Database
    def initialize(path: Settings.new.db_path, sig_finder: SigFinder)
      @path       = path
      @sig_finder = sig_finder
      initialise_store if path.zero? or not path.exist?
      @store      = YAML::Store.new(path)
    end

    def <<(sig)
      sigs << sig
      persist
    end

    def find(tags: Tags.new)
      sig_finder.find(sigs, tags: tags)
    end

    def sigs
      @sigs ||= store.transaction(true) { store['signatures'] }.map do |elem|
        elem.is_a?(Signature) ? elem : Mapper.from_h(elem)
      end
    end

    attr_reader :path, :sig_finder, :store
    private     :path, :sig_finder, :store

    private

    def initialise_store
      FileUtils.mkdir_p path.dirname
      FileUtils.touch path
      YAML::Store.new(path).transaction { |store| store['signatures'] = [] }
    end

    def persist
      store.transaction { store['signatures'] = sigs.map(&:to_h) }
    end
  end
end<commit_msg>Database: switch to Mapper.to_h<commit_after>require 'fileutils'
require 'yaml/store'
require_relative 'mapper'
require_relative 'settings'
require_relative 'sig_finder'
require_relative 'signature'
require_relative 'tags'

module Signore
  class Database
    def initialize(path: Settings.new.db_path, sig_finder: SigFinder)
      @path       = path
      @sig_finder = sig_finder
      initialise_store if path.zero? or not path.exist?
      @store      = YAML::Store.new(path)
    end

    def <<(sig)
      sigs << sig
      persist
    end

    def find(tags: Tags.new)
      sig_finder.find(sigs, tags: tags)
    end

    def sigs
      @sigs ||= store.transaction(true) { store['signatures'] }.map do |elem|
        elem.is_a?(Signature) ? elem : Mapper.from_h(elem)
      end
    end

    attr_reader :path, :sig_finder, :store
    private     :path, :sig_finder, :store

    private

    def initialise_store
      FileUtils.mkdir_p path.dirname
      FileUtils.touch path
      YAML::Store.new(path).transaction { |store| store['signatures'] = [] }
    end

    def persist
      hashes = sigs.map { |sig| Mapper.to_h(sig) }
      store.transaction { store['signatures'] = hashes }
    end
  end
end
{'fraction_non_alphanumeric': 0.10682492581602374, 'fraction_numerical': 0.0, 'mean_word_length': 2.8752052545155995, 'pattern_counts': {'":': 0, '<': 11, '<?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': '2096381', 'n_tokens_mistral': 785, 'n_tokens_neox': 766, 'n_words': 184}