<commit_before>/**
 * Jooby https://jooby.io
 * Apache License Version 2.0 https://jooby.io/LICENSE.txt
 * Copyright 2014 <NAME>
 */
package io.jooby.hikari;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.jooby.Environment;
import io.jooby.Extension;
import io.jooby.Jooby;
import io.jooby.ServiceKey;
import io.jooby.ServiceRegistry;

import javax.annotation.Nonnull;
import javax.sql.DataSource;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Hikari connection pool module: https://jooby.io/modules/hikari.
 *
 * mySQL Example:
 *
 * application.conf:
 * <pre>{@code
 *  db.url = "jdbc:mysql://localhost/mydb"
 *  db.user = myuser
 *  db.password = <PASSWORD>
 * }</pre>
 *
 * App.java:
 * <pre>{@code
 * {
 *
 *   install(new HikariModule());
 *
 * }
 * }</pre>
 *
 * To simplify development Jooby offers 3 special databases based on H2 Java database engine:
 *
 * - mem: for in-memory database
 * - local: for a file system database stored in the current project directory
 * - tmp: for a file system database stored in the operating system temporary directory
 *
 * To use any of these database you first need to add the h2 driver to your project and then:
 *
 * - define the <code>db</code> property in your application configuration file, like
 *     <code>db=mem</code> or
 * - pass the database type to the HikariModule. <code>new HikariModule("mem")</code>
 *
 * Alternative you can specify a jdbc connection string:
 *
 * <pre>{@code
 *   install(new HikariModule("jdbc:mysql://localhost/mydb"));
 * }</pre>
 *
 * The module exposes a {@link DataSource} instance which can be retrieve it after installing:
 *
 * <pre>{@code
 *
 *    install(new HikariModule("jdbc:mysql://localhost/mydb"));
 *
 *    DataSource dataSource = require(DataSource.class);
 * }</pre>
 *
 * Supports multiple database connections:
 *
 * <pre>{@code
 *  install(new HikariModule("maindb"));
 *
 *  install(new HikariModule("auditdb"));
 *
 *  DataSource maindb = require(DataSource.class, "maindb");
 *
 *  DataSource auditdb = require(DataSource.class, "auditdb");
 * }</pre>
 *
 * Complete documentation is available at: https://jooby.io/modules/hikari.
 *
 * @author edgar
 * @since 2.0.0
 */
public class HikariModule implements Extension {

  static {
    System.setProperty("log4jdbc.auto.load.popular.drivers", "false");
  }

  /** Default factor number of worker threads. */
  private static final int WORKER_FACTOR = 8;

  private static final Object MYSQL5_STT_CACHE_SIZE = 250;

  private static final Object MYSQL5_STT_CACHE_SQL_LIMIT = 2048;

  private static final String DATASOURCE_CLASS_NAME = "dataSourceClassName";

  private static final String DRIVER_CLASS_NAME = "driverClassName";

  /** Minimum connection pool size. */
  private static final int MINIMUM_SIZE = 10;

  private static final Set<String> SKIP_TOKENS = Stream.of("jdbc", "jtds")
      .collect(Collectors.toSet());

  /** Default datasource key. Used for retrieving the default datasource. */
  public static final ServiceKey<DataSource> KEY = ServiceKey.key(DataSource.class);

  private HikariConfig hikari;

  private String database;

  /**
   * Creates a new Hikari module. The database parameter can be one of:
   *
   * - A property key defined in your application configuration file, like <code>db</code>.
   * - A special h2 database: mem, local or tmp.
   * - A jdbc connection string, like: <code>jdbc:mysql://localhost/db</code>
   *
   * @param database Database key, database type or connection string.
   */
  public HikariModule(@Nonnull String database) {
    this.database = database;
  }

  /**
   * Creates a new Hikari module using the <code>db</code> property key. This key must be
   * present in the application configuration file, like:
   *
   * <pre>{@code
   *  db.url = "jdbc:url"
   *  db.user = dbuser
   *  db.password = <PASSWORD>
   * }</pre>
   */
  public HikariModule() {
    this("db");
  }

  /**
   * Creates a new Hikari module using the Hikari configuration.
   *
   * @param hikari Hikari configuration.
   */
  public HikariModule(@Nonnull HikariConfig hikari) {
    this(hikari.getPoolName());
    this.hikari = hikari;
  }

