keyvalue.html   [plain text]


<!--$Id: keyvalue.html,v 1.2 2004/03/30 01:22:45 jtownsen 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: Defining serialized key and value classes</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/intro.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/env.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Defining serialized key and value classes</h3>
<p>The key and value classes for each type of shipment record -- Parts,
Suppliers and Shipments -- are defined as ordinary Java classes.  In this
example the serialized form of the key and value objects is stored directly in
the database.  Therefore these classes must implement the standard Java
java.io.Serializable interface.  A compact form of Java serialization is used
that does not duplicate the class description in each record.  Instead the
class descriptions are stored in the class catalog store, which is described in
the next section.  But in all other respects, standard Java serialization is
used.</p>
<p>An important point is that instances of these classes are passed and
returned by value, not by reference, when they are stored and retrieved from
the database.  This means that changing a key or value object does not
automatically change the database.  The object must be explicitly stored in the
database after changing it.  To emphasize this point the key and value
classes defined here have no field setter methods.  Setter methods can be
defined, but it is important to remember that calling a setter method will not
cause the change to be stored in the database.  How to store and retrieve
objects in the database will be described later.</p>
<p>Each key and value class contains a toString method that is used to
output the contents of the object in the example program.  This is meant for
illustration only and is not required for database objects in general.</p>
<p>Notice that the key and value classes defined below do not contain any
references to <b>com.sleepycat</b> packages.  An important characteristic of
these classes is that they are independent of the database.  Therefore, they
may be easily used in other contexts and may be defined in a way that is
compatible with other tools and libraries.</p>
<hr size=1 noshade>
<p>The <b>PartKey</b> class contains only the Part's Number field.</p>
<p>Note that <b>PartKey</b> (as well as <b>SupplierKey</b> below)
contain only a single String field.  Instead of defining a specific class for
each type of key, the String class by itself could have been used.  Specific
key classes were used to illustrate strong typing and for consistency in the
example.  The use of a plain String as an index key is illustrated in the next
example program.  It is up to the developer to use either primitive Java
classes such as String and Integer, or strongly typed classes instead.  When
there is the possibility that fields will be added later to a key or value,
a specific class should be used.</p>
<blockquote><pre><b>import java.io.Serializable;
<p>
public class PartKey implements Serializable
{
    private String number;
<p>
    public PartKey(String number) {
        this.number = number;
    }
<p>
    public final String getNumber() {
        return number;
    }
<p>
    public String toString() {
        return "[PartKey: number=" + number + ']';
    }
}
</b></pre></blockquote>
<hr size=1 noshade>
<p>The <b>PartValue</b> class contains the Part's Name, Color, Weight and
City fields.</p>
<blockquote><pre><b>import java.io.Serializable;
<p>
public class PartValue implements Serializable
{
    private String name;
    private String color;
    private Weight weight;
    private String city;
<p>
    public PartValue(String name, String color, Weight weight, String city)
    {
        this.name = name;
        this.color = color;
        this.weight = weight;
        this.city = city;
    }
<p>
    public final String getName()
    {
        return name;
    }
<p>
    public final String getColor()
    {
        return color;
    }
<p>
    public final Weight getWeight()
    {
        return weight;
    }
<p>
    public final String getCity()
    {
        return city;
    }
<p>
    public String toString()
    {
        return "[PartValue: name=" + name +
               " color=" + color +
               " weight=" + weight +
               " city=" + city + ']';
    }
}
</b></pre></blockquote>
<p>The <b>Weight</b> class is also defined here, and is used as the type
of the Part's Weight field.  Just as in standard Java serialization, nothing
special is needed to store nested objects as long as they are all
Serializable.</p>
<blockquote><pre><b>import java.io.Serializable;
<p>
public class Weight implements Serializable
{
    public final static String GRAMS = "grams";
    public final static String OUNCES = "ounces";
<p>
    private double amount;
    private String units;
<p>
    public Weight(double amount, String units)
    {
        this.amount = amount;
        this.units = units;
    }
<p>
    public final double getAmount()
    {
        return amount;
    }
<p>
    public final String getUnits()
    {
        return units;
    }
<p>
    public String toString()
    {
        return "[" + amount + ' ' + units + ']';
    }
}
</b></pre></blockquote>
<hr size=1 noshade>
<p>The <b>SupplierKey</b> class contains the Supplier's Number field.</p>
<blockquote><pre><b>import java.io.Serializable;
<p>
public class SupplierKey implements Serializable
{
    private String number;
<p>
    public SupplierKey(String number)
    {
        this.number = number;
    }
<p>
    public final String getNumber()
    {
        return number;
    }
<p>
    public String toString()
    {
        return "[SupplierKey: number=" + number + ']';
    }
}
</b></pre></blockquote>
<hr size=1 noshade>
<p>The <b>SupplierValue</b> class contains the Supplier's Name, Status and
City fields.</p>
<blockquote><pre><b>import java.io.Serializable;
<p>
public class SupplierValue implements Serializable
{
    private String name;
    private int status;
    private String city;
<p>
    public SupplierValue(String name, int status, String city)
    {
        this.name = name;
        this.status = status;
        this.city = city;
    }
<p>
    public final String getName()
    {
        return name;
    }
<p>
    public final int getStatus()
    {
        return status;
    }
<p>
    public final String getCity()
    {
        return city;
    }
<p>
    public String toString()
    {
        return "[SupplierValue: name=" + name +
               " status=" + status +
               " city=" + city + ']';
    }
}
</b></pre></blockquote>
<hr size=1 noshade>
<p>The <b>ShipmentKey</b> class contains the keys of both the Part and
Supplier.</p>
<blockquote><pre><b>import java.io.Serializable;
<p>
public class ShipmentKey implements Serializable
{
    private String partNumber;
    private String supplierNumber;
<p>
    public ShipmentKey(String partNumber, String supplierNumber)
    {
        this.partNumber = partNumber;
        this.supplierNumber = supplierNumber;
    }
<p>
    public final String getPartNumber()
    {
        return partNumber;
    }
<p>
    public final String getSupplierNumber()
    {
        return supplierNumber;
    }
<p>
    public String toString()
    {
        return "[ShipmentKey: supplier=" + supplierNumber +
                " part=" + partNumber + ']';
    }
}
</b></pre></blockquote>
<hr size=1 noshade>
<p>The <b>ShipmentValue</b> class contains only the Shipment's Quantity
field.  Like <b>PartKey</b> and <b>SupplierKey</b>,
<b>ShipmentValue</b> contains only a single primitive field.  Therefore the
the Integer class could have been used instead of defining a specific value
class.</p>
<blockquote><pre><b>import java.io.Serializable;
<p>
public class ShipmentValue implements Serializable
{
    private int quantity;
<p>
    public ShipmentValue(int quantity)
    {
        this.quantity = quantity;
    }
<p>
    public final int getQuantity()
    {
        return quantity;
    }
<p>
    public String toString()
    {
        return "[ShipmentValue: quantity=" + quantity + ']';
    }
}
</b></pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_basic/intro.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/env.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>