Berkeley DB Reference Guide:
Java API Tutorial - Basic

PrevRefNext

Creating bindings and collections

Bindings convert between stored records and Java objects. In this example, Java serialization bindings are used. Serial bindings are the simplest type of bindings because no mapping of fields or type conversion is needed. Tuple bindings, which are more difficult to create than serial bindings but have some advantages, will be introduced later in the Tuple example program.

Standard Java collections are used to access records in a database store. Stored collections use bindings transparently to convert the records to objects when they are retrieved from the collection, and to convert the objects to records when they are stored in the collection.

An important characteristic of stored collections is that they do not perform object caching. Every time an object is accessed via a collection it will be added to or retrieved from the store, and the bindings will be invoked to convert the data. Objects are therefore always passed and returned by value, not by reference. Because Berkeley DB is an embedded database, efficient caching of stored records is performed by the database library.


The SampleViews class is used to create the bindings and collections. This class is separate from the SampleDatabase class to illustrate the idea that a single set of stored data can be accessed via multiple bindings and collections, or views. The skeleton for the SampleViews class follows.

import com.sleepycat.bdb.bind.DataBinding;
import com.sleepycat.bdb.bind.serial.SerialBinding;
import com.sleepycat.bdb.collection.StoredEntrySet;
import com.sleepycat.bdb.collection.StoredMap;

public class SampleViews { private StoredMap partMap; private StoredMap supplierMap; private StoredMap shipmentMap;

public SampleViews(SampleDatabase db) { } }

A StoredMap field is used for each store. The StoredMap class implements the standard Java Map interface, which has methods for obtaining a Set of keys, a Collection of values, or a Set of Map.Entry key/value pairs. Because stores contain key/value pairs, any Berkeley DB store may be represented as a Java map.


The following statements create the bindings using the SerialBinding class.

    public SampleViews(SampleDatabase db)
    {
        DataBinding partKeyBinding = new SerialBinding(db.getPartKeyFormat());
        DataBinding partValueBinding = new SerialBinding(db.getPartValueFormat());
        DataBinding supplierKeyBinding = new SerialBinding(db.getSupplierKeyFormat());
        DataBinding supplierValueBinding = new SerialBinding(db.getSupplierValueFormat());
        DataBinding shipmentKeyBinding = new SerialBinding(db.getShipmentKeyFormat());
        DataBinding shipmentValueBinding = new SerialBinding(db.getShipmentValueFormat());
    }

A serial binding is created from a serial format. The reader may wonder why a binding is needed at all, since the format contains all the information needed to perform Java serialization. In fact, the serial bindings used here add very little value to the serial format.

Bindings are distinct from formats because a single format can be used with multiple bindings. With serial bindings, an example is a specialized binding that extracts a single field from the stored object and returns that field only. Another example is a binding that converts the stored object to an instance of a different class. These are examples of how bindings can provide multiple views of the same data.


The following statements create standard Java maps using the StoredMap class.

    public SampleViews(SampleDatabase db)
    {
        partMap =
            new StoredMap(db.getPartStore(),
                          partKeyBinding, partValueBinding, true);
        supplierMap =
            new StoredMap(db.getSupplierStore(),
                          supplierKeyBinding, supplierValueBinding, true);
        shipmentMap =
            new StoredMap(db.getShipmentStore(),
                          shipmentKeyBinding, shipmentValueBinding, true);
    }

The first parameter of the StoredMap constructor is the store. Creating a map from a store will use the store keys (the primary keys) as the map keys. The Index example shows how to use index keys as map keys.

The second and third parameters are the key and value bindings to use when storing and retrieving objects via the map.

The fourth and last parameter specifies whether changes will be allowed via the collection. If false is passed, the collection will be read-only.


The following getter methods return the stored maps for use by other classes in the example program. Convenience methods for returning entry sets are also included.

public class SampleViews
{
    ...
    public final StoredMap getPartMap()
    {
        return partMap;
    }

public final StoredMap getSupplierMap() { return supplierMap; }

public final StoredMap getShipmentMap() { return shipmentMap; }

public final StoredEntrySet getPartEntrySet() { return (StoredEntrySet) partMap.entrySet(); }

public final StoredEntrySet getSupplierEntrySet() { return (StoredEntrySet) supplierMap.entrySet(); }

public final StoredEntrySet getShipmentEntrySet() { return (StoredEntrySet) shipmentMap.entrySet(); } ... }

Note that StoredMap and StoredEntrySet are returned rather than just returning Map and Set. Since StoredMap implements the Map interface and StoredEntrySet implements the Set interface, you may ask why Map and Set were not returned directly.

StoredMap, StoredEntrySet, and other stored collection classes have a small number of extra methods beyond those in the Java collection interfaces. The stored collection types are therefore returned to avoid casting when using the extended methods. Normally, however, only a Map or Set is needed, and may be used as follows.

    SampleViews views = ...
    Map partMap = views.getPartMap();
    Set supplierEntries = views.getSupplierEntrySet();

PrevRefNext

Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.