1<!--$Id: dbc_get.so,v 10.121 2007/10/24 16:06:07 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: Dbc::get</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>Dbc::get</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
25Dbc::get(Dbt *key, Dbt *data, u_int32_t flags);
26<p>
27int
28Dbc::pget(Dbt *key, Dbt *pkey, Dbt *data, u_int32_t flags);
29</pre></b>
30<hr size=1 noshade>
31<b>Description: Dbc::get</b>
32<p>The Dbc::get method retrieves key/data pairs from the database.  The
33address and length of the key are returned in the object to which
34<b>key</b> refers (except for the case of the DB_SET flag, in
35which the <b>key</b> object is unchanged), and the address and length
36of the data are returned in the object to which <b>data</b> refers.</p>
37<p>When called on a cursor opened on a database that has been made into a
38secondary index using the <a href="../api_cxx/db_associate.html">Db::associate</a> method, the Dbc::get
39and Dbc::pget methods return the key from the secondary index and the
40data item from the primary database. In addition, the Dbc::pget method
41returns the key from the primary database.  In databases that are not
42secondary indices, the Dbc::pget method will always fail.</p>
43<p>Modifications to the database during a sequential scan will be reflected
44in the scan; that is, records inserted behind a cursor will not be
45returned while records inserted in front of a cursor will be returned.</p>
46<p>In Queue and Recno databases, missing entries (that is, entries that
47were never explicitly created or that were created and then deleted)
48will be skipped during a sequential scan.</p>
49<p>Unless otherwise specified, the Dbc::get method
50either returns a non-zero error value
51or throws an exception that encapsulates a non-zero error value on
52failure, and returns 0 on success.
53</p>
54<p>If Dbc::get fails for any reason, the state of the cursor will be
55unchanged.</p>
56<b>Parameters</b> <br>
57 <b>data</b><ul compact><li>The data <a href="../api_cxx/dbt_class.html">Dbt</a> operated on.</ul>
58 <b>flags</b><ul compact><li>The <b>flags</b> parameter must be set to one of the following values:
59<br>
60<b><a name="DB_CURRENT">DB_CURRENT</a></b><ul compact><li>Return the key/data pair to which the cursor refers.
61<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_KEYEMPTY">DB_KEYEMPTY</a> if DB_CURRENT is set and the cursor key/data pair was deleted.
62</p></ul>
63<b><a name="DB_FIRST">DB_FIRST</a></b><ul compact><li>The cursor is set to refer to the first key/data pair of the database,
64and that pair is returned.  If the first key has duplicate values, the
65first data item in the set of duplicates is returned.
66<p>If the database is a Queue or Recno database, Dbc::get using the
67DB_FIRST flag will ignore any keys that exist but were never
68explicitly created by the application, or were created and later
69deleted.</p>
70<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_FIRST is set and the database is empty.
71</p></ul>
72<b><a name="DB_GET_BOTH">DB_GET_BOTH</a></b><ul compact><li>The DB_GET_BOTH flag is identical to the DB_SET flag,
73except that both the key and the data parameters must be matched by the
74key and data item in the database.
75<p>When used with the Dbc::pget method version of this method on a
76secondary index handle, both the secondary and primary keys must be
77matched by the secondary and primary key item in the database.  It is
78an error to use the DB_GET_BOTH flag with the Dbc::get
79version of this method and a cursor that has been opened on a secondary
80index handle.</p></ul>
81<b><a name="DB_GET_BOTH_RANGE">DB_GET_BOTH_RANGE</a></b><ul compact><li>The DB_GET_BOTH_RANGE flag is identical to the DB_GET_BOTH
82flag, except that, in the case of any database supporting sorted
83duplicate sets, the returned key/data pair is the smallest data item
84greater than or equal to the specified data item (as determined by the
85comparison function), permitting partial matches and range searches in
86duplicate data sets.</ul>
87<b><a name="DB_GET_RECNO">DB_GET_RECNO</a></b><ul compact><li>Return the record number associated with the cursor.  The record number
88will be returned in <b>data</b>, as described in <a href="../api_cxx/dbt_class.html">Dbt</a>.  The
89<b>key</b> parameter is ignored.
90<p>For DB_GET_RECNO to be specified, the underlying database must be
91of type Btree, and it must have been created with the <a href="../api_cxx/db_set_flags.html#DB_RECNUM">DB_RECNUM</a>
92flag.</p>
93<p>When called on a cursor opened on a database that has been made into a
94secondary index, the Dbc::get and Dbc::pget methods return the
95record number of the primary database in <b>data</b>.  In addition, the
96Dbc::pget method returns the record number of the secondary index in
97<b>pkey</b>.  If either underlying database is not of type Btree or is
98not created with the <a href="../api_cxx/db_set_flags.html#DB_RECNUM">DB_RECNUM</a> flag, the out-of-band record
99number of 0 is returned.</p></ul>
100<b><a name="DB_JOIN_ITEM">DB_JOIN_ITEM</a></b><ul compact><li>Do not use the data value found in all of the cursors as a lookup key for
101the primary database, but simply return it in the key parameter instead.
102The data parameter is left unchanged.
103<p>For DB_JOIN_ITEM to be specified, the underlying cursor must have
104been returned from the <a href="../api_cxx/db_join.html">Db::join</a> method.</p></ul>
105<b><a name="DB_LAST">DB_LAST</a></b><ul compact><li>The cursor is set to refer to the last key/data pair of the database,
106and that pair is returned.  If the last key has duplicate values, the
107last data item in the set of duplicates is returned.
108<p>If the database is a Queue or Recno database, Dbc::get using the
109DB_LAST flag will ignore any keys that exist but were never
110explicitly created by the application, or were created and later
111deleted.</p>
112<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_LAST is set and the database is empty.
113</p></ul>
114<b><a name="DB_NEXT">DB_NEXT</a></b><ul compact><li>If the cursor is not yet initialized, DB_NEXT is identical to
115DB_FIRST.  Otherwise, the cursor is moved to the next key/data
116pair of the database, and that pair is returned.  In the presence of
117duplicate key values, the value of the key may not change.
118<p>If the database is a Queue or Recno database, Dbc::get using the
119DB_NEXT flag will skip any keys that exist but were never
120explicitly created by the application, or those that were created and
121later deleted.</p>
122<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_NEXT is set and the cursor is already on the last record
123in the database.
124</p></ul>
125<b><a name="DB_NEXT_DUP">DB_NEXT_DUP</a></b><ul compact><li>If the next key/data pair of the database is a duplicate data record for
126the current key/data pair, the cursor is moved to the next key/data pair
127of the database, and that pair is returned.
128<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_NEXT_DUP is set and the next key/data pair of the
129database is not a duplicate data record for the current key/data pair.
130</p></ul>
131<b><a name="DB_NEXT_NODUP">DB_NEXT_NODUP</a></b><ul compact><li>If the cursor is not yet initialized, DB_NEXT_NODUP is identical
132to DB_FIRST.  Otherwise, the cursor is moved to the next
133non-duplicate key of the database, and that key/data pair is returned.
134<p>If the database is a Queue or Recno database, Dbc::get using the
135DB_NEXT_NODUP flag will ignore any keys that exist but were
136never explicitly created by the application, or those that were created
137and later deleted.</p>
138<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_NEXT_NODUP is set and no non-duplicate key/data pairs
139occur after the cursor position in the database.
140</p></ul>
141<b><a name="DB_PREV">DB_PREV</a></b><ul compact><li>If the cursor is not yet initialized, DB_PREV is identical to
142DB_LAST.  Otherwise, the cursor is moved to the previous
143key/data pair of the database, and that pair is returned.  In the
144presence of duplicate key values, the value of the key may not change.
145<p>If the database is a Queue or Recno database, Dbc::get using the
146DB_PREV flag will skip any keys that exist but were never
147explicitly created by the application, or those that were created and
148later deleted.</p>
149<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_PREV is set and the cursor is already on the first record
150in the database.
151</p></ul>
152<b><a name="DB_PREV_DUP">DB_PREV_DUP</a></b><ul compact><li>If the previous key/data pair of the database is a duplicate data record
153for the current key/data pair, the cursor is moved to the previous key/data
154pair of the database, and that pair is returned.
155<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_PREV_DUP is set and the previous key/data pair of the
156database is not a duplicate data record for the current key/data pair.
157</p></ul>
158<b><a name="DB_PREV_NODUP">DB_PREV_NODUP</a></b><ul compact><li>If the cursor is not yet initialized, DB_PREV_NODUP is identical
159to DB_LAST.  Otherwise, the cursor is moved to the previous
160non-duplicate key of the database, and that key/data pair is returned.
161<p>If the database is a Queue or Recno database, Dbc::get using the
162DB_PREV_NODUP flag will ignore any keys that exist but were
163never explicitly created by the application, or those that were created
164and later deleted.</p>
165<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_PREV_NODUP is set and no non-duplicate key/data pairs
166occur before the cursor position in the database.
167</p></ul>
168<b><a name="DB_SET">DB_SET</a></b><ul compact><li>Move the cursor to the specified key/data pair of the database, and
169return the datum associated with the given key.
170<p>The Dbc::get method will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if DB_SET is set and
171no matching keys are found.
172The Dbc::get method will return <a href="../ref/program/errorret.html#DB_KEYEMPTY">DB_KEYEMPTY</a> if DB_SET is set and the database is a
173Queue or Recno database, and the specified key exists, but was never
174explicitly created by the application or was later deleted.
175In the presence of duplicate key values, Dbc::get will return the
176first data item for the given key.
177</p></ul>
178<b><a name="DB_SET_RANGE">DB_SET_RANGE</a></b><ul compact><li>The DB_SET_RANGE flag is identical to the DB_SET flag,
179except that in the case of the Btree access method, the key is returned
180as well as the data item and the returned key/data pair is the smallest
181key greater than or equal to the specified key (as determined by the
182Btree comparison function), permitting partial key matches and range
183searches.</ul>
184<b><a name="DB_SET_RECNO">DB_SET_RECNO</a></b><ul compact><li>Move the cursor to the specific numbered record of the database, and
185return the associated key/data pair.  The <b>data</b> field of the
186specified <b>key</b> must be a pointer to a memory location from which
187a <a href="../api_cxx/dbt_class.html#db_recno_t">db_recno_t</a> may be read, as described in <a href="../api_cxx/dbt_class.html">Dbt</a>.  This
188memory location will be read to determine the record to be retrieved.
189<p>For DB_SET_RECNO to be specified, the underlying database must be
190of type Btree, and it must have been created with the <a href="../api_cxx/db_set_flags.html#DB_RECNUM">DB_RECNUM</a>
191flag.</p></ul>
192<br>
193In addition, the following flags may be set by
194bitwise inclusively <b>OR</b>'ing them into the <b>flags</b> parameter:
195<br>
196<b><a name="DB_IGNORE_LEASE">DB_IGNORE_LEASE</a></b><ul compact><li>Return the data item irrespective of the state of master leases.  The
197item will be returned under all conditions: if master leases are not
198configured, if the request is made to a client, if the request is made
199to a master with a valid lease, or if the request is made to a master
200without a valid lease.</ul>
201<b><a name="DB_READ_UNCOMMITTED">DB_READ_UNCOMMITTED</a></b><ul compact><li>Database items read during a transactional call will have degree 1
202isolation, including modified but not yet committed data.  Silently
203ignored if the <a href="../api_cxx/db_open.html#DB_READ_UNCOMMITTED">DB_READ_UNCOMMITTED</a> flag was not specified when
204the underlying database was opened.</ul>
205<b><a name="DB_MULTIPLE">DB_MULTIPLE</a></b><ul compact><li>Return multiple data items in the <b>data</b> parameter.
206<p>In the case of Btree or Hash databases, duplicate data items for the
207current key, starting at the current cursor position, are entered into
208the buffer.  Subsequent calls with both the DB_NEXT_DUP and
209DB_MULTIPLE flags specified will return additional duplicate
210data items associated with the current key or <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if
211there are no additional duplicate data items to return.  Subsequent
212calls with both the DB_NEXT and DB_MULTIPLE flags
213specified will return additional duplicate data items associated with
214the current key or if there are no additional duplicate data items will
215return the next key and its data items or <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if there
216are no additional keys in the database.</p>
217<p>In the case of Queue or Recno databases, data items starting at the
218current cursor position are entered into the buffer.  The record number
219of the first record will be returned in the <b>key</b> parameter.  The
220record number of each subsequent returned record must be calculated from
221this value.  Subsequent calls with the DB_MULTIPLE flag
222specified will return additional data items or <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if
223there are no additional data items to return.</p>
224<p>The buffer to which the <b>data</b> parameter refers must be provided
225from user memory (see <a href="../api_cxx/dbt_class.html#DB_DBT_USERMEM">DB_DBT_USERMEM</a>).  The buffer must be at
226least as large as the page size of the underlying database, aligned for
227unsigned integer access, and be a multiple of 1024 bytes in size.  If
228the buffer size is insufficient, then upon return from the call the size
229field of the <b>data</b> parameter will have been set to an estimated
230buffer size, and the error DB_BUFFER_SMALL is returned.  (The size is an estimate as the
231exact size needed may not be known until all entries are read.  It is
232best to initially provide a relatively large buffer, but applications
233should be prepared to resize the buffer as necessary and repeatedly call
234the method.)</p>
235<p>The multiple data items can be iterated over using the
236<a href="../api_cxx/dbt_bulk_class.html">DbMultipleDataIterator</a> class.</p>
237<p>The DB_MULTIPLE flag may only be used with the
238DB_CURRENT, DB_FIRST, DB_GET_BOTH,
239DB_GET_BOTH_RANGE, DB_NEXT, DB_NEXT_DUP,
240DB_NEXT_NODUP, DB_SET, DB_SET_RANGE, and
241DB_SET_RECNO options.  The DB_MULTIPLE flag may not be
242used when accessing databases made into secondary indices using the
243<a href="../api_cxx/db_associate.html">Db::associate</a> method.</p></ul>
244<b><a name="DB_MULTIPLE_KEY">DB_MULTIPLE_KEY</a></b><ul compact><li>Return multiple key and data pairs in the <b>data</b> parameter.
245<p>Key and data pairs, starting at the current cursor position, are entered
246into the buffer.  Subsequent calls with both the DB_NEXT and
247DB_MULTIPLE_KEY flags specified will return additional key and data
248pairs or <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if there are no additional key and data
249items to return.</p>
250<p>In the case of Btree or Hash databases,
251the multiple key and data pairs can be iterated over using the
252<a href="../api_cxx/dbt_bulk_class.html">DbMultipleKeyDataIterator</a> class.</p>
253<p>In the case of Queue or Recno databases,
254the multiple record number and data pairs can be iterated over using the
255<a href="../api_cxx/dbt_bulk_class.html">DbMultipleRecnoDataIterator</a> class.</p>
256<p>The buffer to which the <b>data</b> parameter refers must be provided
257from user memory (see <a href="../api_cxx/dbt_class.html#DB_DBT_USERMEM">DB_DBT_USERMEM</a>).  The buffer must be at
258least as large as the page size of the underlying database, aligned for
259unsigned integer access, and be a multiple of 1024 bytes in size.  If
260the buffer size is insufficient, then upon return from the call the size
261field of the <b>data</b> parameter will have been set to an estimated
262buffer size, and the error DB_BUFFER_SMALL is returned.  (The size is an estimate as the
263exact size needed may not be known until all entries are read.  It is
264best to initially provide a relatively large buffer, but applications
265should be prepared to resize the buffer as necessary and repeatedly call
266the method.)</p>
267<p>The DB_MULTIPLE_KEY flag may only be used with the
268DB_CURRENT, DB_FIRST, DB_GET_BOTH,
269DB_GET_BOTH_RANGE, DB_NEXT, DB_NEXT_DUP,
270DB_NEXT_NODUP, DB_SET, DB_SET_RANGE, and
271DB_SET_RECNO options.  The DB_MULTIPLE_KEY flag may not
272be used when accessing databases made into secondary indices using the
273<a href="../api_cxx/db_associate.html">Db::associate</a> method.</p></ul>
274<b><a name="DB_RMW">DB_RMW</a></b><ul compact><li>Acquire write locks instead of read locks when doing the read, if
275locking is configured.  Setting this flag can eliminate deadlock during
276a read-modify-write cycle by acquiring the write lock during the read
277part of the cycle so that another thread of control acquiring a read
278lock for the same item, in its own read-modify-write cycle, will not
279result in deadlock.
280</ul>
281<br></ul>
282 <b>key</b><ul compact><li>The key <a href="../api_cxx/dbt_class.html">Dbt</a> operated on.</ul>
283 <b>pkey</b><ul compact><li>The secondary index key <a href="../api_cxx/dbt_class.html">Dbt</a> operated on.</ul>
284<br>
285<br><b>Errors</b>
286<p>The Dbc::get method
287may fail and throw
288<a href="../api_cxx/except_class.html">DbException</a>,
289encapsulating one of the following non-zero errors, or return one of
290the following non-zero errors:</p>
291<br>
292<b>DB_REP_HANDLE_DEAD</b><ul compact><li>The database handle has been invalidated because a replication election
293unrolled a committed transaction.</ul>
294<br>
295<br>
296<b>DB_REP_LEASE_EXPIRED</b><ul compact><li>The operation failed because the site's replication master lease has expired.</ul>
297<br>
298<br>
299<b>DB_REP_LOCKOUT</b><ul compact><li>The operation was blocked by client/master synchronization.</ul>
300<br>
301<br>
302<b>DB_SECONDARY_BAD</b><ul compact><li>A secondary index references a nonexistent primary key.</ul>
303<br>
304<br>
305<b>EINVAL</b><ul compact><li>If the DB_CURRENT, DB_NEXT_DUP or DB_PREV_DUP flags
306were specified and the cursor has not been initialized;
307the Dbc::pget method was called with a cursor that does not refer to a
308secondary index; or if an
309invalid flag value or parameter was specified.</ul>
310<br>
311<p>If the requested item could not be returned due to undersized buffer, the Dbc::get method will fail and
312either return DB_BUFFER_SMALL or
313throw a <a href="../api_cxx/memp_class.html">DbMemoryException</a> exception.</p>
314<p>If a transactional database environment operation was selected to
315resolve a deadlock, the Dbc::get method will fail and
316either return <a href="../ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> or
317throw a <a href="../api_cxx/deadlock_class.html">DbDeadlockException</a> exception.</p>
318<p>If a Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable
319to grant a lock in the allowed time, the Dbc::get method will fail and
320either return <a href="../ref/program/errorret.html#DB_LOCK_NOTGRANTED">DB_LOCK_NOTGRANTED</a> or
321throw a <a href="../api_cxx/lockng_class.html">DbLockNotGrantedException</a> exception.</p>
322<hr size=1 noshade>
323<br><b>Class</b>
324<a href="../api_cxx/dbc_class.html">Dbc</a>
325<br><b>See Also</b>
326<a href="../api_cxx/dbc_list.html">Database Cursors and Related Methods</a>
327</tt>
328<table width="100%"><tr><td><br></td><td align=right>
329<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>
330</td></tr></table>
331<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
332</body>
333</html>
334