2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Btree prefix comparison</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td>
14
<td align=right><a href="../../ref/am_conf/bt_compare.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_conf/bt_minkey.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Btree prefix comparison</h1>
18
<p>The Berkeley DB Btree implementation maximizes the number of keys that can be
19
stored on an internal page by storing only as many bytes of each key as
20
are necessary to distinguish it from adjacent keys. The prefix
21
comparison routine is what determines this minimum number of bytes (that
22
is, the length of the unique prefix), that must be stored. A prefix
23
comparison function for the Btree can be specified by calling
24
<a href="../../api_c/db_set_bt_prefix.html">DB->set_bt_prefix</a>.
25
<p>The prefix comparison routine must be compatible with the overall
26
comparison function of the Btree, since what distinguishes any two keys
27
depends entirely on the function used to compare them. This means that
28
if a prefix comparison routine is specified by the application, a
29
compatible overall comparison routine must also have been specified.
30
<p>Prefix comparison routines are passed pointers to keys as arguments. The
31
keys are represented as <a href="../../api_c/dbt_class.html">DBT</a> structures. The prefix comparison
32
function must return the number of bytes of the second key argument that
33
are necessary to determine if it is greater than the first key argument.
34
If the keys are equal, the length of the second key should be returned.
35
The only fields that the routines may examine in the <a href="../../api_c/dbt_class.html">DBT</a>
36
structures are <b>data</b> and <b>size</b> fields.
37
<p>An example prefix comparison routine follows:
38
<p><blockquote><pre>u_int32_t
39
compare_prefix(dbp, a, b)
47
len = a->size > b->size ? b->size : a->size;
49
a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
53
* They match up to the smaller of the two sizes.
54
* Collate the longer after the shorter.
56
if (a->size < b->size)
57
return (a->size + 1);
58
if (b->size < a->size)
59
return (b->size + 1);
62
<p>The usefulness of this functionality is data-dependent, but in some data
63
sets can produce significantly reduced tree sizes and faster search times.
64
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/am_conf/bt_compare.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_conf/bt_minkey.html"><img src="../../images/next.gif" alt="Next"></a>
66
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>