Sophie

Sophie

distrib > Mandriva > 2007.0 > x86_64 > media > main-release > by-pkgid > a4c98df40e78f6c892308fd6841f950a > files > 838

lib64db4.2-devel-4.2.52-11mdv2007.0.x86_64.rpm

<!--$Id: views.so,v 1.6 2003/11/17 19:13:35 bostic Exp $-->
<!--Copyright 1997-2003 by Sleepycat Software, Inc.-->
<!--All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Creating bindings and collections</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<a name="2"><!--meow--></a>
<table width="100%"><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Java API Tutorial - Basic</dl></h3></td>
<td align=right><a href="../bdb_basic/stores.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_basic/main.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Creating bindings and collections</h3>
<p><i>Bindings</i> 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.</p>
<p>Standard Java <i>collections</i> 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.</p>
<p>An important characteristic of stored collections is that they do
<i>not</i> 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.</p>
<hr size=1 noshade>
<p>The <b>SampleViews</b> class is used to create the bindings and
collections.  This class is separate from the <b>SampleDatabase</b> class to
illustrate the idea that a single set of stored data can be accessed via
multiple bindings and collections, or <i>views</i>.  The skeleton for the
<b>SampleViews</b> class follows.</p>
<blockquote><pre><b>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;
<p>
public class SampleViews
{
    private StoredMap partMap;
    private StoredMap supplierMap;
    private StoredMap shipmentMap;
<p>
    public SampleViews(SampleDatabase db)
    {
    }
}
</b></pre></blockquote>
<p>A 
<a href="../../java/com/sleepycat/bdb/collection/StoredMap.html">StoredMap</a>
 field is used
for each store.  The StoredMap class implements the standard Java
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html">Map</a>
 interface, which has methods for obtaining a
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Set.html">Set</a>
 of keys, a
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html">Collection</a>
 of values, or a
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Set.html">Set</a>
 of
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.Entry.html">Map.Entry</a>
 key/value pairs.
Because stores contain key/value pairs, any Berkeley DB store may be represented as a
Java map.</p>
<hr size=1 noshade>
<p>The following statements create the bindings using the
<a href="../../java/com/sleepycat/bdb/bind/serial/SerialBinding.html">SerialBinding</a>
 class.</p>
<blockquote><pre>
    public SampleViews(SampleDatabase db)
    {
<b>        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());
</b>    }
</pre></blockquote>
<p>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.</p>
<p>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.</p>
<hr size=1 noshade>
<p>The following statements create standard Java maps using the
<a href="../../java/com/sleepycat/bdb/collection/StoredMap.html">StoredMap</a>
 class.</p>
<blockquote><pre>
    public SampleViews(SampleDatabase db)
    {
<b>        partMap =
            new StoredMap(db.getPartStore(),
                          partKeyBinding, partValueBinding, true);
        supplierMap =
            new StoredMap(db.getSupplierStore(),
                          supplierKeyBinding, supplierValueBinding, true);
        shipmentMap =
            new StoredMap(db.getShipmentStore(),
                          shipmentKeyBinding, shipmentValueBinding, true);
</b>    }
</pre></blockquote>
<p>The first parameter of the 
<a href="../../java/com/sleepycat/bdb/collection/StoredMap.html">StoredMap</a>
 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.</p>
<p>The second and third parameters are the key and value bindings to use
when storing and retrieving objects via the map.</p>
<p>The fourth and last parameter specifies whether changes will be allowed
via the collection.  If false is passed, the collection will be read-only.</p>
<hr size=1 noshade>
<p>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.</p>
<blockquote><pre>
public class SampleViews
{
    ...
<b>    public final StoredMap getPartMap()
    {
        return partMap;
    }
<p>
    public final StoredMap getSupplierMap()
    {
        return supplierMap;
    }
<p>
    public final StoredMap getShipmentMap()
    {
        return shipmentMap;
    }
<p>
    public final StoredEntrySet getPartEntrySet()
    {
        return (StoredEntrySet) partMap.entrySet();
    }
<p>
    public final StoredEntrySet getSupplierEntrySet()
    {
        return (StoredEntrySet) supplierMap.entrySet();
    }
<p>
    public final StoredEntrySet getShipmentEntrySet()
    {
        return (StoredEntrySet) shipmentMap.entrySet();
    }
</b>    ...
}
</pre></blockquote>
<p>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.</p>
<p>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.</p>
<blockquote><pre>
<b>    SampleViews views = ...
    Map partMap = views.getPartMap();
    Set supplierEntries = views.getSupplierEntrySet();
</b></pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_basic/stores.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_basic/main.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1><a href="../../sleepycat/legal.html">Copyright (c) 1996-2003</a> <a href="http://www.sleepycat.com">Sleepycat Software, Inc.</a> - All rights reserved.</font>
</body>
</html>