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