CHips L MINI SHELL

CHips L pro

Current Path : /proc/2/root/usr/share/doc/db4-devel-4.7.25/collections/tutorial/
Upload File :
Current File : //proc/2/root/usr/share/doc/db4-devel-4.7.25/collections/tutorial/createbindingscollections.html

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>
		Creating Bindings and Collections
	</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
    <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
    <link rel="up" href="BasicProgram.html" title="Chapter 2. &#10;&#9;&#9;The Basic Program&#10;&#9;" />
    <link rel="previous" href="opendatabases.html" title="&#10;&#9;&#9;Opening and Closing Databases&#10;&#9;" />
    <link rel="next" href="implementingmain.html" title="&#10;&#9;&#9;Implementing the Main Program&#10;&#9;" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">
		Creating Bindings and Collections
	</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="opendatabases.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 2. 
		The Basic Program
	</th>
          <td width="20%" align="right"> <a accesskey="n" href="implementingmain.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="createbindingscollections"></a>
		Creating Bindings and Collections
	</h2>
          </div>
        </div>
        <div></div>
      </div>
      <p>
    <span class="emphasis"><em>Bindings</em></span> translate 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 <span class="emphasis"><em>collections</em></span> are used to access records in a
	database. 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 <span class="emphasis"><em>not</em></span> perform object caching. Every time an object is
	accessed via a collection it will be added to or retrieved from the
	database, 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 raw record data is performed by the database library.
</p>
      <p>
    The <tt class="classname">SampleViews</tt> class is used to create the bindings and
	collections. This class is separate from the <tt class="classname">SampleDatabase</tt>
	class to illustrate the idea that a single set of stored data can
	be accessed via multiple bindings and collections, or <span class="emphasis"><em>views</em></span>.
	The skeleton for the <tt class="classname">SampleViews</tt> class follows.
</p>
      <a id="cb_sampleviews"></a>
      <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.ClassCatalog;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.collections.StoredEntrySet;
import com.sleepycat.collections.StoredMap;
...

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

    ...
    public SampleViews(SampleDatabase db)
    {
    }
}</tt></b> </pre>
      <p>
    A 
    <a href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
    
	field is used for each database. The StoredMap class implements the
	standard Java 
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html" target="_top">Map</a>
    
	interface, which has methods for obtaining a 
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html" target="_top">Set</a>
    
	of keys, a 
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>
    
	of values, or a 
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html" target="_top">Set</a>
    
	of 
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.Entry.html" target="_top">Map.Entry</a>
    
	key/value pairs. Because databases contain key/value pairs, any
	Berkeley DB database may be represented as a Java map.
</p>
      <p>
    The following statements create the key and data bindings using
	the 
    <a href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
    
	class.
</p>
      <a id="cb_sampleviews1"></a>
      <pre class="programlisting">    public SampleViews(SampleDatabase db)
    {
<b class="userinput"><tt>        ClassCatalog catalog = db.getClassCatalog();
        EntryBinding partKeyBinding =
            new SerialBinding(catalog, PartKey.class);
        EntryBinding partValueBinding =
            new SerialBinding(catalog, PartData.class);
        EntryBinding supplierKeyBinding =
            new SerialBinding(catalog, SupplierKey.class);
        EntryBinding supplierValueBinding =
            new SerialBinding(catalog, SupplierData.class);
        EntryBinding shipmentKeyBinding =
            new SerialBinding(catalog, ShipmentKey.class);
        EntryBinding shipmentValueBinding =
            new SerialBinding(catalog, ShipmentData.class);</tt></b>
        ...
    } </pre>
      <p>
    The first parameter of the 
    <a href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
    
	constructor is the class catalog, and is used to store the class
	descriptions of the serialized objects.
</p>
      <p>
    The second parameter is the base class for the serialized
	objects and is used for type checking of the stored objects. If
	<tt class="literal">null</tt> or <tt class="literal">Object.class</tt> is specified, then any Java
	class is allowed. Otherwise, all objects stored in that format must
	be instances of the specified class or derived from the specified
	class. In the example, specific classes are used to enable strong
	type checking.
</p>
      <p>
    The following statements create standard Java maps using the
	<a href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
	
	class.
</p>
      <a id="cb_sampleviews2"></a>
      <pre class="programlisting">    public SampleViews(SampleDatabase db)
    {
        ...
<b class="userinput"><tt>        partMap =
            new StoredMap(db.getPartDatabase(),
                          partKeyBinding, partValueBinding, true);
        supplierMap =
            new StoredMap(db.getSupplierDatabase(),
                          supplierKeyBinding, supplierValueBinding, true);
        shipmentMap =
            new StoredMap(db.getShipmentDatabase(),
                          shipmentKeyBinding, shipmentValueBinding, true);</tt></b>
    ...
    } </pre>
      <p>
    The first parameter of the 
    <a href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
    
	constructor is the database. In a StoredMap, the database keys (the primary
    keys) are used as the map keys. The Index
	example shows how to use secondary 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>
      <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>
      <a id="cb_sampleviewsgetters"></a>
      <pre class="programlisting">public class SampleViews
{
    ...
<b class="userinput"><tt>    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();
    }</tt></b>
    ...
} </pre>
      <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>
    <tt class="classname">StoredMap</tt>, <tt class="classname">StoredEntrySet</tt>, 
    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>
      <a id="cb_sampleviews_usage"></a>
      <pre class="programlisting"><b class="userinput"><tt>    SampleDatabase sd = new SampleDatabase(new String("/home"));
    SampleViews views = new SampleViews(sd);
    Map partMap = views.getPartMap();
    Set supplierEntries = views.getSupplierEntrySet();</tt></b> </pre>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="opendatabases.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="BasicProgram.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="implementingmain.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">
		Opening and Closing Databases
	 </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> 
		Implementing the Main Program
	</td>
        </tr>
      </table>
    </div>
  </body>
</html>

Copyright 2K16 - 2K18 Indonesian Hacker Rulez