<commit_before>/**
 * This file is part of the CernVM file system.
 */

#include "catalog_sql.h"

#include <cstdlib>
#include <cstring>
#include <cstdlib>

#include "catalog.h"
#include "logging.h"
#include "util.h"

using namespace std;  // NOLINT

namespace catalog {

// Changelog
const float CatalogDatabase::kLatestSchema = 2.5;
const float CatalogDatabase::kLatestSupportedSchema = 2.5;  // + 1.X catalogs (r/o)
// ChangeLog
//   0 --> 1: add size column to nested catalog table,
//            add schema_revision property
const unsigned CatalogDatabase::kLatestSchemaRevision = 1;


bool CatalogDatabase::CheckSchemaCompatibility() {
  return !( (schema_version() >= 2.0-kSchemaEpsilon)                   &&
            (!IsEqualSchema(schema_version(), kLatestSupportedSchema)) &&
            (!IsEqualSchema(schema_version(), 2.4)           ||
             !IsEqualSchema(kLatestSupportedSchema, 2.5)) );
}


bool CatalogDatabase::LiveSchemaUpgradeIfNecessary() {
  assert (read_write());

  if (IsEqualSchema(schema_version(), 2.5) && (schema_revision() == 0)) {
    LogCvmfs(kLogCatalog, kLogDebug, "upgrading schema revision");

    Sql sql_upgrade(*this, "ALTER TABLE nested_catalogs ADD size INTEGER;");
    if (! sql_upgrade.Execute()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade nested_catalogs");
      return false;
    }

    set_schema_revision(1);
    if (! StoreSchemaRevision()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade schema revision");
      return false;
    }
  } else {
    LogCvmfs(kLogCatalog, kLogDebug, "schema revision is up to date.");
  }

  return true;
}


bool CatalogDatabase::CreateEmptyDatabase() {
  assert (read_write());

  // generate the catalog table and index structure
  const bool retval =
  Sql(*this,
    "CREATE TABLE catalog "
    "(md5path_1 INTEGER, md5path_2 INTEGER, parent_1 INTEGER, parent_2 INTEGER,"
    " hardlinks INTEGER, hash BLOB, size INTEGER, mode INTEGER, mtime INTEGER,"
    " flags INTEGER, name TEXT, symlink TEXT, uid INTEGER, gid INTEGER, "
    " CONSTRAINT pk_catalog PRIMARY KEY (md5path_1, md5path_2));").Execute()  &&
  Sql(*this,
    "CREATE INDEX idx_catalog_parent "
    "ON catalog (parent_1, parent_2);")                           .Execute()  &&
  Sql(*this,
    "CREATE TABLE chunks "
    "(md5path_1 INTEGER, md5path_2 INTEGER, offset INTEGER, size INTEGER, "
    " hash BLOB, "
    " CONSTRAINT pk_chunks PRIMARY KEY (md5path_1, md5path_2, offset, size), "<commit_msg>add xattr column to catalog table<commit_after>/**
 * This file is part of the CernVM file system.
 */

#include "catalog_sql.h"

#include <cstdlib>
#include <cstring>
#include <cstdlib>

#include "catalog.h"
#include "logging.h"
#include "util.h"

using namespace std;  // NOLINT

namespace catalog {

// Changelog
const float CatalogDatabase::kLatestSchema = 2.5;
const float CatalogDatabase::kLatestSupportedSchema = 2.5;  // + 1.X (r/o)
// ChangeLog
//   0 --> 1: add size column to nested catalog table,
//            add schema_revision property
//   1 --> 2: add xattr column to catalog table
const unsigned CatalogDatabase::kLatestSchemaRevision = 2;


bool CatalogDatabase::CheckSchemaCompatibility() {
  return !( (schema_version() >= 2.0-kSchemaEpsilon)                   &&
            (!IsEqualSchema(schema_version(), kLatestSupportedSchema)) &&
            (!IsEqualSchema(schema_version(), 2.4)           ||
             !IsEqualSchema(kLatestSupportedSchema, 2.5)) );
}


bool CatalogDatabase::LiveSchemaUpgradeIfNecessary() {
  assert (read_write());

  if (IsEqualSchema(schema_version(), 2.5) && (schema_revision() == 0)) {
    LogCvmfs(kLogCatalog, kLogDebug, "upgrading schema revision");

    Sql sql_upgrade(*this, "ALTER TABLE nested_catalogs ADD size INTEGER;");
    if (! sql_upgrade.Execute()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade nested_catalogs");
      return false;
    }

    set_schema_revision(1);
    if (! StoreSchemaRevision()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade schema revision");
      return false;
    }
  } if (IsEqualSchema(schema_version(), 2.5) && (schema_revision() == 1)) {
    LogCvmfs(kLogCatalog, kLogDebug, "upgrading schema revision");

    Sql sql_upgrade(*this, "ALTER TABLE catalog ADD xattr BLOB;");
    if (!sql_upgrade.Execute()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade catalogs (1 --> 2)");
      return false;
    }

    set_schema_revision(2);
    if (!StoreSchemaRevision()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade schema revision");
      return false;
    }
  } else {
    LogCvmfs(kLogCatalog, kLogDebug, "schema revision is up to date.");
  }

  return true;
}


bool CatalogDatabase::CreateEmptyDatabase() {
  assert (read_write());

  // generate the catalog table and index structure
  const bool retval =
  Sql(*this,
    "CREATE TABLE catalog "
    "(md5path_1 INTEGER, md5path_2 INTEGER, parent_1 INTEGER, parent_2 INTEGER,"
    " hardlinks INTEGER, hash BLOB, size INTEGER, mode INTEGER, mtime INTEGER,"
    " flags INTEGER, name TEXT, symlink TEXT, uid INTEGER, gid INTEGER, "
    " xattr BLOB, "  
    " CONSTRAINT pk_catalog PRIMARY KEY (md5path_1, md5path_2));").Execute()  &&
  Sql(*this,
    "CREATE INDEX idx_catalog_parent "
    "ON catalog (parent_1, parent_2);")                           .Execute()  &&
  Sql(*this,
    "CREATE TABLE chunks "
    "(md5path_1 INTEGER, md5path_2 INTEGER, offset INTEGER, size INTEGER, "
    " hash BLOB, "
    " CONSTRAINT pk_chunks PRIMARY KEY (md5path_1, md5path_2, offset, size), "
{'fraction_non_alphanumeric': 0.12204301075268817, 'fraction_numerical': 0.015053763440860216, 'mean_word_length': 3.51537216828479, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '193152', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1763, 'n_words': 472}