1<!--$Id: db_key_range.so,v 10.24 2007/10/24 16:06:06 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::key_range</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::key_range</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>
24int
25Db::key_range(DbTxn *txnid
26    Dbt *key, DB_KEY_RANGE *key_range, u_int32_t flags);
27</pre></b>
28<hr size=1 noshade>
29<b>Description: Db::key_range</b>
30<p>The Db::key_range method returns an estimate of the proportion of keys
31that are less than, equal to, and greater than the specified key.  The
32underlying database must be of type Btree.</p>
33<p>The Db::key_range method fills in a structure of type DB_KEY_RANGE.  The
34following data fields are available from the DB_KEY_RANGE structure:</p>
35<br>
36<b>double less;</b><ul compact><li>A value between 0 and 1, the proportion of keys less than the specified
37key.</ul>
38<b>double equal;</b><ul compact><li>A value between 0 and 1, the proportion of keys equal to the specified
39key.</ul>
40<b>double greater;</b><ul compact><li>A value between 0 and 1, the proportion of keys greater than the
41specified key.</ul>
42<br>
43<p>Values are in the range of 0 to 1; for example, if the field
44<b>less</b> is 0.05, 5% of the keys in the database are less than the
45<b>key</b> parameter.  The value for <b>equal</b> will be zero if
46there is no matching key, and will be non-zero otherwise.</p>
47<p>The Db::key_range method
48either returns a non-zero error value
49or throws an exception that encapsulates a non-zero error value on
50failure, and returns 0 on success.
51</p>
52<b>Parameters</b> <br>
53 <b>key</b><ul compact><li>The key <a href="/api_cxx/dbt_class.html">Dbt</a> operated on.</ul>
54 <b>key_range</b><ul compact><li>The estimates are returned in the <b>key_range</b> parameter, which
55contains three elements of type double: <b>less</b>, <b>equal</b>, and
56<b>greater</b>.  Values are in the range of 0 to 1; for example, if the
57field <b>less</b> is 0.05, 5% of the keys in the database are less than
58the <b>key</b> parameter.  The value for <b>equal</b> will be zero if
59there is no matching key, and will be non-zero otherwise.</ul>
60 <b>txnid</b><ul compact><li>If the operation is part of an application-specified transaction, the
61<b>txnid</b> parameter is a transaction handle returned from
62<a href="/api_cxx/txn_begin.html">DbEnv::txn_begin</a>; if the operation is part of a Berkeley DB Concurrent Data Store group, the
63<b>txnid</b> parameter is a handle returned from
64<a href="/api_cxx/env_cdsgroup_begin.html">DbEnv::cdsgroup_begin</a>; otherwise NULL.
65If no transaction handle is
66specified, but the
67operation occurs in a transactional
68database,
69the operation will be implicitly transaction protected.
70The Db::key_range method does not retain the locks it acquires for the
71life of the transaction, so estimates may not be repeatable.</ul>
72 <b>flags</b><ul compact><li>The <b>flags</b> parameter is currently unused, and must be set to 0.</ul>
73<br>
74<br><b>Errors</b>
75<p>The Db::key_range 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>DB_REP_HANDLE_DEAD</b><ul compact><li>The database handle has been invalidated because a replication election
82unrolled a committed transaction.</ul>
83<br>
84<br>
85<b>DB_REP_LOCKOUT</b><ul compact><li>The operation was blocked by client/master synchronization.</ul>
86<br>
87<br>
88<b>EINVAL</b><ul compact><li>If the underlying database was not of type Btree; or if an
89invalid flag value or parameter was specified.</ul>
90<br>
91<p>If a transactional database environment operation was selected to
92resolve a deadlock, the Db::key_range method will fail and
93either return <a href="/ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> or
94throw a <a href="/api_cxx/deadlock_class.html">DbDeadlockException</a> exception.</p>
95<p>If a Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable
96to grant a lock in the allowed time, the Db::key_range method will fail and
97either return <a href="/ref/program/errorret.html#DB_LOCK_NOTGRANTED">DB_LOCK_NOTGRANTED</a> or
98throw a <a href="/api_cxx/lockng_class.html">DbLockNotGrantedException</a> exception.</p>
99<hr size=1 noshade>
100<br><b>Class</b>
101<a href="/api_cxx/db_class.html">Db</a>
102<br><b>See Also</b>
103<a href="/api_cxx/db_list.html">Databases and Related Methods</a>
104</tt>
105<table width="100%"><tr><td><br></td><td align=right>
106<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>
107</td></tr></table>
108<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
109</body>
110</html>
111