1<!--$Id: db_set_bt_compare.so,v 10.50 2006/10/26 20:59:24 bostic 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::set_bt_compare</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::set_bt_compare</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>
24extern "C" {
25    typedef int (*bt_compare_fcn_type)(DB *db, const DBT *dbt1, const DBT *dbt2);
26};
27int
28Db::set_bt_compare(bt_compare_fcn_type bt_compare_fcn);
29</pre></b>
30<hr size=1 noshade>
31<b>Description: Db::set_bt_compare</b>
32<p>Set the Btree key comparison function.  The comparison function is
33called whenever it is necessary to compare a key specified by the
34application with a key currently stored in the tree.</p>
35<p>If no comparison function is specified, the keys are compared lexically,
36with shorter keys collating before longer keys.</p>
37<p>The Db::set_bt_compare method configures operations performed using the specified
38<a href="../api_cxx/db_class.html">Db</a> handle, not all operations performed on the underlying
39database.</p>
40<p>The Db::set_bt_compare method may not be called after the <a href="../api_cxx/db_open.html">Db::open</a> method is called.
41If the database already exists when
42<a href="../api_cxx/db_open.html">Db::open</a> is called, the information specified to Db::set_bt_compare must
43be the same as that historically used to create the database or
44corruption can occur.</p>
45<p>The Db::set_bt_compare method
46either returns a non-zero error value
47or throws an exception that encapsulates a non-zero error value on
48failure, and returns 0 on success.
49</p>
50<b>Parameters</b> <br>
51 <b>bt_compare_fcn</b><ul compact><li>The <b>bt_compare_fcn</b> function is the application-specified Btree
52comparison function.  The comparison function takes three parameters:
53<br>
54<b><b>db</b></b><ul compact><li>The <b>db</b> parameter is the enclosing database handle.</ul>
55<b><b>dbt1</b></b><ul compact><li>The <b>dbt1</b> parameter is the <a href="../api_cxx/dbt_class.html">Dbt</a> representing the
56application supplied key.</ul>
57<b><b>dbt2</b></b><ul compact><li>The <b>dbt2</b> parameter is the <a href="../api_cxx/dbt_class.html">Dbt</a> representing the
58current tree's key.</ul>
59<br></ul>
60<p>The <b>bt_compare_fcn</b> function must return an integer value less
61than, equal to, or greater than zero if the first key parameter is
62considered to be respectively less than, equal to, or greater than the
63second key parameter.  In addition, the comparison function must cause
64the keys in the database to be <i>well-ordered</i>.  The comparison
65function must correctly handle any key values used by the application
66(possibly including zero-length keys).  In addition, when Btree key
67prefix comparison is being performed (see <a href="../api_cxx/db_set_bt_prefix.html">Db::set_bt_prefix</a> for
68more information), the comparison routine may be passed a prefix of any
69database key.  The <b>data</b> and <b>size</b> fields of the
70<a href="../api_cxx/dbt_class.html">Dbt</a> are the only fields that may be used for the purposes of
71this comparison, and no particular alignment of the memory to which by
72the <b>data</b> field refers may be assumed.</p>
73<br>
74<br><b>Errors</b>
75<p>The Db::set_bt_compare method
76may fail and throw
77<a href="../api_cxx/except_class.html">DbException</a>,
78encapsulating one of the following non-zero errors, or return one of
79the following non-zero errors:</p>
80<br>
81<b>EINVAL</b><ul compact><li>If the method was called after <a href="../api_cxx/db_open.html">Db::open</a> was called; or if an
82invalid flag value or parameter was specified.</ul>
83<br>
84<hr size=1 noshade>
85<br><b>Class</b>
86<a href="../api_cxx/db_class.html">Db</a>
87<br><b>See Also</b>
88<a href="../api_cxx/db_list.html">Databases and Related Methods</a>
89</tt>
90<table width="100%"><tr><td><br></td><td align=right>
91<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>
92</td></tr></table>
93<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
94</body>
95</html>
96