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_c/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 <db.h> 23<p> 24int 25DB->compact(DB *db, DB_TXN *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 34returns a non-zero error value on failure 35and 0 on success. 36</p> 37<b>Parameters</b> <br> 38 <b>txnid</b><ul compact><li>If the operation is part of an application-specified transaction, the 39<b>txnid</b> parameter is a transaction handle returned from 40<a href="../api_c/txn_begin.html">DB_ENV->txn_begin</a>; otherwise NULL. If no transaction handle is 41specified, but the operation occurs in a transactional database, the 42operation will be implicitly transaction protected using multiple 43transactions. These transactions will be periodically committed to 44avoid locking large sections of the tree. Any deadlocks encountered 45cause the compaction operation to retried from the point of the last 46transaction commit.</ul> 47 <b>start</b><ul compact><li>If non-NULL, the <b>start</b> parameter is the starting point for 48compaction in a Btree or Recno database. Compaction will start at the 49smallest key greater than or equal to the specified key. If NULL, 50compaction will start at the beginning of the database.</ul> 51 <b>stop</b><ul compact><li>If non-NULL, the <b>stop</b> parameter is the stopping point for 52compaction in a Btree or Recno database. Compaction will stop at the 53page with the smallest key greater than the specified key. If NULL, 54compaction will stop at the end of the database.</ul> 55 <b>c_data</b><ul compact><li><p>If non-NULL, the <b>c_data</b> parameter contains additional compaction 56configuration parameters, and returns compaction operation statistics, 57in a structure of type DB_COMPACT.</p> 58<p>The following input configuration fields are available from the 59DB_COMPACT structure:</p> 60<br> 61<b>int compact_fillpercent;</b><ul compact><li>If non-zero, the goal for filling pages, specified as a percentage 62between 1 and 100. Any page in a Btree or Recno databases not at or 63above this percentage full will be considered for compaction. The 64default behavior is to consider every page for compaction, regardless 65of its page fill percentage.</ul> 66<b>int compact_pages;</b><ul compact><li>If non-zero, the call will return after that number of pages have been 67freed.</ul> 68<b>db_timeout_t compact_timeout;</b><ul compact><li>If non-zero, and no <b>txnid</b> parameter was specified, the lock 69timeout set for implicit transactions, in microseconds.</ul> 70<br> 71<p>The following output statistics fields are available from the 72DB_COMPACT structure:</p> 73<br> 74<b>u_int32_t compact_deadlock;</b><ul compact><li>An output statistics parameter: if no <b>txnid</b> parameter was 75specified, the number of deadlocks which occurred.</ul> 76<b>u_int32_t compact_pages_examine;</b><ul compact><li>An output statistics parameter: the number of database pages reviewed 77during the compaction phase.</ul> 78<b>u_int32_t compact_pages_free;</b><ul compact><li>An output statistics parameter: the number of database pages freed during 79the compaction phase.</ul> 80<b>u_int32_t compact_levels;</b><ul compact><li>An output statistics parameter: the number of levels removed from the 81Btree or Recno database during the compaction phase.</ul> 82<b>u_int32_t compact_pages_truncated;</b><ul compact><li>An output statistics parameter: the number of database pages returned 83to the filesystem.</ul> 84<br></ul> 85 <b>flags</b><ul compact><li>The <b>flags</b> parameter must be set to 0 or 86one of the following values: 87<br> 88<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 89already free and at the end of the file. This flag must be set if the 90database is a Hash access method database.</ul> 91<b><a name="DB_FREE_SPACE">DB_FREE_SPACE</a></b><ul compact><li>Return pages to the filesystem when possible. 92If this flag is not specified, pages emptied as a result of compaction 93will be placed on the free list for re-use, but never returned to the 94filesystem. 95<p>Note that only pages at the end of a file can be returned to the 96filesystem. Because of the one-pass nature of the compaction algorithm, 97any not emptied page near the end of the file inhibits returning pages to 98the file system. A repeated call to the DB->compact method with a low 99<b>compact_fillpercent</b> may be used to return pages in this case.</p></ul> 100<br></ul> 101 <b>end</b><ul compact><li>If non-NULL, the <b>end</b> parameter will be filled in with the 102database key marking the end of the compaction operation in a Btree or 103Recno database. This is generally the first key of the page where the 104operation stopped.</ul> 105<br> 106<br><b>Errors</b> 107<p>The DB->compact method 108may fail and return one of the following non-zero errors:</p> 109<br> 110<b>DB_LOCK_DEADLOCK</b><ul compact><li>A transactional database environment operation was selected to resolve 111a deadlock.</ul> 112<b>DB_LOCK_NOTGRANTED</b><ul compact><li>A Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable 113to grant a lock in the allowed time.</ul> 114<br> 115<br> 116<b>DB_REP_HANDLE_DEAD</b><ul compact><li>The database handle has been invalidated because a replication election 117unrolled a committed transaction.</ul> 118<br> 119<br> 120<b>DB_REP_LOCKOUT</b><ul compact><li>The operation was blocked by client/master synchronization.</ul> 121<br> 122<br> 123<b>EACCES</b><ul compact><li>An attempt was made to modify a read-only database.</ul> 124<br> 125<br> 126<b>EINVAL</b><ul compact><li>An 127invalid flag value or parameter was specified.</ul> 128<br> 129<hr size=1 noshade> 130<br><b>Class</b> 131<a href="../api_c/db_class.html">DB</a> 132<br><b>See Also</b> 133<a href="../api_c/db_list.html">Databases and Related Methods</a> 134</tt> 135<table width="100%"><tr><td><br></td><td align=right> 136<a href="../api_c/api_core.html"><img src="../images/api.gif" alt="API"></a><a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a> 137</td></tr></table> 138<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 139</body> 140</html> 141