~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to db/docs/ref/am_misc/get_bulk.html

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--$Id: get_bulk.html,v 1.1.2.1 2001/07/23 20:40:23 jbj Exp $-->
 
2
<!--Copyright 1997-2001 by Sleepycat Software, Inc.-->
 
3
<!--All rights reserved.-->
 
4
<html>
 
5
<head>
 
6
<title>Berkeley DB Reference Guide: Retrieving records in bulk</title>
 
7
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
 
8
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
 
9
</head>
 
10
<body bgcolor=white>
 
11
<a name="2"><!--meow--></a>
 
12
<table width="100%"><tr valign=top>
 
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td>
 
14
<td align=right><a href="../../ref/am_misc/align.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_misc/partial.html"><img src="../../images/next.gif" alt="Next"></a>
 
15
</td></tr></table>
 
16
<p>
 
17
<h1 align=center>Retrieving records in bulk</h1>
 
18
<p>When retrieving large numbers of records from the database, the number
 
19
of method calls can often dominate performance.  Berkeley DB offers bulk get
 
20
interfaces which can significantly increase performance for some
 
21
applications.  To retrieve records in bulk, an application buffer must
 
22
be specified to the <a href="../../api_c/db_get.html">DB-&gt;get</a> or <a href="../../api_c/dbc_get.html">DBcursor-&gt;c_get</a> functions.  This is done
 
23
in the C API by setting the <b>data</b> and <b>ulen</b> fields of the
 
24
<b>data</b> <a href="../../api_c/dbt.html">DBT</a> to reference an application buffer, and the
 
25
<b>flags</b> field of that structure to <a href="../../api_c/dbt.html#DB_DBT_USERMEM">DB_DBT_USERMEM</a>.  In
 
26
the Berkeley DB C++ and Java APIs, the actions are similar, although there
 
27
are API-specific methods to set the <a href="../../api_c/dbt.html">DBT</a> values.  Then, the
 
28
<a href="../../api_c/dbc_get.html#DB_MULTIPLE">DB_MULTIPLE</a> or <a href="../../api_c/dbc_get.html#DB_MULTIPLE_KEY">DB_MULTIPLE_KEY</a> flags are specified to
 
29
the <a href="../../api_c/db_get.html">DB-&gt;get</a> or <a href="../../api_c/dbc_get.html">DBcursor-&gt;c_get</a> functions, which cause multiple records
 
30
to be returned in the specified buffer.
 
31
<p>The difference between <a href="../../api_c/dbc_get.html#DB_MULTIPLE">DB_MULTIPLE</a> and <a href="../../api_c/dbc_get.html#DB_MULTIPLE_KEY">DB_MULTIPLE_KEY</a>
 
32
is as follows: <a href="../../api_c/dbc_get.html#DB_MULTIPLE">DB_MULTIPLE</a> returns multiple data items for a
 
33
single key.  For example, the <a href="../../api_c/dbc_get.html#DB_MULTIPLE">DB_MULTIPLE</a> flag would be used to
 
34
retrieve all of the duplicate data items for a single key in a single
 
35
call.  The <a href="../../api_c/dbc_get.html#DB_MULTIPLE_KEY">DB_MULTIPLE_KEY</a> flag is used to retrieve multiple
 
36
key/data pairs, where each returned key may or may not have duplicate
 
37
data items.
 
38
<p>Once the <a href="../../api_c/db_get.html">DB-&gt;get</a> or <a href="../../api_c/dbc_get.html">DBcursor-&gt;c_get</a> function has returned, the
 
39
application will walk through the buffer handling the returned records.
 
40
This is implemented for the C and C++ APIs using four macros:
 
41
<a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_INIT">DB_MULTIPLE_INIT</a>, <a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_NEXT">DB_MULTIPLE_NEXT</a>,
 
42
<a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_KEY_NEXT">DB_MULTIPLE_KEY_NEXT</a>, and <a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_RECNO_NEXT">DB_MULTIPLE_RECNO_NEXT</a>.  For
 
43
the Java API, this is implemented as three iterator classes:
 
44
<a href="../../api_java/dbt_bulk_class.html">DbMultipleDataIterator</a>,
 
45
<a href="../../api_java/dbt_bulk_class.html">DbMultipleKeyDataIterator</a>, and
 
46
<a href="../../api_java/dbt_bulk_class.html">DbMultipleRecnoDataIterator</a>.
 
47
<p>The <a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_INIT">DB_MULTIPLE_INIT</a> macro is always called first.  It
 
48
initializes a local application variable and the <b>data</b>
 
49
<a href="../../api_c/dbt.html">DBT</a> for stepping through the set of returned records.  Then,
 
50
the application calls one of the remaining three macros:
 
51
<a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_NEXT">DB_MULTIPLE_NEXT</a>, <a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_KEY_NEXT">DB_MULTIPLE_KEY_NEXT</a>, and
 
52
<a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_RECNO_NEXT">DB_MULTIPLE_RECNO_NEXT</a>.
 
