<commit_before>package com.puresoltechnologies.genesis.tracker.ductiledb;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.time.Instant;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.puresoltechnologies.commons.misc.hash.HashId;
import com.puresoltechnologies.commons.misc.hash.HashUtilities;
import com.puresoltechnologies.ductiledb.core.DuctileDB;
import com.puresoltechnologies.ductiledb.core.DuctileDBBootstrap;
import com.puresoltechnologies.ductiledb.core.DuctileDBConfiguration;
import com.puresoltechnologies.ductiledb.core.tables.ExecutionException;
import com.puresoltechnologies.ductiledb.core.tables.TableStore;
import com.puresoltechnologies.ductiledb.core.tables.columns.ColumnType;
import com.puresoltechnologies.ductiledb.core.tables.ddl.CreateNamespace;
import com.puresoltechnologies.ductiledb.core.tables.ddl.CreateTable;
import com.puresoltechnologies.ductiledb.core.tables.ddl.DataDefinitionLanguage;
import com.puresoltechnologies.ductiledb.core.tables.dml.BoundStatement;
import com.puresoltechnologies.ductiledb.core.tables.dml.CompareOperator;
import com.puresoltechnologies.ductiledb.core.tables.dml.DataManipulationLanguage;
import com.puresoltechnologies.ductiledb.core.tables.dml.PreparedDelete;
import com.puresoltechnologies.ductiledb.core.tables.dml.PreparedInsert;
import com.puresoltechnologies.ductiledb.core.tables.dml.PreparedSelect;
import com.puresoltechnologies.ductiledb.core.tables.dml.TableRow;
import com.puresoltechnologies.ductiledb.core.tables.dml.TableRowIterable;
import com.puresoltechnologies.genesis.commons.ProvidedVersionRange;
import com.puresoltechnologies.genesis.commons.SequenceMetadata;
import com.puresoltechnologies.genesis.commons.TransformationException;
import com.puresoltechnologies.genesis.commons.TransformationMetadata;
import com.puresoltechnologies.genesis.tracker.spi.Severity;
import com.puresoltechnologies.genesis.tracker.spi.TransformationTracker;
import com.puresoltechnologies.versioning.Version;

/**
 * This is the default migration tracker for Purifinity which puts everything
 * into Cassandra.
 * 
 * @author <NAME>
 */
public class DuctileDBTransformationTracker implements TransformationTracker {

    private static final Logger logger = LoggerFactory.getLogger(DuctileDBTransformationTracker.class);

    public static final String NAMESPACE_NAME = "genesis";

    public static final String CHANGELOG_TABLE = "changelog";
    public static final String MIGRATIONLOG_TABLE = "migrationlog";
    public static final String LAST_TRANSFORMATIONS_TABLE = "last_transformations";

    private PreparedInsert preparedInsertStatement = null;
    private PreparedSelect preparedSelectStatement = null;
    private PreparedDelete preparedDropComponentStatement = null;
    private PreparedInsert preparedLoggingStatement = null;
    private PreparedInsert preparedInsertLastTransformationStatement = null;
    private PreparedSelect preparedSelectLastTransformationStatement = null;
    private PreparedDelete preparedDropComponentLastTransformtaionStatement = null;

    private File configFile = null;
    private DuctileDB ductileDB;

    @Override
    public void open() throws TransformationException {
	loadConfiguration();
	connect();
	prepareKeyspace();
    }

    private void loadConfiguration() {
	configFile = new File(System.getProperty("genesis.tracker.ductiledb.config"));
    }

    private void connect() throws TransformationException {
	try (FileInputStream inputStream = new FileInputStream(configFile)) {
	    DuctileDBConfiguration configuration = DuctileDBBootstrap.readConfiguration(inputStream);
	    DuctileDBBootstrap.start(configuration);
	    ductileDB = DuctileDBBootstrap.getInstance();
	} catch (IOException e) {
	    throw new TransformationException("Could not connect to DuctileDB.", e);
	}
    }

