<commit_before>/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 <NAME>, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.engine;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.cinchapi.common.io.Byteable;
import org.cinchapi.common.tools.Transformers;
import org.cinchapi.concourse.server.Properties;
import org.cinchapi.concourse.thrift.Operator;
import org.cinchapi.concourse.thrift.TObject;
import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Throwables;

import static org.cinchapi.concourse.server.engine.Record.loadPrimaryRecord;
import static org.cinchapi.concourse.server.engine.Record.loadSecondaryIndex;
import static org.cinchapi.concourse.server.engine.Record.loadSearchIndex;

/**
 * The {@code Database} is the permanent {@link Destination} for {@link Write}
 * objects that are initially stored in a {@link Buffer}.
 * <p>
 * When the Database accepts a write, it creates relevant indexes for efficient
 * retrieval, query and search reads.
 * </p>
 * 
 * @author jnelson
 */
public class Database implements Readable, Destination {

	/**
	 * Return a {@link Runnable} that will execute the appropriate write
	 * function in {@code record} to store {@code write} in the {@link Database}
	 * .
	 * 
	 * @param record
	 * @param write
	 * @return the appropriate Runnable
	 */
	public static <L extends Byteable, K extends Byteable, V extends Storable> Runnable getWriteRunnable(
			final Record<L, K, V> record, final Write write) {
		return new Runnable() {

			@Override
			public void run() {
				log.debug("Writing {} to {}", write, record);
				if(record instanceof PrimaryRecord) {
					if(write.getType() == WriteType.ADD) {
						((PrimaryRecord) record).add(write.getKey(),
								write.getValue());
					}
					else if(write.getType() == WriteType.REMOVE) {
						((PrimaryRecord) record).remove(write.getKey(),
								write.getValue());
					}
					else {
						throw new IllegalArgumentException();
					}
				}
				else if(record instanceof SecondaryIndex) {
					if(write.getType() == WriteType.ADD) {
						((SecondaryIndex) record).add(write.getValue(),
								write.getRecord());
					}
					else if(write.getType() == WriteType.REMOVE) {
						((SecondaryIndex) record).remove(write.getValue(),
								write.getRecord());
					}
					else {
						throw new IllegalArgumentException();
					}
				}
				else if(record instanceof SearchIndex) {
					if(write.getType() == WriteType.ADD) {
						((SearchIndex) record).add(write.getValue(),
								write.getRecord());
					}
					else if(write.getType() == WriteType.REMOVE) {
						((SearchIndex) record).remove(write.getValue(),
								write.getRecord());
					}
					else {
						throw new IllegalArgumentException();
					}
				}
				else {
					throw new IllegalArgumentException();
				}
			}

		};
	}

	/**
	 * The location where the Database stores data.
	 */
	private final String backingStore;

	private static final String threadNamePrefix = "database-write-thread";

	private static final Logger log = LoggerFactory.getLogger(Database.class);

	/**
	 * Construct a Database that is backed by the default location which is in a
	 * "db" directory under {@link Properties#DATA_HOME}.
	 */
	public Database() {
		this(Properties.DATA_HOME + File.separator + "db");
	}

	/**
	 * Construct a Database that is backed by {@link backingStore} directory.
	 * The {@link backingStore} is passed to each {@link Record} as the
	 * {@code parentStore}.
	 * 
	 * @param backingStore
	 */<commit_msg>Make Database use standard logger<commit_after>/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 <NAME>, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.engine;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.cinchapi.common.io.Byteable;
import org.cinchapi.common.tools.Transformers;
import org.cinchapi.concourse.server.Properties;
import org.cinchapi.concourse.server.util.Loggers;
import org.cinchapi.concourse.thrift.Operator;
import org.cinchapi.concourse.thrift.TObject;
import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Throwables;

import static org.cinchapi.concourse.server.engine.Record.loadPrimaryRecord;
import static org.cinchapi.concourse.server.engine.Record.loadSecondaryIndex;
import static org.cinchapi.concourse.server.engine.Record.loadSearchIndex;

/**
 * The {@code Database} is the permanent {@link Destination} for {@link Write}
 * objects that are initially stored in a {@link Buffer}.
 * <p>
 * When the Database accepts a write, it creates relevant indexes for efficient
 * retrieval, query and search reads.
 * </p>
 * 
 * @author jnelson
 */
public class Database implements Readable, Destination {

	/**
	 * Return a {@link Runnable} that will execute the appropriate write
	 * function in {@code record} to store {@code write} in the {@link Database}
	 * .
	 * 
	 * @param record
	 * @param write
	 * @return the appropriate Runnable
	 */
	public static <L extends Byteable, K extends Byteable, V extends Storable> Runnable getWriteRunnable(
			final Record<L, K, V> record, final Write write) {
		return new Runnable() {

			@Override
			public void run() {
				log.debug("Writing {} to {}", write, record);
				if(record instanceof PrimaryRecord) {
					if(write.getType() == WriteType.ADD) {
						((PrimaryRecord) record).add(write.getKey(),
								write.getValue());
					}
					else if(write.getType() == WriteType.REMOVE) {
						((PrimaryRecord) record).remove(write.getKey(),
								write.getValue());
					}
					else {
						throw new IllegalArgumentException();
					}
				}
				else if(record instanceof SecondaryIndex) {
					if(write.getType() == WriteType.ADD) {
						((SecondaryIndex) record).add(write.getValue(),
								write.getRecord());
					}
					else if(write.getType() == WriteType.REMOVE) {
						((SecondaryIndex) record).remove(write.getValue(),
								write.getRecord());
					}
					else {
						throw new IllegalArgumentException();
					}
				}
				else if(record instanceof SearchIndex) {
					if(write.getType() == WriteType.ADD) {
						((SearchIndex) record).add(write.getValue(),
								write.getRecord());
					}
					else if(write.getType() == WriteType.REMOVE) {
						((SearchIndex) record).remove(write.getValue(),
								write.getRecord());
					}
					else {
						throw new IllegalArgumentException();
					}
				}
				else {
					throw new IllegalArgumentException();
				}
			}

		};
	}

	/**
	 * The location where the Database stores data.
	 */
	private final String backingStore;

	private static final String threadNamePrefix = "database-write-thread";
	private static final Logger log = Loggers.getLogger();

	/**
	 * Construct a Database that is backed by the default location which is in a
	 * "db" directory under {@link Properties#DATA_HOME}.
	 */
	public Database() {
		this(Properties.DATA_HOME + File.separator + "db");
	}

	/**
	 * Construct a Database that is backed by {@link backingStore} directory.
	 * The {@link backingStore} is passed to each {@link Record} as the
	 * {@code parentStore}.
	 * 
	 * @param backingStore
	 */
{'fraction_non_alphanumeric': 0.10649271844660194, 'fraction_numerical': 0.0014158576051779936, 'mean_word_length': 3.6558380414312617, 'pattern_counts': {'":': 0, '<': 13, '<?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': '3876185', 'n_tokens_mistral': 3218, 'n_tokens_neox': 2702, 'n_words': 934}