<commit_before>require 'semian'
require 'mysql2'

module Mysql2
  class SemianError < Mysql2::Error
    def initialize(semian_identifier, *args)
      super(*args)
      @semian_identifier = semian_identifier
    end

    def to_s
      "[#{@semian_identifier}] #{super}"
    end
  end

  ResourceOccupiedError = Class.new(SemianError)
  CircuitOpenError = Class.new(SemianError)
end

module Semian
  module Mysql2
    DEFAULT_HOST = 'localhost'
    DEFAULT_PORT = 3306

    def semian_identifier
      @semian_identifier ||= begin
        unless name = query_options[:semian] && query_options[:semian][:name]
          host = query_options[:host] || DEFAULT_HOST
          port = query_options[:port] || DEFAULT_PORT
          name = "#{host}:#{port}"
        end
        :"mysql_#{name}"
      end
    end

    def query(*)
      semian_resource.acquire { super }
    rescue ::Semian::CircuitBreaker::OpenCircuitError => error
      raise ::Mysql2::CircuitOpenError.new(semian_identifier, error)
    rescue ::Semian::BaseError => error
      raise ::Mysql2::ResourceOccupiedError.new(semian_identifier, error)
    end

    private

    def connect(*)
      semian_resource.acquire { super }
    rescue ::Semian::CircuitBreaker::OpenCircuitError => error
      raise ::Mysql2::CircuitOpenError.new(semian_identifier, error)
    rescue ::Semian::BaseError => error
      raise ::Mysql2::ResourceOccupiedError.new(semian_identifier, error)
    end

    def semian_resource
      @semian_resource ||= ::Semian.retrieve_or_register(semian_identifier, **semian_options)
    end

    def semian_options
      options = query_options[:semian] || {}
      options = options.map { |k, v| [k.to_sym, v] }.to_h
      options.delete(:name)
      options
    end
  end
end

::Mysql2::Client.prepend(Semian::Mysql2)
<commit_msg>Semian option keys might not be symbols<commit_after>require 'semian'
require 'mysql2'

module Mysql2
  class SemianError < Mysql2::Error
    def initialize(semian_identifier, *args)
      super(*args)
      @semian_identifier = semian_identifier
    end

    def to_s
      "[#{@semian_identifier}] #{super}"
    end
  end

  ResourceOccupiedError = Class.new(SemianError)
  CircuitOpenError = Class.new(SemianError)
end

module Semian
  module Mysql2
    DEFAULT_HOST = 'localhost'
    DEFAULT_PORT = 3306

    def semian_identifier
      @semian_identifier ||= begin
        semian_options = query_options[:semian] || {}
        unless name = semian_options['name'.freeze] || semian_options[:name]
          host = query_options[:host] || DEFAULT_HOST
          port = query_options[:port] || DEFAULT_PORT
          name = "#{host}:#{port}"
        end
        :"mysql_#{name}"
      end
    end

    def query(*)
      semian_resource.acquire { super }
    rescue ::Semian::CircuitBreaker::OpenCircuitError => error
      raise ::Mysql2::CircuitOpenError.new(semian_identifier, error)
    rescue ::Semian::BaseError => error
      raise ::Mysql2::ResourceOccupiedError.new(semian_identifier, error)
    end

    private

    def connect(*)
      semian_resource.acquire { super }
    rescue ::Semian::CircuitBreaker::OpenCircuitError => error
      raise ::Mysql2::CircuitOpenError.new(semian_identifier, error)
    rescue ::Semian::BaseError => error
      raise ::Mysql2::ResourceOccupiedError.new(semian_identifier, error)
    end

    def semian_resource
      @semian_resource ||= ::Semian.retrieve_or_register(semian_identifier, **semian_options)
    end

    def semian_options
      options = query_options[:semian] || {}
      options = options.map { |k, v| [k.to_sym, v] }.to_h
      options.delete(:name)
      options
    end
  end
end

::Mysql2::Client.prepend(Semian::Mysql2)

{'fraction_non_alphanumeric': 0.11135076840118631, 'fraction_numerical': 0.007549204637368563, 'mean_word_length': 3.3905325443786984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4768074', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1195, 'n_words': 234}