1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Db::set_h_compare()</title> 7 <link rel="stylesheet" href="apiReference.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Berkeley DB C++ API Reference" /> 10 <link rel="up" href="db.html" title="Chapter 2. The Db Handle" /> 11 <link rel="prev" href="dbset_flags.html" title="Db::set_flags()" /> 12 <link rel="next" href="dbset_h_ffactor.html" title="Db::set_h_ffactor()" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Db::set_h_compare()</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dbset_flags.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 2. 23 The Db Handle 24 </th> 25 <td width="20%" align="right"> <a accesskey="n" href="dbset_h_ffactor.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="dbset_h_compare"></a>Db::set_h_compare()</h2> 35 </div> 36 </div> 37 </div> 38 <pre class="programlisting">#include <db_cxx.h> 39 40extern "C" { 41 typedef int (*compare_fcn_type)(DB *db, const DBT *dbt1, const DBT *dbt2); 42}; 43int 44Db::set_h_compare(compare_fcn_type compare_fcn);</pre> 45 <p> 46 Set the Hash key comparison function. The comparison function is 47 called whenever it is necessary to compare a key specified by the 48 application with a key currently stored in the database. 49 </p> 50 <p> 51 If no comparison function is specified, a byte-by-byte comparison is 52 performed. 53 </p> 54 <p> 55 The <code class="methodname">Db::set_h_compare()</code> method configures operations performed using 56 the specified <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> handle, 57 not all operations performed on the underlying database. 58 </p> 59 <p> 60 The <code class="methodname">Db::set_h_compare()</code> method may not be called after the 61 <a class="xref" href="dbopen.html" title="Db::open()">Db::open()</a> method is called. If 62 the database already exists when <a class="xref" href="dbopen.html" title="Db::open()">Db::open()</a> 63 is called, the information specified to <code class="methodname">Db::set_h_compare()</code> 64 must be the same as that historically used to create the database or corruption can occur. 65 </p> 66 <p> 67 The <code class="methodname">Db::set_h_compare()</code> <span> 68 69 <span> 70 method either returns a non-zero error value or throws an 71 exception that encapsulates a non-zero error value on 72 failure, and returns 0 on success. 73 </span> 74 </span> 75 </p> 76 <div class="sect2" lang="en" xml:lang="en"> 77 <div class="titlepage"> 78 <div> 79 <div> 80 <h3 class="title"><a id="id1647428"></a>Parameters</h3> 81 </div> 82 </div> 83 </div> 84 <div class="sect3" lang="en" xml:lang="en"> 85 <div class="titlepage"> 86 <div> 87 <div> 88 <h4 class="title"><a id="id1647013"></a>compare_fcn</h4> 89 </div> 90 </div> 91 </div> 92 <p> 93 The <span class="bold"><strong>compare_fcn</strong></span> function is the 94 application-specified Hash comparison function. The comparison 95 function takes three parameters: 96 </p> 97 <div class="itemizedlist"> 98 <ul type="disc"> 99 <li> 100 <p> 101 <code class="literal">db</code> 102 </p> 103 <p> 104 The <span class="bold"><strong>db</strong></span> parameter is the enclosing 105 database handle. 106 </p> 107 </li> 108 <li> 109 <p> 110 <code class="literal">dbt1</code> 111 </p> 112 <p> 113 The <span class="bold"><strong>dbt1</strong></span> parameter is the <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> 114 representing the application supplied key. 115 </p> 116 </li> 117 <li> 118 <p> 119 <code class="literal">dbt2</code> 120 </p> 121 <p> 122 The <span class="bold"><strong>dbt2</strong></span> parameter is the <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> 123 representing the current database's key. 124 </p> 125 </li> 126 </ul> 127 </div> 128 <p> 129 The <span class="bold"><strong>compare_fcn</strong></span> function must return an integer 130 value less than, equal to, or greater than zero if the first key parameter is 131 considered to be respectively less than, equal to, or greater than the second 132 key parameter. The comparison function must correctly handle any key values used 133 by the application (possibly including zero-length keys). The 134 <span class="bold"><strong>data</strong></span> and <span class="bold"><strong>size</strong></span> 135 fields of the <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> are the only fields 136 that may be used for the purposes of this comparison, and no particular 137 alignment of the memory to which by the <span class="bold"><strong>data</strong></span> 138 field refers may be assumed. 139 </p> 140 </div> 141 </div> 142 <div class="sect2" lang="en" xml:lang="en"> 143 <div class="titlepage"> 144 <div> 145 <div> 146 <h3 class="title"><a id="id1648374"></a>Errors</h3> 147 </div> 148 </div> 149 </div> 150 <p> 151 The <code class="methodname">Db::set_h_compare()</code> <span> 152 153 <span> 154 method may fail and throw a <a class="link" href="dbexception.html" title="Chapter 6. The DbException Class">DbException</a> 155 exception, encapsulating one of the following non-zero errors, or return one 156 of the following non-zero errors: 157 </span> 158 </span> 159 </p> 160 <div class="sect3" lang="en" xml:lang="en"> 161 <div class="titlepage"> 162 <div> 163 <div> 164 <h4 class="title"><a id="id1647131"></a>EINVAL</h4> 165 </div> 166 </div> 167 </div> 168 <p> 169 If the method was called after <a class="xref" href="dbopen.html" title="Db::open()">Db::open()</a> 170 was called; or if an invalid flag value or parameter was specified. 171 </p> 172 </div> 173 </div> 174 <div class="sect2" lang="en" xml:lang="en"> 175 <div class="titlepage"> 176 <div> 177 <div> 178 <h3 class="title"><a id="id1647956"></a>Class</h3> 179 </div> 180 </div> 181 </div> 182 <p> 183 <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> 184 </p> 185 </div> 186 <div class="sect2" lang="en" xml:lang="en"> 187 <div class="titlepage"> 188 <div> 189 <div> 190 <h3 class="title"><a id="id1648306"></a>See Also</h3> 191 </div> 192 </div> 193 </div> 194 <p> 195 <a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a> 196 </p> 197 </div> 198 </div> 199 <div class="navfooter"> 200 <hr /> 201 <table width="100%" summary="Navigation footer"> 202 <tr> 203 <td width="40%" align="left"><a accesskey="p" href="dbset_flags.html">Prev</a> </td> 204 <td width="20%" align="center"> 205 <a accesskey="u" href="db.html">Up</a> 206 </td> 207 <td width="40%" align="right"> <a accesskey="n" href="dbset_h_ffactor.html">Next</a></td> 208 </tr> 209 <tr> 210 <td width="40%" align="left" valign="top">Db::set_flags() </td> 211 <td width="20%" align="center"> 212 <a accesskey="h" href="index.html">Home</a> 213 </td> 214 <td width="40%" align="right" valign="top"> Db::set_h_ffactor()</td> 215 </tr> 216 </table> 217 </div> 218 </body> 219</html> 220