    private void prepareKeyspace() throws TransformationException {
	try {
	    TableStore tableStore = ductileDB.getTableStore();
	    DataDefinitionLanguage dataDefinitionLanguage = tableStore.getDataDefinitionLanguage();
	    if (dataDefinitionLanguage.getNamespace(NAMESPACE_NAME) == null) {
		logger.info("Keyspace for Cassandra migration is missing. Needs to be created...");
		CreateNamespace createNamespace = dataDefinitionLanguage.createCreateNamespace(NAMESPACE_NAME);
		createNamespace.execute(tableStore);
		if (dataDefinitionLanguage.getNamespace(NAMESPACE_NAME) == null) {
		    throw new TransformationException("Could not create namespace '" + NAMESPACE_NAME + "'.");
		}
		logger.info("Namespace for DuctileDB migration tracker created.");
	    }
	    if (dataDefinitionLanguage.getTable(NAMESPACE_NAME, CHANGELOG_TABLE) == null) {
		logger.info("ChangeLog table for Cassandra migration is missing. Needs to be created...");
		CreateTable createTable = dataDefinitionLanguage.createCreateTable(NAMESPACE_NAME, CHANGELOG_TABLE);
		createTable.addColumn("changelog", "time", ColumnType.DATETIME);
		createTable.addColumn("changelog", "component", ColumnType.VARCHAR);
		createTable.addColumn("changelog", "machine", ColumnType.VARCHAR);
		createTable.addColumn("changelog", "version", ColumnType.VARCHAR);
		createTable.addColumn("changelog", "command", ColumnType.VARCHAR);
		createTable.addColumn("changelog", "developer", ColumnType.VARCHAR);
		createTable.addColumn("changelog", "comment", ColumnType.VARCHAR);
		createTable.addColumn("changelog", "hashid", ColumnType.VARCHAR);
		createTable.setPrimaryKey("component", "machine", "version", "command");
		logger.info("ChangeLog table for Cassandra migration created.");
	    }
	    if (dataDefinitionLanguage.getTable(NAMESPACE_NAME, MIGRATIONLOG_TABLE) == null) {
		logger.info("MigrationLog table for Cassandra migration is missing. Needs to be created...");
		CreateTable table = dataDefinitionLanguage.createCreateTable(NAMESPACE_NAME, MIGRATIONLOG_TABLE);
		table.addColumn("migrationlog", "time", ColumnType.DATETIME);
		table.addColumn("migrationlog", "severity", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "machine", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "thread", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "message", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "exception_type", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "exception_message", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "stacktrace", ColumnType.VARCHAR);
		table.setPrimaryKey("time", "machine", "thread", "message");
		logger.info("MigrationLog table for Cassandra migration created.");
	    }
	    if (dataDefinitionLanguage.getTable(NAMESPACE_NAME, LAST_TRANSFORMATIONS_TABLE) == null) {
		logger.info("LastTransformations table for Cassandra migration is missing. Needs to be created...");
		CreateTable table = dataDefinitionLanguage.createCreateTable(NAMESPACE_NAME,
			LAST_TRANSFORMATIONS_TABLE);
		table.addColumn("transformations", "time", ColumnType.DATETIME);
		table.addColumn("transformations", "component", ColumnType.VARCHAR);
		table.addColumn("transformations", "machine", ColumnType.VARCHAR);
		table.addColumn("transformations", "start_version", ColumnType.VARCHAR);
		table.addColumn("transformations", "target_version", ColumnType.VARCHAR);
		table.addColumn("transformations", "next_version", ColumnType.VARCHAR);
		table.addColumn("transformations", "command", ColumnType.VARCHAR);
		table.addColumn("transformations", "developer", ColumnType.VARCHAR);
		table.addColumn("transformations", "comment", ColumnType.VARCHAR);
		table.addColumn("transformations", "hashid", ColumnType.VARCHAR);
		table.setPrimaryKey("component", "machine");
		logger.info("LastTransformations table for Cassandra migration created.");
	    }
	} catch (ExecutionException e) {<commit_msg>Fixed namespace creation.<commit_after>package com.puresoltechnologies.genesis.tracker.ductiledb;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.time.Instant;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.puresoltechnologies.commons.misc.hash.HashId;
import com.puresoltechnologies.commons.misc.hash.HashUtilities;
import com.puresoltechnologies.ductiledb.core.DuctileDB;
import com.puresoltechnologies.ductiledb.core.DuctileDBBootstrap;
import com.puresoltechnologies.ductiledb.core.DuctileDBConfiguration;
import com.puresoltechnologies.ductiledb.core.tables.ExecutionException;
import com.puresoltechnologies.ductiledb.core.tables.TableStore;
import com.puresoltechnologies.ductiledb.core.tables.columns.ColumnType;
import com.puresoltechnologies.ductiledb.core.tables.ddl.CreateNamespace;
import com.puresoltechnologies.ductiledb.core.tables.ddl.CreateTable;
import com.puresoltechnologies.ductiledb.core.tables.ddl.DataDefinitionLanguage;
import com.puresoltechnologies.ductiledb.core.tables.dml.BoundStatement;
import com.puresoltechnologies.ductiledb.core.tables.dml.CompareOperator;
import com.puresoltechnologies.ductiledb.core.tables.dml.DataManipulationLanguage;
import com.puresoltechnologies.ductiledb.core.tables.dml.PreparedDelete;
import com.puresoltechnologies.ductiledb.core.tables.dml.PreparedInsert;
import com.puresoltechnologies.ductiledb.core.tables.dml.PreparedSelect;
import com.puresoltechnologies.ductiledb.core.tables.dml.TableRow;
import com.puresoltechnologies.ductiledb.core.tables.dml.TableRowIterable;
import com.puresoltechnologies.genesis.commons.ProvidedVersionRange;
import com.puresoltechnologies.genesis.commons.SequenceMetadata;
import com.puresoltechnologies.genesis.commons.TransformationException;
import com.puresoltechnologies.genesis.commons.TransformationMetadata;
import com.puresoltechnologies.genesis.tracker.spi.Severity;
import com.puresoltechnologies.genesis.tracker.spi.TransformationTracker;
import com.puresoltechnologies.versioning.Version;

/**
 * This is the default migration tracker for Purifinity which puts everything
 * into Cassandra.
 * 
 * @author <NAME>
 */
public class DuctileDBTransformationTracker implements TransformationTracker {

    private static final Logger logger = LoggerFactory.getLogger(DuctileDBTransformationTracker.class);

    public static final String NAMESPACE_NAME = "genesis";

    public static final String CHANGELOG_TABLE = "changelog";
    public static final String MIGRATIONLOG_TABLE = "migrationlog";
    public static final String LAST_TRANSFORMATIONS_TABLE = "last_transformations";

    private PreparedInsert preparedInsertStatement = null;
    private PreparedSelect preparedSelectStatement = null;
    private PreparedDelete preparedDropComponentStatement = null;
    private PreparedInsert preparedLoggingStatement = null;
    private PreparedInsert preparedInsertLastTransformationStatement = null;
    private PreparedSelect preparedSelectLastTransformationStatement = null;
    private PreparedDelete preparedDropComponentLastTransformtaionStatement = null;

    private File configFile = null;
    private DuctileDB ductileDB;

    @Override
    public void open() throws TransformationException {
	loadConfiguration();
	connect();
	prepareKeyspace();
    }

    private void loadConfiguration() {
	configFile = new File(System.getProperty("genesis.tracker.ductiledb.config"));
    }

    private void connect() throws TransformationException {
	try (FileInputStream inputStream = new FileInputStream(configFile)) {
	    DuctileDBConfiguration configuration = DuctileDBBootstrap.readConfiguration(inputStream);
	    DuctileDBBootstrap.start(configuration);
	    ductileDB = DuctileDBBootstrap.getInstance();
	} catch (IOException e) {
	    throw new TransformationException("Could not connect to DuctileDB.", e);
	}
    }

    private void prepareKeyspace() throws TransformationException {
	try {
	    TableStore tableStore = ductileDB.getTableStore();
	    DataDefinitionLanguage dataDefinitionLanguage = tableStore.getDataDefinitionLanguage();
	    if (dataDefinitionLanguage.getNamespace(NAMESPACE_NAME) == null) {
		logger.info("Keyspace for Cassandra migration is missing. Needs to be created...");
		CreateNamespace createNamespace = dataDefinitionLanguage.createCreateNamespace(NAMESPACE_NAME);
		createNamespace.execute(tableStore);
		if (dataDefinitionLanguage.getNamespace(NAMESPACE_NAME) == null) {
		    throw new TransformationException("Could not create namespace '" + NAMESPACE_NAME + "'.");
		}
		logger.info("Namespace for DuctileDB migration tracker created.");
	    }
	    if (dataDefinitionLanguage.getTable(NAMESPACE_NAME, CHANGELOG_TABLE) == null) {
		logger.info("ChangeLog table for Cassandra migration is missing. Needs to be created...");
		CreateTable table = dataDefinitionLanguage.createCreateTable(NAMESPACE_NAME, CHANGELOG_TABLE);
		table.addColumn("changelog", "time", ColumnType.DATETIME);
		table.addColumn("changelog", "component", ColumnType.VARCHAR);
		table.addColumn("changelog", "machine", ColumnType.VARCHAR);
		table.addColumn("changelog", "version", ColumnType.VARCHAR);
		table.addColumn("changelog", "command", ColumnType.VARCHAR);
		table.addColumn("changelog", "developer", ColumnType.VARCHAR);
		table.addColumn("changelog", "comment", ColumnType.VARCHAR);
		table.addColumn("changelog", "hashid", ColumnType.VARCHAR);
		table.setPrimaryKey("component", "machine", "version", "command");
		table.execute(tableStore);
		logger.info("ChangeLog table for Cassandra migration created.");
	    }
	    if (dataDefinitionLanguage.getTable(NAMESPACE_NAME, MIGRATIONLOG_TABLE) == null) {
		logger.info("MigrationLog table for Cassandra migration is missing. Needs to be created...");
		CreateTable table = dataDefinitionLanguage.createCreateTable(NAMESPACE_NAME, MIGRATIONLOG_TABLE);
		table.addColumn("migrationlog", "time", ColumnType.DATETIME);
		table.addColumn("migrationlog", "severity", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "machine", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "thread", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "message", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "exception_type", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "exception_message", ColumnType.VARCHAR);
		table.addColumn("migrationlog", "stacktrace", ColumnType.VARCHAR);
		table.setPrimaryKey("time", "machine", "thread", "message");
		table.execute(tableStore);
		logger.info("MigrationLog table for Cassandra migration created.");
	    }
	    if (dataDefinitionLanguage.getTable(NAMESPACE_NAME, LAST_TRANSFORMATIONS_TABLE) == null) {
		logger.info("LastTransformations table for Cassandra migration is missing. Needs to be created...");
		CreateTable table = dataDefinitionLanguage.createCreateTable(NAMESPACE_NAME,
			LAST_TRANSFORMATIONS_TABLE);
		table.addColumn("transformations", "time", ColumnType.DATETIME);
		table.addColumn("transformations", "component", ColumnType.VARCHAR);
		table.addColumn("transformations", "machine", ColumnType.VARCHAR);
		table.addColumn("transformations", "start_version", ColumnType.VARCHAR);
		table.addColumn("transformations", "target_version", ColumnType.VARCHAR);
		table.addColumn("transformations", "next_version", ColumnType.VARCHAR);
		table.addColumn("transformations", "command", ColumnType.VARCHAR);
		table.addColumn("transformations", "developer", ColumnType.VARCHAR);
		table.addColumn("transformations", "comment", ColumnType.VARCHAR);
		table.addColumn("transformations", "hashid", ColumnType.VARCHAR);
		table.setPrimaryKey("component", "machine");
		table.execute(tableStore);
		logger.info("LastTransformations table for Cassandra migration created.");
	    }
	} catch (ExecutionException e) {
{'fraction_non_alphanumeric': 0.10355349310279066, 'fraction_numerical': 0.00025427499841078127, 'mean_word_length': 6.689149560117302, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5402073', 'n_tokens_mistral': 4593, 'n_tokens_neox': 4439, 'n_words': 821}