<commit_before>package org.basex.query.up.primitives;

import static org.basex.query.QueryError.*;
import static org.basex.util.Token.*;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.List;

import org.basex.build.*;
import org.basex.core.*;
import org.basex.core.MainOptions.MainParser;
import org.basex.core.cmd.*;
import org.basex.data.*;
import org.basex.io.*;
import org.basex.query.*;
import org.basex.query.value.node.*;
import org.basex.query.value.type.*;
import org.basex.util.*;

/**
 * Contains helper methods for adding documents.
 *
 * @author BaseX Team 2005-19, BSD License
 * @author <NAME>
 */
public final class DBNew {
  /** Inputs to be added. */
  public final List<NewInput> inputs;
  /** New database nodes. */
  public Data data;

  /** Query context. */
  private final QueryContext qc;
  /** Input info. */
  private final InputInfo info;
  /** Main options for all inputs to be added. */
  private final List<DBOptions> dboptions;

  /**
   * Constructor.
   * @param qc query context
   * @param options database options
   * @param info input info
   * @param list list of inputs
   */
  public DBNew(final QueryContext qc, final DBOptions options, final InputInfo info,
      final NewInput... list) {

    this.qc = qc;
    this.info = info;

    final int is = list.length;
    inputs = new ArrayList<>(is);
    dboptions = new ArrayList<>(is);
    for(final NewInput input : list) {
      inputs.add(input);
      dboptions.add(options);
    }
  }

  /**
   * Merges updates.
   * @param add inputs to be added
   */
  public void merge(final DBNew add) {
    inputs.addAll(add.inputs);
    dboptions.addAll(add.dboptions);
  }

  /**
   * Inserts all documents to be added to a temporary database.
   * @param name name of database
   * @param create create new database
   * @throws QueryException query exception
   */
  public void prepare(final String name, final boolean create) throws QueryException {
    final long is = inputs.size();
    if(is == 0) return;

    // check if new resources will be cached on disk
    final boolean cache = cache(create);
    try {
      // multiple input: create temporary database and insert inputs
      final Context ctx = qc.context;
      final MainOptions mopts = ctx.options;
      final StaticOptions sopts = ctx.soptions;
      final String dbname = cache ? sopts.createRandomDb(name) : name;
      data = cache ? CreateDB.create(dbname, Parser.emptyParser(mopts), ctx, mopts) :
        new MemData(mopts);
      data.startUpdate(mopts);
      try {
        for(int i = 0; i < is; i++) {
          final Data tmpData = tmpData(dbname, i, cache);
          try {
            copy(tmpData, data);
          } finally {
            DropDB.drop(tmpData, sopts);
          }
        }
      } finally {
        data.finishUpdate(mopts);
      }
    } catch(final IOException ex) {
      finish();
      throw UPDBERROR_X.get(info, ex);
    }
  }

  /**
   * Adds the contents of the temporary database to the target database.
   * @param target database instance
   * @throws QueryException exception<commit_msg>[ADD] Database Module, ADDCACHE: Speed up processing. Closes #1721<commit_after>package org.basex.query.up.primitives;

import static org.basex.query.QueryError.*;
import static org.basex.util.Token.*;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.List;

import org.basex.build.*;
import org.basex.core.*;
import org.basex.core.MainOptions.MainParser;
import org.basex.core.cmd.*;
import org.basex.data.*;
import org.basex.io.*;
import org.basex.query.*;
import org.basex.query.value.node.*;
import org.basex.query.value.type.*;
import org.basex.util.*;

/**
 * Contains helper methods for adding documents.
 *
 * @author BaseX Team 2005-19, BSD License
 * @author <NAME>
 */
public final class DBNew {
  /** Inputs to be added. */
  public final List<NewInput> inputs;
  /** New database nodes. */
  public Data data;

  /** Query context. */
  private final QueryContext qc;
  /** Input info. */
  private final InputInfo info;
  /** Main options for all inputs to be added. */
  private final List<DBOptions> dboptions;

  /**
   * Constructor.
   * @param qc query context
   * @param options database options
   * @param info input info
   * @param list list of inputs
   */
  public DBNew(final QueryContext qc, final DBOptions options, final InputInfo info,
      final NewInput... list) {

    this.qc = qc;
    this.info = info;

    final int is = list.length;
    inputs = new ArrayList<>(is);
    dboptions = new ArrayList<>(is);
    for(final NewInput input : list) {
      inputs.add(input);
      dboptions.add(options);
    }
  }

  /**
   * Merges updates.
   * @param add inputs to be added
   */
  public void merge(final DBNew add) {
    inputs.addAll(add.inputs);
    dboptions.addAll(add.dboptions);
  }

  /**
   * Inserts all documents to be added to a temporary database.
   * @param name name of database
   * @param create create new database
   * @throws QueryException query exception
   */
  public void prepare(final String name, final boolean create) throws QueryException {
    final long is = inputs.size();
    if(is == 0) return;

    // check if new resources will be cached on disk
    final boolean cache = cache(create);
    try {
      if(is == 1) {
        // single input: create temporary database
        data = tmpData(name, 0, cache);
      } else {
        // multiple input: create temporary database and insert inputs
        final Context ctx = qc.context;
        final MainOptions mopts = ctx.options;
        final StaticOptions sopts = ctx.soptions;
        final String dbname = cache ? sopts.createRandomDb(name) : name;
        data = cache ? CreateDB.create(dbname, Parser.emptyParser(mopts), ctx, mopts) :
          new MemData(mopts);
        data.startUpdate(mopts);
        try {
          for(int i = 0; i < is; i++) {
            final Data tmpData = tmpData(dbname, i, cache);
            try {
              copy(tmpData, data);
            } finally {
              DropDB.drop(tmpData, sopts);
            }
          }
        } finally {
          data.finishUpdate(mopts);
        }
      }
    } catch(final IOException ex) {
      finish();
      throw UPDBERROR_X.get(info, ex);
    }
  }

  /**
   * Adds the contents of the temporary database to the target database.
   * @param target database instance
   * @throws QueryException exception
{'fraction_non_alphanumeric': 0.11502238690751891, 'fraction_numerical': 0.0033966342442488806, 'mean_word_length': 2.596890616324264, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '1889893', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1925, 'n_words': 607}