Debian Codegraph @

docs/programmer_reference/am_second.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>Secondary indexes</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.html" title="Chapter 3.  Access Method Operations" />
11 <link rel="prev" href="am_close.html" title="Database close" />
12 <link rel="next" href="am_foreign.html" title="Foreign key indexes" />
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">Secondary indexes</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="am_close.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 3. 
26 Access Method Operations
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="am_foreign.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="am_second"></a>Secondary indexes</h2>
38 </div>
39 </div>
40 </div>
41 <div class="toc">
42 <dl>
43 <dt>
44 <span class="sect2">
45 <a href="am_second.html#idp51040080">Error Handling With Secondary Indexes</a>
46 </span>
47 </dt>
48 </dl>
49 </div>
50 <p>A secondary index, put simply, is a way to efficiently access records
51 in a database (the primary) by means of some piece of information other
52 than the usual (primary) key. In Berkeley DB, this index is simply another
53 database whose keys are these pieces of information (the secondary
54 keys), and whose data are the primary keys. Secondary indexes can be
55 created manually by the application; there is no disadvantage, other
56 than complexity, to doing so. However, when the secondary key can be
57 mechanically derived from the primary key and datum that it points to,
58 as is frequently the case, Berkeley DB can automatically and transparently
59 manage secondary indexes.</p>
60 <p>As an example of how secondary indexes might be used, consider a
61 database containing a list of students at a college, each of whom has
62 a unique student ID number. A typical database would use the student
63 ID number as the key; however, one might also reasonably want to be
64 able to look up students by last name. To do this, one would construct
65 a secondary index in which the secondary key was this last name.</p>
66 <p>In SQL, this would be done by executing something like the following:</p>
67 <pre class="programlisting">CREATE TABLE students(student_id CHAR(4) NOT NULL,
68 lastname CHAR(15), firstname CHAR(15), PRIMARY KEY(student_id));
69 CREATE INDEX lname ON students(lastname);</pre>
70 <p>In Berkeley DB, this would work as follows (a
71 <a class="ulink" href="second.javas" target="_top">Java API example is also available</a>):</p>
72 <a id="prog_am13"></a>
73 <pre class="programlisting">struct student_record {
74 char student_id[4];
75 char last_name[15];
76 char first_name[15];
77 };
78
79 ....
80
81 void
82 second()
83 {
84 DB *dbp, *sdbp;
85 int ret;
86
87 /* Open/create primary */
88 if ((ret = db_create(&amp;dbp, dbenv, 0)) != 0)
89 handle_error(ret);
90 if ((ret = dbp-&gt;open(dbp, NULL,
91 "students.db", NULL, DB_BTREE, DB_CREATE, 0600)) != 0)
92 handle_error(ret);
93
94 /*
95 * Open/create secondary. Note that it supports duplicate data
96 * items, since last names might not be unique.
97 */
98 if ((ret = db_create(&amp;sdbp, dbenv, 0)) != 0)
99 handle_error(ret);
100 if ((ret = sdbp-&gt;set_flags(sdbp, DB_DUP | DB_DUPSORT)) != 0)
101 handle_error(ret);
102 if ((ret = sdbp-&gt;open(sdbp, NULL,
103 "lastname.db", NULL, DB_BTREE, DB_CREATE, 0600)) != 0)
104 handle_error(ret);
105
106 /* Associate the secondary with the primary. */
107 if ((ret = dbp-&gt;associate(dbp, NULL, sdbp, getname, 0)) != 0)
108 handle_error(ret);
109 }
110
111 /*
112 * getname -- extracts a secondary key (the last name) from a primary
113 * key/data pair
114 */
115 int
116 getname(DB *secondary, const DBT *pkey, const DBT *pdata, DBT *skey)
117
118 {
119 /*
120 * Since the secondary key is a simple structure member of the
121 * record, we don't have to do anything fancy to return it. If
122 * we have composite keys that need to be constructed from the
123 * record, rather than simply pointing into it, then the user's
124 * function might need to allocate space and copy data. In
125 * this case, the DB_DBT_APPMALLOC flag should be set in the
126 * secondary key DBT.
127 */
128 memset(skey, 0, sizeof(DBT));
129 skey-&gt;data = ((struct student_record *)pdata-&gt;data)-&gt;last_name;
130 skey-&gt;size = sizeof(((struct student_record *)pdata-&gt;data)-&gt;last_name);
131 return (0);
132 }</pre>
133 <p>From the application's perspective, putting things into the database
134 works exactly as it does without a secondary index; one can simply
135 insert records into the primary database. In SQL one would do the
136 following:</p>
137 <pre class="programlisting">INSERT INTO student
138 VALUES ("WC42", "Churchill ", "Winston ");</pre>
139 <p>and in Berkeley DB, one does:</p>
140 <a id="prog_am14"></a>
141 <pre class="programlisting">struct student_record s;
142 DBT data, key;
143
144 memset(&amp;key, 0, sizeof(DBT));
145 memset(&amp;data, 0, sizeof(DBT));
146 memset(&amp;s, 0, sizeof(struct student_record));
147 key.data = "WC42";
148 key.size = 4;
149 memcpy(&amp;s.student_id, "WC42", sizeof(s.student_id));
150 memcpy(&amp;s.last_name, "Churchill ", sizeof(s.last_name));
151 memcpy(&amp;s.first_name, "Winston ", sizeof(s.first_name));
152 data.data = &amp;s;
153 data.size = sizeof(s);
154 if ((ret = dbp-&gt;put(dbp, txn, &amp;key, &amp;data, 0)) != 0)
155 handle_error(ret);
156 </pre>
157 <p>Internally, a record with secondary key "Churchill" is inserted into
158 the secondary database (in addition to the insertion of "WC42" into the
159 primary, of course).</p>
160 <p>Deletes are similar. The SQL clause:</p>
161 <pre class="programlisting">DELETE FROM student WHERE (student_id = "WC42");</pre>
162 <p>looks like:</p>
163 <a id="prog_am15"></a>
164 <pre class="programlisting">DBT key;
165
166 memset(&amp;key, 0, sizeof(DBT));
167 key.data = "WC42";
168 key.size = 4;
169 if ((ret = dbp-&gt;del(dbp, txn, &amp;key, 0)) != 0)
170 handle_error(ret);</pre>
171 <p>Deletes can also be performed on the secondary index directly; a delete
172 done this way will delete the "real" record in the primary as well. If
173 the secondary supports duplicates and there are duplicate occurrences of
174 the secondary key, then all records with that secondary key are removed
175 from both the secondary index and the primary database. In
176 SQL:</p>
177 <pre class="programlisting">DELETE FROM lname WHERE (lastname = "Churchill ");</pre>
178 <p>In Berkeley DB:</p>
179 <a id="prog_am16"></a>
180 <pre class="programlisting">DBT skey;
181
182 memset(&amp;skey, 0, sizeof(DBT));
183 skey.data = "Churchill ";
184 skey.size = 15;
185 if ((ret = sdbp-&gt;del(sdbp, txn, &amp;skey, 0)) != 0)
186 handle_error(ret);</pre>
187 <p>Gets on a secondary automatically return the primary datum. If
188 <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;pget()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;pget()</a> is used in lieu of <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a>, the primary key is returned as well. Thus, the
189 equivalent of:</p>
190 <pre class="programlisting">SELECT * from lname WHERE (lastname = "Churchill ");</pre>
191 <p>would be:</p>
192 <a id="prog_am17"></a>
193 <pre class="programlisting">DBT data, pkey, skey;
194
195 memset(&amp;skey, 0, sizeof(DBT));
196 memset(&amp;pkey, 0, sizeof(DBT));
197 memset(&amp;data, 0, sizeof(DBT));
198 skey.data = "Churchill ";
199 skey.size = 15;
200 if ((ret = sdbp-&gt;pget(sdbp, txn, &amp;skey, &amp;pkey, &amp;data, 0)) != 0)
201 handle_error(ret);
202 /*
203 * Now pkey contains "WC42" and data contains Winston's record.
204 */</pre>
205 <p>To create a secondary index to a Berkeley DB database, open the database that
206 is to become a secondary index normally, then pass it as the "secondary"
207 argument to the <a href="../api_reference/C/dbassociate.html" class="olink">DB-&gt;associate()</a> method for some primary database.</p>
208 <p>After a <a href="../api_reference/C/dbassociate.html" class="olink">DB-&gt;associate()</a> call is made, the secondary indexes become
209 alternate interfaces to the primary database. All updates to the
210 primary will be automatically reflected in each secondary index that has
211 been associated with it. All get operations using the <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;get()</a>
212 or <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a> methods on the secondary index return the primary datum
213 associated with the specified (or otherwise current, in the case of
214 cursor operations) secondary key. The <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;pget()</a> and
215 <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;pget()</a> methods also become usable; these behave just like
216 <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>, but return the primary key in
217 addition to the primary datum, for those applications that need it as
218 well.</p>
219 <p>Cursor get operations on a secondary index perform as expected; although
220 the data returned will by default be those of the primary database, a
221 position in the secondary index is maintained normally, and records will
222 appear in the order determined by the secondary key and the comparison
223 function or other structure of the secondary database.</p>
224 <p>Delete operations on a secondary index delete the item from the primary
225 database and all relevant secondaries, including the current one.</p>
226 <p>Put operations of any kind are forbidden on secondary indexes, as there
227 is no way to specify a primary key for a newly put item. Instead, the
228 application should use the <a href="../api_reference/C/dbput.html" class="olink">DB-&gt;put()</a> or <a href="../api_reference/C/dbcput.html" class="olink">DBC-&gt;put()</a> methods
229 on the primary database.</p>
230 <p>Any number of secondary indexes may be associated with a given primary
231 database, up to limitations on available memory and the number of open
232 file descriptors.</p>
233 <p>Note that although Berkeley DB guarantees that updates made using any
234 <a href="../api_reference/C/db.html" class="olink">DB</a> handle with an associated secondary will be reflected in the
235 that secondary, associating each primary handle with all the appropriate
236 secondaries is the responsibility of the application and is not enforced
237 by Berkeley DB. It is generally unsafe, but not forbidden by Berkeley DB, to modify
238 a database that has secondary indexes without having those indexes open
239 and associated. Similarly, it is generally unsafe, but not forbidden,
240 to modify a secondary index directly. Applications that violate these
241 rules face the possibility of outdated or incorrect results if the
242 secondary indexes are later used.</p>
243 <p>If a secondary index becomes outdated for any reason, it should be
244 discarded using the <a href="../api_reference/C/dbremove.html" class="olink">DB-&gt;remove()</a> method and a new one created
245 using the <a href="../api_reference/C/dbassociate.html" class="olink">DB-&gt;associate()</a> method. If a secondary index is no
246 longer needed, all of its handles should be closed using the
247 <a href="../api_reference/C/dbclose.html" class="olink">DB-&gt;close()</a> method, and then the database should be removed using
248 a new database handle and the <a href="../api_reference/C/dbremove.html" class="olink">DB-&gt;remove()</a> method.</p>
249 <p>Closing a primary database handle automatically dis-associates all
250 secondary database handles associated with it.</p>
251 <div class="sect2" lang="en" xml:lang="en">
252 <div class="titlepage">
253 <div>
254 <div>
255 <h3 class="title"><a id="idp51040080"></a>Error Handling With Secondary Indexes</h3>
256 </div>
257 </div>
258 </div>
259 <p>An error return during a secondary update in CDS or DS (which requires an abort in TDS) may leave a secondary index inconsistent in CDS or DS. There are a few non-error returns:
260 </p>
261 <div class="itemizedlist">
262 <ul type="disc">
263 <li>0
264 </li>
265 <li>DB_BUFFER_SMALL
266 </li>
267 <li>DB_NOTFOUND
268 </li>
269 <li>DB_KEYEMPTY
270 </li>
271 <li>
272 DB_KEYEXIST
273 </li>
274 </ul>
275 </div>
276 <p>
277 In the case of any other error return during a secondary update in CDS or DS, delete the secondary indices, recreate them and set the <code class="literal">DB_CREATE flag</code> to the <code class="literal">DB-&gt;associate</code> method. Some examples of error returns that need to be handled this way are:</p>
278 <div class="itemizedlist">
279 <ul type="disc">
280 <li>ENOMEM - indicating there is insufficient memory to return the requested item</li>
281 <li>EINVAL - indicating that an invalid flag value or parameter is specified</li>
282 </ul>
283 </div>
284 <p>Note that <code class="literal">DB_RUNRECOVERY</code> and <code class="literal">DB_PAGE_NOTFOUND</code> are fatal errors which should never occur during normal use of CDS or DS. If those errors are returned by Berkeley DB when running without transactions, check the database integrity with the <code class="literal">DB-&gt;verify</code> method before rebuilding the secondary indices. </p>
285 </div>
286 </div>
287 <div class="navfooter">
288 <hr />
289 <table width="100%" summary="Navigation footer">
290 <tr>
291 <td width="40%" align="left"><a accesskey="p" href="am_close.html">Prev</a> </td>
292 <td width="20%" align="center">
293 <a accesskey="u" href="am.html">Up</a>
294 </td>
295 <td width="40%" align="right"> <a accesskey="n" href="am_foreign.html">Next</a></td>
296 </tr>
297 <tr>
298 <td width="40%" align="left" valign="top">Database close </td>
299 <td width="20%" align="center">
300 <a accesskey="h" href="index.html">Home</a>
301 </td>
302 <td width="40%" align="right" valign="top"> Foreign key indexes</td>
303 </tr>
304 </table>
305 </div>
306 </body>
307 </html>