<commit_before>/*
 * Copyright (C) 2017-2018 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <dirent.h>
#include <fstream>
#include <map>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>

#include "item_rpm.hpp"
#include "swdb.hpp"
#include "transaction.hpp"
#include "transactionitem.hpp"
#include "transformer.hpp"

static const char *sql_create_tables =
#include "sql/create_tables.sql"
    ;

void
Transformer::createDatabase(SQLite3Ptr conn)
{
    conn->exec(sql_create_tables);
}

/**
 * Map of supported actions (originally states): string -> enum
 */
static const std::unordered_map< std::string, TransactionItemAction > actions = {
    {"Install", TransactionItemAction::INSTALL},
    {"True-Install", TransactionItemAction::INSTALL},
    {"Dep-Install", TransactionItemAction::INSTALL},
    {"Downgrade", TransactionItemAction::DOWNGRADE},
    {"Downgraded", TransactionItemAction::DOWNGRADED},
    {"Obsoleting", TransactionItemAction::OBSOLETE},
    {"Obsoleted", TransactionItemAction::OBSOLETED},
    {"Update", TransactionItemAction::UPGRADE},
    {"Updated", TransactionItemAction::UPGRADED},
    {"Erase", TransactionItemAction::REMOVE},
    {"Reinstall", TransactionItemAction::REINSTALL},
    {"Reinstalled", TransactionItemAction::REINSTALL}};

/**
 * Map of supported reasons: string -> enum
 */
static const std::unordered_map< std::string, TransactionItemReason > reasons = {
    {"dep", TransactionItemReason::DEPENDENCY},
    {"user", TransactionItemReason::USER},
    {"clean", TransactionItemReason::CLEAN},
    {"weak", TransactionItemReason::WEAK_DEPENDENCY},
    {"group", TransactionItemReason::GROUP}};

/**
 * Covert string reason into appropriate enumerated variant
 */
TransactionItemReason
Transformer::getReason(const std::string &reason)
{
    auto it = reasons.find(reason);
    if (it == reasons.end()) {
        return TransactionItemReason::UNKNOWN;
    }
    return it->second;
}

/**
 * Default constructor of the Transformer object
 * \param outputFile path to output SQLite3 database
 * \param inputDir directory to load data from (e.g. `/var/lib/dnf/`)
 */
Transformer::Transformer(const std::string &outputFile, const std::string &inputDir)
  : inputDir(inputDir)
  , outputFile(outputFile)
{
}

/**
 * Perform the database transformation routine.
 * The database is transformed in-memory.
 * Final scheme is dumped into outputFile
 */
void
Transformer::transform()
{
    auto swdb = std::make_shared< SQLite3 >(":memory:");
    auto history = std::make_shared< SQLite3 >(historyPath().c_str());

    // create a new database file
    createDatabase(swdb);

    // transform groups
    transformGroups(swdb);

    // transform objects
    transformTrans(swdb, history);

    // dump database to a file<commit_msg>[swdb] transform groups after transaction to avoid tid #1 conflict.<commit_after>/*
 * Copyright (C) 2017-2018 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <dirent.h>
#include <fstream>
#include <map>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>

#include "item_rpm.hpp"
#include "swdb.hpp"
#include "transaction.hpp"
#include "transactionitem.hpp"
#include "transformer.hpp"

static const char *sql_create_tables =
#include "sql/create_tables.sql"
    ;

void
Transformer::createDatabase(SQLite3Ptr conn)
{
    conn->exec(sql_create_tables);
}

/**
 * Map of supported actions (originally states): string -> enum
 */
static const std::unordered_map< std::string, TransactionItemAction > actions = {
    {"Install", TransactionItemAction::INSTALL},
    {"True-Install", TransactionItemAction::INSTALL},
    {"Dep-Install", TransactionItemAction::INSTALL},
    {"Downgrade", TransactionItemAction::DOWNGRADE},
    {"Downgraded", TransactionItemAction::DOWNGRADED},
    {"Obsoleting", TransactionItemAction::OBSOLETE},
    {"Obsoleted", TransactionItemAction::OBSOLETED},
    {"Update", TransactionItemAction::UPGRADE},
    {"Updated", TransactionItemAction::UPGRADED},
    {"Erase", TransactionItemAction::REMOVE},
    {"Reinstall", TransactionItemAction::REINSTALL},
    {"Reinstalled", TransactionItemAction::REINSTALL}};

/**
 * Map of supported reasons: string -> enum
 */
static const std::unordered_map< std::string, TransactionItemReason > reasons = {
    {"dep", TransactionItemReason::DEPENDENCY},
    {"user", TransactionItemReason::USER},
    {"clean", TransactionItemReason::CLEAN},
    {"weak", TransactionItemReason::WEAK_DEPENDENCY},
    {"group", TransactionItemReason::GROUP}};

/**
 * Covert string reason into appropriate enumerated variant
 */
TransactionItemReason
Transformer::getReason(const std::string &reason)
{
    auto it = reasons.find(reason);
    if (it == reasons.end()) {
        return TransactionItemReason::UNKNOWN;
    }
    return it->second;
}

/**
 * Default constructor of the Transformer object
 * \param outputFile path to output SQLite3 database
 * \param inputDir directory to load data from (e.g. `/var/lib/dnf/`)
 */
Transformer::Transformer(const std::string &outputFile, const std::string &inputDir)
  : inputDir(inputDir)
  , outputFile(outputFile)
{
}

/**
 * Perform the database transformation routine.
 * The database is transformed in-memory.
 * Final scheme is dumped into outputFile
 */
void
Transformer::transform()
{
    auto swdb = std::make_shared< SQLite3 >(":memory:");
    auto history = std::make_shared< SQLite3 >(historyPath().c_str());

    // create a new database file
    createDatabase(swdb);

    // transform objects
    transformTrans(swdb, history);

    // transform groups
    transformGroups(swdb);

    // dump database to a file
{'fraction_non_alphanumeric': 0.1182239171887769, 'fraction_numerical': 0.007491146826477799, 'mean_word_length': 4.705516705516706, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 39, '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': '5771876', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2034, 'n_words': 668}