### Db::compact |

`

###

    #include <db_cxx.h>

    int
    Db::compact(DbTxn *txnid,
        Dbt *start, Dbt *stop, DB_COMPACT *c_data, u_int32_t flags, Dbt *end);

### Description: Db::compact

The Db::compact method compacts Btree and Recno access method databases, and optionally returns unused Btree, Hash or Recno database pages to the underlying filesystem.

The Db::compact method either returns a non-zero error value or throws an exception that encapsulates a non-zero error value on failure, and returns 0 on success.

### Parameters

txnid
  If the operation is part of an application-specified transaction, the txnid parameter is a transaction handle returned from DbEnv::txn_begin; otherwise NULL. If no transaction handle is specified, but the operation occurs in a transactional database, the operation will be implicitly transaction protected using multiple transactions. These transactions will be periodically committed to avoid locking large sections of the tree. Any deadlocks encountered cause the compaction operation to retried from the point of the last transaction commit.
start
  If non-NULL, the start parameter is the starting point for compaction in a Btree or Recno database. Compaction will start at the smallest key greater than or equal to the specified key. If NULL, compaction will start at the beginning of the database.
stop
  If non-NULL, the stop parameter is the stopping point for compaction in a Btree or Recno database. Compaction will stop at the page with the smallest key greater than the specified key. If NULL, compaction will stop at the end of the database.
c_data

  If non-NULL, the c_data parameter contains additional compaction configuration parameters, and returns compaction operation statistics, in a structure of type DB_COMPACT.

  The following input configuration fields are available from the DB_COMPACT structure:

  int compact_fillpercent;
    If non-zero, the goal for filling pages, specified as a percentage between 1 and 100. Any page in a Btree or Recno databases not at or above this percentage full will be considered for compaction. The default behavior is to consider every page for compaction, regardless of its page fill percentage.
  int compact_pages;
    If non-zero, the call will return after that number of pages have been freed.
  db_timeout_t compact_timeout;
    If non-zero, and no txnid parameter was specified, the lock timeout set for implicit transactions, in microseconds.

  The following output statistics fields are available from the DB_COMPACT structure:

  u_int32_t compact_deadlock;
    An output statistics parameter: if no txnid parameter was specified, the number of deadlocks which occurred.
  u_int32_t compact_pages_examine;
    An output statistics parameter: the number of database pages reviewed during the compaction phase.
  u_int32_t compact_pages_free;
    An output statistics parameter: the number of database pages freed during the compaction phase.
  u_int32_t compact_levels;
    An output statistics parameter: the number of levels removed from the Btree or Recno database during the compaction phase.
  u_int32_t compact_pages_truncated;
    An output statistics parameter: the number of database pages returned to the filesystem.
flags
  The flags parameter must be set to 0 or one of the following values:
  DB_FREELIST_ONLY
    Do no page compaction, only returning pages to the filesystem that are already free and at the end of the file. This flag must be set if the database is a Hash access method database.
  DB_FREE_SPACE
    Return pages to the filesystem when possible. If this flag is not specified, pages emptied as a result of compaction will be placed on the free list for re-use, but never returned to the filesystem.

    Note that only pages at the end of a file can be returned to the filesystem. Because of the one-pass nature of the compaction algorithm, any unemptied page near the end of the file inhibits returning pages to the file system. A repeated call to the Db::compact method with a low compact_fillpercent may be used to return pages in this case.

end
  If non-NULL, the end parameter will be filled in with the database key marking the end of the compaction operation in a Btree or Recno database. This is generally the first key of the page where the operation stopped.

### Errors

The Db::compact method may fail and throw DbException, encapsulating one of the following non-zero errors, or return one of the following non-zero errors:

DB_REP_HANDLE_DEAD
  The database handle has been invalidated because a replication election unrolled a committed transaction.
DB_REP_LOCKOUT
  The operation was blocked by client/master synchronization.

EACCES
  An attempt was made to modify a read-only database.

EINVAL
  An invalid flag value or parameter was specified.

If a transactional database environment operation was selected to resolve a deadlock, the Db::compact method will fail and either return DB_LOCK_DEADLOCK or throw a DbDeadlockException exception.

If a Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable to grant a lock in the allowed time, the Db::compact method will fail and either return DB_LOCK_NOTGRANTED or throw a DbLockNotGrantedException exception.

### Class

Db

### See Also

Databases and Related Methods`

Copyright (c) 1996,2007 Oracle. All rights reserved.