Sophie

Sophie

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

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

<!--$Id: read.so,v 1.3 2003/10/14 16:51:59 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: Retrieving database items</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/write.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/except.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Retrieving database items</h3>
<p>Retrieving information from the database is accomplished via the standard
Java collections API.  In the example, the 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Set.html#iterator">Set.iterator</a>
 method is used to iterate all 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.Entry.html">Map.Entry</a>
objects for each store.  All standard Java methods for retrieving objects
from a collection may be used with the Java API.</p>
<hr size=1 noshade>
<p>The <b>PrintDatabase.doWork</b> method calls <b>printEntries</b> to
print the map entries for each database store.  It is called via the
<a href="../../java/com/sleepycat/bdb/TransactionRunner.html">TransactionRunner</a>
 class and was outlined in
the previous section.</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
        {
<b>            printEntries("Parts",
                          views.getPartEntrySet().iterator());
            printEntries("Suppliers",
                          views.getSupplierEntrySet().iterator());
            printEntries("Shipments",
                          views.getShipmentEntrySet().iterator());
</b>        }
    }
<p>
<b>    private void printEntries(String label, Iterator iterator)
    {
    }
</b>}
</pre></blockquote>
<p>The 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Set.html">Set</a>
 of 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.Entry.html">Map.Entry</a>
 objects for each store is obtained from the <b>SampleViews</b>
object.  This set can also be obtained by calling the 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#entrySet">Map.entrySet</a>
 method of a stored map.</p>
<hr size=1 noshade>
<p>The <b>printEntries</b> prints the map entries for any stored map.
The 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#toString">Object.toString</a>
 method of each key and value
is called to obtain a printable representation of each object.</p>
<blockquote><pre>
    private void printEntries(String label, Iterator iterator)
    {
<b>        System.out.println("\n--- " + label + " ---");
        try
        {
            while (iterator.hasNext())
            {
                Map.Entry entry = (Map.Entry) iterator.next();
                System.out.println(entry.getKey().toString());
                System.out.println(entry.getValue().toString());
            }
        }
        finally
        {
            StoredIterator.close(iterator);
        }
</b>    }
</pre></blockquote>
<p>It is very important that all iterators for stored collections are
explicitly closed. To ensure they are closed, a <b>finally</b> clause should
be used as shown above.  If the iterator is not closed, the underlying Berkeley DB
cursor is not closed either and the store may become unusable.</p>
<p>If the iterator is cast to 
<a href="../../java/com/sleepycat/bdb/collection/StoredIterator.html">StoredIterator</a>
 then its 
<a href="../../java/com/sleepycat/bdb/collection/StoredIterator.html#close()">StoredIterator.close()</a>
 method can be called.  Or, as shown above, the static
<a href="../../java/com/sleepycat/bdb/collection/StoredIterator.html#close(java.util.Iterator)">StoredIterator.close(java.util.Iterator)</a>
 method can be called to avoid casting.  The static
form of this method can be called safely for any 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Iterator.html">Iterator</a>
.  If an iterator for a non-stored collection is passed, it is simply
ignored.</p>
<p>This is one of a small number of behavioral differences between standard
Java collections and stored collections.  For a complete list see
<a href="../bdb/cs_bdb_collection.html">Using Stored Collections</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>
--- 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
</pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_basic/write.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/except.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>