Sophie

Sophie

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

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

<!--$Id: read.so,v 1.4 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: Retrieving items by index key</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/views.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/intro.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Retrieving items by index key</h3>
<p>Retrieving information via database index keys can be accomplished using
the standard Java collections API, using a collection created from a
<a href="../../java/com/sleepycat/bdb/DataIndex.html">DataIndex</a>
 rather than a
<a href="../../java/com/sleepycat/bdb/DataStore.html">DataStore</a>
.  However, the standard Java API
does not support <i>duplicate keys</i>: more than one element in a
collection having the same key.  All three indices created in the prior section
have duplicate keys because of the nature of the city, part number and supplier
number index keys.  More than one supplier may be in the same city, and more
than one shipment may have the same part number or supplier number.  This
section describes how to use extended methods for stored collections to return
all values for a given key.</p>
<p>Using the standard Java collections API, the 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#get">Map.get</a>
 method for a stored collection with duplicate keys will return only
the first value for a given key.  To obtain all values for a given key, the
<a href="../../java/com/sleepycat/bdb/collection/StoredMap.html#duplicates">StoredMap.duplicates</a>
 method may
be called.  This returns a 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html">Collection</a>
 of values for
the given key.  If duplicate keys are not allowed, the returned collection will
have at most one value.  If the key is not present in the map, an empty
collection is returned.</p>
<hr size=1 noshade>
<p>The <b>Sample</b> class is extended to retrieve duplicates for
specific index keys that are present in the database.</p>
<blockquote><pre>
import com.sleepycat.bdb.collection.StoredIterator;
import java.util.Iterator;
...
public class Sample
{
    ...
    private SampleViews views;
    ...
    private class PrintDatabase implements TransactionWorker
    {
        public void doWork()
            throws Exception
        {
            printEntries("Parts",
                          views.getPartEntrySet().iterator());
            printEntries("Suppliers",
                          views.getSupplierEntrySet().iterator());
<b>            printValues("Suppliers for City Paris",
                         views.getSupplierByCityMap().duplicates(
                                            "Paris").iterator());
</b>            printEntries("Shipments",
                          views.getShipmentEntrySet().iterator());
<b>            printValues("Shipments for Part P1",
                         views.getShipmentByPartMap().duplicates(
                                            new PartKey("P1")).iterator());
            printValues("Shipments for Supplier S1",
                         views.getShipmentBySupplierMap().duplicates(
                                            new SupplierKey("S1")).iterator());
</b>        }
    }
<p>
<b>    private void printValues(String label, Iterator iterator)
    {
        System.out.println("\n--- " + label + " ---");
        try
        {
            while (iterator.hasNext())
            {
                System.out.println(iterator.next().toString());
            }
        }
        finally
        {
            StoredIterator.close(iterator);
        }
    }
</b>}
</pre></blockquote>
<p>The 
<a href="../../java/com/sleepycat/bdb/collection/StoredMap.html#duplicates">StoredMap.duplicates</a>
method is called passing the desired key.  The returned value is a standard
Java 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html">Collection</a>
 containing the values for the
specified key.  A standard Java 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Iterator.html">Iterator</a>
 is then
obtained for this collection and all values returned by that iterator are
printed.</p>
<p>Another technique for retrieving duplicates is to use the collection
returned by 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#entrySet">Map.entrySet</a>
.  When duplicate keys are
present, a 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.Entry.html">Map.Entry</a>
 object will be present in this
collection for each duplicate.  This collection can then be iterated or a
subset can be created from it, all using the standard Java collection API.</p>
<hr size=1 noshade>
<p>Note that we did not discuss how duplicates keys can be explicitly added
or removed in a collection.  For index keys, the addition and deletion of
duplicate keys happens automatically when records containing the index key
are added, updated, or removed.</p>
<p>While not shown in the example program, it is also possible to create a
store with duplicate keys in the same way as an index with duplicate keys--by
calling 
<a href="../../java/com/sleepycat/db/Db.html#setFlags">Db.setFlags</a>
 with the
<a href="../../java/com/sleepycat/db/Db.html#DB_DUPSORT">Db.DB_DUPSORT</a>
 flag.  In that case, calling
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#put">Map.put</a>
 will add duplicate keys.  To remove all
duplicate keys, call 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#remove">Map.remove</a>
.  To remove a
specific duplicate key, call 
<a href="../../java/com/sleepycat/bdb/collection/StoredMap.html#duplicates">StoredMap.duplicates</a>
 and then call 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html#remove">Collection.remove</a>
 using the returned collection.  Duplicate values may also be added to
this collection using 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html#add">Collection.add</a>
.</p>
<hr size=1 noshade>
<p>The output of the example program is shown below.</p>
<blockquote><pre>
Adding Suppliers
Adding Parts
Adding Shipments
<p>
--- Parts ---
PartKey: number=P1
PartValue: name=Nut color=Red weight=[12.0 grams] city=London
PartKey: number=P2
PartValue: name=Bolt color=Green weight=[17.0 grams] city=Paris
PartKey: number=P3
PartValue: name=Screw color=Blue weight=[17.0 grams] city=Rome
PartKey: number=P4
PartValue: name=Screw color=Red weight=[14.0 grams] city=London
PartKey: number=P5
PartValue: name=Cam color=Blue weight=[12.0 grams] city=Paris
PartKey: number=P6
PartValue: name=Cog color=Red weight=[19.0 grams] city=London
<p>
--- Suppliers ---
SupplierKey: number=S1
SupplierValue: name=Smith status=20 city=London
SupplierKey: number=S2
SupplierValue: name=Jones status=10 city=Paris
SupplierKey: number=S3
SupplierValue: name=Blake status=30 city=Paris
SupplierKey: number=S4
SupplierValue: name=Clark status=20 city=London
SupplierKey: number=S5
SupplierValue: name=Adams status=30 city=Athens
<p>
<b>--- Suppliers for City Paris ---
SupplierValue: name=Jones status=10 city=Paris
SupplierValue: name=Blake status=30 city=Paris
</b><p>
--- Shipments ---
ShipmentKey: supplier=S1 part=P1
ShipmentValue: quantity=300
ShipmentKey: supplier=S2 part=P1
ShipmentValue: quantity=300
ShipmentKey: supplier=S1 part=P2
ShipmentValue: quantity=200
ShipmentKey: supplier=S2 part=P2
ShipmentValue: quantity=400
ShipmentKey: supplier=S3 part=P2
ShipmentValue: quantity=200
ShipmentKey: supplier=S4 part=P2
ShipmentValue: quantity=200
ShipmentKey: supplier=S1 part=P3
ShipmentValue: quantity=400
ShipmentKey: supplier=S1 part=P4
ShipmentValue: quantity=200
ShipmentKey: supplier=S4 part=P4
ShipmentValue: quantity=300
ShipmentKey: supplier=S1 part=P5
ShipmentValue: quantity=100
ShipmentKey: supplier=S4 part=P5
ShipmentValue: quantity=400
ShipmentKey: supplier=S1 part=P6
ShipmentValue: quantity=100
<b><p>
--- Shipments for Part P1 ---
ShipmentValue: quantity=300
ShipmentValue: quantity=300
<p>
--- Shipments for Supplier S1 ---
ShipmentValue: quantity=300
ShipmentValue: quantity=200
ShipmentValue: quantity=400
ShipmentValue: quantity=200
ShipmentValue: quantity=100
ShipmentValue: quantity=100
</b></pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_index/views.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/intro.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>