Sophie

Sophie

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

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

<!--$Id: bindings.so,v 1.3 2003/10/14 16:52:00 gburd 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 entity bindings</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 - Entity</dl></h3></td>
<td align=right><a href="../bdb_entity/classes.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_entity/views.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Creating entity bindings</h3>
<p><i>Entity bindings</i> are similar to ordinary bindings in that they
convert between Java objects and the stored data format of keys and values. In
addition, entity bindings map between key/value pairs and entity objects.  An
ordinary binding is a one-to-one mapping, while an entity binding is a
two-to-one mapping.</p>
<hr size=1 noshade>
<p>The <b>partValueBinding</b>, <b>supplierValueBinding</b> and
<b>shipmentValueBinding</b> bindings are created below as entity bindings
rather than (in the prior examples) serial bindings.</p>
<blockquote><pre>
import com.sleepycat.bdb.bind.DataBinding;
<b>import com.sleepycat.bdb.bind.EntityBinding;
</b>import com.sleepycat.bdb.bind.serial.SerialBinding;
<b>import com.sleepycat.bdb.bind.serial.SerialSerialBinding;
</b><p>
public class SampleViews
{
    ...
    public SampleViews(SampleDatabase db)
    {
        DataBinding partKeyBinding =
            new SerialBinding(db.getPartKeyFormat());
<b>        EntityBinding partValueBinding =
            new PartBinding(db.getPartKeyFormat(), db.getPartValueFormat());
</b>        DataBinding supplierKeyBinding =
            new SerialBinding(db.getSupplierKeyFormat());
<b>        EntityBinding supplierValueBinding =
            new SupplierBinding(db.getSupplierKeyFormat(),
                                db.getSupplierValueFormat());
</b>        DataBinding shipmentKeyBinding =
            new SerialBinding(db.getShipmentKeyFormat());
<b>        EntityBinding shipmentValueBinding =
            new ShipmentBinding(db.getShipmentKeyFormat(),
                                db.getShipmentValueFormat());
</b>        DataBinding cityKeyBinding =
            new SerialBinding(db.getCityKeyFormat());
        ...
    }
}
</pre></blockquote>
<p>The entity bindings will be used in the next section to construct stored
map objects.</p>
<hr size=1 noshade>
<p>The <b>PartBinding</b> class is defined below.</p>
<blockquote><pre>
public class SampleViews
{
    ...
<b>    private static class PartBinding extends SerialSerialBinding
    {
        private PartBinding(SerialFormat keyFormat,
                            SerialFormat valueFormat)
        {
            super(keyFormat, valueFormat);
        }
<p>
        public Object dataToObject(Object keyInput, Object valueInput)
            throws IOException
        {
            PartKey key = (PartKey) keyInput;
            PartValue value = (PartValue) valueInput;
            return new Part(key.getNumber(), value.getName(), value.getColor(),
                            value.getWeight(), value.getCity());
        }
<p>
        public Object objectToKey(Object object)
            throws IOException
        {
            Part part = (Part) object;
            return new PartKey(part.getNumber());
        }
<p>
        public Object objectToValue(Object object)
            throws IOException
        {
            Part part = (Part) object;
            return new PartValue(part.getName(), part.getColor(),
                                 part.getWeight(), part.getCity());
        }
    }
</b>}
</pre></blockquote>
<p>In general, an entity binding is any class that implements the
<a href="../../java/com/sleepycat/bdb/bind/EntityBinding.html">EntityBinding</a>
 interface, just as an
ordinary binding is any class that implements the
<a href="../../java/com/sleepycat/bdb/bind/DataBinding.html">DataBinding</a>
 interface.  In the prior
examples the built-in 
<a href="../../java/com/sleepycat/bdb/bind/serial/SerialBinding.html">SerialBinding</a>
 class (which implements
<a href="../../java/com/sleepycat/bdb/bind/DataBinding.html">DataBinding</a>
) was used and
no application-defined binding classes were needed.</p>
<p>In this example, application-defined binding classes are used that extend
the 
<a href="../../java/com/sleepycat/bdb/bind/serial/SerialSerialBinding.html">SerialSerialBinding</a>
 abstract base
class.  This base class implements 
<a href="../../java/com/sleepycat/bdb/bind/EntityBinding.html">EntityBinding</a>
and provides the conversions between key/value bytes and key/value objects,
just as the 
<a href="../../java/com/sleepycat/bdb/bind/serial/SerialBinding.html">SerialBinding</a>
class does.  The application-defined entity class implements the abstract
methods defined in the base class that map between key/value objects and
entity objects.</p>
<p>Three abstract methods are implemented for each entity binding.  The
<b>dataToObject</b> method takes as input the key and value objects, which
have been deserialized automatically by the base class.  As output, it returns
the combined <b>Part</b> entity.</p>
<p>The <b>objectToKey</b> and <b>objectToValue</b> methods take an
entity object as input.  As output they return the part key or value object
that is extracted from the entity object.  The key or value will then be
serialized automatically by the base class.</p>
<hr size=1 noshade>
<p>The <b>SupplierBinding</b> and <b>ShipmentBinding</b> classes are
very similar to the <b>PartBinding</b> class.</p>
<blockquote><pre>
public class SampleViews
{
    ...
<b>    private static class SupplierBinding extends SerialSerialBinding
    {
        private SupplierBinding(SerialFormat keyFormat,
                                SerialFormat valueFormat)
        {
            super(keyFormat, valueFormat);
        }
<p>
        public Object dataToObject(Object keyInput, Object valueInput)
            throws IOException
        {
            SupplierKey key = (SupplierKey) keyInput;
            SupplierValue value = (SupplierValue) valueInput;
            return new Supplier(key.getNumber(), value.getName(),
                                value.getStatus(), value.getCity());
        }
<p>
        public Object objectToKey(Object object)
            throws IOException
        {
            Supplier supplier = (Supplier) object;
            return new SupplierKey(supplier.getNumber());
        }
<p>
        public Object objectToValue(Object object)
            throws IOException
        {
            Supplier supplier = (Supplier) object;
            return new SupplierValue(supplier.getName(), supplier.getStatus(),
                                     supplier.getCity());
        }
    }
<p>
    private static class ShipmentBinding extends SerialSerialBinding
    {
        private ShipmentBinding(SerialFormat keyFormat,
                                SerialFormat valueFormat)
        {
            super(keyFormat, valueFormat);
        }
<p>
        public Object dataToObject(Object keyInput, Object valueInput)
            throws IOException
        {
            ShipmentKey key = (ShipmentKey) keyInput;
            ShipmentValue value = (ShipmentValue) valueInput;
            return new Shipment(key.getPartNumber(), key.getSupplierNumber(),
                                value.getQuantity());
        }
<p>
        public Object objectToKey(Object object)
            throws IOException
        {
            Shipment shipment = (Shipment) object;
            return new ShipmentKey(shipment.getPartNumber(),
                                   shipment.getSupplierNumber());
        }
<p>
        public Object objectToValue(Object object)
            throws IOException
        {
            Shipment shipment = (Shipment) object;
            return new ShipmentValue(shipment.getQuantity());
        }
    }
</b>}
</pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_entity/classes.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_entity/views.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>