  @Override public void install(@Nonnull Jooby application) {
    if (hikari == null) {
      hikari = build(application.getEnvironment(), database);
    }
    HikariDataSource dataSource = new HikariDataSource(hikari);

    ServiceRegistry registry = application.getServices();
    ServiceKey<DataSource> key = ServiceKey.key(DataSource.class, database);
    /** Global default database: */
    registry.putIfAbsent(KEY, dataSource);
<commit_msg>Support setting metric and health check registries with config methods.<commit_after>/**
 * Jooby https://jooby.io
 * Apache License Version 2.0 https://jooby.io/LICENSE.txt
 * Copyright 2014 <NAME>
 */
package io.jooby.hikari;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.jooby.Environment;
import io.jooby.Extension;
import io.jooby.Jooby;
import io.jooby.ServiceKey;
import io.jooby.ServiceRegistry;

import javax.annotation.Nonnull;
import javax.sql.DataSource;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Hikari connection pool module: https://jooby.io/modules/hikari.
 *
 * mySQL Example:
 *
 * application.conf:
 * <pre>{@code
 *  db.url = "jdbc:mysql://localhost/mydb"
 *  db.user = myuser
 *  db.password = <PASSWORD>
 * }</pre>
 *
 * App.java:
 * <pre>{@code
 * {
 *
 *   install(new HikariModule());
 *
 * }
 * }</pre>
 *
 * To simplify development Jooby offers 3 special databases based on H2 Java database engine:
 *
 * - mem: for in-memory database
 * - local: for a file system database stored in the current project directory
 * - tmp: for a file system database stored in the operating system temporary directory
 *
 * To use any of these database you first need to add the h2 driver to your project and then:
 *
 * - define the <code>db</code> property in your application configuration file, like
 *     <code>db=mem</code> or
 * - pass the database type to the HikariModule. <code>new HikariModule("mem")</code>
 *
 * Alternative you can specify a jdbc connection string:
 *
 * <pre>{@code
 *   install(new HikariModule("jdbc:mysql://localhost/mydb"));
 * }</pre>
 *
 * The module exposes a {@link DataSource} instance which can be retrieve it after installing:
 *
 * <pre>{@code
 *
 *    install(new HikariModule("jdbc:mysql://localhost/mydb"));
 *
 *    DataSource dataSource = require(DataSource.class);
 * }</pre>
 *
 * Supports multiple database connections:
 *
 * <pre>{@code
 *  install(new HikariModule("maindb"));
 *
 *  install(new HikariModule("auditdb"));
 *
 *  DataSource maindb = require(DataSource.class, "maindb");
 *
 *  DataSource auditdb = require(DataSource.class, "auditdb");
 * }</pre>
 *
 * Complete documentation is available at: https://jooby.io/modules/hikari.
 *
 * @author edgar
 * @since 2.0.0
 */
public class HikariModule implements Extension {

  static {
    System.setProperty("log4jdbc.auto.load.popular.drivers", "false");
  }

  /** Default factor number of worker threads. */
  private static final int WORKER_FACTOR = 8;

  private static final Object MYSQL5_STT_CACHE_SIZE = 250;

  private static final Object MYSQL5_STT_CACHE_SQL_LIMIT = 2048;

  private static final String DATASOURCE_CLASS_NAME = "dataSourceClassName";

  private static final String DRIVER_CLASS_NAME = "driverClassName";

  /** Minimum connection pool size. */
  private static final int MINIMUM_SIZE = 10;

  private static final Set<String> SKIP_TOKENS = Stream.of("jdbc", "jtds")
      .collect(Collectors.toSet());

  /** Default datasource key. Used for retrieving the default datasource. */
  public static final ServiceKey<DataSource> KEY = ServiceKey.key(DataSource.class);

  private HikariConfig hikari;

  private String database;

  private Object metricRegistry;
  private Object healthCheckRegistry;

  /**
   * Creates a new Hikari module. The database parameter can be one of:
   *
   * - A property key defined in your application configuration file, like <code>db</code>.
   * - A special h2 database: mem, local or tmp.
   * - A jdbc connection string, like: <code>jdbc:mysql://localhost/db</code>
   *
   * @param database Database key, database type or connection string.
   */
  public HikariModule(@Nonnull String database) {
    this.database = database;
  }

  /**
   * Creates a new Hikari module using the <code>db</code> property key. This key must be
   * present in the application configuration file, like:
   *
   * <pre>{@code
   *  db.url = "jdbc:url"
   *  db.user = dbuser
   *  db.password = <PASSWORD>
   * }</pre>
   */
  public HikariModule() {
    this("db");
  }

  /**
   * Creates a new Hikari module using the Hikari configuration.
   *
   * @param hikari Hikari configuration.
   */
  public HikariModule(@Nonnull HikariConfig hikari) {
    this(hikari.getPoolName());
    this.hikari = hikari;
  }

  /**
   * Sets a {@code MetricRegistry} to pass it forward to
   * {@link HikariConfig} for instrumentation.
   *
   * @param metricRegistry an instance compatible with {@link HikariConfig#setMetricRegistry(Object)}
   * @return this instance
   * @see HikariConfig#setMetricRegistry(Object) 
   */
  public HikariModule metricRegistry(Object metricRegistry) {
    this.metricRegistry = metricRegistry;
    return this;
  }

  /**
   * Sets a {@code HealthCheckRegistry} to pass it forward to
   * {@link HikariConfig} for instrumentation.
   *
   * @param healthCheckRegistry an instance compatible with {@link HikariConfig#setHealthCheckRegistry(Object)}
   * @return this instance
   * @see HikariConfig#setHealthCheckRegistry(Object) 
   */
  public HikariModule healthCheckRegistry(Object healthCheckRegistry) {
    this.healthCheckRegistry = healthCheckRegistry;
    return this;
  }

  @Override public void install(@Nonnull Jooby application) {
    if (hikari == null) {
      hikari = build(application.getEnvironment(), database);
    }

    if (metricRegistry != null) hikari.setMetricRegistry(metricRegistry);
    if (healthCheckRegistry != null) hikari.setHealthCheckRegistry(healthCheckRegistry);

    HikariDataSource dataSource = new HikariDataSource(hikari);

    ServiceRegistry registry = application.getServices();
    ServiceKey<DataSource> key = ServiceKey.key(DataSource.class, database);
    /** Global default database: */
    registry.putIfAbsent(KEY, dataSource);

{'fraction_non_alphanumeric': 0.11680811969551141, 'fraction_numerical': 0.004549829381398198, 'mean_word_length': 3.9097938144329896, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 8, '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': '5137014', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3481, 'n_words': 981}