1
<!--$Id: dbc_get.so,v 10.66 2001/07/12 21:13:02 bostic Exp $-->
2
<!--Copyright 1997-2001 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
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,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
11
<a name="2"><!--meow--></a>
12
<table width="100%"><tr valign=top>
17
<a href="../api_cxx/cxx_index.html"><img src="../images/api.gif" alt="API"></a><a href="../reftoc.html"><img src="../images/ref.gif" alt="Ref"></a>
22
#include <db_cxx.h>
25
Dbc::get(Dbt *key, Dbt *data, u_int32_t flags);
27
Dbc::pget(Dbt *key, Dbt *pkey, Dbt *data, u_int32_t flags);
30
<p>The Dbc::get method retrieves key/data pairs from the database. The
31
address and length of the key
32
are returned in the object to which <b>key</b> refers (except for the
33
case of the DB_SET flag, in which the <b>key</b> object is
34
unchanged), and the address
35
and length of the data are returned in the object to which <b>data</b>
37
<p>When called on a cursor opened on a database that has been made into a
38
secondary index using the <a href="../api_cxx/db_associate.html">Db::associate</a> method, the Dbc::get
39
and Dbc::pget methods return the key from the secondary index and the
40
data item from the primary database. In addition, the Dbc::pget method
41
returns the key from the primary database. In databases that are not
42
secondary indices, the Dbc::pget interface will always fail and
44
<p>Modifications to the database during a sequential scan will be reflected
45
in the scan; that is, records inserted behind a cursor will not be
46
returned while records inserted in front of a cursor will be returned.
47
<p>In Queue and Recno databases, missing entries (that is, entries that
48
were never explicitly created or that were created and then deleted)
49
will be skipped during a sequential scan.
50
<p>The <b>flags</b> value must be set to one of the following values:
52
<p><dt><a name="DB_CURRENT">DB_CURRENT</a><dd>Return the key/data pair to which the cursor refers.
53
<p>If the cursor key/data pair was deleted, Dbc::get will return
54
<a href="../ref/program/errorret.html#DB_KEYEMPTY">DB_KEYEMPTY</a>.
55
<p>If the cursor is not yet initialized, the Dbc::get method either returns EINVAL or throws an exception that encapsulates EINVAL.
56
<p><dt><a name="DB_FIRST">DB_FIRST</a>, <a name="DB_LAST">DB_LAST</a><dd>The cursor is set to refer to the first (last) key/data pair of the
57
database, and that pair is returned. In the presence of duplicate key
58
values, the first (last) data item in the set of duplicates is returned.
59
<p>If the database is a Queue or Recno database, Dbc::get using the
60
DB_FIRST (DB_LAST) flags will ignore any keys that exist
61
but were never explicitly created by the application, or were created and
63
<p>If the database is empty, Dbc::get will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a>.
64
<p><dt><a name="DB_GET_BOTH">DB_GET_BOTH</a><dd>identical to the DB_SET flag, except that both the key and the data
65
arguments must be matched by the key and data item in the database.
66
<p>When used with the Dbc::pget version of this interface on a secondary
67
index handle, both the secondary and primary keys must be matched by the
68
secondary and primary key item in the database. It is an error to use the
69
DB_GET_BOTH flag with the Dbc::get version of this interface and
70
a cursor that has been opened on a secondary index handle.
71
<p><dt><a name="DB_GET_RECNO">DB_GET_RECNO</a><dd>Return the record number associated with the cursor. The record number
72
will be returned in <b>data</b>, as described in <a href="../api_cxx/dbt_class.html">Dbt</a>. The
73
<b>key</b> parameter is ignored.
74
<p>For DB_GET_RECNO to be specified, the underlying database must be
75
of type Btree, and it must have been created with the <a href="../api_cxx/db_set_flags.html#DB_RECNUM">DB_RECNUM</a>
77
<p><dt><a name="DB_JOIN_ITEM">DB_JOIN_ITEM</a><dd>Do not use the data value found in all of the cursors as a lookup key for
78
the primary database, but simply return it in the key parameter instead.
79
The data parameter is left unchanged.
80
<p>For DB_JOIN_ITEM to be specified, the underlying cursor must have
81
been returned from the <a href="../api_cxx/db_join.html">Db::join</a> method.
82
<p><dt><a name="DB_NEXT">DB_NEXT</a>, <a name="DB_PREV">DB_PREV</a><dd>If the cursor is not yet initialized, DB_NEXT (DB_PREV)
83
is identical to DB_FIRST (DB_LAST). Otherwise, the cursor
84
is moved to the next (previous) key/data pair of the database, and that
85
pair is returned. In the presence of duplicate key values, the value of
86
the key may not change.
87
<p>If the database is a Queue or Recno database, Dbc::get using the
88
DB_NEXT (DB_PREV) flag will skip any keys that exist
89
but were never explicitly created by the application, or those that were
90
created and later deleted.
91
<p>If the cursor is already on the last (first) record in the database,
92
Dbc::get will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a>.
93
<p><dt><a name="DB_NEXT_DUP">DB_NEXT_DUP</a><dd>If the next key/data pair of the database is a duplicate data record for
94
the current key/data pair, the cursor is moved to the next key/data pair
95
of the database, and that pair is returned. Otherwise, Dbc::get
96
will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a>.
97
<p>If the cursor is not yet initialized, the Dbc::get method either returns EINVAL or throws an exception that encapsulates EINVAL.
98
<p><dt><a name="DB_NEXT_NODUP">DB_NEXT_NODUP</a>, <a name="DB_PREV_NODUP">DB_PREV_NODUP</a><dd>If the cursor is not yet initialized, DB_NEXT_NODUP
99
(DB_PREV_NODUP) is identical to DB_FIRST
100
(DB_LAST). Otherwise, the cursor is moved to the next (previous)
101
non-duplicate key of the database, and that key/data pair is returned.
102
<p>If the database is a Queue or Recno database, Dbc::get using the
103
DB_NEXT_NODUP (DB_PREV_NODUP) flags will ignore any keys
104
that exist but were never explicitly created by the application, or those
105
that were created and later deleted.
106
<p>If no non-duplicate key/data pairs occur after (before) the cursor
107
position in the database, Dbc::get will return <a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a>.
108
<p><dt><a name="DB_SET">DB_SET</a><dd>Move the cursor to the specified key/data pair of the database, and
109
return the datum associated with the given key.
110
<p>In the presence of duplicate key values, Dbc::get will return the
111
first data item for the given key.
112
<p>If the database is a Queue or Recno database, and the specified key exists,
113
but was never explicitly created by the application or was later deleted,
114
Dbc::get will return <a href="../ref/program/errorret.html#DB_KEYEMPTY">DB_KEYEMPTY</a>.
115
<p>If no matching keys are found, Dbc::get will return
116
<a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a>.
117
<p><dt><a name="DB_SET_RANGE">DB_SET_RANGE</a><dd>The DB_SET_RANGE flag is identical to the DB_SET flag,
118
except that the key is returned as well as the data item; and, in the case
119
of the Btree access method, the returned key/data pair is the smallest
120
key greater than or equal to the specified key (as determined by the
121
comparison method), permitting partial key matches and range
123
<p><dt><a name="DB_SET_RECNO">DB_SET_RECNO</a><dd>Move the cursor to the specific numbered record of the database, and
124
return the associated key/data pair. The <b>data</b> field of the
126
must be a pointer to a memory location from which a <a href="../api_cxx/dbt_class.html#db_recno_t">db_recno_t</a>
127
may be read, as described in <a href="../api_cxx/dbt_class.html">Dbt</a>. This memory location will be
128
read to determine the record to be retrieved.
129
<p>For DB_SET_RECNO to be specified, the underlying database must be
130
of type Btree, and it must have been created with the <a href="../api_cxx/db_set_flags.html#DB_RECNUM">DB_RECNUM</a>
133
<p>In addition, the following flags may be set by
134
bitwise inclusively <b>OR</b>'ing them into the <b>flags</b> parameter:
136
<p><dt><a name="DB_DIRTY_READ">DB_DIRTY_READ</a><dd>Read modified but not yet committed data. Silently ignored if the
137
<a href="../api_cxx/db_open.html#DB_DIRTY_READ">DB_DIRTY_READ</a> flag was not specified when the underlying
139
<p><dt><a name="DB_MULTIPLE">DB_MULTIPLE</a><dd>Return multiple data items. The buffer to which the <b>data</b>
140
argument refers is filled with the specified key's data items. If the
141
first data item associated with the key cannot fit into the buffer, the
142
size field of the <b>data</b> argument is set to the length needed to
143
retrieve it, and the error ENOMEM is returned. Subsequent calls with both the
144
DB_NEXT_DUP and DB_MULTIPLE flags specified will return
145
additional data items associated with the current key or
146
<a href="../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a> if there is no additional data items to return.
147
<p>If DB_MULTIPLE is specified for the Queue and Recno access
148
methods, the buffer will be filled with as many data records as
149
possible. The record number of the first record will be returned in
150
the <b>key</b> argument. The record number of each subsequent returned
151
record must be calculated from this value.
152
<p>The buffer to which the <b>data</b> argument refers should be large
153
relative to the page size of the underlying database, aligned for
154
unsigned integer access, and be a multiple of 1024 bytes in size.
155
<p>The DB_MULTIPLE flag may only be used with the
156
DB_CURRENT, DB_FIRST, DB_GET_BOTH,
157
DB_NEXT, DB_NEXT_DUP, DB_NEXT_NODUP,
158
DB_SET, DB_SET_RANGE, and DB_SET_RECNO
160
<p>The DB_MULTIPLE flag may not be used when accessing databases
161
made into secondary indices using the <a href="../api_cxx/db_associate.html">Db::associate</a> method.
162
<p>See <a href="../api_cxx/dbt_bulk.html#DB_MULTIPLE_INIT">DB_MULTIPLE_INIT</a> for more information.
163
<p><dt><a name="DB_MULTIPLE_KEY">DB_MULTIPLE_KEY</a><dd>Return multiple key and data pairs. The buffer to which the
164
<b>data</b> argument refers is filled with key and data pairs. If the
165
first key and data pair cannot fit into the buffer, the size field of
166
the <b>data</b> argument is set to the length needed to retrieve them,
167
and the error ENOMEM is returned.
168
<p>The buffer to which the <b>data</b> argument refers should be large
169
relative to the page size of the underlying database, aligned for
170
unsigned integer access, and be a multiple of 1024 bytes in size.
171
<p>The DB_MULTIPLE_KEY flag may only be used with the
172
DB_CURRENT, DB_FIRST, DB_GET_BOTH,
173
DB_NEXT, DB_NEXT_NODUP, DB_SET,
174
DB_SET_RANGE, and DB_SET_RECNO options. The
175
DB_MULTIPLE_KEY flag may not be used when accessing databases
176
made into secondary indices using the <a href="../api_cxx/db_associate.html">Db::associate</a> method.
177
<p>See <a href="../api_cxx/dbt_bulk.html#DB_MULTIPLE_INIT">DB_MULTIPLE_INIT</a> for more information.
178
<p><dt><a name="DB_RMW">DB_RMW</a><dd>Acquire write locks instead of read locks when doing the retrieval.
179
Setting this flag can eliminate deadlock during a read-modify-write
180
cycle by acquiring the write lock during the read part of the cycle so
181
that another thread of control acquiring a read lock for the same item,
182
in its own read-modify-write cycle, will not result in deadlock.
184
<p>Otherwise, the Dbc::get method either returns a non-zero error value or throws an exception that
185
encapsulates a non-zero error value on failure, and returns 0 on success.
186
<p>If Dbc::get fails for any reason, the state of the cursor will be
189
<p>The Dbc::get method may fail and throw an exception or return a non-zero error for the following conditions:
191
<p><dt>DB_LOCK_DEADLOCK<dd>The operation was selected to resolve a deadlock.
194
<p><dt>DB_SECONDARY_BAD<dd>A secondary index references a nonexistent primary key.
197
<p><dt>ENOMEM<dd>There was insufficient memory to return the requested item.
200
<p><dt>EINVAL<dd>An invalid flag value or parameter was specified.
201
<p>The specified cursor was not currently initialized.
202
<p>The Dbc::pget interface was called with a cursor that does not
203
refer to a secondary index.
205
<p>The Dbc::get method may fail and throw an exception or return a non-zero error for errors specified for other Berkeley DB and C library or system methods.
206
If a catastrophic error has occurred, the Dbc::get method may fail and either
207
return <a href="../ref/program/errorret.html#DB_RUNRECOVERY">DB_RUNRECOVERY</a> or throw an exception encapsulating
208
<a href="../ref/program/errorret.html#DB_RUNRECOVERY">DB_RUNRECOVERY</a>, in which case all subsequent Berkeley DB calls will fail
211
<a href="../api_cxx/dbc_class.html">Dbc</a>
213
<a href="../api_cxx/dbc_close.html">Dbc::close</a>,
214
<a href="../api_cxx/dbc_count.html">Dbc::count</a>,
215
<a href="../api_cxx/dbc_del.html">Dbc::del</a>,
216
<a href="../api_cxx/dbc_dup.html">Dbc::dup</a>,
217
<a href="../api_cxx/dbc_get.html">Dbc::get</a>,
218
<a href="../api_cxx/dbc_get.html">Dbc::pget</a>,
220
<a href="../api_cxx/dbc_put.html">Dbc::put</a>.
222
<table width="100%"><tr><td><br></td><td align=right>
223
<a href="../api_cxx/cxx_index.html"><img src="../images/api.gif" alt="API"></a><a href="../reftoc.html"><img src="../images/ref.gif" alt="Ref"></a>
225
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>