Reading and Writing Database Records

JE provides two basic mechanisms for the storage and retrieval of database key/data pairs:

Writing Records to the Database

Database records are stored in the internal BTree based on whatever sorting routine is available to the database. Records are sorted first by their key. If the database supports duplicate records, then the records for a specific key are sorted by their data.

By default, JE sorts both keys and duplicate data using byte-by-byte lexicographic comparisons. This default comparison works well for the majority of cases. However, in some case performance benefits can be realized by overriding the default comparison routine. See Using Comparators for more information.

You can use the following methods to put database records:

  • Database.put()

    Puts a database record into the database. If your database does not support duplicate records, and if the provided key already exists in the database, then the currently existing record is replaced with the new data.

  • Database.putNoDupData()

    Puts a database record into the database. If the provided key already exists in the database, then this method returns OperationStatus.KEYEXIST.

  • Database.putNoOverwrite()

    Puts a database record into the database. If the provided key already exists in the database, and if the database does not support duplicate data, then this method returns OperationStatus.KEYEXIST.

When you put database records, you provide both the key and the data as DatabaseEntry objects. This means you must convert your key and data into a Java byte array. For example:

import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Database;

...

// Environment and database opens omitted for clarity.
// Environment and database must NOT be opened read-only.

String aKey = "myFirstKey";
String aData = "myFirstData";

try {
    DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
    DatabaseEntry theData = new DatabaseEntry(aData.getBytes("UTF-8"));
    myDatabase.put(null, theKey, the Data);
} catch (Exception e) {
    // Exception handling goes here
} 

Getting Records from the Database

The Database class provides several methods that you can use to retrieve database records. Note that if your database supports duplicate records, then these methods will only ever return the first record in a duplicate set. For this reason, if your database supports duplicates, you should use a cursor to retrieve records from it. Cursors are described in Using Cursors.

You can use either of the following methods to retrieve records from the database:

  • Database.get()

    Retrieves the record whose key matches the key provided to the method. If no records exists that uses the provided key, then OperationStatus.NOTFOUND is returned.

  • Database.getSearchBoth()

    Retrieve the record whose key matches both the key and the data provided to the method. If no record exists that uses the provided key and data, then OperationStatus.NOTFOUND is returned.

Both the key and data for a database record are returned as DatabaseEntry objects. These objects are passed as parameter values to the Database.get() method.

In order to retrieve your data once Database.get() has completed, you must retrieve the byte array stored in the DatabaseEntry and then convert that byte array back to the appropriate datatype. For example:

import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Database;
import com.sleepycat.je.LockMode;

...

// Environment and database opens omitted for clarity.
// Environment and database may be opened read-only.  
  
String aKey = "myFirstKey";

try {
    // Create a pair of DatabaseEntry objects. theKey
    // is used to perform the search. theData is used
    // to store the data returned by the get() operation.
    DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
    DatabaseEntry theData = new DatabaseEntry();
    
    // Perform the get.
    if (myDatabase.get(null, theKey, theData, LockMode.DEFAULT) ==
        OperationStatus.SUCCESS) {

        // Recreate the data String.
        byte[] retData = theData.getData();
        String foundData = new String(retData);
        System.out.println("For key: '" + aKey + "' found data: '" + 
                            foundData + "'.");
    } else {
        System.out.println("No record found for key '" + aKey + "'.");
    } 
} catch (Exception e) {
    // Exception handling goes here
}

Deleting Records

You can use the Database.delete() method to delete a record from the database. If your database supports duplicate records, then all records associated with the provided key are deleted. To delete just one record from a list of duplicates, use a cursor. Cursors are described in Using Cursors.

You can also delete every record in the database by using Database.truncate().

For example:

import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Database;

...

// Environment and database opens omitted for clarity.
// Environment and database can NOT be opened read-only.  
  
try {
    String aKey = "myFirstKey";
    DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
    
    // Perform the deletion. All records that use this key are
    // deleted.
    myDatabase.delete(null, theKey); 
} catch (Exception e) {
    // Exception handling goes here
}

Data Persistence

When you perform a database modification, your modification is made using the in-memory cache. This means that your data modifications are not necessarily written to disk, and so your data may not appear in the database after an application restart.

Therefore, if you care about whether your data persists across application runs, then you should use transactions to protect your database modifications. Every time you commit a transaction, the data modified as a part of that transaction is written to disk. Transaction usage is described in Transactions.

If you do not want to use transactions, then the assumption is that your data is of a nature that it need not exist the next time your application starts. You may want this if, for example, you are using JE to cache data relevant only to the current application runtime.

If, however, you are not using transactions for some reason and you still want some guarantee that your database modifications are persistent, then you should periodically run environment syncs. Syncs cause the entire contents of your in-memory cache to be written to disk. As such, they are quite expensive and you should use them sparingly.

You run a sync by calling the Environment.sync() method. For applications that use them, syncs are at a minimum performed immediately before the environment is closed. This close should occur in the appropriate finally block. See Opening Database Environments for an example of this.

For a brief description of how JE manages its data in the cache and in the log files, and how sync works, see Databases and Log Files.