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��10.��Secondary Databases</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="Getting Started with Berkeley DB" />
10    <link rel="up" href="baseapi.html" title="Part��II.��Programming with the Base API" />
11    <link rel="prev" href="cursorJavaUsage.html" title="Cursor Example" />
12    <link rel="next" href="keyCreator.html" title="Implementing Key Creators" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter��10.��Secondary Databases</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="cursorJavaUsage.html">Prev</a>��</td>
22          <th width="60%" align="center">Part��II.��Programming with the Base API</th>
23          <td width="20%" align="right">��<a accesskey="n" href="keyCreator.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="chapter" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title"><a id="indexes"></a>Chapter��10.��Secondary Databases</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <p>
38          <b>Table of Contents</b>
39        </p>
40        <dl>
41          <dt>
42            <span class="sect1">
43              <a href="indexes.html#DbAssociate">Opening and Closing Secondary Databases</a>
44            </span>
45          </dt>
46          <dt>
47            <span class="sect1">
48              <a href="keyCreator.html">Implementing Key 
49        <span>Creators</span>
50        
51        </a>
52            </span>
53          </dt>
54          <dd>
55            <dl>
56              <dt>
57                <span class="sect2">
58                  <a href="keyCreator.html#multikeys">Working with Multiple Keys</a>
59                </span>
60              </dt>
61            </dl>
62          </dd>
63          <dt>
64            <span class="sect1">
65              <a href="secondaryProps.html">Secondary Database Properties</a>
66            </span>
67          </dt>
68          <dt>
69            <span class="sect1">
70              <a href="readSecondary.html">Reading Secondary Databases</a>
71            </span>
72          </dt>
73          <dt>
74            <span class="sect1">
75              <a href="secondaryDelete.html">Deleting Secondary Database Records</a>
76            </span>
77          </dt>
78          <dt>
79            <span class="sect1">
80              <a href="secondaryCursor.html">
81        <span>Using Secondary Cursors</span>
82        
83    </a>
84            </span>
85          </dt>
86          <dt>
87            <span class="sect1">
88              <a href="joins.html">Database Joins</a>
89            </span>
90          </dt>
91          <dd>
92            <dl>
93              <dt>
94                <span class="sect2">
95                  <a href="joins.html#joinUsage">Using Join Cursors</a>
96                </span>
97              </dt>
98              <dt>
99                <span class="sect2">
100                  <a href="joins.html#joinconfig">JoinCursor Properties</a>
101                </span>
102              </dt>
103            </dl>
104          </dd>
105          <dt>
106            <span class="sect1">
107              <a href="javaindexusage.html">Secondary Database Example</a>
108            </span>
109          </dt>
110          <dd>
111            <dl>
112              <dt>
113                <span class="sect2">
114                  <a href="javaindexusage.html#secondaryMyDbs">Opening Secondary Databases with MyDbs</a>
115                </span>
116              </dt>
117              <dt>
118                <span class="sect2">
119                  <a href="javaindexusage.html#exampleReadJavaSecondaries">Using Secondary Databases with ExampleDatabaseRead</a>
120                </span>
121              </dt>
122            </dl>
123          </dd>
124        </dl>
125      </div>
126      <p>
127    Usually you find database records by means of the record's key.  However,
128    the key that you use for your record will not always contain the
129    information required to provide you with rapid access to the data that you
130    want to retrieve. For example, suppose your 
131        <code class="classname">Database</code>
132        
133    contains records related to users. The key might be a string that is some
134    unique identifier for the person, such as a user ID. Each record's data,
135    however, would likely contain a complex object containing details about
136    people such as names, addresses, phone numbers, and so forth.
137    While your application may frequently want to query a person by user
138    ID (that is, by the information stored in the key), it may also on occasion
139    want to locate people by, say, their name.
140  </p>
141      <p>
142    Rather than iterate through all of the records in your database, examining
143    each in turn for a given person's name, you create indexes based on names
144    and then just search that index for the name that you want.  You can do this
145    using secondary databases. In DB, the 
146        <code class="classname">Database</code>
147         
148    that contains your data is called a
149    <span class="emphasis"><em>primary database</em></span>. A database that provides an
150    alternative set of keys to access that data is called a <span class="emphasis"><em>secondary
151    database</em></span> In a secondary database, the keys are your alternative 
152    (or secondary) index, and the data corresponds to a primary record's key.
153  </p>
154      <p>
155    You create a secondary database by using a <code class="classname">SecondaryConfig</code>
156    class object to identify an implementation of a
157    <code class="classname">SecondaryKeyCreator</code>
158    class object that is used to create keys based on data found in the primary
159    database. You then pass this <code class="classname">SecondaryConfig</code>
160    object to the <code class="classname">SecondaryDatabase</code> constructor.
161  </p>
162      <p>
163  Once opened, DB manages secondary databases for you. Adding or deleting
164  records in your primary database causes DB to update the secondary as
165  necessary. Further, changing a record's data in the primary database may cause
166  DB to modify a record in the secondary, depending on whether the change
167  forces a modification of a key in the secondary database.
168  </p>
169      <p>
170    Note that you can not write directly to a secondary database. 
171
172    
173
174    
175    
176    To change the data referenced by a 
177        <code class="classname">SecondaryDatabase</code>
178        
179    record, modify the primary database instead. The exception to this rule is
180    that delete operations are allowed on the
181        <span><code class="classname">SecondaryDatabase</code> object.</span> 
182         
183    
184    See <a class="xref" href="secondaryDelete.html" title="Deleting Secondary Database Records">Deleting Secondary Database Records</a> for more
185    information.
186  </p>
187      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
188        <h3 class="title">Note</h3>
189        <p>
190
191        Secondary database records are updated/created by DB 
192        only if the 
193            <span><code class="methodname">SecondaryKeyCreator.createSecondaryKey()</code> method</span>
194            
195        returns 
196            <span><code class="literal">true</code>.</span> 
197             
198        If 
199            <code class="literal">false</code> 
200            
201        is returned, then DB will not add the key to the secondary database, and 
202        in the event of a record update it will remove any existing key. 
203
204        
205
206     </p>
207        <p>
208        See <a class="xref" href="keyCreator.html" title="Implementing Key Creators">Implementing Key 
209        <span>Creators</span>
210        
211        </a> for more
212            <span>information on this interface and method.</span>
213            
214    
215    </p>
216      </div>
217      <p>
218    When you read a record from a secondary database, DB automatically
219    returns 
220         
221        <span>the data and optionally the key</span> 
222    from the corresponding record in the primary database.
223    
224  </p>
225      <div class="sect1" lang="en" xml:lang="en">
226        <div class="titlepage">
227          <div>
228            <div>
229              <h2 class="title" style="clear: both"><a id="DbAssociate"></a>Opening and Closing Secondary Databases</h2>
230            </div>
231          </div>
232        </div>
233        <p>
234            You manage secondary database opens and closes using the
235            
236            <span>
237                <code class="classname">SecondaryDatabase</code> constructor.
238            </span>
239            Just as is the case with primary databases, you must provide
240             
241            <span>
242                the <code class="classname">SecondaryDatabase()</code> constructor
243            </span>
244            with the database's
245            name and, optionally, other properties such as whether duplicate
246            records are allowed, or whether the secondary database can be created on
247            open. In addition, you must also provide:
248        </p>
249        <div class="itemizedlist">
250          <ul type="disc">
251            <li>
252              <p>A handle to the primary database that this secondary database is
253        indexing. Note that this means that secondary databases are maintained
254        only for the specified <code class="classname">Database</code> handle. If you
255        open the same <code class="classname">Database</code> multiple times for write
256        (such as might occur when opening a database for read-only and read-write in the same application),
257        then you should open the <code class="classname">SecondaryDatabase</code> for
258        each such <code class="classname">Database</code> handle.</p>
259            </li>
260            <li>
261              <p>A <code class="classname">SecondaryConfig</code> object that provides
262        properties specific to a secondary database. The most important of
263        these is used to identify the key creator for the database. The key
264        creator is responsible for generating keys for the secondary database.
265        See <a class="xref" href="secondaryProps.html" title="Secondary Database Properties">Secondary Database Properties</a>  for details.</p>
266            </li>
267          </ul>
268        </div>
269        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
270          <h3 class="title">Note</h3>
271          <p>
272        Primary databases <span class="emphasis"><em>must not</em></span> support duplicate records. 
273        Secondary records point to primary records using the primary key, so that key must be unique.
274        </p>
275        </div>
276        <p>So to open (create) a secondary database, you:</p>
277        <div class="orderedlist">
278          <ol type="1">
279            <li>
280              <p>Open your primary database.</p>
281            </li>
282            <li>
283              <p>Instantiate your key creator.</p>
284            </li>
285            <li>
286              <p>Instantiate your <code class="classname">SecondaryConfig</code> object.</p>
287            </li>
288            <li>
289              <p>Set your key creator object on your <code class="classname">SecondaryConfig</code>
290        object.</p>
291            </li>
292            <li>
293              <p>Open your secondary database, specifying your primary database
294        and your <code class="classname">SecondaryConfig</code> at that time.</p>
295            </li>
296          </ol>
297        </div>
298        <p>For example:</p>
299        <a id="java_index1"></a>
300        <pre class="programlisting">package db.GettingStarted;
301
302import com.sleepycat.bind.tuple.TupleBinding;
303import com.sleepycat.db.Database;
304import com.sleepycat.db.DatabaseType;
305import com.sleepycat.db.DatabaseConfig;
306import com.sleepycat.db.DatabaseException;
307import com.sleepycat.db.SecondaryDatabase;
308import com.sleepycat.db.SecondaryConfig;
309
310import java.io.FileNotFoundException;
311
312...
313
314DatabaseConfig myDbConfig = new DatabaseConfig();
315myDbConfig.setAllowCreate(true);
316myDbConfig.setType(DatabaseType.BTREE);
317
318SecondaryConfig mySecConfig = new SecondaryConfig();
319mySecConfig.setAllowCreate(true);
320mySecConfig.setType(DatabaseType.BTREE);
321// Duplicates are frequently required for secondary databases.
322mySecConfig.setSortedDuplicates(true);
323
324// Open the primary
325Database myDb = null;
326SecondaryDatabase mySecDb = null;
327try {
328    String dbName = "myPrimaryDatabase";
329
330    myDb = new Database(dbName, null, myDbConfig);
331
332    // A fake tuple binding that is not actually implemented anywhere.
333    // The tuple binding is dependent on the data in use.
334    // Tuple bindings are described earlier in this manual.
335    TupleBinding myTupleBinding = new MyTupleBinding();
336
337    // Open the secondary.
338    // Key creators are described in the next section.
339    FullNameKeyCreator keyCreator = new FullNameKeyCreator(myTupleBinding);
340
341    // Get a secondary object and set the key creator on it.
342    mySecConfig.setKeyCreator(keyCreator);
343
344    // Perform the actual open
345    String secDbName = "mySecondaryDatabase";
346    mySecDb = new SecondaryDatabase(secDbName, null, myDb, mySecConfig); 
347} catch (DatabaseException de) {
348    // Exception handling goes here ...
349} catch (FileNotFoundException fnfe) {
350    // Exception handling goes here ...
351}</pre>
352        <p>To close a secondary database, call its close() method. Note that
353    for best results, you should close all the secondary databases associated
354    with a primary database before closing the primary.</p>
355        <p>For example:</p>
356        <a id="java_index2"></a>
357        <pre class="programlisting">try {
358    if (mySecDb != null) {
359        mySecDb.close();
360    }
361
362    if (myDb != null) {
363        myDb.close(); 
364    }
365} catch (DatabaseException dbe) {
366    // Exception handling goes here
367}</pre>
368      </div>
369    </div>
370    <div class="navfooter">
371      <hr />
372      <table width="100%" summary="Navigation footer">
373        <tr>
374          <td width="40%" align="left"><a accesskey="p" href="cursorJavaUsage.html">Prev</a>��</td>
375          <td width="20%" align="center">
376            <a accesskey="u" href="baseapi.html">Up</a>
377          </td>
378          <td width="40%" align="right">��<a accesskey="n" href="keyCreator.html">Next</a></td>
379        </tr>
380        <tr>
381          <td width="40%" align="left" valign="top">Cursor Example��</td>
382          <td width="20%" align="center">
383            <a accesskey="h" href="index.html">Home</a>
384          </td>
385          <td width="40%" align="right" valign="top">��Implementing Key 
386        <span>Creators</span>
387        
388        </td>
389        </tr>
390      </table>
391    </div>
392  </body>
393</html>
394