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