Debian Codegraph @

docs/api_reference/C/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="DBcursor-&gt;set_priority()" />
12 <link rel="next" href="DB_MULTIPLE_INIT.html" title="DB_MULTIPLE_INIT" />
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="DB_MULTIPLE_INIT.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.h&gt;
44
45 typedef struct {
46 void *data;
47 u_int32_t size;
48 u_int32_t ulen;
49 u_int32_t dlen;
50 u_int32_t doff;
51 u_int32_t flags;
52 } DBT;
53
54 </pre>
55 <p>
56 Storage and retrieval for the <a class="link" href="db.html" title="Chapter 2.  The DB Handle">DB</a> access methods are
57 based on key/data pairs. Both key and data items are represented by
58 <span>
59 the DBT data structure. (The name DBT is a mnemonic for data base thang, and was used
60 because no one could think of a reasonable name that wasn't already in use somewhere
61 else.)
62 </span>
63
64 Key and data byte strings may refer to strings of zero length up to strings of essentially
65 unlimited length. See <a href="../../programmer_reference/am_misc_dbsizes.html" class="olink">Database limits</a> for
66 more information.
67 </p>
68 <p>
69 All fields of the DBT structure that are not explicitly set should
70 be initialized to nul bytes before the first time the structure is
71 used. Do this by declaring the structure external or static, or by
72 calling the C library routine
73 <span class="bold"><strong>memset</strong></span>(3).
74
75 </p>
76 <p>
77 <span>
78 By default, the <span class="bold"><strong>flags</strong></span> structure element is expected to
79 be set to 0. In this default case,
80 </span>
81
82
83 when the application is providing Berkeley DB a key or data item to store into the database,
84 Berkeley DB expects the <span class="bold"><strong>data</strong></span>
85
86 <span>
87 structure element
88 </span>
89
90
91 to point to a byte string of <span class="bold"><strong>size</strong></span> bytes. When returning a
92 key/data item to the application, Berkeley DB will store into the <span class="bold"><strong>data</strong></span>
93
94 <span>
95 structure element
96 </span>
97
98 a pointer to a byte string of <span class="bold"><strong>size</strong></span> bytes,
99 and the memory to which the pointer refers will be
100 allocated and managed by Berkeley DB. Note that using the default flags
101 for returned <code class="classname">DBT</code>s is only compatible with
102 single threaded usage of Berkeley DB.
103 </p>
104 <p>
105 The elements of the DBT structure are defined as follows:
106 </p>
107 <div class="itemizedlist">
108 <ul type="disc">
109 <li>
110 <p>
111 <span class="bold"><strong>void *data;</strong></span>
112 </p>
113 <p>
114 A pointer to a byte string.
115 </p>
116 </li>
117 <li>
118 <p>
119 <span class="bold"><strong>u_int32_t size;</strong></span>
120 </p>
121 <p>
122 The length of <span class="bold"><strong>data</strong></span>, in bytes.
123 </p>
124 </li>
125 <li>
126 <p>
127 <span class="bold"><strong>u_int32_t ulen;</strong></span>
128 </p>
129 <p>
130 The size of the user's buffer (to which <span class="bold"><strong>data</strong></span> refers), in bytes. This
131 location is not written by the Berkeley DB functions.
132 </p>
133 <p>
134 Set the byte size of the user-specified buffer.
135 </p>
136 <p>
137 Note that applications can determine the length of a record by setting
138 the <code class="literal">ulen</code> field to 0 and checking the return value in the
139 <span class="bold"><strong>size</strong></span> field. See
140 the <code class="literal">DB_DBT_USERMEM</code> flag for more information.
141 </p>
142 </li>
143 <li>
144 <p>
145 <span class="bold"><strong>u_int32_t dlen;</strong></span>
146 </p>
147 <p>
148 <span>
149 The
150 </span>
151
152 length of the partial record being read or written by the
153 application, in bytes. See the <code class="literal">DB_DBT_PARTIAL</code>
154 flag for more information.
155 </p>
156 </li>
157 <li>
158 <p>
159 <span class="bold"><strong>u_int32_t doff;</strong></span>
160 </p>
161 <p>
162 <span>
163 The
164 </span>
165
166 offset of the partial record being read or written by the application,
167 in bytes. See the <code class="literal">DB_DBT_PARTIAL</code> flag for more information.
168 </p>
169 </li>
170 <li>
171 <p>
172 <span class="bold"><strong>u_int32_t flags;</strong></span>
173 </p>
174 <p>
175 The <span class="bold"><strong>flags</strong></span> parameter must be set to 0 or
176 by bitwise inclusively <span class="bold"><strong>OR</strong></span>'ing together
177 one or more of the following values:
178 </p>
179 <span>
180 <div class="itemizedlist"><ul type="circle"><li><p><a id="dbt_DB_DBT_MALLOC"></a>
181 <code class="literal">DB_DBT_MALLOC</code>
182 </p><p>
183 When this flag is set, Berkeley DB will allocate memory
184 for the returned key or data item (using
185 <span class="bold"><strong>malloc</strong></span>(3), or
186 the user-specified malloc function), and return a
187 pointer to it in the
188 <span class="bold"><strong>data</strong></span> field of the key or data
189 <code class="literal">DBT</code>
190 structure. Because any allocated memory becomes the
191 responsibility of the calling application, the caller
192 must determine whether memory was allocated using the
193 returned value of the
194 <span class="bold"><strong>data</strong></span> field.
195 </p><p>
196 It is an error to specify more than one of
197 <code class="literal">DB_DBT_MALLOC</code>,
198 <code class="literal">DB_DBT_REALLOC</code>, and
199 <code class="literal">DB_DBT_USERMEM</code>.
200 </p></li><li><p><a id="dbt_DB_DBT_REALLOC"></a>
201 <code class="literal">DB_DBT_REALLOC</code>
202 </p><p>
203 When this flag is set Berkeley DB will allocate memory
204 for the returned key or data item (using
205 <span class="bold"><strong>realloc</strong></span>(3), or
206 the user-specified realloc function), and return a
207 pointer to it in the
208 <span class="bold"><strong>data</strong></span> field of the key or data DBT
209 structure. Because any allocated memory becomes the
210 responsibility of the calling application, the caller
211 must determine whether memory was allocated using the
212 returned value of the
213 <span class="bold"><strong>data</strong></span> field.
214 </p><p>
215 The difference between
216 <code class="literal">DB_DBT_MALLOC</code> and
217 <code class="literal">DB_DBT_REALLOC</code>
218 is that the latter will call
219 <span class="bold"><strong>realloc</strong></span>(3) instead of
220 <span class="bold"><strong>malloc</strong></span>(3),
221 so the allocated memory will be grown as
222 necessary instead of the application doing repeated
223 free/malloc calls.
224 </p><p>
225 It is an error to specify more than one of
226 <code class="literal">DB_DBT_MALLOC</code>,
227 <code class="literal">DB_DBT_REALLOC</code>, and
228 <code class="literal">DB_DBT_USERMEM</code>.
229 </p></li><li><p><a id="dbt_DB_DBT_USERMEM"></a>
230 <code class="literal">DB_DBT_USERMEM</code>
231 </p><p>
232 The <span class="emphasis"><em>data</em></span>
233 field of the key or data structure must refer
234 to memory that is at least
235 <span class="emphasis"><em>ulen</em></span>
236 bytes in length. If the
237 length of the requested item is less than or equal to
238 that number of bytes, the item is copied into the memory
239 to which the
240 <span class="emphasis"><em>data</em></span>
241 field refers. Otherwise, the
242 <span class="emphasis"><em>size</em></span>
243 field is set to the length needed for the requested
244 item, and the error
245 <code class="literal">DB_BUFFER_SMALL</code> is returned.
246 </p><p>
247 It is an error to specify more than one of
248 <code class="literal">DB_DBT_MALLOC</code>,
249 <code class="literal">DB_DBT_REALLOC</code>, and
250 <code class="literal">DB_DBT_USERMEM</code>.
251 </p></li></ul></div>
252
253
254 <div class="itemizedlist"><ul type="circle"><li><p><a id="dbt_DB_DBT_PARTIAL"></a>
255 <code class="literal">DB_DBT_PARTIAL</code>
256 </p><p>
257 Do partial retrieval or storage of an item. If the
258 calling application is doing a get, the
259 <span class="bold"><strong>dlen</strong></span> bytes
260 starting
261 <span class="bold"><strong>doff</strong></span>
262 bytes from the beginning of the retrieved
263 data record are returned as if they comprised the entire
264 record. If any or all of the specified bytes do not
265 exist in the record, the get is successful, and any
266 existing bytes are returned.
267 </p><p>
268 For example, if the data portion of a retrieved record
269 was 100 bytes, and a partial retrieval was done using a
270 DBT having a
271 <span class="bold"><strong>dlen</strong></span>
272 field of 20 and a
273 <span class="bold"><strong>doff</strong></span>
274 field of 85,
275 the get call would succeed, the
276 <span class="bold"><strong>data</strong></span>
277 field would refer
278 to the last 15 bytes of the record, and the
279 <span class="bold"><strong>size</strong></span>
280 field would be set to 15.
281 </p><p>
282 If the calling application is doing a put, the
283 <span class="bold"><strong>dlen</strong></span>
284 bytes starting
285 <span class="bold"><strong>doff</strong></span>
286 bytes from the beginning of the
287 specified key's data record are replaced by the data
288 specified by the
289 <span class="bold"><strong>data</strong></span>
290 and
291 <span class="bold"><strong>size</strong></span>
292 structure elements. If
293 <span class="bold"><strong>dlen</strong></span>
294 is smaller than
295 <span class="bold"><strong>size</strong></span>
296 the record will grow; if
297 <span class="bold"><strong>dlen</strong></span>
298 is larger than
299 <span class="bold"><strong>size</strong></span>
300 the record will shrink. If the
301 specified bytes do not exist, the record will be
302 extended using nul bytes as necessary, and the put call
303 will succeed.
304 </p><p>
305 It is an error to attempt a partial put using the
306 <a class="xref" href="dbput.html" title="DB-&gt;put()">DB-&gt;put()</a>
307 method in a database that supports duplicate
308 records. Partial puts in databases supporting duplicate
309 records must be done using a
310 <a class="xref" href="dbcput.html" title="DBcursor-&gt;put()">DBcursor-&gt;put()</a>
311 method.
312 </p><p>
313 It is an error to attempt a partial put with differing
314 <span class="bold"><strong>dlen</strong></span>
315 and
316 <span class="bold"><strong>size</strong></span>
317 values in Queue or Recno databases with
318 fixed-length records.
319 </p><p>
320 For example, if the data portion of a retrieved record
321 was 100 bytes, and a partial put was done using a DBT
322 having a
323 <span class="bold"><strong>dlen</strong></span>
324 field of 20, a
325 <span class="bold"><strong>doff</strong></span>
326 field of 85, and a
327 <span class="bold"><strong>size</strong></span>
328 field of 30, the resulting record would be 115
329 bytes in length, where the last 30 bytes would be those
330 specified by the put call.
331 </p><p>
332 This flag is ignored when used with the
333 <code class="literal">pkey</code> parameter on
334 <a class="link" href="dbget.html" title="DB-&gt;get()">DB-&gt;pget()</a> or
335 <a class="link" href="dbcget.html" title="DBcursor-&gt;get()">DBcursor-&gt;pget()</a>.
336 </p></li><li><p><a id="dbt_DB_DBT_APPMALLOC"></a>
337 <code class="literal">DB_DBT_APPMALLOC</code>
338 </p><p>
339 After an application-supplied callback routine passed to
340 either
341 <a class="xref" href="dbassociate.html" title="DB-&gt;associate()">DB-&gt;associate()</a>
342 or
343 <a class="xref" href="dbset_append_recno.html" title="DB-&gt;set_append_recno()">DB-&gt;set_append_recno()</a>
344 is executed, the
345 <span class="bold"><strong>data</strong></span>
346 field of a DBT may refer to memory allocated with
347 <span class="bold"><strong>malloc</strong></span>(3)
348 or
349 <span class="bold"><strong>realloc</strong></span>(3).
350 In that case,
351 the callback sets the
352 <code class="literal">DB_DBT_APPMALLOC</code>
353 flag in the DBT
354 so that Berkeley DB will call
355 <span class="bold"><strong>free</strong></span>(3)
356 to deallocate the
357 memory when it is no longer required.
358 </p></li><li><p><a id="dbt_DB_DBT_MULTIPLE"></a>
359 <code class="literal">DB_DBT_MULTIPLE</code>
360 </p><p>
361 Set in a secondary key creation callback routine passed
362 to
363 <a class="xref" href="dbassociate.html" title="DB-&gt;associate()">DB-&gt;associate()</a>
364 to indicate that multiple secondary
365 keys should be associated with the given primary
366 key/data pair. If set, the
367 <span class="bold"><strong>size</strong></span>
368 field indicates the
369 number of secondary keys and the
370 <span class="bold"><strong>data</strong></span>
371 field refers to an
372 array of that number of DBT structures.
373 </p><p>
374 The <code class="literal">DB_DBT_APPMALLOC</code> flag may be set on any of the DBT
375 structures to indicate that their
376 <span class="bold"><strong>data</strong></span>
377 field needs to be
378 freed.
379 </p></li><li><p><a id="dbt_DB_DBT_READONLY"></a>
380 <code class="literal">DB_DBT_READONLY</code>
381 </p><p>
382 When this flag is set Berkeley DB will not write
383 into the DBT. This may be set on key values in
384 cases where the key is a static string that cannot
385 be written and Berkeley DB might try to update it
386 because the application has set a user defined
387 comparison function.
388 </p></li></ul></div>
389 </span>
390 </li>
391 </ul>
392 </div>
393 <div class="sect1" lang="en" xml:lang="en">
394 <div class="titlepage">
395 <div>
396 <div>
397 <h2 class="title" style="clear: both"><a id="dbtlist"></a>DBT and Bulk Operations</h2>
398 </div>
399 </div>
400 </div>
401 <div class="navtable">
402 <table border="1" width="80%">
403 <thead>
404 <tr>
405 <th>DBT and Bulk Operations</th>
406 <th>Description</th>
407 </tr>
408 </thead>
409 <tbody>
410 <tr>
411 <td>
412 <a class="xref" href="dbsort_multiple.html" title="DB-&gt;sort_multiple()">DB-&gt;sort_multiple()</a>
413 </td>
414 <td>Sort a set of DBTs</td>
415 </tr>
416 <tr>
417 <td>
418 <a class="xref" href="DB_MULTIPLE_INIT.html" title="DB_MULTIPLE_INIT">DB_MULTIPLE_INIT</a>
419 </td>
420 <td>Initialize bulk get retrieval</td>
421 </tr>
422 <tr>
423 <td>
424 <a class="xref" href="DB_MULTIPLE_NEXT.html" title="DB_MULTIPLE_NEXT">DB_MULTIPLE_NEXT</a>
425 </td>
426 <td>Next bulk get retrieval</td>
427 </tr>
428 <tr>
429 <td>
430 <a class="xref" href="DB_MULTIPLE_KEY_NEXT.html" title="DB_MULTIPLE_KEY_NEXT">DB_MULTIPLE_KEY_NEXT</a>
431 </td>
432 <td>Next bulk get retrieval</td>
433 </tr>
434 <tr>
435 <td>
436 <a class="xref" href="DB_MULTIPLE_RECNO_NEXT.html" title="DB_MULTIPLE_RECNO_NEXT">DB_MULTIPLE_RECNO_NEXT</a>
437 </td>
438 <td>Next bulk get retrieval</td>
439 </tr>
440 <tr>
441 <td>
442 <a class="xref" href="DB_MULTIPLE_WRITE_INIT.html" title="DB_MULTIPLE_WRITE_INIT">DB_MULTIPLE_WRITE_INIT</a>
443 </td>
444 <td>Initialize a bulk buffer to hold key/data pairs</td>
445 </tr>
446 <tr>
447 <td>
448 <a class="xref" href="DB_MULTIPLE_WRITE_NEXT.html" title="DB_MULTIPLE_WRITE_NEXT">DB_MULTIPLE_WRITE_NEXT</a>
449 </td>
450 <td>Append a data item to a bulk buffer</td>
451 </tr>
452 <tr>
453 <td>
454 <a class="xref" href="DB_MULTIPLE_RESERVE_NEXT.html" title="DB_MULTIPLE_RESERVE_NEXT">DB_MULTIPLE_RESERVE_NEXT</a>
455 </td>
456 <td>Reserve space for a data item in a bulk buffer</td>
457 </tr>
458 <tr>
459 <td>
460 <a class="xref" href="DB_MULTIPLE_KEY_WRITE_NEXT.html" title="DB_MULTIPLE_KEY_WRITE_NEXT">DB_MULTIPLE_KEY_WRITE_NEXT</a>
461 </td>
462 <td>Append a key / data pair to a bulk buffer</td>
463 </tr>
464 <tr>
465 <td>
466 <a class="xref" href="DB_MULTIPLE_KEY_RESERVE_NEXT.html" title="DB_MULTIPLE_KEY_RESERVE_NEXT">DB_MULTIPLE_KEY_RESERVE_NEXT</a>
467 </td>
468 <td>Reserve space for a key / data pair in a bulk buffer</td>
469 </tr>
470 <tr>
471 <td>
472 <a class="xref" href="DB_MULTIPLE_RECNO_WRITE_INIT.html" title="DB_MULTIPLE_RECNO_WRITE_INIT">DB_MULTIPLE_RECNO_WRITE_INIT</a>
473 </td>
474 <td>Initialize a bulk buffer to hold recno/data pairs</td>
475 </tr>
476 <tr>
477 <td>
478 <a class="xref" href="DB_MULTIPLE_RECNO_WRITE_NEXT.html" title="DB_MULTIPLE_RECNO_WRITE_NEXT">DB_MULTIPLE_RECNO_WRITE_NEXT</a>
479 </td>
480 <td>Append a record number / data pair to a bulk buffer</td>
481 </tr>
482 <tr>
483 <td>
484 <a class="xref" href="DB_MULTIPLE_RECNO_RESERVE_NEXT.html" title="DB_MULTIPLE_RECNO_RESERVE_NEXT">DB_MULTIPLE_RECNO_RESERVE_NEXT</a>
485 </td>
486 <td>Reserve space for a record number / data pair in a bulk buffer</td>
487 </tr>
488 </tbody>
489 </table>
490 </div>
491 </div>
492 </div>
493 <div class="navfooter">
494 <hr />
495 <table width="100%" summary="Navigation footer">
496 <tr>
497 <td width="40%" align="left"><a accesskey="p" href="dbcset_priority.html">Prev</a> </td>
498 <td width="20%" align="center"> </td>
499 <td width="40%" align="right"> <a accesskey="n" href="DB_MULTIPLE_INIT.html">Next</a></td>
500 </tr>
501 <tr>
502 <td width="40%" align="left" valign="top">DBcursor-&gt;set_priority() </td>
503 <td width="20%" align="center">
504 <a accesskey="h" href="index.html">Home</a>
505 </td>
506 <td width="40%" align="right" valign="top"> DB_MULTIPLE_INIT</td>
507 </tr>
508 </table>
509 </div>
510 </body>
511 </html>