Debian Codegraph @

docs/api_reference/CXX/dbt.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Chapter 4.  The Dbt Handle</title>
7 <link rel="stylesheet" href="apiReference.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB C++ API Reference" />
10 <link rel="up" href="index.html" title="Berkeley DB C++ API Reference" />
11 <link rel="prev" href="dbcset_priority.html" title="Dbc::set_priority()" />
12 <link rel="next" href="dbmultipleiterator.html" title="DbMultipleIterator" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Chapter 4. 
22 The Dbt Handle
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="dbcset_priority.html">Prev</a> </td>
27 <th width="60%" align="center"> </th>
28 <td width="20%" align="right"> <a accesskey="n" href="dbmultipleiterator.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="chapter" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title"><a id="dbt"></a>Chapter 4. 
38 The Dbt Handle
39 </h2>
40 </div>
41 </div>
42 </div>
43 <pre class="programlisting">#include &lt;db_cxx.h&gt;
44
45 class Dbt {
46 public:
47 Dbt(void *data, size_t size);
48 Dbt();
49 Dbt(const Dbt &amp;);
50 Dbt &amp;operator = (const Dbt &amp;);
51 ~Dbt();
52
53 void *get_data() const;
54 void set_data(void *);
55
56 u_int32_t get_size() const;
57 void set_size(u_int32_t);
58
59 u_int32_t get_ulen() const;
60 void set_ulen(u_int32_t);
61
62 u_int32_t get_dlen() const;
63 void set_dlen(u_int32_t);
64
65 u_int32_t get_doff() const;
66 void set_doff(u_int32_t);
67
68 u_int32_t get_flags() const;
69 void set_flags(u_int32_t);
70
71 DBT *Dbt::get_DBT();
72 const DBT *Dbt::get_const_DBT() const;
73 static Dbt *Dbt::get_Dbt(DBT *dbt);
74 static const Dbt *Dbt::get_const_Dbt(const DBT *dbt);
75 }; </pre>
76 <p>
77 The <code class="classname">Dbt</code> class is used to encode key and data
78 items in a Berkeley DB database.
79 </p>
80 <p>
81 Storage and retrieval for the <a class="link" href="db.html" title="Chapter 2.  The Db Handle">Db</a> access methods are
82 based on key/data pairs. Both key and data items are represented by
83
84 <span>
85 <code class="classname">Dbt</code> objects.
86 </span>
87 Key and data byte strings may refer to strings of zero length up to strings of essentially
88 unlimited length. See <a href="../../programmer_reference/am_misc_dbsizes.html" class="olink">Database limits</a> for
89 more information.
90 </p>
91 <p>
92
93 <span>
94 In the case when the <span class="bold"><strong>flags</strong></span> structure element is set to
95 0,
96 </span>
97
98 when the application is providing Berkeley DB a key or data item to store into the database,
99 Berkeley DB expects the <span class="bold"><strong>data</strong></span>
100
101
102 <span>
103 object
104 </span>
105
106 to point to a byte string of <span class="bold"><strong>size</strong></span> bytes. When returning a
107 key/data item to the application, Berkeley DB will store into the <span class="bold"><strong>data</strong></span>
108
109
110 <span>
111 object
112 </span>
113 a pointer to a byte string of <span class="bold"><strong>size</strong></span> bytes,
114 and the memory to which the pointer refers will be
115 allocated and managed by Berkeley DB. Note that using the default flags
116 for returned <code class="classname">Dbt</code>s is only compatible with
117 single threaded usage of Berkeley DB.
118 </p>
119 <p>
120 Access to <code class="classname">Dbt</code> objects is not re-entrant. In particular, if multiple
121 threads simultaneously access the same Dbt object using
122 <a class="link" href="db.html" title="Chapter 2.  The Db Handle">Db</a> API calls, the results are undefined, and may
123 result in a crash. One easy way to avoid problems is to use <code class="classname">Dbt</code>
124 objects that are constructed as stack variables.
125 </p>
126 <p>
127 Each <code class="classname">Dbt</code> object has an associated <code class="literal">DBT</code> struct,
128 which is used by the underlying implementation of Berkeley DB and its C-language API. The
129 <code class="methodname">Dbt::get_DBT()</code> method returns a pointer to this struct. Given a
130 const <code class="classname">Dbt</code> object, <code class="methodname">Dbt::get_const_DBT()</code>
131 returns a const pointer to the same struct.
132 </p>
133 <p>
134 Given a <code class="literal">DBT</code> struct, the <code class="methodname">Dbt::get_Dbt()</code> method
135 returns the corresponding <code class="classname">Dbt</code> object, if there is one. If the
136 <code class="literal">DBT</code> object was not associated with a <code class="classname">Dbt</code> (that is,
137 it was not returned from a call to <code class="methodname">Dbt::get_DBT()</code>), then the
138 result of <code class="methodname">Dbt::get_Dbt()</code> is undefined. Given a const
139 <code class="literal">DBT</code> struct, <code class="methodname">Dbt::get_const_Dbt()</code> returns the
140 associated const <code class="classname">Dbt</code> object, if there is one.
141 </p>
142 <p>
143 These methods may be useful for Berkeley DB applications including both C and C++ language
144 software. It should not be necessary to use these calls in a purely C++ application.
145 </p>
146 <div class="itemizedlist">
147 <ul type="disc">
148 <li>
149 <p>
150 <code class="methodname">Dbt::set_data(void *data)</code>
151 </p>
152 <p>
153 Set the data array.
154 </p>
155 <p>
156 The <code class="literal">data</code> parameter is an array
157 of bytes to be used to set the content for the
158 <code class="classname">Dbt</code>.
159 </p>
160 </li>
161 <li>
162 <p>
163 <code class="methodname">Dbt::get_data()</code>
164 </p>
165 <p>
166 Return the data array.
167 </p>
168 </li>
169 <li>
170 <p>
171 <code class="methodname">Dbt::set_size(u_int32_t size)</code>
172 </p>
173 <p>
174 Sets the byte size of the data array, in bytes.
175 </p>
176 </li>
177 <li>
178 <p>
179 <code class="methodname">Dbt::get_size()</code>
180 </p>
181 <p>
182 Return the data array size.
183 </p>
184 </li>
185 <li>
186 <p>
187 <code class="methodname">Dbt::set_ulen(u_int32_t value)</code>
188 </p>
189 <p>
190 Set the byte size of the user-specified buffer.
191 </p>
192 <p>
193 Note that applications can determine the length of a record by setting
194 the <code class="literal">ulen</code> field to 0 and checking the return value in the
195 <span class="bold"><strong>size</strong></span> field. See
196 the <code class="literal">DB_DBT_USERMEM</code> flag for more information.
197 </p>
198 </li>
199 <li>
200 <p>
201 <code class="methodname">Dbt::get_ulen()</code>
202 </p>
203 <p>
204 Return the length in bytes of the user-specified
205 buffer.
206 </p>
207 <p>
208 Note that applications can determine the length of a record by setting
209 the <code class="literal">ulen</code> field to 0 and checking the return value in the
210 <span class="bold"><strong>size</strong></span> field. See
211 the <code class="literal">DB_DBT_USERMEM</code> flag for more information.
212 </p>
213 </li>
214 <li>
215 <p>
216 <code class="methodname">Dbt::set_dlen(u_int32_t dlen)</code>
217 </p>
218 <p>
219
220 <span>
221 Set the
222 </span>
223 length of the partial record being read or written by the
224 application, in bytes. See the <code class="literal">DB_DBT_PARTIAL</code>
225 flag for more information.
226 </p>
227 </li>
228 <li>
229 <p>
230 <code class="methodname">Dbt::get_dlen()</code>
231 </p>
232 <p>
233 Return the length of the partial record, in bytes.
234 </p>
235 </li>
236 <li>
237 <p>
238 <code class="methodname">Dbt::set_doff(u_int32_t value)</code>
239 </p>
240 <p>
241
242 <span>
243 Sets the
244 </span>
245 offset of the partial record being read or written by the application,
246 in bytes. See the <code class="literal">DB_DBT_PARTIAL</code> flag for more information.
247 </p>
248 </li>
249 <li>
250 <p>
251 <code class="methodname">Dbt::get_doff()</code>
252 </p>
253 <p>
254 Return the offset of the partial record, in bytes.
255 </p>
256 </li>
257 <li>
258 <p>
259 <code class="methodname">Dbt::set_flags(u_int32_t flags)</code>
260 </p>
261 <p>
262 Set the object flag value.
263 </p>
264 <p>
265 The <span class="bold"><strong>flags</strong></span> parameter must be set to 0 or
266 by bitwise inclusively <span class="bold"><strong>OR</strong></span>'ing together
267 one or more of the following values:
268 </p>
269 <span>
270 <div class="itemizedlist"><ul type="circle"><li><p><a id="dbt_DB_DBT_MALLOC"></a>
271 <code class="literal">DB_DBT_MALLOC</code>
272 </p><p>
273 When this flag is set, Berkeley DB will allocate memory
274 for the returned key or data item (using
275 <span class="bold"><strong>malloc</strong></span>(3), or
276 the user-specified malloc function), and return a
277 pointer to it in the
278 <span class="bold"><strong>data</strong></span> field of the key or data
279 <code class="literal">DBT</code>
280 structure. Because any allocated memory becomes the
281 responsibility of the calling application, the caller
282 must determine whether memory was allocated using the
283 returned value of the
284 <span class="bold"><strong>data</strong></span> field.
285 </p><p>
286 It is an error to specify more than one of
287 <code class="literal">DB_DBT_MALLOC</code>,
288 <code class="literal">DB_DBT_REALLOC</code>, and
289 <code class="literal">DB_DBT_USERMEM</code>.
290 </p></li><li><p><a id="dbt_DB_DBT_REALLOC"></a>
291 <code class="literal">DB_DBT_REALLOC</code>
292 </p><p>
293 When this flag is set Berkeley DB will allocate memory
294 for the returned key or data item (using
295 <span class="bold"><strong>realloc</strong></span>(3), or
296 the user-specified realloc function), and return a
297 pointer to it in the
298 <span class="bold"><strong>data</strong></span> field of the key or data DBT
299 structure. Because any allocated memory becomes the
300 responsibility of the calling application, the caller
301 must determine whether memory was allocated using the
302 returned value of the
303 <span class="bold"><strong>data</strong></span> field.
304 </p><p>
305 It is an error to specify more than one of
306 <code class="literal">DB_DBT_MALLOC</code>,
307 <code class="literal">DB_DBT_REALLOC</code>, and
308 <code class="literal">DB_DBT_USERMEM</code>.
309 </p></li><li><p><a id="dbt_DB_DBT_USERMEM"></a>
310 <code class="literal">DB_DBT_USERMEM</code>
311 </p><p>
312 The <span class="emphasis"><em>data</em></span>
313 field of the key or data structure must refer
314 to memory that is at least
315 <span class="emphasis"><em>ulen</em></span>
316 bytes in length. If the
317 length of the requested item is less than or equal to
318 that number of bytes, the item is copied into the memory
319 to which the
320 <span class="emphasis"><em>data</em></span>
321 field refers. Otherwise, the
322 <span class="emphasis"><em>size</em></span>
323 field is set to the length needed for the requested
324 item, and the error
325 <code class="literal">DB_BUFFER_SMALL</code> is returned.
326 </p><p>
327 It is an error to specify more than one of
328 <code class="literal">DB_DBT_MALLOC</code>,
329 <code class="literal">DB_DBT_REALLOC</code>, and
330 <code class="literal">DB_DBT_USERMEM</code>.
331 </p></li></ul></div>
332
333 <span>
334 <p>
335 If <code class="literal">DB_DBT_MALLOC</code> or
336 <code class="literal">DB_DBT_REALLOC</code> is specified, Berkeley DB
337 allocates a properly sized byte array to contain the data. This
338 can be convenient if you know little about the nature of the
339 data, specifically the size of data in the database. However, if
340 your application makes repeated calls to retrieve keys or data,
341 you may notice increased garbage collection due to this
342 allocation. If you know the maximum size of data you are
343 retrieving, you might decrease the memory burden and speed your
344 application by allocating your own byte array and using
345 <code class="literal">DB_DBT_USERMEM</code>. Even if you don't know the
346 maximum size, you can use this option and reallocate your array
347 whenever your retrieval API call returns an
348 <code class="literal">DB_BUFFER_SMALL</code> error or throws an exception
349 encapsulating an <code class="literal">DB_BUFFER_SMALL</code>.
350 </p>
351 </span>
352 <div class="itemizedlist"><ul type="circle"><li><p><a id="dbt_DB_DBT_PARTIAL"></a>
353 <code class="literal">DB_DBT_PARTIAL</code>
354 </p><p>
355 Do partial retrieval or storage of an item. If the
356 calling application is doing a get, the
357 <span class="bold"><strong>dlen</strong></span> bytes
358 starting
359 <span class="bold"><strong>doff</strong></span>
360 bytes from the beginning of the retrieved
361 data record are returned as if they comprised the entire
362 record. If any or all of the specified bytes do not
363 exist in the record, the get is successful, and any
364 existing bytes are returned.
365 </p><p>
366 For example, if the data portion of a retrieved record
367 was 100 bytes, and a partial retrieval was done using a
368 DBT having a
369 <span class="bold"><strong>dlen</strong></span>
370 field of 20 and a
371 <span class="bold"><strong>doff</strong></span>
372 field of 85,
373 the get call would succeed, the
374 <span class="bold"><strong>data</strong></span>
375 field would refer
376 to the last 15 bytes of the record, and the
377 <span class="bold"><strong>size</strong></span>
378 field would be set to 15.
379 </p><p>
380 If the calling application is doing a put, the
381 <span class="bold"><strong>dlen</strong></span>
382 bytes starting
383 <span class="bold"><strong>doff</strong></span>
384 bytes from the beginning of the
385 specified key's data record are replaced by the data
386 specified by the
387 <span class="bold"><strong>data</strong></span>
388 and
389 <span class="bold"><strong>size</strong></span>
390 structure elements. If
391 <span class="bold"><strong>dlen</strong></span>
392 is smaller than
393 <span class="bold"><strong>size</strong></span>
394 the record will grow; if
395 <span class="bold"><strong>dlen</strong></span>
396 is larger than
397 <span class="bold"><strong>size</strong></span>
398 the record will shrink. If the
399 specified bytes do not exist, the record will be
400 extended using nul bytes as necessary, and the put call
401 will succeed.
402 </p><p>
403 It is an error to attempt a partial put using the
404 <a class="xref" href="dbput.html" title="Db::put()">Db::put()</a>
405 method in a database that supports duplicate
406 records. Partial puts in databases supporting duplicate
407 records must be done using a
408 <a class="xref" href="dbcput.html" title="Dbc::put()">Dbc::put()</a>
409 method.
410 </p><p>
411 It is an error to attempt a partial put with differing
412 <span class="bold"><strong>dlen</strong></span>
413 and
414 <span class="bold"><strong>size</strong></span>
415 values in Queue or Recno databases with
416 fixed-length records.
417 </p><p>
418 For example, if the data portion of a retrieved record
419 was 100 bytes, and a partial put was done using a DBT
420 having a
421 <span class="bold"><strong>dlen</strong></span>
422 field of 20, a
423 <span class="bold"><strong>doff</strong></span>
424 field of 85, and a
425 <span class="bold"><strong>size</strong></span>
426 field of 30, the resulting record would be 115
427 bytes in length, where the last 30 bytes would be those
428 specified by the put call.
429 </p><p>
430 This flag is ignored when used with the
431 <code class="literal">pkey</code> parameter on
432 <a class="link" href="dbget.html" title="Db::get()">DB-&gt;pget()</a> or
433 <a class="link" href="dbcget.html" title="Dbc::get()">DBcursor-&gt;pget()</a>.
434 </p></li><li><p><a id="dbt_DB_DBT_APPMALLOC"></a>
435 <code class="literal">DB_DBT_APPMALLOC</code>
436 </p><p>
437 After an application-supplied callback routine passed to
438 either
439 <a class="xref" href="dbassociate.html" title="Db::associate()">Db::associate()</a>
440 or
441 <a class="xref" href="dbset_append_recno.html" title="Db::set_append_recno()">Db::set_append_recno()</a>
442 is executed, the
443 <span class="bold"><strong>data</strong></span>
444 field of a DBT may refer to memory allocated with
445 <span class="bold"><strong>malloc</strong></span>(3)
446 or
447 <span class="bold"><strong>realloc</strong></span>(3).
448 In that case,
449 the callback sets the
450 <code class="literal">DB_DBT_APPMALLOC</code>
451 flag in the DBT
452 so that Berkeley DB will call
453 <span class="bold"><strong>free</strong></span>(3)
454 to deallocate the
455 memory when it is no longer required.
456 </p></li><li><p><a id="dbt_DB_DBT_MULTIPLE"></a>
457 <code class="literal">DB_DBT_MULTIPLE</code>
458 </p><p>
459 Set in a secondary key creation callback routine passed
460 to
461 <a class="xref" href="dbassociate.html" title="Db::associate()">Db::associate()</a>
462 to indicate that multiple secondary
463 keys should be associated with the given primary
464 key/data pair. If set, the
465 <span class="bold"><strong>size</strong></span>
466 field indicates the
467 number of secondary keys and the
468 <span class="bold"><strong>data</strong></span>
469 field refers to an
470 array of that number of DBT structures.
471 </p><p>
472 The <code class="literal">DB_DBT_APPMALLOC</code> flag may be set on any of the DBT
473 structures to indicate that their
474 <span class="bold"><strong>data</strong></span>
475 field needs to be
476 freed.
477 </p></li><li><p><a id="dbt_DB_DBT_READONLY"></a>
478 <code class="literal">DB_DBT_READONLY</code>
479 </p><p>
480 When this flag is set Berkeley DB will not write
481 into the DBT. This may be set on key values in
482 cases where the key is a static string that cannot
483 be written and Berkeley DB might try to update it
484 because the application has set a user defined
485 comparison function.
486 </p></li></ul></div>
487 </span>
488 </li>
489 </ul>
490 </div>
491 <div class="sect1" lang="en" xml:lang="en">
492 <div class="titlepage">
493 <div>
494 <div>
495 <h2 class="title" style="clear: both"><a id="dbtlist"></a>DBT and Bulk Operations</h2>
496 </div>
497 </div>
498 </div>
499 <div class="navtable">
500 <table border="1" width="80%">
501 <thead>
502 <tr>
503 <th>DBT and Bulk Operations</th>
504 <th>Description</th>
505 </tr>
506 </thead>
507 <tbody>
508 <tr>
509 <td>
510 <a class="xref" href="dbsort_multiple.html" title="Db::sort_multiple()">Db::sort_multiple()</a>
511 </td>
512 <td>Sort a set of DBTs</td>
513 </tr>
514 <tr>
515 <td>
516 <a class="xref" href="dbmultipleiterator.html" title="DbMultipleIterator">DbMultipleIterator</a>
517 </td>
518 <td>Base class for bulk get retrieval</td>
519 </tr>
520 <tr>
521 <td>
522 <a class="xref" href="dbmultipledataiterator.html" title="DbMultipleDataIterator">DbMultipleDataIterator</a>
523 </td>
524 <td>Bulk retrieval iterator for data items</td>
525 </tr>
526 <tr>
527 <td>
528 <a class="xref" href="dbmultiplekeydataiterator.html" title="DbMultipleKeyDataIterator">DbMultipleKeyDataIterator</a>
529 </td>
530 <td>Bulk retrieval iterator for key/data pairs</td>
531 </tr>
532 <tr>
533 <td>
534 <a class="xref" href="dbmultiplerecnodataiterator.html" title="DbMultipleRecnoDataIterator">DbMultipleRecnoDataIterator</a>
535 </td>
536 <td>Bulk retrieval iterator for record number / data item pairs</td>
537 </tr>
538 <tr>
539 <td>
540 <a class="xref" href="dbmultiplebuilder.html" title="DbMultipleBuilder">DbMultipleBuilder</a>
541 </td>
542 <td>Base class for bulk buffer building</td>
543 </tr>
544 <tr>
545 <td>
546 <a class="xref" href="dbmultipledatabuilder.html" title="DbMultipleDataBuilder">DbMultipleDataBuilder</a>
547 </td>
548 <td>Bulk buffer builder for data items</td>
549 </tr>
550 <tr>
551 <td>
552 <a class="xref" href="dbmultiplekeydatabuilder.html" title="DbMultipleKeyDataBuilder">DbMultipleKeyDataBuilder</a>
553 </td>
554 <td>Bulk buffer builder for key/data pairs</td>
555 </tr>
556 <tr>
557 <td>
558 <a class="xref" href="dbmultiplerecnodatabuilder.html" title="DbMultipleRecnoDataBuilder">DbMultipleRecnoDataBuilder</a>
559 </td>
560 <td>Bulk buffer builder for record number / data pairs</td>
561 </tr>
562 </tbody>
563 </table>
564 </div>
565 </div>
566 </div>
567 <div class="navfooter">
568 <hr />
569 <table width="100%" summary="Navigation footer">
570 <tr>
571 <td width="40%" align="left"><a accesskey="p" href="dbcset_priority.html">Prev</a> </td>
572 <td width="20%" align="center"> </td>
573 <td width="40%" align="right"> <a accesskey="n" href="dbmultipleiterator.html">Next</a></td>
574 </tr>
575 <tr>
576 <td width="40%" align="left" valign="top">Dbc::set_priority() </td>
577 <td width="20%" align="center">
578 <a accesskey="h" href="index.html">Home</a>
579 </td>
580 <td width="40%" align="right" valign="top"> DbMultipleIterator</td>
581 </tr>
582 </table>
583 </div>
584 </body>
585 </html>