<commit_before>/* changes-table.c : operations on the `changes' table
 *
 * ====================================================================
 * Copyright (c) 2000-2004 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://subversion.tigris.org/.
 * ====================================================================
 */

#include "bdb_compat.h"

#include <apr_hash.h>
#include <apr_tables.h>

#include "svn_fs.h"
#include "svn_pools.h"
#include "svn_path.h"
#include "../fs.h"
#include "../id.h"
#include "../err.h"
#include "../trail.h"
#include "../util/fs_skels.h"
#include "bdb-err.h"
#include "dbt.h"
#include "changes-table.h"




/*** Creating and opening the changes table. ***/

int
svn_fs__bdb_open_changes_table (DB **changes_p,
                                DB_ENV *env,
                                svn_boolean_t create)
{
  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
  DB *changes;

  BDB_ERR (svn_fs__bdb_check_version());
  BDB_ERR (db_create (&changes, env, 0));

  /* Enable duplicate keys. This allows us to store the changes
     one-per-row.  Note: this must occur before ->open().  */
  BDB_ERR (changes->set_flags (changes, DB_DUP));

  BDB_ERR (changes->open (SVN_BDB_OPEN_PARAMS(changes, NULL),
                         "changes", 0, DB_BTREE,
                         open_flags | SVN_BDB_AUTO_COMMIT,
                         0666));

  *changes_p = changes;
  return 0;
}




/*** Storing and retrieving changes.  ***/

svn_error_t *
svn_fs__bdb_changes_add (svn_fs_t *fs,
                         const char *key,
                         svn_fs__change_t *change,
                         trail_t *trail)
{
  DBT query, value;
  skel_t *skel;

  /* Convert native type to skel. */
  SVN_ERR (svn_fs__unparse_change_skel (&skel, change, trail->pool));

  /* Store a new record into the database. */
  svn_fs__str_to_dbt (&query, key);
  svn_fs__skel_to_dbt (&value, skel, trail->pool);
  svn_fs__trail_debug (trail, "changes", "put");
  SVN_ERR (BDB_WRAP (fs, "creating change", 
                    fs->changes->put (fs->changes, trail->db_txn,
                                      &query, &value, 0)));

  return SVN_NO_ERROR;
}


svn_error_t *
svn_fs__bdb_changes_delete (svn_fs_t *fs,
                            const char *key,
                            trail_t *trail)
{
  int db_err;
  DBT query;
  
  svn_fs__trail_debug (trail, "changes", "del");
  db_err = fs->changes->del (fs->changes, trail->db_txn,
                             svn_fs__str_to_dbt (&query, key), 0);

  /* If there're no changes for KEY, that is acceptable.  Any other
     error should be propogated to the caller, though.  */
  if ((db_err) && (db_err != DB_NOTFOUND))
    {
      SVN_ERR (BDB_WRAP (fs, "deleting changes", db_err));
    }

  return SVN_NO_ERROR;
}


/* Merge the internal-use-only CHANGE into a hash of public-FS
   svn_fs_path_change_t CHANGES, collapsing multiple changes into a
   single summarical (is that real word?) change per path. */