53
<p>If the <a href="../../api_c/dbc_get.html#DB_MULTIPLE">DB_MULTIPLE</a> flag was specified to the <a href="../../api_c/db_get.html">DB-&gt;get</a> or
 
54
<a href="../../api_c/dbc_get.html">DBcursor-&gt;c_get</a> function, the application will always call the
 
55
<a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_NEXT">DB_MULTIPLE_NEXT</a> macro.  If the <a href="../../api_c/dbc_get.html#DB_MULTIPLE_KEY">DB_MULTIPLE_KEY</a> flag
 
56
was specified to the <a href="../../api_c/db_get.html">DB-&gt;get</a> or <a href="../../api_c/dbc_get.html">DBcursor-&gt;c_get</a> function, and, the
 
57
underlying database is a Btree or Hash database, the application will
 
58
always call the <a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_KEY_NEXT">DB_MULTIPLE_KEY_NEXT</a> macro.  If the
 
59
<a href="../../api_c/dbc_get.html#DB_MULTIPLE_KEY">DB_MULTIPLE_KEY</a> flag was specified to the <a href="../../api_c/db_get.html">DB-&gt;get</a> or
 
60
<a href="../../api_c/dbc_get.html">DBcursor-&gt;c_get</a> function, and, the underlying database is a Queue or Recno
 
61
database, the application will always call the
 
62
<a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_RECNO_NEXT">DB_MULTIPLE_RECNO_NEXT</a> macro.  The <a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_NEXT">DB_MULTIPLE_NEXT</a>,
 
63
<a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_KEY_NEXT">DB_MULTIPLE_KEY_NEXT</a>, and <a href="../../api_c/dbt_bulk.html#DB_MULTIPLE_RECNO_NEXT">DB_MULTIPLE_RECNO_NEXT</a> macros
 
64
are called repeatedly, until the end of the returned records is reached.
 
65
The end of the returned records is detected by the application's local
 
66
pointer variable being set to NULL.
 
67
<p>The following is an example of a routine that displays the contents of
 
68
a Btree database using the bulk return interfaces.
 
69
<p><blockquote><pre>int
 
70
rec_display(dbp)
 
71
        DB *dbp;
 
72
{
 
73
        DBC *dbcp;
 
74
        DBT key, data;
 
75
        size_t retklen, retdlen;
 
76
        char *retkey, *retdata;
 
77
        int ret, t_ret;
 
78
        void *p;
 
79
<p>
 
80
        memset(&key, 0, sizeof(key));
 
81
        memset(&data, 0, sizeof(data));
 
82
<p>
 
83
        /* Review the database in 5MB chunks. */
 
84
#define BUFFER_LENGTH   (5 * 1024 * 1024)
 
85
        if ((data.data = malloc(BUFFER_LENGTH)) == NULL)
 
86
                return (errno);
 
87
        data.ulen = BUFFER_LENGTH;
 
88
        data.flags = DB_DBT_USERMEM;
 
89
<p>
 
90
        /* Acquire a cursor for the database. */
 
91
        if ((ret = dbp-&gt;cursor(dbp, NULL, &dbcp, 0)) != 0) {
 
92
                dbp-&gt;err(dbp, ret, "DB-&gt;cursor");
 
93
                free(data.data);
 
94
                return (ret);
 
95
        }
 
96
<p>
 
97
        for (;;) {
 
98
                /*
 
99
                 * Acquire the next set of key/data pairs.  This code does
 
100
                 * not handle single key/data pairs that won't fit in a
 
101
                 * BUFFER_LENGTH size buffer, instead returning ENOMEM to
 
102
                 * our caller.
 
103
                 */
 
104
                if ((ret = dbcp-&gt;c_get(dbcp,
 
105
                    &key, &data, DB_MULTIPLE_KEY | DB_NEXT)) != 0) {
 
106
                        if (ret != DB_NOTFOUND)
 
107
                                dbp-&gt;err(dbp, ret, "DBcursor-&gt;c_get");
 
108
                        break;
 
109
                }
 
110
<p>
 
111
                for (DB_MULTIPLE_INIT(p, &data);;) {
 
112
                        DB_MULTIPLE_KEY_NEXT(p,
 
113
                            &data, retkey, retklen, retdata, retdlen);
 
114
                        if (p == NULL)
 
115
                                break;
 
116
                        printf("key: %.*s, data: %.*s\n",
 
117
                            (int)retklen, retkey, (int)retdlen, retdata);
 
118
                }
 
119
        }
 
120
<p>
 
121
        if ((t_ret = dbcp-&gt;c_close(dbcp)) != 0) {
 
122
                dbp-&gt;err(dbp, ret, "DBcursor-&gt;close");
 
123
                if (ret == 0)
 
124
                        ret = t_ret;
 
125
        }
 
126
<p>
 
127
        free(data.data);
 
128
<p>
 
129
        return (ret);
 
130
}</pre></blockquote>
 
131
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/am_misc/align.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_misc/partial.html"><img src="../../images/next.gif" alt="Next"></a>
 
132
</td></tr></table>
 
133
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>
 
134
</body>
 
135
</html>