Debian Codegraph @

docs/programmer_reference/bt_conf.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>Btree access method specific configuration</title>
7 <link rel="stylesheet" href="gettingStarted.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 Programmer's Reference Guide" />
10 <link rel="up" href="am_conf.html" title="Chapter 2.  Access Method Configuration" />
11 <link rel="prev" href="general_am_conf.html" title="General access method configuration" />
12 <link rel="next" href="hash_conf.html" title="Hash access method specific configuration" />
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">Btree access method specific configuration</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="general_am_conf.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 2. 
26 Access Method Configuration
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="hash_conf.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="sect1" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title" style="clear: both"><a id="bt_conf"></a>Btree access method specific configuration</h2>
38 </div>
39 </div>
40 </div>
41 <div class="toc">
42 <dl>
43 <dt>
44 <span class="sect2">
45 <a href="bt_conf.html#am_conf_bt_compare">Btree comparison</a>
46 </span>
47 </dt>
48 <dt>
49 <span class="sect2">
50 <a href="bt_conf.html#am_conf_bt_prefix">Btree prefix comparison</a>
51 </span>
52 </dt>
53 <dt>
54 <span class="sect2">
55 <a href="bt_conf.html#am_conf_bt_minkey">Minimum keys per page</a>
56 </span>
57 </dt>
58 <dt>
59 <span class="sect2">
60 <a href="bt_conf.html#am_conf_bt_recnum">Retrieving Btree records by logical record number</a>
61 </span>
62 </dt>
63 <dt>
64 <span class="sect2">
65 <a href="bt_conf.html#am_conf_bt_compress">Compression</a>
66 </span>
67 </dt>
68 </dl>
69 </div>
70 <p>
71 There are a series of configuration tasks which you can perform when
72 using the Btree access method. They are described in the following sections.
73 </p>
74 <div class="sect2" lang="en" xml:lang="en">
75 <div class="titlepage">
76 <div>
77 <div>
78 <h3 class="title"><a id="am_conf_bt_compare"></a>Btree comparison</h3>
79 </div>
80 </div>
81 </div>
82 <p>The Btree data structure is a sorted, balanced tree structure storing
83 associated key/data pairs. By default, the sort order is lexicographical,
84 with shorter keys collating before longer keys. The user can specify the
85 sort order for the Btree by using the <a href="../api_reference/C/dbset_bt_compare.html" class="olink">DB-&gt;set_bt_compare()</a> method.</p>
86 <p>Sort routines are passed pointers to keys as arguments. The keys are
87 represented as <a href="../api_reference/C/dbt.html" class="olink">DBT</a> structures. The routine must return an integer
88 less than, equal to, or greater than zero if the first argument is
89 considered to be respectively less than, equal to, or greater than the
90 second argument. The only fields that the routines may examine in the
91 <a href="../api_reference/C/dbt.html" class="olink">DBT</a> structures are <span class="bold"><strong>data</strong></span> and <span class="bold"><strong>size</strong></span> fields.</p>
92 <p>An example routine that might be used to sort integer keys in the database
93 is as follows:</p>
94 <a id="prog_am2"></a>
95 <pre class="programlisting">
96 int
97 compare_int(DB *dbp, const DBT *a, const DBT *b)
98
99 {
100 int ai, bi;
101 /*
102 * Returns:
103 * &lt; 0 if a &lt; b
104 * = 0 if a = b
105 * &gt; 0 if a &gt; b
106 */
107 memcpy(&amp;ai, a-&gt;data, sizeof(int));
108 memcpy(&amp;bi, b-&gt;data, sizeof(int));
109 return (ai - bi);
110 }
111 </pre>
112 <p>Note that the data must first be copied into memory that is appropriately
113 aligned, as Berkeley DB does not guarantee any kind of alignment of the
114 underlying data, including for comparison routines. When writing
115 comparison routines, remember that databases created on machines of
116 different architectures may have different integer byte orders, for which
117 your code may need to compensate.</p>
118 <p>An example routine that might be used to sort keys based on the first
119 five bytes of the key (ignoring any subsequent bytes) is as follows:</p>
120 <a id="prog_am3"></a>
121 <pre class="programlisting">
122 int
123 compare_dbt(DB *dbp, const DBT *a, const DBT *b)
124
125 {
126 int len;
127 u_char *p1, *p2;
128
129 /*
130 * Returns:
131 * &lt; 0 if a &lt; b
132 * = 0 if a = b
133 * &gt; 0 if a &gt; b
134 */
135 for (p1 = a-&gt;data, p2 = b-&gt;data, len = 5; len--; ++p1, ++p2)
136 if (*p1 != *p2)
137 return ((long)*p1 - (long)*p2);
138 return (0);
139 }
140 </pre>
141 <p>All comparison functions must cause the keys in the database to be
142 well-ordered. The most important implication of being well-ordered is
143 that the key relations must be transitive, that is, if key A is less
144 than key B, and key B is less than key C, then the comparison routine
145 must also return that key A is less than key C.</p>
146 <p>It is reasonable for a comparison function to not examine an entire key
147 in some applications, which implies partial keys may be specified to the
148 Berkeley DB interfaces. When partial keys are specified to Berkeley DB, interfaces
149 which retrieve data items based on a user-specified key (for example,
150 <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;get()</a> and <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a> with the <a href="../api_reference/C/dbcget.html#dbcget_DB_SET" class="olink">DB_SET</a> flag), will
151 modify the user-specified key by returning the actual key stored in the
152 database.</p>
153 </div>
154 <div class="sect2" lang="en" xml:lang="en">
155 <div class="titlepage">
156 <div>
157 <div>
158 <h3 class="title"><a id="am_conf_bt_prefix"></a>Btree prefix comparison</h3>
159 </div>
160 </div>
161 </div>
162 <p>The Berkeley DB Btree implementation maximizes the number of keys that can be
163 stored on an internal page by storing only as many bytes of each key as
164 are necessary to distinguish it from adjacent keys. The prefix
165 comparison routine is what determines this minimum number of bytes (that
166 is, the length of the unique prefix), that must be stored. A prefix
167 comparison function for the Btree can be specified by calling
168 <a href="../api_reference/C/dbset_bt_prefix.html" class="olink">DB-&gt;set_bt_prefix()</a>.</p>
169 <p>The prefix comparison routine must be compatible with the overall
170 comparison function of the Btree, since what distinguishes any two keys
171 depends entirely on the function used to compare them. This means that
172 if a prefix comparison routine is specified by the application, a
173 compatible overall comparison routine must also have been specified.</p>
174 <p>Prefix comparison routines are passed pointers to keys as arguments.
175 The keys are represented as <a href="../api_reference/C/dbt.html" class="olink">DBT</a> structures. The only fields
176 the routines may examine in the <a href="../api_reference/C/dbt.html" class="olink">DBT</a> structures are <span class="bold"><strong>data</strong></span>
177 and <span class="bold"><strong>size</strong></span> fields.</p>
178 <p>The prefix comparison function must return the number of bytes necessary
179 to distinguish the two keys. If the keys are identical (equal and equal
180 in length), the length should be returned. If the keys are equal up to
181 the smaller of the two lengths, then the length of the smaller key plus
182 1 should be returned.</p>
183 <p>An example prefix comparison routine follows:</p>
184 <a id="prog_am4"></a>
185 <pre class="programlisting">
186 size_t
187 compare_prefix(DB *dbp, const DBT *a, const DBT *b)
188
189 {
190 size_t cnt, len;
191 u_int8_t *p1, *p2;
192
193 cnt = 1;
194 len = a-&gt;size &gt; b-&gt;size ? b-&gt;size : a-&gt;size;
195 for (p1 =
196 a-&gt;data, p2 = b-&gt;data; len--; ++p1, ++p2, ++cnt)
197 if (*p1 != *p2)
198 return (cnt);
199 /*
200 * They match up to the smaller of the two sizes.
201 * Collate the longer after the shorter.
202 */
203 if (a-&gt;size &lt; b-&gt;size)
204 return (a-&gt;size + 1);
205 if (b-&gt;size &lt; a-&gt;size)
206 return (b-&gt;size + 1);
207 return (b-&gt;size);
208 }
209 </pre>
210 <p>The usefulness of this functionality is data-dependent, but in some data
211 sets can produce significantly reduced tree sizes and faster search times.</p>
212 </div>
213 <div class="sect2" lang="en" xml:lang="en">
214 <div class="titlepage">
215 <div>
216 <div>
217 <h3 class="title"><a id="am_conf_bt_minkey"></a>Minimum keys per page</h3>
218 </div>
219 </div>
220 </div>
221 <p>The number of keys stored on each page affects the size of a Btree and
222 how it is maintained. Therefore, it also affects the retrieval and search
223 performance of the tree. For each Btree, Berkeley DB computes a maximum key
224 and data size. This size is a function of the page size and the fact that
225 at least two key/data pairs must fit on any Btree page. Whenever key or
226 data items exceed the calculated size, they are stored on overflow pages
227 instead of in the standard Btree leaf pages.</p>
228 <p>Applications may use the <a href="../api_reference/C/dbset_bt_minkey.html" class="olink">DB-&gt;set_bt_minkey()</a> method to change the minimum
229 number of keys that must fit on a Btree page from two to another value.
230 Altering this value in turn alters the on-page maximum size, and can be
231 used to force key and data items which would normally be stored in the
232 Btree leaf pages onto overflow pages.</p>
233 <p>Some data sets can benefit from this tuning. For example, consider an
234 application using large page sizes, with a data set almost entirely
235 consisting of small key and data items, but with a few large items. By
236 setting the minimum number of keys that must fit on a page, the
237 application can force the outsized items to be stored on overflow pages.
238 That in turn can potentially keep the tree more compact, that is, with
239 fewer internal levels to traverse during searches.</p>
240 <p>The following calculation is similar to the one performed by the Btree
241 implementation. (The <span class="bold"><strong>minimum_keys</strong></span> value is multiplied by 2
242 because each key/data pair requires 2 slots on a Btree page.)</p>
243 <pre class="programlisting">maximum_size = page_size / (minimum_keys * 2)</pre>
244 <p>Using this calculation, if the page size is 8KB and the default
245 <span class="bold"><strong>minimum_keys</strong></span> value of 2 is used, then any key or data items
246 larger than 2KB will be forced to an overflow page. If an application
247 were to specify a <span class="bold"><strong>minimum_key</strong></span> value of 100, then any key or data
248 items larger than roughly 40 bytes would be forced to overflow pages.</p>
249 <p>It is important to remember that accesses to overflow pages do not perform
250 as well as accesses to the standard Btree leaf pages, and so setting the
251 value incorrectly can result in overusing overflow pages and decreasing
252 the application's overall performance.</p>
253 </div>
254 <div class="sect2" lang="en" xml:lang="en">
255 <div class="titlepage">
256 <div>
257 <div>
258 <h3 class="title"><a id="am_conf_bt_recnum"></a>Retrieving Btree records by logical record number</h3>
259 </div>
260 </div>
261 </div>
262 <p>The Btree access method optionally supports retrieval by logical record
263 numbers. To configure a Btree to support record numbers, call the
264 <a href="../api_reference/C/dbset_flags.html" class="olink">DB-&gt;set_flags()</a> method with the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_RECNUM" class="olink">DB_RECNUM</a> flag.</p>
265 <p>Configuring a Btree for record numbers should not be done lightly.
266 While often useful, it may significantly slow down the speed at which
267 items can be stored into the database, and can severely impact
268 application throughput. Generally it should be avoided in trees with
269 a need for high write concurrency.</p>
270 <p>To retrieve by record number, use the <a href="../api_reference/C/dbget.html#dbget_DB_SET_RECNO" class="olink">DB_SET_RECNO</a> flag to the
271 <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;get()</a> and <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a> methods. The following is an example of
272 a routine that displays the data item for a Btree database created with
273 the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_RECNUM" class="olink">DB_RECNUM</a> option.</p>
274 <a id="prog_am5"></a>
275 <pre class="programlisting">
276 int
277 rec_display(DB *dbp, db_recno_t recno)
278
279 {
280 DBT key, data;
281 int ret;
282
283 memset(&amp;key, 0, sizeof(key));
284 key.data = &amp;recno;
285 key.size = sizeof(recno);
286 memset(&amp;data, 0, sizeof(data));
287
288 if ((ret = dbp-&gt;get(dbp, NULL, &amp;key, &amp;data, DB_SET_RECNO)) != 0)
289 return (ret);
290 printf("data for %lu: %.*s\n",
291 (u_long)recno, (int)data.size, (char *)data.data);
292 return (0);
293 }
294 </pre>
295 <p>To determine a key's record number, use the <a href="../api_reference/C/dbcget.html#dbcget_DB_GET_RECNO" class="olink">DB_GET_RECNO</a> flag
296 to the <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a> method. The following is an example of a routine that
297 displays the record number associated with a specific key.</p>
298 <a id="prog_am6"></a>
299 <pre class="programlisting">
300 int
301 recno_display(DB *dbp, char *keyvalue)
302
303 {
304 DBC *dbcp;
305 DBT key, data;
306 db_recno_t recno;
307 int ret, t_ret;
308
309 /* Acquire a cursor for the database. */
310 if ((ret = dbp-&gt;cursor(dbp, NULL, &amp;dbcp, 0)) != 0) {
311 dbp-&gt;err(dbp, ret, "DB-&gt;cursor");
312 goto err;
313 }
314
315 /* Position the cursor. */
316 memset(&amp;key, 0, sizeof(key));
317 key.data = keyvalue;
318 key.size = strlen(keyvalue);
319 memset(&amp;data, 0, sizeof(data));
320 if ((ret = dbcp-&gt;get(dbcp, &amp;key, &amp;data, DB_SET)) != 0) {
321 dbp-&gt;err(dbp, ret, "DBC-&gt;get(DB_SET): %s", keyvalue);
322 goto err;
323 }
324
325 /*
326 * Request the record number, and store it into appropriately
327 * sized and aligned local memory.
328 */
329 memset(&amp;data, 0, sizeof(data));
330 data.data = &amp;recno;
331 data.ulen = sizeof(recno);
332 data.flags = DB_DBT_USERMEM;
333 if ((ret = dbcp-&gt;get(dbcp, &amp;key, &amp;data, DB_GET_RECNO)) != 0) {
334 dbp-&gt;err(dbp, ret, "DBC-&gt;get(DB_GET_RECNO)");
335 goto err;
336 }
337
338 printf("key for requested key was %lu\n", (u_long)recno);
339
340 err: /* Close the cursor. */
341 if ((t_ret = dbcp-&gt;close(dbcp)) != 0) {
342 if (ret == 0)
343 ret = t_ret;
344 dbp-&gt;err(dbp, ret, "DBC-&gt;close");
345 }
346 return (ret);
347 }
348 </pre>
349 </div>
350 <div class="sect2" lang="en" xml:lang="en">
351 <div class="titlepage">
352 <div>
353 <div>
354 <h3 class="title"><a id="am_conf_bt_compress"></a>Compression</h3>
355 </div>
356 </div>
357 </div>
358 <p>
359 The Btree access method supports the automatic compression of key/data
360 pairs upon their insertion into the database. The key/data pairs are
361 decompressed before they are returned to the application, making an
362 application's interaction with a compressed database identical to that
363 for a non-compressed database. To configure Berkeley DB for
364 compression, call the <a href="../api_reference/C/dbset_bt_compress.html" class="olink">DB-&gt;set_bt_compress()</a> method and specify custom
365 compression and decompression functions. If <a href="../api_reference/C/dbset_bt_compress.html" class="olink">DB-&gt;set_bt_compress()</a> is
366 called with NULL compression and decompression functions, Berkeley DB
367 will use its default compression functions.
368 </p>
369 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
370 <h3 class="title">Note</h3>
371 <p>
372 Compression only works with the Btree access method, and then only
373 so long as your database is not configured for unsorted duplicates.
374 </p>
375 </div>
376 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
377 <h3 class="title">Note</h3>
378 <p>
379 The default compression function is not guaranteed to reduce the
380 size of the on-disk database in every case. It has been tested
381 and shown to work well with English-language text. Of course, in
382 order to determine if the default compression algorithm is beneficial
383 for your application, it is important to test both the final size
384 and the performance using a representative set of data and access
385 patterns.
386 </p>
387 </div>
388 <p>
389 The default compression function performs prefix compression on each
390 key added to the database. This means that, for a key
391 <span class="emphasis"><em>n</em></span> bytes in length, the first
392 <span class="emphasis"><em>i</em></span> bytes that match the first
393 <span class="emphasis"><em>i</em></span> bytes of the previous key exactly are omitted
394 and only the final <span class="emphasis"><em>n-i</em></span> bytes are stored in the
395 database. If the bytes of key being stored match the bytes of the
396 previous key exactly, then the same prefix compression algorithm is
397 applied to the data value being stored. To use Berkeley DB's default
398 compression behavior, both the default compression and decompression
399 functions must be used.
400 </p>
401 <p>
402 For example, to configure your database for default compression:
403 </p>
404 <a id="prog_am7"></a>
405 <pre class="programlisting">
406 DB *dbp = NULL;
407 DB_ENV *envp = NULL;
408 u_int32_t db_flags;
409 const char *file_name = "mydb.db";
410 int ret;
411
412 ...
413
414 /* Skipping environment open to shorten this example */
415 /* Initialize the DB handle */
416 ret = db_create(&amp;dbp, envp, 0);
417 if (ret != 0) {
418 fprintf(stderr, "%s\n", db_strerror(ret));
419 return (EXIT_FAILURE);
420 }
421
422 /* Turn on default data compression */
423 dbp-&gt;set_bt_compress(dbp, NULL, NULL);
424
425 /* Now open the database */
426 db_flags = DB_CREATE; /* Allow database creation */
427
428 ret = dbp-&gt;open(dbp, /* Pointer to the database */
429 NULL, /* Txn pointer */
430 file_name, /* File name */
431 NULL, /* Logical db name */
432 DB_BTREE, /* Database type (using btree) */
433 db_flags, /* Open flags */
434 0); /* File mode. Using defaults */
435 if (ret != 0) {
436 dbp-&gt;err(dbp, ret, "Database '%s' open failed",
437 file_name);
438 return (EXIT_FAILURE);
439 }</pre>
440 <div class="sect3" lang="en" xml:lang="en">
441 <div class="titlepage">
442 <div>
443 <div>
444 <h4 class="title"><a id="am_conf_bt_custom_compress"></a>Custom compression</h4>
445 </div>
446 </div>
447 </div>
448 <p>
449 An application wishing to perform its own compression may supply a
450 compression and decompression function which will be called instead of
451 Berkeley DB's default functions. The compression function is
452 passed five <a href="../api_reference/C/dbt.html" class="olink">DBT</a> structures:
453 </p>
454 <div class="itemizedlist">
455 <ul type="disc">
456 <li>
457 <p>
458 The key and data immediately preceeding the key/data pair
459 that is being stored.
460 </p>
461 </li>
462 <li>
463 <p>
464 The key and data being stored in the tree.
465 </p>
466 </li>
467 <li>
468 <p>
469 The buffer where the compressed data should be written.
470 </p>
471 </li>
472 </ul>
473 </div>
474 <p>
475 The total size of the buffer used to store the compressed data is
476 identified in the <a href="../api_reference/C/dbt.html" class="olink">DBT</a>'s <code class="literal">ulen</code> field. If the
477 compressed data cannot fit in the buffer, the compression function
478 should store the amount of space needed in <a href="../api_reference/C/dbt.html" class="olink">DBT</a>'s
479 <code class="literal">size</code> field and then return
480 <code class="literal">DB_BUFFER_SMALL</code>. Berkeley DB will subsequently
481 re-call the compression function with the required amount of space
482 allocated in the compression data buffer.
483 </p>
484 <p>
485 Multiple compressed key/data pairs will likely be written to the
486 same buffer and the compression function should take steps to
487 ensure it does not overwrite data.
488 </p>
489 <p>
490 For example, the following code fragments illustrate the use of a custom
491 compression routine. This code is actually a much simplified
492 example of the default compression provided by Berkeley DB. It does
493 simple prefix compression on the key part of the data.
494 </p>
495 <a id="prog_am8"></a>
496 <pre class="programlisting">
497 int compress(DB *dbp, const DBT *prevKey, const DBT *prevData,
498 const DBT *key, const DBT *data, DBT *dest)
499 {
500 u_int8_t *dest_data_ptr;
501 const u_int8_t *key_data, *prevKey_data;
502 size_t len, prefix, suffix;
503
504 key_data = (const u_int8_t*)key-&gt;data;
505 prevKey_data = (const u_int8_t*)prevKey-&gt;data;
506 len = key-&gt;size &gt; prevKey-&gt;size ? prevKey-&gt;size : key-&gt;size;
507 for (; len-- &amp;&amp; *key_data == *prevKey_data; ++key_data,
508 ++prevKey_data)
509 continue;
510
511 prefix = (size_t)(key_data - (u_int8_t*)key-&gt;data);
512 suffix = key-&gt;size - prefix;
513
514 /* Check that we have enough space in dest */
515 dest-&gt;size = (u_int32_t)(__db_compress_count_int(prefix) +
516 __db_compress_count_int(suffix) +
517 __db_compress_count_int(data-&gt;size) + suffix + data-&gt;size);
518 if (dest-&gt;size &gt; dest-&gt;ulen)
519 return (DB_BUFFER_SMALL);
520
521 /* prefix length */
522 dest_data_ptr = (u_int8_t*)dest-&gt;data;
523 dest_data_ptr += __db_compress_int(dest_data_ptr, prefix);
524
525 /* suffix length */
526 dest_data_ptr += __db_compress_int(dest_data_ptr, suffix);
527
528 /* data length */
529 dest_data_ptr += __db_compress_int(dest_data_ptr, data-&gt;size);
530
531 /* suffix */
532 memcpy(dest_data_ptr, key_data, suffix);
533 dest_data_ptr += suffix;
534
535 /* data */
536 memcpy(dest_data_ptr, data-&gt;data, data-&gt;size);
537
538 return (0);
539 } </pre>
540 <p>
541 The corresponding decompression function is likewise passed five <a href="../api_reference/C/dbt.html" class="olink">DBT</a> structures:
542 </p>
543 <div class="itemizedlist">
544 <ul type="disc">
545 <li>
546 <p>
547 The key and data <a href="../api_reference/C/dbt.html" class="olink">DBT</a>s immediately preceding the
548 decompressed key and data.
549 </p>
550 </li>
551 <li>
552 <p>
553 The compressed data from the database.
554 </p>
555 </li>
556 <li>
557 <p>
558 One to store the decompressed key and another one for the
559 decompressed data.
560 </p>
561 </li>
562 </ul>
563 </div>
564 <p>
565 Because the compression of <code class="literal">record X</code> relies upon
566 <code class="literal">record X-1</code>, the decompression function can be
567 called repeatedly to linearally decompress a set of records stored
568 in the compressed buffer.
569 </p>
570 <p>
571 The total size of the buffer available to store the decompressed data is
572 identified in the destination <a href="../api_reference/C/dbt.html" class="olink">DBT</a>'s <code class="literal">ulen</code> field. If the
573 decompressed data cannot fit in the buffer, the decompression function
574 should store the amount of space needed in the destination <a href="../api_reference/C/dbt.html" class="olink">DBT</a>'s
575 <code class="literal">size</code> field and then return
576 <code class="literal">DB_BUFFER_SMALL</code>. Berkeley DB will subsequently
577 re-call the decompression function with the required amount of space
578 allocated in the decompression data buffer.
579 </p>
580 <p>
581 For example, the decompression routine that corresponds to the
582 example compression routine provided above is:
583 </p>
584 <a id="prog_am9"></a>
585 <pre class="programlisting">int decompress(DB *dbp, const DBT *prevKey, const DBT *prevData,
586 DBT *compressed, DBT *destKey, DBT *destData)
587 {
588 u_int8_t *comp_data, *dest_data;
589 u_int32_t prefix, suffix, size;
590
591 /* Unmarshal prefix, suffix and data length */
592 comp_data = (u_int8_t*)compressed-&gt;data;
593 size = __db_decompress_count_int(comp_data);
594 if (size &gt; compressed-&gt;size)
595 return (EINVAL);
596 comp_data += __db_decompress_int32(comp_data, &amp;prefix);
597
598 size += __db_decompress_count_int(comp_data);
599 if (size &gt; compressed-&gt;size)
600 return (EINVAL);
601 comp_data += __db_decompress_int32(comp_data, &amp;suffix);
602
603 size += __db_decompress_count_int(comp_data);
604 if (size &gt; compressed-&gt;size)
605 return (EINVAL);
606 comp_data += __db_decompress_int32(comp_data, &amp;destData-&gt;size);
607
608 /* Check destination lengths */
609 destKey-&gt;size = prefix + suffix;
610 if (destKey-&gt;size &gt; destKey-&gt;ulen ||
611 destData-&gt;size &gt; destData-&gt;ulen)
612 return (DB_BUFFER_SMALL);
613
614 /* Write the prefix */
615 if (prefix &gt; prevKey-&gt;size)
616 return (EINVAL);
617 dest_data = (u_int8_t*)destKey-&gt;data;
618 memcpy(dest_data, prevKey-&gt;data, prefix);
619 dest_data += prefix;
620
621 /* Write the suffix */
622 size += suffix;
623 if (size &gt; compressed-&gt;size)
624 return (EINVAL);
625 memcpy(dest_data, comp_data, suffix);
626 comp_data += suffix;
627
628 /* Write the data */
629 size += destData-&gt;size;
630 if (size &gt; compressed-&gt;size)
631 return (EINVAL);
632 memcpy(destData-&gt;data, comp_data, destData-&gt;size);
633 comp_data += destData-&gt;size;
634
635 /* Return bytes read */
636 compressed-&gt;size =
637 (u_int32_t)(comp_data - (u_int8_t*)compressed-&gt;data);
638 return (0);
639 } </pre>
640 </div>
641 <div class="sect3" lang="en" xml:lang="en">
642 <div class="titlepage">
643 <div>
644 <div>
645 <h4 class="title"><a id="idp50856856"></a>Programmer Notes</h4>
646 </div>
647 </div>
648 </div>
649 <p>
650 As you use compression with your databases, be aware of the
651 following:
652 </p>
653 <div class="itemizedlist">
654 <ul type="disc">
655 <li>
656 <p>
657 Compression works by placing key/data pairs from a single
658 database page into a single block of compressed data. This is true
659 whether you use DB's default compression, or you write
660 your own compression. Because all of key/data data is
661 placed in a single block of memory, you cannot decompress
662 data unless you have decompressed everything that came
663 before it in the block. That is, you cannot decompress item
664 <span class="emphasis"><em>n</em></span> in the data block, unless you also
665 decompress items <span class="emphasis"><em>0</em></span> through
666 <span class="emphasis"><em>n-1</em></span>.
667 </p>
668 </li>
669 <li>
670 <p>
671 If you increase the minimum number of key/data pairs placed
672 on a Btree leaf page (using <a href="../api_reference/C/dbset_bt_minkey.html" class="olink">DB-&gt;set_bt_minkey()</a>), you will
673 decrease your seek times on a compressed database. However,
674 this will also decrease the effectiveness of the
675 compression.
676 </p>
677 </li>
678 <li>
679 <p>
680 Compressed databases are fastest if bulk load is used to
681 add data to them. See
682 <a class="xref" href="am_misc_bulk.html" title="Retrieving and updating records in bulk">Retrieving and updating records in bulk</a>
683 for information on using bulk load.
684 </p>
685 </li>
686 </ul>
687 </div>
688 </div>
689 </div>
690 </div>
691 <div class="navfooter">
692 <hr />
693 <table width="100%" summary="Navigation footer">
694 <tr>
695 <td width="40%" align="left"><a accesskey="p" href="general_am_conf.html">Prev</a> </td>
696 <td width="20%" align="center">
697 <a accesskey="u" href="am_conf.html">Up</a>
698 </td>
699 <td width="40%" align="right"> <a accesskey="n" href="hash_conf.html">Next</a></td>
700 </tr>
701 <tr>
702 <td width="40%" align="left" valign="top">General access method configuration </td>
703 <td width="20%" align="center">
704 <a accesskey="h" href="index.html">Home</a>
705 </td>
706 <td width="40%" align="right" valign="top"> Hash access method specific configuration</td>
707 </tr>
708 </table>
709 </div>
710 </body>
711 </html>