Sophie

Sophie

distrib > Mandriva > 2007.0 > x86_64 > media > main-release > by-pkgid > a4c98df40e78f6c892308fd6841f950a > files > 776

lib64db4.2-devel-4.2.52-11mdv2007.0.x86_64.rpm

<!--$Id: bt_prefix.so,v 10.21 2002/06/24 14:50:23 bostic Exp $-->
<!--Copyright 1997-2003 by Sleepycat Software, Inc.-->
<!--All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Btree prefix comparison</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<table width="100%"><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td>
<td align=right><a href="../am_conf/bt_compare.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am_conf/bt_minkey.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Btree prefix comparison</h3>
<p>The Berkeley DB Btree implementation maximizes the number of keys that can be
stored on an internal page by storing only as many bytes of each key as
are necessary to distinguish it from adjacent keys.  The prefix
comparison routine is what determines this minimum number of bytes (that
is, the length of the unique prefix), that must be stored.  A prefix
comparison function for the Btree can be specified by calling
<a href="../../api_c/db_set_bt_prefix.html">DB-&gt;set_bt_prefix</a>.</p>
<p>The prefix comparison routine must be compatible with the overall
comparison function of the Btree, since what distinguishes any two keys
depends entirely on the function used to compare them.  This means that
if a prefix comparison routine is specified by the application, a
compatible overall comparison routine must also have been specified.</p>
<p>Prefix comparison routines are passed pointers to keys as arguments.  The
keys are represented as <a href="../../api_c/dbt_class.html">DBT</a> structures.  The prefix comparison
function must return the number of bytes of the second key argument that
are necessary to determine if it is greater than the first key argument.
If the keys are equal, the length of the second key should be returned.
The only fields that the routines may examine in the <a href="../../api_c/dbt_class.html">DBT</a>
structures are <b>data</b> and <b>size</b> fields.</p>
<p>An example prefix comparison routine follows:</p>
<blockquote><pre>u_int32_t
compare_prefix(dbp, a, b)
	DB *dbp;
	const DBT *a, *b;
{
	size_t cnt, len;
	u_int8_t *p1, *p2;
<p>
	cnt = 1;
	len = a-&gt;size &gt; b-&gt;size ? b-&gt;size : a-&gt;size;
	for (p1 =
		a-&gt;data, p2 = b-&gt;data; len--; ++p1, ++p2, ++cnt)
			if (*p1 != *p2)
				return (cnt);
	/*
	 * They match up to the smaller of the two sizes.
	 * Collate the longer after the shorter.
	 */
	if (a-&gt;size &lt; b-&gt;size)
		return (a-&gt;size + 1);
	if (b-&gt;size &lt; a-&gt;size)
		return (b-&gt;size + 1);
	return (b-&gt;size);
}</pre></blockquote>
<p>The usefulness of this functionality is data-dependent, but in some data
sets can produce significantly reduced tree sizes and faster search times.</p>
<table width="100%"><tr><td><br></td><td align=right><a href="../am_conf/bt_compare.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am_conf/bt_minkey.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1><a href="../../sleepycat/legal.html">Copyright (c) 1996-2003</a> <a href="http://www.sleepycat.com">Sleepycat Software, Inc.</a> - All rights reserved.</font>
</body>
</html>