libdb/docs/api_reference/C/dbget.html
2012-11-14 16:35:20 -05:00

591 lines
26 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>DB-&gt;get()</title>
<link rel="stylesheet" href="apiReference.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Berkeley DB C API Reference" />
<link rel="up" href="db.html" title="Chapter 2.  The DB Handle" />
<link rel="prev" href="dbfd.html" title="DB-&gt;fd()" />
<link rel="next" href="dbget_bt_minkey.html" title="DB-&gt;get_bt_minkey()" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 11.2.5.3</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">DB-&gt;get()</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="dbfd.html">Prev</a> </td>
<th width="60%" align="center">Chapter 2. 
The DB Handle
</th>
<td width="20%" align="right"> <a accesskey="n" href="dbget_bt_minkey.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dbget"></a>DB-&gt;get()</h2>
</div>
</div>
</div>
<pre class="programlisting">#include &lt;db.h&gt;
int
DB-&gt;get(DB *db,
DB_TXN *txnid, DBT *key, DBT *data, u_int32_t flags);
int
DB-&gt;pget(DB *db,
DB_TXN *txnid, DBT *key, DBT *pkey, DBT *data, u_int32_t flags); </pre>
<p>
The <code class="methodname">DB-&gt;get()</code> method retrieves key/data
pairs from the database. The address and length of the data
associated with the specified <span class="bold"><strong>key</strong></span>
are returned in the structure to which
<span class="bold"><strong>data</strong></span> refers.
</p>
<p>
In the presence of duplicate key values,
<code class="methodname">DB-&gt;get()</code> will return the first data
item for the designated key. Duplicates are sorted by:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
Their sort order, if a duplicate sort function was specified.
</p>
</li>
<li>
<p>
Any explicit cursor designated insertion.
</p>
</li>
<li>
<p>
By insert order. This is the default behavior.
</p>
</li>
</ul>
</div>
<p>
<span class="bold"><strong>Retrieval of duplicates requires the use of cursor operations.</strong></span>
See <a class="xref" href="dbcget.html" title="DBcursor-&gt;get()">DBcursor-&gt;get()</a> for details.
</p>
<p>
When called on a database that has been made into a secondary
index using the
<a class="xref" href="dbassociate.html" title="DB-&gt;associate()">DB-&gt;associate()</a> method, the
<code class="methodname">DB-&gt;get()</code> and
<code class="methodname">DB-&gt;pget()</code> methods return the key from
the secondary index and the data item from the primary database.
In addition, the <code class="methodname">DB-&gt;pget()</code> method
returns the key from the primary database. In databases that are
not secondary indices, the <code class="methodname">DB-&gt;pget()</code>
method will always fail.
</p>
<p>
The <code class="methodname">DB-&gt;get()</code> method will return
<a href="../../programmer_reference/program_errorret.html#program_errorret.DB_NOTFOUND" class="olink">DB_NOTFOUND</a> if
the specified key is not in the database. The
<code class="methodname">DB-&gt;get()</code> method will return
<a href="../../programmer_reference/program_errorret.html#program_errorret.DB_KEYEMPTY" class="olink">DB_KEYEMPTY</a> if
the database is a Queue or Recno database and the specified key
exists, but was never explicitly created by the application or was
later deleted. Unless otherwise specified, the
<code class="methodname">DB-&gt;get()</code> <span>
<span>
method returns a non-zero error value on failure and 0 on success.
</span>
</span>
</p>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="idp57527232"></a>Parameters</h3>
</div>
</div>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57496096"></a>txnid</h4>
</div>
</div>
</div>
<p>
If the operation is part of an application-specified transaction, the
<span class="bold"><strong>txnid</strong></span> parameter is a transaction
handle returned from <a class="xref" href="txnbegin.html" title="DB_ENV-&gt;txn_begin()">DB_ENV-&gt;txn_begin()</a>;
if the operation is part of a Berkeley DB Concurrent Data Store group, the
<span class="bold"><strong>txnid</strong></span> parameter is a handle returned
from <a class="xref" href="envcdsgroup_begin.html" title="DB_ENV-&gt;cdsgroup_begin()">DB_ENV-&gt;cdsgroup_begin()</a>;
otherwise NULL. If no transaction handle is specified, but the
operation occurs in a transactional database, the operation will be
implicitly transaction protected.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57496528"></a>key</h4>
</div>
</div>
</div>
<p>
The key <a class="link" href="dbt.html" title="Chapter 4.  The DBT Handle">DBT</a> operated on.
</p>
<p>
If <a class="link" href="dbt.html#dbt_DB_DBT_PARTIAL">DB_DBT_PARTIAL</a>
is set for the DBT used for this parameter,
and if the <span class="bold"><strong>flags</strong></span>
parameter is not set to
<a class="link" href="dbget.html#dbget_DB_CONSUME">DB_CONSUME</a>
<a class="link" href="dbget.html#dbget_DB_CONSUME_WAIT">DB_CONSUME_WAIT</a>, or
<a class="link" href="dbget.html#dbget_DB_SET_RECNO">DB_SET_RECNO</a>,
then this method will fail and return <code class="literal">EINVAL</code>.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57538288"></a>pkey</h4>
</div>
</div>
</div>
<p>
The <span class="bold"><strong>pkey</strong></span> parameter is the return key
from the primary database. If
<a class="link" href="dbt.html#dbt_DB_DBT_PARTIAL">DB_DBT_PARTIAL</a>
is set for the DBT used for this parameter,
then this method will fail and return <code class="literal">EINVAL</code>.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57512056"></a>data</h4>
</div>
</div>
</div>
<p>
The data <a class="link" href="dbt.html" title="Chapter 4.  The DBT Handle">DBT</a> operated on.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57501880"></a>flags</h4>
</div>
</div>
</div>
<p>
The <span class="bold"><strong>flags</strong></span> parameter must be set to 0
or one of the following values:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><a id="dbget_DB_CONSUME"></a>
<code class="literal">DB_CONSUME</code>
</p>
<p>
Return the record number and data from the available record closest to
the head of the queue, and delete the record. The record number will
be returned in <span class="bold"><strong>key</strong></span>, as described in
<a class="link" href="dbt.html" title="Chapter 4.  The DBT Handle">DBT</a>. The data will be
returned in the <span class="bold"><strong>data</strong></span> parameter. A
record is available if it is not deleted and is not currently locked.
The underlying database must be of type Queue for DB_CONSUME to be
specified.
</p>
</li>
<li>
<p><a id="dbget_DB_CONSUME_WAIT"></a>
<code class="literal">DB_CONSUME_WAIT</code>
</p>
<p>
The DB_CONSUME_WAIT flag is the same as the DB_CONSUME flag, except
that if the Queue database is empty, the thread of control will wait
until there is data in the queue before returning. The underlying
database must be of type Queue for DB_CONSUME_WAIT to be specified.
</p>
<p>
If lock or transaction timeouts have been
specified, the <code class="methodname">DB-&gt;get()</code>
method with the DB_CONSUME_WAIT flag may return
<a href="../../programmer_reference/program_errorret.html#program_errorret.DB_LOCK_NOTGRANTED" class="olink">DB_LOCK_NOTGRANTED</a>.
This failure, by itself, does not require the
enclosing transaction be aborted.
</p>
</li>
<li>
<p><a id="get_DB_GET_BOTH"></a>
<code class="literal">DB_GET_BOTH</code>
</p>
<p>
Retrieve the key/data pair only if both the key and data match the
arguments.
</p>
<p>
When using a secondary index handle, the <code class="literal">DB_GET_BOTH</code>:
flag causes:
</p>
<div class="itemizedlist">
<ul type="circle">
<li>
<p>
the <code class="methodname">DB-&gt;pget()</code> version of
this method to retun the secondary key/primary
key/data tuple only if both the primary and
secondary keys match the arguments.
</p>
</li>
<li>
<p>
the <code class="methodname">DB-&gt;get()</code> version of this method
to result in an error.
</p>
</li>
</ul>
</div>
</li>
<li>
<p><a id="dbget_DB_SET_RECNO"></a>
<code class="literal">DB_SET_RECNO</code>
</p>
<p>
Retrieve the specified numbered key/data pair from a database. Upon
return, both the <span class="bold"><strong>key</strong></span> and <span class="bold"><strong>data</strong></span> items will have been filled in.
</p>
<p>
The <span class="bold"><strong>data</strong></span> field of the specified
<span class="bold"><strong>key</strong></span> must be a pointer to a logical
record number (that is, a <span class="bold"><strong>db_recno_t</strong></span>). This record number determines the
record to be retrieved.
</p>
<p>
For DB_SET_RECNO to be specified, the underlying database must be of
type Btree, and it must have been created with the DB_RECNUM flag.
</p>
</li>
</ul>
</div>
<p>
In addition, the following flags may be set by bitwise inclusively
<span class="bold"><strong>OR</strong></span>'ing them into the <span class="bold"><strong>flags</strong></span> parameter:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><a id="get_DB_IGNORE_LEASE"></a>
<code class="literal">DB_IGNORE_LEASE</code>
</p>
<p>
Return the data item irrespective of the state of master leases. The
item will be returned under all conditions: if master leases are not
configured, if the request is made to a client, if the request is made
to a master with a valid lease, or if the request is made to a master
without a valid lease.
</p>
</li>
<li>
<p><a id="get_DB_MULTIPLE"></a>
<code class="literal">DB_MULTIPLE</code>
</p>
<p>
Return multiple data items in the buffer to which the <span class="bold"><strong>data</strong></span> parameter refers.
</p>
<p>
In the case of Btree or Hash databases, all of the data items
associated with the specified key are entered into the buffer. In the
case of Queue, Recno or Heap databases, all of the data items in the
database, starting at, and subsequent to, the specified key, are
entered into the buffer.
</p>
<p>
The buffer to which the <span class="bold"><strong>data</strong></span>
parameter refers must be provided from user memory (see
<a class="link" href="dbt.html#dbt_DB_DBT_USERMEM">DB_DBT_USERMEM</a>).
The buffer must be at least as large as the page size of the
underlying database, aligned for unsigned integer access, and be a
multiple of 1024 bytes in size. If the buffer size is insufficient,
then upon return from the call the size field of the <span class="bold"><strong>data</strong></span> parameter will have been set to an
estimated buffer size, and the error DB_BUFFER_SMALL is returned.
(The size is an estimate as the exact size needed may not be known
until all entries are read. It is best to initially provide a
relatively large buffer, but applications should be prepared to resize
the buffer as necessary and repeatedly call the method.)
</p>
<p>
The DB_MULTIPLE flag may only be used alone, or with the DB_GET_BOTH
and DB_SET_RECNO options. The DB_MULTIPLE flag may not be used when
accessing databases made into secondary indices using the
<a class="xref" href="dbassociate.html" title="DB-&gt;associate()">DB-&gt;associate()</a> method.
</p>
<p>
See the <a class="xref" href="dbt.html#dbtlist" title="DBT and Bulk Operations">DBT and Bulk Operations</a>
for more information on working with bulk get.
</p>
</li>
<li>
<p><a id="get_DB_READ_COMMITTED"></a>
<code class="literal">DB_READ_COMMITTED</code>
</p>
<p>
Configure a transactional get operation to have degree 2 isolation
(the read is not repeatable).
</p>
</li>
<li>
<p><a id="get_DB_READ_UNCOMMITTED"></a>
<code class="literal">DB_READ_UNCOMMITTED</code>
</p>
<p>
Configure a transactional get operation to have degree 1 isolation,
reading modified but not yet committed data. Silently ignored if the
<a class="link" href="dbopen.html#dbopen_DB_READ_UNCOMMITTED">DB_READ_UNCOMMITTED</a>
flag was not specified when the underlying database was opened.
</p>
</li>
<li>
<p><a id="get_DB_RMW"></a>
<code class="literal">DB_RMW</code>
</p>
<p>
Acquire write locks instead of read locks when doing the read, if
locking is configured. Setting this flag can eliminate deadlock
during a read-modify-write cycle by acquiring the write lock during
the read part of the cycle so that another thread of control acquiring
a read lock for the same item, in its own read-modify-write cycle,
will not result in deadlock.
</p>
<p>
Because the <code class="methodname">DB-&gt;get()</code>
method will not hold locks across Berkeley DB
calls in non-transactional operations, the
<a class="link" href="dbcget.html#dbcget_DB_RMW">DB_RMW</a>
flag to the <code class="methodname">DB-&gt;get()</code> call is
meaningful only in the presence of transactions.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="idp57550240"></a>Errors</h3>
</div>
</div>
</div>
<p>
The <code class="methodname">DB-&gt;get()</code> <span>
<span>
method may fail and return one of the following non-zero errors:
</span>
</span>
</p>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57547752"></a>DB_BUFFER_SMALL</h4>
</div>
</div>
</div>
<p>
The requested item could not be returned due to undersized buffer.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57497080"></a>DB_LOCK_DEADLOCK</h4>
</div>
</div>
</div>
<p>
A transactional database environment operation was selected to resolve
a deadlock.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57553616"></a>DB_LOCK_NOTGRANTED</h4>
</div>
</div>
</div>
<p>
A Berkeley DB Concurrent Data Store database environment configured
for lock timeouts was unable to grant a lock in the allowed time.
</p>
<p>
You attempted to open a database handle that is configured
for no waiting exclusive locking, but the exclusive lock could not be
immediately obtained. See
<a class="xref" href="dbset_lk_exclusive.html" title="DB-&gt;set_lk_exclusive()">DB-&gt;set_lk_exclusive()</a>
for more information.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57568056"></a>DB_LOCK_NOTGRANTED</h4>
</div>
</div>
</div>
<p>
The <code class="literal">DB_CONSUME_WAIT</code> flag was specified, lock or transaction timers
were configured and the lock could not be granted before the wait-time
expired.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57569816"></a> DB_REP_HANDLE_DEAD</h4>
</div>
</div>
</div>
<p>
When a client synchronizes with the master, it is possible for committed
transactions to be rolled back. This invalidates all the database and cursor
handles opened in the replication environment. Once this occurs, an attempt to use
such a handle will
return <code class="literal">DB_REP_HANDLE_DEAD</code>.
The application will need to discard the handle and open a new one in order to
continue processing.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57571640"></a>DB_REP_LEASE_EXPIRED</h4>
</div>
</div>
</div>
<p>
The operation failed because the site's replication master lease has expired.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57542992"></a>DB_REP_LOCKOUT</h4>
</div>
</div>
</div>
<p>
The operation was blocked by client/master synchronization.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57550736"></a>DB_SECONDARY_BAD</h4>
</div>
</div>
</div>
<p>
A secondary index references a nonexistent primary key.
</p>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp57531168"></a>EINVAL</h4>
</div>
</div>
</div>
<p>
If a record number of 0 was specified; the
<a class="link" href="envopen.html#envopen_DB_THREAD">DB_THREAD</a> flag was
specified to the <a class="xref" href="dbopen.html" title="DB-&gt;open()">DB-&gt;open()</a> method and none of the
<a class="link" href="dbt.html#dbt_DB_DBT_MALLOC">DB_DBT_MALLOC</a>,
<a class="link" href="dbt.html#dbt_DB_DBT_REALLOC">DB_DBT_REALLOC</a> or
<a class="link" href="dbt.html#dbt_DB_DBT_USERMEM">DB_DBT_USERMEM</a>
flags were set in the <a class="link" href="dbt.html" title="Chapter 4.  The DBT Handle">DBT</a>;
the <code class="methodname">DB-&gt;pget()</code> method was called with a
<a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a> handle
that does not refer to a secondary index; or if an invalid flag value
or parameter was specified.
</p>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="idp57579680"></a>Class</h3>
</div>
</div>
</div>
<p>
<a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a>
</p>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="idp57534808"></a>See Also</h3>
</div>
</div>
</div>
<p>
<a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a>
</p>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="dbfd.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="db.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="dbget_bt_minkey.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">DB-&gt;fd() </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> DB-&gt;get_bt_minkey()</td>
</tr>
</table>
</div>
</body>
</html>