Sophie

Sophie

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

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

<!--$Id: views.so,v 1.3 2003/09/25 15:27:52 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 indexed 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 - Index</dl></h3></td>
<td align=right><a href="../bdb_index/foreign.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_index/read.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Creating indexed collections</h3>
<p>In the prior Basic example, bindings and Java collections were created
for accessing stores via their primary keys.  In this example, bindings and
collections are added for accessing the same stores via their index keys.  As
in the prior example, serial bindings and the Java 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html">Map</a>
 class are used.</p>
<p>When a map is created from a 
<a href="../../java/com/sleepycat/bdb/DataIndex.html">DataIndex</a>
or a 
<a href="../../java/com/sleepycat/bdb/ForeignKeyIndex.html">ForeignKeyIndex</a>
, the keys of the map
will be the index keys.  However, the values of the map will be the values of
the store associated with the index.  This is how index keys can be used to
access the values in a store.</p>
<p>For example, the Supplier's City field is an index key that can be used
to access the Supplier store.  When a map is created using the
<b>supplierByCityIndex</b>, the key to the map will be the City field, a
<a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html">String</a>
 object.  When 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#get">Map.get</a>
 is called passing the City as the key parameter, a <b>SupplierValue</b>
object will be returned.</p>
<hr size=1 noshade>
<p>The <b>SampleViews</b> class is extended to create an index key
binding for the Supplier's City field and three Java maps based on the three
indices created in the prior section.</p>
<blockquote><pre>
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
{
    ...
<b>    private StoredMap supplierByCityMap;
    private StoredMap shipmentByPartMap;
    private StoredMap shipmentBySupplierMap;
</b><p>
    public SampleViews(SampleDatabase db)
    {
<b>        ...
        DataBinding cityKeyBinding =
            new SerialBinding(db.getCityKeyFormat());
        ...
        supplierByCityMap =
            new StoredMap(db.getSupplierByCityIndex(),
                          cityKeyBinding, supplierValueBinding, true);
        shipmentByPartMap =
            new StoredMap(db.getShipmentByPartIndex(),
                          partKeyBinding, shipmentValueBinding, true);
        shipmentBySupplierMap =
            new StoredMap(db.getShipmentBySupplierIndex(),
                          supplierKeyBinding, shipmentValueBinding, true);
</b>
    }
}
</pre></blockquote>
<p>In general, the indexed maps are created here in the same way as the
unindexed maps were created in the Basic example.  The differences are:</p>
<p><ul type=disc>
<li>The first parameter of the
<a href="../../java/com/sleepycat/bdb/collection/StoredMap.html">StoredMap</a>
 constructor is a
<a href="../../java/com/sleepycat/bdb/DataIndex.html">DataIndex</a>
 rather than a
<a href="../../java/com/sleepycat/bdb/DataStore.html">DataStore</a>
.
<li>The second parameter is the index key binding rather than the
primary key binding.
</ul>
<p>For the <b>supplierByCityMap</b>, the <b>cityKeyBinding</b> must
first be created.  This binding was not created in the Basic example because
the City field is not a primary key.</p>
<p>For the <b>shipmentByPartMap</b> and <b>shipmentBySupplierMap</b>,
the <b>partKeyBinding</b> and <b>supplierKeyBinding</b> are used.  These
were created in the Basic example and used as the primary key bindings for the
<b>partMap</b> and <b>supplierMap</b>.</p>
<p>The value bindings--<b>supplierValueBinding</b> and
<b>shipmentValueBinding</b>--were also created in the Basic example.</p>
<p>This illustrates that bindings and formats may and should be reused where
appropriate for creating maps and other collections.</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 getShipmentByPartMap()
    {
        return shipmentByPartMap;
    }
<p>
    public final StoredMap getShipmentBySupplierMap()
    {
        return shipmentBySupplierMap;
    }
<p>
    public final StoredMap getSupplierByCityMap()
    {
        return supplierByCityMap;
    }
<p>
    public final StoredEntrySet getShipmentByPartEntrySet()
    {
        return (StoredEntrySet) shipmentByPartMap.entrySet();
    }
<p>
    public final StoredEntrySet getShipmentBySupplierEntrySet()
    {
        return (StoredEntrySet) shipmentBySupplierMap.entrySet();
    }
<p>
    public final StoredEntrySet getSupplierByCityEntrySet()
    {
        return (StoredEntrySet) supplierByCityMap.entrySet();
    }
</b>}
</pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_index/foreign.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_index/read.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>