M a k e r s of B e r k e l e y DB
Getting Started
with Berkeley DB
for Java
.
Legal Notice
This documentation is distributed under the terms of the Sleepycat public license. You may review the terms
of this license at: http://www.sleepycat.com/download/oslicense.html
Sleepycat Software, Berkeley DB, Berkeley DB XML and the Sleepycat logo are trademarks or service marks of
Sleepycat Software, Inc. All rights to these marks are reserved. No third-party use is permitted without the
express prior written consent of Sleepycat Software, Inc.
Java™ and all Java-based marks are a trademark or registered trademark of Sun Microsystems, Inc, in the United
States and other countries.
To obtain a copy of this document's original source code, please write to
<support@sleepycat.com>
.
Published 9/22/2004
TableofContents
Preface .............................................................................................. iv
Conventions Used in this Book ............................................................. iv
1. Introduction to Berkeley DB .................................................................. 1
About This Manual ........................................................................... 2
Berkeley DB Concepts ....................................................................... 2
Access Methods ............................................................................... 4
Selecting Access Methods ............................................................ 4
Choosing between BTree and Hash ................................................. 5
Choosing between Queue and Recno ............................................... 5
Database Limits and Portability ............................................................ 6
Environments ................................................................................. 6
Exception Handling .......................................................................... 7
Error Returns .................................................................................. 8
Getting and Using DB ....................................................................... 8
2. Databases ......................................................................................... 9
Opening Databases ........................................................................... 9
Closing Databases ........................................................................... 10
Database Properties ........................................................................ 11
Administrative Methods .................................................................... 12
Error Reporting Functions ................................................................. 13
Managing Databases in Environments .................................................... 14
Database Example .......................................................................... 16
3. Database Records .............................................................................. 19
Using Database Records .................................................................... 19
Reading and Writing Database Records ................................................. 20
Writing Records to the Database .................................................. 21
Getting Records from the Database ............................................... 22
Deleting Records ..................................................................... 23
Data Persistence ..................................................................... 24
Using the BIND APIs ......................................................................... 25
Numerical and String Objects ...................................................... 25
Serializeable Complex Objects ..................................................... 27
Usage Caveats .................................................................. 28
Serializing Objects ............................................................. 28
Deserializing Objects .......................................................... 31
Custom Tuple Bindings .............................................................. 32
Database Usage Example .................................................................. 35
4. Using Cursors ................................................................................... 47
Opening and Closing Cursors .............................................................. 47
Getting Records Using the Cursor ........................................................ 48
Searching for Records ............................................................... 50
Working with Duplicate Records ................................................... 53
Putting Records Using Cursors ............................................................ 55
Deleting Records Using Cursors ........................................................... 57
Replacing Records Using Cursors ......................................................... 58
Cursor Example ............................................................................. 59
Page iiGetting Started with DB9/22/2004
5. Secondary Databases .......................................................................... 64
Opening and Closing Secondary Databases ............................................. 64
Implementing Key Creators .............................................................. 67
Secondary Database Properties ........................................................... 70
Reading Secondary Databases ............................................................ 70
Deleting Secondary Database Records ................................................... 71
Using Secondary Cursors .................................................................. 72
Database Joins .............................................................................. 73
Using Join Cursors .................................................................... 74
JoinCursor Properties ................................................................ 77
Secondary Database Example ............................................................. 77
Opening Secondary Databases with MyDbs ....................................... 79
Using Secondary Databases with ExampleDatabaseRead ....................... 82
6. Database Configuration ....................................................................... 86
Setting the Page Size ....................................................................... 86
Overflow Pages ....................................................................... 86
Locking ................................................................................. 87
IO Efficiency .......................................................................... 88
Page Sizing Advice ................................................................... 88
Selecting the Cache Size .................................................................. 89
BTree Configuration ........................................................................ 89
Allowing Duplicate Records ......................................................... 90
Sorted Duplicates .............................................................. 90
Unsorted Duplicates ........................................................... 90
Configuring a Database to Support Duplicates ............................ 91
Setting Comparison Functions ...................................................... 92
Creating Java Comparators .................................................. 93
Page iiiGetting Started with DB9/22/2004
Preface
Welcome to Berkeley DB (DB). This document introduces DB, version 4.3. It is intended
to provide a rapid introduction to the DB API set and related concepts. The goal of this
document is to provide you with an efficient mechanism with which you can evaluate DB
against your project's technical requirements. As such, this document is intended for Java
developers and senior software architects who are looking for an in-process data
management solution. No prior experience with Sleepycat technologies is expected or
required.
ConventionsUsedinthisBook
The following typographical conventions are used within in this manual:
Class names are represented in
monospaced font
, as are
method names
. For example: "The
Database()
constructor returns a
Database
class object."
Variable or non-literal text is presented in italics. For example: "Go to your DB_INSTALL
directory."
Program examples are displayed in a
monospaced font
on a shaded background. For
example:
import com.sleepycat.db.DatabaseConfig;
...
// Allow the database to be created.
DatabaseConfig myDbConfig = new DatabaseConfig();
myDbConfig.setAllowCreate(true);
In some situations, programming examples are updated from one chapter to the next.
When this occurs, the new code is presented in
monospaced bold
font. For example:
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
...
// Allow the database to be created.
DatabaseConfig myDbConfig = new DatabaseConfig();
myDbConfig.setAllowCreate(true);
Database myDb = new Database("mydb.db", null, myDbConfig);
Finally, notes of interest are represented using a note block such as this.
☞
Page ivGetting Started with DB9/22/2004
评论0