Sophie

Sophie

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

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

<!--$Id: extract.so,v 1.3 2003/09/05 00:02:18 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: Tuple - Using tuples with key extractors</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 - Tuple</dl></h3></td>
<td align=right><a href="../bdb_tuple/format.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_tuple/tbinding.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Tuple - Using tuples with key extractors</h3>
<p>Key extractors were used in prior examples to extract index keys from
value objects.  The keys were returned as deserialized key objects, since the
serial format was used for keys.  In this example, the tuple format is used for
keys and the key extractors return keys by writing information to a tuple.  The
differences between this example and the prior example are:</p>
<p><ul type=disc>
<li>The
<a href="../../java/com/sleepycat/bdb/bind/serial/TupleSerialKeyExtractor.html">TupleSerialKeyExtractor</a>
base class is used instead of the
<a href="../../java/com/sleepycat/bdb/bind/serial/SerialSerialKeyExtractor.html">SerialSerialKeyExtractor</a>
base class.
<li>For all keys, the
<a href="../../java/com/sleepycat/bdb/bind/tuple/TupleFormat.html">TupleFormat</a>
class is used instead of the
<a href="../../java/com/sleepycat/bdb/bind/serial/SerialFormat.html">SerialFormat</a>
class.
<li>For all key input and output parameters, the
<a href="../../java/com/sleepycat/bdb/bind/tuple/TupleInput.html">TupleInput</a>
 and
<a href="../../java/com/sleepycat/bdb/bind/tuple/TupleOutput.html">TupleOutput</a>
classes are used instead of
<a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html">Object</a>
 (representing a deserialized object).
<li>Instead of returning a key output object, these methods call tuple
write methods such as 
<a href="../../java/com/sleepycat/bdb/bind/tuple/TupleOutput.html#writeString">TupleOutput.writeString</a>
.
</ul>
<p>In addition to writing key tuples, the <b>ShipmentByPartExtractor</b>
and <b>ShipmentBySupplierExtractor</b> classes also read the key tuple of
the primary key.  This is because they extract the index key from fields in the
Shipment's primary key.  Instead of calling getter methods on the
<b>ShipmentKey</b> object, as in prior examples, these methods call
<a href="../../java/com/sleepycat/bdb/bind/tuple/TupleInput.html#readString">TupleInput.readString</a>
.  The
<b>ShipmentKey</b> consists of two string fields that are read in
sequence.</p>
<hr size=1 noshade>
<p>The modified key extractors are shown below:
<b>SupplierByCityExtractor</b>,
<b>ShipmentByPartExtractor</b> and
<b>ShipmentBySupplierExtractor</b>.</p>
<blockquote><pre>
<b>import com.sleepycat.bdb.bind.serial.TupleSerialKeyExtractor;
import com.sleepycat.bdb.bind.tuple.TupleFormat;
import com.sleepycat.bdb.bind.tuple.TupleInput;
import com.sleepycat.bdb.bind.tuple.TupleOutput;
...
</b>public class SampleDatabase
{
    private static class SupplierByCityExtractor
<b>        extends TupleSerialKeyExtractor
</b>    {
        private SupplierByCityExtractor(<b>TupleFormat</b> primaryKeyFormat,
                                        SerialFormat valueFormat,
                                        <b>TupleFormat</b> indexKeyFormat)
        {
            super(primaryKeyFormat, valueFormat, indexKeyFormat);
        }
<p>
        public void extractIndexKey(<b>TupleInput</b> primaryKeyInput,
                                    Object valueInput,
                                    <b>TupleOutput</b> indexKeyOutput)
            throws IOException
        {
            SupplierValue supplierValue = (SupplierValue) valueInput;
<b>            indexKeyOutput.writeString(supplierValue.getCity());
</b>        }
<p>
        public void clearIndexKey(Object valueInputOutput)
            throws IOException
        {
            throw new UnsupportedOperationException();
        }
    }
<p>
    private static class ShipmentByPartExtractor
<b>        extends TupleSerialKeyExtractor
</b>    {
        private ShipmentByPartExtractor(<b>TupleFormat</b> primaryKeyFormat,
                                        SerialFormat valueFormat,
                                        <b>TupleFormat</b> indexKeyFormat)
        {
            super(primaryKeyFormat, valueFormat, indexKeyFormat);
        }
<p>
        public void extractIndexKey(<b>TupleInput</b> primaryKeyInput,
                                    Object valueInput,
                                    <b>TupleOutput</b> indexKeyOutput)
            throws IOException
        {
<b>            String partNumber = primaryKeyInput.readString();
            // don't bother reading the supplierNumber
            indexKeyOutput.writeString(partNumber);
</b>        }
<p>
        public void clearIndexKey(Object valueInputOutput)
            throws IOException
        {
            throw new UnsupportedOperationException();
        }
    }
<p>
    private static class ShipmentBySupplierExtractor
<b>        extends TupleSerialKeyExtractor
</b>    {
        private ShipmentBySupplierExtractor(<b>TupleFormat</b> primaryKeyFormat,
                                            SerialFormat valueFormat,
                                            <b>TupleFormat</b> indexKeyFormat)
        {
            super(primaryKeyFormat, valueFormat, indexKeyFormat);
        }
<p>
        public void extractIndexKey(<b>TupleInput</b> primaryKeyInput,
                                    Object valueInput,
                                    <b>TupleOutput</b> indexKeyOutput)
            throws IOException
        {
<b>            primaryKeyInput.readString(); // skip the partNumber
            String supplierNumber = primaryKeyInput.readString();
            indexKeyOutput.writeString(supplierNumber);
</b>        }
<p>
        public void clearIndexKey(Object valueInputOutput)
            throws IOException
        {
            throw new UnsupportedOperationException();
        }
    }
}
</pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_tuple/format.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_tuple/tbinding.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>