<commit_before>require "set"
require_relative "./error"

module Fingerprint
  class Ingest < Struct.new(:fp, :external_id, :duration, :version)
    def ingest
      Track.find(
        Fingerprint::Match.new(fp, version).match[:track_id]
      )
    rescue Fingerprint::NoRecord
      find_track
    end

    private

    def find_track
      track = Track.create!({
        external_id: external_id,
        duration: duration,
        version: version
      })

      result = fp[:codes].length.times.each_with_object(Set.new) do |index, set|
        code = fp[:codes][index]
        time = fp[:times][index]
        set.add("(" + [code, time, track.id].join(",") + ")")
      end

      ActiveRecord::Base.connection.execute(%Q{
        DROP TABLE IF EXISTS tcodes;
<commit_msg>Use transactions<commit_after>require "set"
require_relative "./error"

module Fingerprint
  class Ingest < Struct.new(:fp, :external_id, :duration, :version)
    def ingest
      Track.find(
        Fingerprint::Match.new(fp, version).match[:track_id]
      )
    rescue Fingerprint::NoRecord
      Track.transaction { find_track }
    end

    private

    def find_track
      track = Track.create!({
        external_id: external_id,
        duration: duration,
        version: version
      })

      result = fp[:codes].length.times.each_with_object(Set.new) do |index, set|
        code = fp[:codes][index]
        time = fp[:times][index]
        set.add("(" + [code, time, track.id].join(",") + ")")
      end

      ActiveRecord::Base.connection.execute(%Q{
        DROP TABLE IF EXISTS tcodes;

{'fraction_non_alphanumeric': 0.13043478260869565, 'fraction_numerical': 0.0, 'mean_word_length': 2.609090909090909, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5970140', 'n_tokens_mistral': 497, 'n_tokens_neox': 490, 'n_words': 106}