<commit_before>package edu.ucsc.dbtune.inum;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import edu.ucsc.dbtune.core.DBIndex;
import edu.ucsc.dbtune.core.DatabaseConnection;
import edu.ucsc.dbtune.spi.core.Console;
import edu.ucsc.dbtune.util.StopWatch;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * It serves as the entry point to the INUM functionality. It will
 * orchestrate the execution of INUM.
 *
 * @author <EMAIL> (<NAME>)
 */
public class Inum {
  private final DatabaseConnection connection;
  private final Precomputation     precomputation;
  private final MatchingStrategy   matchingLogic;
  private final AtomicBoolean      isStarted;

  private static final Set<String> WORKLOADS;
  static {
    final SetupWorkloadVisitor  loader            = new SetupWorkloadVisitor();
    final WorkloadDirectoryNode workloadDirectory = new WorkloadDirectoryNode();
    WORKLOADS = ImmutableSet.copyOf(workloadDirectory.accept(loader));
  }

  private Inum(DatabaseConnection connection, Precomputation precomputation, MatchingStrategy matchingLogic){
    this.connection     = connection;
    this.precomputation = precomputation;
    this.matchingLogic  = matchingLogic;
    this.isStarted      = new AtomicBoolean(false);
  }

  public static Inum newInumInstance(DatabaseConnection connection,
      Precomputation precomputation,
      MatchingStrategy matchingLogic){
    final DatabaseConnection nonNullConnection     = Preconditions.checkNotNull(connection);
    final Precomputation     nonNullPrecomputation = Preconditions.checkNotNull(precomputation);
    final MatchingStrategy   nonNullMatchingLogic  = Preconditions.checkNotNull(matchingLogic);
    return new Inum(nonNullConnection, nonNullPrecomputation, nonNullMatchingLogic);
  }

  public static Inum newInumInstance(DatabaseConnection connection){
    final DatabaseConnection nonNullConnection = Preconditions.checkNotNull(connection);
    return newInumInstance(
        nonNullConnection,
        new InumPrecomputation(nonNullConnection),
        new InumMatchingStrategy()
    );
  }

  public InumSpace getInumSpace(){
    return precomputation.getInumSpace();
  }

  /**
   * INUM setup will load any representative workload found in the inum workload
   * directory.
   */
  public void start(){
    try {
      start(WORKLOADS);
    } catch (IOException e) {
      Console.streaming().error("unable to load workload", e);
    }
  }

  /**
   * INUM will get prepopulated first with representative workloads and configurations.
   * @param input
   *    a list of representative workloads.
   * @throws IOException
   *    if unable to parse the input.
   */
  public void start(Set<String> input) throws IOException {
    isStarted.set(true);

    final StopWatch timing = new StopWatch();
    for(String eachQuery : input){
      // todo(Huascar) question to Team: should we parse the interesting orders or rely on
      //           the recommended indexes by the extractor? For sake of speed, I am
      //           using the recommended indexes. For parsing the interesting orders, we
      //           need using Zql parser (include in Dash's code.<commit_msg>Fixed JavaDoc error on Inum class<commit_after>package edu.ucsc.dbtune.inum;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import edu.ucsc.dbtune.core.DBIndex;
import edu.ucsc.dbtune.core.DatabaseConnection;
import edu.ucsc.dbtune.spi.core.Console;
import edu.ucsc.dbtune.util.StopWatch;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * It serves as the entry point to the INUM functionality. It will
 * orchestrate the execution of INUM.
 *
 * @author <EMAIL> (<NAME>)
 */
public class Inum {
  private final DatabaseConnection connection;
  private final Precomputation     precomputation;
  private final MatchingStrategy   matchingLogic;
  private final AtomicBoolean      isStarted;

  private static final Set<String> WORKLOADS;
  static {
    final SetupWorkloadVisitor  loader            = new SetupWorkloadVisitor();
    final WorkloadDirectoryNode workloadDirectory = new WorkloadDirectoryNode();
    WORKLOADS = ImmutableSet.copyOf(workloadDirectory.accept(loader));
  }

  private Inum(DatabaseConnection connection, Precomputation precomputation, MatchingStrategy matchingLogic){
    this.connection     = connection;
    this.precomputation = precomputation;
    this.matchingLogic  = matchingLogic;
    this.isStarted      = new AtomicBoolean(false);
  }

  public static Inum newInumInstance(DatabaseConnection connection,
      Precomputation precomputation,
      MatchingStrategy matchingLogic){
    final DatabaseConnection nonNullConnection     = Preconditions.checkNotNull(connection);
    final Precomputation     nonNullPrecomputation = Preconditions.checkNotNull(precomputation);
    final MatchingStrategy   nonNullMatchingLogic  = Preconditions.checkNotNull(matchingLogic);
    return new Inum(nonNullConnection, nonNullPrecomputation, nonNullMatchingLogic);
  }

  public static Inum newInumInstance(DatabaseConnection connection){
    final DatabaseConnection nonNullConnection = Preconditions.checkNotNull(connection);
    return newInumInstance(
        nonNullConnection,
        new InumPrecomputation(nonNullConnection),
        new InumMatchingStrategy()
    );
  }

  public InumSpace getInumSpace(){
    return precomputation.getInumSpace();
  }

  /**
   * INUM setup will load any representative workload found in the inum workload
   * directory.
   */
  public void start(){
    try {
      start(WORKLOADS);
    } catch (IOException e) {
      Console.streaming().error("unable to load workload", e);
    }
  }

  /**
   * INUM will get prepopulated first with representative workloads and configurations.
   * @param input
   *    a list of representative workloads.
   * @throws IOException
   *    if unable to parse the input.
   */
  public void start(Set<String> input) throws IOException {
    isStarted.set(true);

    final StopWatch timing = new StopWatch();
    for(String eachQuery : input){
      // todo(Huascar) question to Team: should we parse the interesting orders or rely on
      //           the recommended indexes by the extractor? For sake of speed, I am
      //           using the recommended indexes. For parsing the interesting orders, we
      //           need to use Zql parser (included in Dash's code).
{'fraction_non_alphanumeric': 0.07552595731799606, 'fraction_numerical': 0.0, 'mean_word_length': 3.7642393655371307, 'pattern_counts': {'":': 0, '<': 11, '<?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': '5389632', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1771, 'n_words': 507}