static svn_error_t *
fold_change (apr_hash_t *changes,
             const svn_fs__change_t *change)
{
  apr_pool_t *pool = apr_hash_pool_get (changes);
  svn_fs_path_change_t *old_change, *new_change;
  const char *path;

  if ((old_change = apr_hash_get (changes, change->path, APR_HASH_KEY_STRING)))
    {
      /* This path already exists in the hash, so we have to merge
         this change into the already existing one. */
      
      /* Since the path already exists in the hash, we don't have to
         dup the allocation for the path itself. */
      path = change->path;

      /* Sanity check:  only allow NULL node revision ID in the
         `reset' case. */
      if ((! change->noderev_id) && (change->kind != svn_fs_path_change_reset))
        return svn_error_create 
          (SVN_ERR_FS_CORRUPT, NULL,
           "Missing required node revision ID");
        
      /* Sanity check:  we should be talking about the same node
         revision ID as our last change except where the last change
         was a deletion. */
      if (change->noderev_id
          && (! svn_fs__id_eq (old_change->node_rev_id, change->noderev_id))
          && (old_change->change_kind != svn_fs_path_change_delete))
        return svn_error_create 
          (SVN_ERR_FS_CORRUPT, NULL,
           "Invalid change ordering: new node revision ID without delete");

      /* Sanity check: an add, replacement, or reset must be the first
         thing to follow a deletion. */
      if ((old_change->change_kind == svn_fs_path_change_delete)
          && (! ((change->kind == svn_fs_path_change_replace) 
                 || (change->kind == svn_fs_path_change_reset)
                 || (change->kind == svn_fs_path_change_add))))
        return svn_error_create 
          (SVN_ERR_FS_CORRUPT, NULL,
           "Invalid change ordering: non-add change on deleted path");

      /* Now, merge that change in. */
      switch (change->kind)
        {
        case svn_fs_path_change_reset:
          /* A reset here will simply remove the path change from the
             hash. */
          old_change = NULL;
          break;

        case svn_fs_path_change_delete:
          if ((old_change->change_kind == svn_fs_path_change_replace)
              || (old_change->change_kind == svn_fs_path_change_add))
            {
              /* If the path was introduced in this transaction via an
                 add or replace, and we are deleting it, just remove
                 the path altogether.  */
              old_change = NULL;
            }
          else
            {
              /* A deletion overrules all previous changes. */
              old_change->change_kind = svn_fs_path_change_delete;
              old_change->text_mod = change->text_mod;<commit_msg>Fix a bug which causes some cvs2svn-produced repositories to gain spurious additional files when passed through a dump-load cycle.<commit_after>/* changes-table.c : operations on the `changes' table
 *
 * ====================================================================
 * Copyright (c) 2000-2004 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://subversion.tigris.org/.
 * ====================================================================
 */

#include "bdb_compat.h"

#include <apr_hash.h>
#include <apr_tables.h>

#include "svn_fs.h"
#include "svn_pools.h"
#include "svn_path.h"
#include "../fs.h"
#include "../id.h"
#include "../err.h"
#include "../trail.h"
#include "../util/fs_skels.h"
#include "bdb-err.h"
#include "dbt.h"
#include "changes-table.h"




/*** Creating and opening the changes table. ***/

int
svn_fs__bdb_open_changes_table (DB **changes_p,
                                DB_ENV *env,
                                svn_boolean_t create)
{
  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
  DB *changes;

  BDB_ERR (svn_fs__bdb_check_version());
  BDB_ERR (db_create (&changes, env, 0));

  /* Enable duplicate keys. This allows us to store the changes
     one-per-row.  Note: this must occur before ->open().  */
  BDB_ERR (changes->set_flags (changes, DB_DUP));

  BDB_ERR (changes->open (SVN_BDB_OPEN_PARAMS(changes, NULL),
                         "changes", 0, DB_BTREE,
                         open_flags | SVN_BDB_AUTO_COMMIT,
                         0666));

  *changes_p = changes;
  return 0;
}




/*** Storing and retrieving changes.  ***/

svn_error_t *
svn_fs__bdb_changes_add (svn_fs_t *fs,
                         const char *key,
                         svn_fs__change_t *change,
                         trail_t *trail)
{
  DBT query, value;
  skel_t *skel;

  /* Convert native type to skel. */
  SVN_ERR (svn_fs__unparse_change_skel (&skel, change, trail->pool));

  /* Store a new record into the database. */
  svn_fs__str_to_dbt (&query, key);
  svn_fs__skel_to_dbt (&value, skel, trail->pool);
  svn_fs__trail_debug (trail, "changes", "put");
  SVN_ERR (BDB_WRAP (fs, "creating change", 
                    fs->changes->put (fs->changes, trail->db_txn,
                                      &query, &value, 0)));

  return SVN_NO_ERROR;
}


svn_error_t *
svn_fs__bdb_changes_delete (svn_fs_t *fs,
                            const char *key,
                            trail_t *trail)
{
  int db_err;
  DBT query;
  
  svn_fs__trail_debug (trail, "changes", "del");
  db_err = fs->changes->del (fs->changes, trail->db_txn,
                             svn_fs__str_to_dbt (&query, key), 0);

  /* If there're no changes for KEY, that is acceptable.  Any other
     error should be propogated to the caller, though.  */
  if ((db_err) && (db_err != DB_NOTFOUND))
    {
      SVN_ERR (BDB_WRAP (fs, "deleting changes", db_err));
    }

  return SVN_NO_ERROR;
}


/* Merge the internal-use-only CHANGE into a hash of public-FS
   svn_fs_path_change_t CHANGES, collapsing multiple changes into a
   single summarical (is that real word?) change per path. */
static svn_error_t *
fold_change (apr_hash_t *changes,
             const svn_fs__change_t *change)
{
  apr_pool_t *pool = apr_hash_pool_get (changes);
  svn_fs_path_change_t *old_change, *new_change;
  const char *path;

  if ((old_change = apr_hash_get (changes, change->path, APR_HASH_KEY_STRING)))
    {
      /* This path already exists in the hash, so we have to merge
         this change into the already existing one. */
      
      /* Since the path already exists in the hash, we don't have to
         dup the allocation for the path itself. */
      path = change->path;

      /* Sanity check:  only allow NULL node revision ID in the
         `reset' case. */
      if ((! change->noderev_id) && (change->kind != svn_fs_path_change_reset))
        return svn_error_create 
          (SVN_ERR_FS_CORRUPT, NULL,
           "Missing required node revision ID");
        
      /* Sanity check:  we should be talking about the same node
         revision ID as our last change except where the last change
         was a deletion. */
      if (change->noderev_id
          && (! svn_fs__id_eq (old_change->node_rev_id, change->noderev_id))
          && (old_change->change_kind != svn_fs_path_change_delete))
        return svn_error_create 
          (SVN_ERR_FS_CORRUPT, NULL,
           "Invalid change ordering: new node revision ID without delete");

      /* Sanity check: an add, replacement, or reset must be the first
         thing to follow a deletion. */
      if ((old_change->change_kind == svn_fs_path_change_delete)
          && (! ((change->kind == svn_fs_path_change_replace) 
                 || (change->kind == svn_fs_path_change_reset)
                 || (change->kind == svn_fs_path_change_add))))
        return svn_error_create 
          (SVN_ERR_FS_CORRUPT, NULL,
           "Invalid change ordering: non-add change on deleted path");

      /* Now, merge that change in. */
      switch (change->kind)
        {
        case svn_fs_path_change_reset:
          /* A reset here will simply remove the path change from the
             hash. */
          old_change = NULL;
          break;

        case svn_fs_path_change_delete:
          if (old_change->change_kind == svn_fs_path_change_add)
            {
              /* If the path was introduced in this transaction via an
                 add, and we are deleting it, just remove the path
                 altogether. */
              old_change = NULL;
            }
          else
            {
              /* A deletion overrules all previous changes. */
              old_change->change_kind = svn_fs_path_change_delete;
              old_change->text_mod = change->text_mod;
{'fraction_non_alphanumeric': 0.12042343202801656, 'fraction_numerical': 0.0034224769181789238, 'mean_word_length': 2.384967672413793, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4216281', 'n_tokens_mistral': 4031, 'n_tokens_neox': 3862, 'n_words': 1123}