1<!--$Id: db_compact.so,v 1.8 2008/04/30 15:25:34 mbrey Exp $-->
2<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
3<!--See the file LICENSE for redistribution information.-->
4<html>
5<head>
6<title>Berkeley DB: Db::compact</title>
7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
9</head>
10<body bgcolor=white>
11<table width="100%"><tr valign=top>
12<td>
13<b>Db::compact</b>
14</td>
15<td align=right>
16<a href="../api_cxx/api_core.html"><img src="../images/api.gif" alt="API"></a>
17<a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a></td>
18</tr></table>
19<hr size=1 noshade>
20<tt>
21<b><pre>
22#include &lt;db_cxx.h&gt;
23<p>
24int
25Db::compact(DbTxn *txnid,
26    Dbt *start, Dbt *stop, DB_COMPACT *c_data, u_int32_t flags, Dbt *end);
27</pre></b>
28<hr size=1 noshade>
29<b>Description: Db::compact</b>
30<p>The Db::compact method compacts Btree and Recno access method
31databases, and optionally returns unused Btree, Hash or Recno database
32pages to the underlying filesystem.</p>
33<p>The Db::compact method
34either returns a non-zero error value
35or throws an exception that encapsulates a non-zero error value on
36failure, and returns 0 on success.
37</p>
38<b>Parameters</b> <br>
39 <b>txnid</b><ul compact><li>If the operation is part of an application-specified transaction, the
40<b>txnid</b> parameter is a transaction handle returned from
41<a href="../api_cxx/txn_begin.html">DbEnv::txn_begin</a>; otherwise NULL.  If no transaction handle is
42specified, but the operation occurs in a transactional database, the
43operation will be implicitly transaction protected using multiple
44transactions.  These transactions will be periodically committed to
45avoid locking large sections of the tree.  Any deadlocks encountered
46cause the compaction operation to retried from the point of the last
47transaction commit.</ul>
48 <b>start</b><ul compact><li>If non-NULL, the <b>start</b> parameter is the starting point for
49compaction in a Btree or Recno database.  Compaction will start at the
50smallest key greater than or equal to the specified key.  If NULL,
51compaction will start at the beginning of the database.</ul>
52 <b>stop</b><ul compact><li>If non-NULL, the <b>stop</b> parameter is the stopping point for
53compaction in a Btree or Recno database.  Compaction will stop at the
54page with the smallest key greater than the specified key.  If NULL,
55compaction will stop at the end of the database.</ul>
56 <b>c_data</b><ul compact><li><p>If non-NULL, the <b>c_data</b> parameter contains additional compaction
57configuration parameters, and returns compaction operation statistics,
58in a structure of type DB_COMPACT.</p>
59<p>The following input configuration fields are available from the
60DB_COMPACT structure:</p>
61<br>
62<b>int compact_fillpercent;</b><ul compact><li>If non-zero, the goal for filling pages, specified as a percentage
63between 1 and 100.  Any page in a Btree or Recno databases not at or
64above this percentage full will be considered for compaction.  The
65default behavior is to consider every page for compaction, regardless
66of its page fill percentage.</ul>
67<b>int compact_pages;</b><ul compact><li>If non-zero, the call will return after that number of pages have been
68freed.</ul>
69<b>db_timeout_t compact_timeout;</b><ul compact><li>If non-zero, and no <b>txnid</b> parameter was specified, the lock
70timeout set for implicit transactions, in microseconds.</ul>
71<br>
72<p>The following output statistics fields are available from the
73DB_COMPACT structure:</p>
74<br>
75<b>u_int32_t compact_deadlock;</b><ul compact><li>An output statistics parameter: if no <b>txnid</b> parameter was
76specified, the number of deadlocks which occurred.</ul>
77<b>u_int32_t compact_pages_examine;</b><ul compact><li>An output statistics parameter: the number of database pages reviewed
78during the compaction phase.</ul>
79<b>u_int32_t compact_pages_free;</b><ul compact><li>An output statistics parameter: the number of database pages freed during
80the compaction phase.</ul>
81<b>u_int32_t compact_levels;</b><ul compact><li>An output statistics parameter: the number of levels removed from the
82Btree or Recno database during the compaction phase.</ul>
83<b>u_int32_t compact_pages_truncated;</b><ul compact><li>An output statistics parameter: the number of database pages returned
84to the filesystem.</ul>
85<br></ul>
86 <b>flags</b><ul compact><li>The <b>flags</b> parameter must be set to 0 or
87one of the following values:
88<br>
89<b><a name="DB_FREELIST_ONLY">DB_FREELIST_ONLY</a></b><ul compact><li>Do no page compaction, only returning pages to the filesystem that are
90already free and at the end of the file.  This flag must be set if the
91database is a Hash access method database.</ul>
92<b><a name="DB_FREE_SPACE">DB_FREE_SPACE</a></b><ul compact><li>Return pages to the filesystem when possible.
93If this flag is not specified, pages emptied as a result of compaction
94will be placed on the free list for re-use, but never returned to the
95filesystem.
96<p>Note that only pages at the end of a file can be returned to the
97filesystem.  Because of the one-pass nature of the compaction algorithm,
98any not emptied page near the end of the file inhibits returning pages to
99the file system.  A repeated call to the Db::compact method with a low
100<b>compact_fillpercent</b> may be used to return pages in this case.</p></ul>
101<br></ul>
102 <b>end</b><ul compact><li>If non-NULL, the <b>end</b> parameter will be filled in with the
103database key marking the end of the compaction operation in a Btree or
104Recno database.  This is generally the first key of the page where the
105operation stopped.</ul>
106<br>
107<br><b>Errors</b>
108<p>The Db::compact method
109may fail and throw
110<a href="../api_cxx/except_class.html">DbException</a>,
111encapsulating one of the following non-zero errors, or return one of
112the following non-zero errors:</p>
113<br>
114<b>DB_REP_HANDLE_DEAD</b><ul compact><li>The database handle has been invalidated because a replication election
115unrolled a committed transaction.</ul>
116<br>
117<br>
118<b>DB_REP_LOCKOUT</b><ul compact><li>The operation was blocked by client/master synchronization.</ul>
119<br>
120<br>
121<b>EACCES</b><ul compact><li>An attempt was made to modify a read-only database.</ul>
122<br>
123<br>
124<b>EINVAL</b><ul compact><li>An
125invalid flag value or parameter was specified.</ul>
126<br>
127<p>If a transactional database environment operation was selected to
128resolve a deadlock, the Db::compact method will fail and
129either return <a href="../ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> or
130throw a <a href="../api_cxx/deadlock_class.html">DbDeadlockException</a> exception.</p>
131<p>If a Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable
132to grant a lock in the allowed time, the Db::compact method will fail and
133either return <a href="../ref/program/errorret.html#DB_LOCK_NOTGRANTED">DB_LOCK_NOTGRANTED</a> or
134throw a <a href="../api_cxx/lockng_class.html">DbLockNotGrantedException</a> exception.</p>
135<hr size=1 noshade>
136<br><b>Class</b>
137<a href="../api_cxx/db_class.html">Db</a>
138<br><b>See Also</b>
139<a href="../api_cxx/db_list.html">Databases and Related Methods</a>
140</tt>
141<table width="100%"><tr><td><br></td><td align=right>
142<a href="../api_cxx/api_core.html"><img src="../images/api.gif" alt="API"></a><a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a>
143</td></tr></table>
144<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
145</body>
146</html>
147