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