<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>ExampleDatabasePut.java</title> <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> <link rel="start" href="index.html" title="Getting Started with Berkeley DB" /> <link rel="up" href="dpl_example.html" title="Chapter 6. A DPL Example" /> <link rel="prev" href="dataaccessorclass.html" title="DataAccessor.java" /> <link rel="next" href="dpl_exampleinventoryread.html" title="ExampleInventoryRead.java" /> </head> <body> <div xmlns="" class="navheader"> <div class="libver"> <p>Library Version 12.1.6.1</p> </div> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center">ExampleDatabasePut.java</th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="dataaccessorclass.html">Prev</a> </td> <th width="60%" align="center">Chapter 6. A DPL Example</th> <td width="20%" align="right"> <a accesskey="n" href="dpl_exampleinventoryread.html">Next</a></td> </tr> </table> <hr /> </div> <div class="sect1" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h2 class="title" style="clear: both"><a id="dpl_exampledatabaseput"></a>ExampleDatabasePut.java</h2> </div> </div> </div> <p> Our example reads inventory and vendor information from flat text files, encapsulates this data in objects of the appropriate type, and then writes each object to an <code class="classname">EntityStore</code>. </p> <p> To begin, we import the Java classes that our example needs. Most of the imports are related to reading the raw data from flat text files and breaking them apart for usage with our data classes. We also import classes from the DB package, but we do not actually import any classes from the DPL. The reason why is because we have placed almost all of our DPL work off into other classes, so there is no need for direct usage of those APIs here. </p> <pre class="programlisting">package persist.gettingStarted; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import com.sleepycat.db.DatabaseException; </pre> <p> Now we can begin the class itself. Here we set default paths for the on-disk resources that we require (the environment home, and the location of the text files containing our sample data). We also declare <code class="classname">DataAccessor</code> and <code class="classname">MyDbEnv</code> members. We describe these classes and show their implementation in <a class="xref" href="dataaccessorclass.html" title="DataAccessor.java">DataAccessor.java</a> and <a class="xref" href="mydbenv-persist.html" title="MyDbEnv">MyDbEnv</a>. </p> <pre class="programlisting">public class ExampleDatabasePut { private static File myDbEnvPath = new File("/tmp/JEDB"); private static File inventoryFile = new File("./inventory.txt"); private static File vendorsFile = new File("./vendors.txt"); private DataAccessor da; // Encapsulates the environment and data store. private static MyDbEnv myDbEnv = new MyDbEnv();</pre> <p> Next, we provide our <code class="methodname">usage()</code> method. The command line options provided there are necessary only if the default values to the on-disk resources are not sufficient. </p> <pre class="programlisting"> private static void usage() { System.out.println("ExampleDatabasePut [-h <env directory>]"); System.out.println(" [-i <inventory file>]"); System.out.println(" [-v <vendors file>]"); System.exit(-1); } </pre> <p> Our <code class="methodname">main()</code> method is also reasonably self-explanatory. We simply instantiate an <code class="classname">ExampleDatabasePut</code> object there and then call its <code class="methodname">run()</code> method. We also provide a top-level <code class="literal">try</code> block there for any exceptions that might be thrown during runtime. </p> <p> Notice that the <code class="literal">finally</code> statement in the top-level <code class="literal">try</code> block calls <code class="methodname">MyDbEnv.close()</code>. This method closes our <code class="classname">EntityStore</code> and <code class="classname">Environment</code> objects. By placing it here in the <code class="literal">finally</code> statement, we can make sure that our store and environment are always cleanly closed. </p> <pre class="programlisting"> public static void main(String args[]) { ExampleDatabasePut edp = new ExampleDatabasePut(); try { edp.run(args); } catch (DatabaseException dbe) { System.err.println("ExampleDatabasePut: " + dbe.toString()); dbe.printStackTrace(); } catch (Exception e) { System.out.println("Exception: " + e.toString()); e.printStackTrace(); } finally { myDbEnv.close(); } System.out.println("All done."); } </pre> <p> Our <code class="methodname">run()</code> method does four things. It calls <code class="methodname">MyDbEnv.setup()</code>, which opens our <code class="classname">Environment</code> and <code class="classname">EntityStore</code>. It then instantiates a <code class="classname">DataAccessor</code> object, which we will use to write data to the store. It calls <code class="methodname">loadVendorsDb()</code> which loads all of the vendor information. And then it calls <code class="methodname">loadInventoryDb()</code> which loads all of the inventory information. </p> <p> Notice that the <code class="classname">MyDbEnv</code> object is being setup as read-write. This results in the <code class="classname">EntityStore</code> being opened for transactional support. (See <a class="xref" href="mydbenv-persist.html" title="MyDbEnv">MyDbEnv</a> for implementation details.) </p> <pre class="programlisting"> private void run(String args[]) throws DatabaseException { // Parse the arguments list parseArgs(args); myDbEnv.setup(myDbEnvPath, // Path to the environment home false); // Environment read-only? // Open the data accessor. This is used to store // persistent objects. da = new DataAccessor(myDbEnv.getEntityStore()); System.out.println("loading vendors db...."); loadVendorsDb(); System.out.println("loading inventory db...."); loadInventoryDb(); } </pre> <p> We can now implement the <code class="methodname">loadVendorsDb()</code> method. This method is responsible for reading the vendor contact information from the appropriate flat-text file, populating <code class="classname">Vendor</code> class objects with the data and then writing it to the <code class="classname">EntityStore</code>. As explained above, each individual object is written with transactional support. However, because a transaction handle is not explicitly used, the write is performed using auto-commit. This happens because the <code class="classname">EntityStore</code> was opened to support transactions. </p> <p> To actually write each class to the <code class="classname">EntityStore</code>, we simply call the <code class="methodname">PrimaryIndex.put()</code> method for the <code class="classname">Vendor</code> entity instance. We obtain this method from our <code class="classname">DataAccessor</code> class. </p> <pre class="programlisting"> private void loadVendorsDb() throws DatabaseException { // loadFile opens a flat-text file that contains our data // and loads it into a list for us to work with. The integer // parameter represents the number of fields expected in the // file. List vendors = loadFile(vendorsFile, 8); // Now load the data into the store. for (int i = 0; i < vendors.size(); i++) { String[] sArray = (String[])vendors.get(i); Vendor theVendor = new Vendor(); theVendor.setVendorName(sArray[0]); theVendor.setAddress(sArray[1]); theVendor.setCity(sArray[2]); theVendor.setState(sArray[3]); theVendor.setZipcode(sArray[4]); theVendor.setBusinessPhoneNumber(sArray[5]); theVendor.setRepName(sArray[6]); theVendor.setRepPhoneNumber(sArray[7]); // Put it in the store. da.vendorByName.put(theVendor); } } </pre> <p> Now we can implement our <code class="methodname">loadInventoryDb()</code> method. This does exactly the same thing as the <code class="methodname">loadVendorsDb()</code> method. </p> <pre class="programlisting"> private void loadInventoryDb() throws DatabaseException { // loadFile opens a flat-text file that contains our data // and loads it into a list for us to work with. The integer // parameter represents the number of fields expected in the // file. List inventoryArray = loadFile(inventoryFile, 6); // Now load the data into the store. The item's sku is the // key, and the data is an Inventory class object. for (int i = 0; i < inventoryArray.size(); i++) { String[] sArray = (String[])inventoryArray.get(i); String sku = sArray[1]; Inventory theInventory = new Inventory(); theInventory.setItemName(sArray[0]); theInventory.setSku(sArray[1]); theInventory.setVendorPrice( (new Float(sArray[2])).floatValue()); theInventory.setVendorInventory( (new Integer(sArray[3])).intValue()); theInventory.setCategory(sArray[4]); theInventory.setVendor(sArray[5]); // Put it in the store. Note that this causes our secondary key // to be automatically updated for us. da.inventoryBySku.put(theInventory); } } </pre> <p> The remainder of this example simple parses the command line and loads data from a flat-text file. There is nothing here that is of specific interest to the DPL, but we show this part of the example anyway in the interest of completeness. </p> <pre class="programlisting"> private static void parseArgs(String args[]) { for(int i = 0; i < args.length; ++i) { if (args[i].startsWith("-")) { switch(args[i].charAt(1)) { case 'h': myDbEnvPath = new File(args[++i]); break; case 'i': inventoryFile = new File(args[++i]); break; case 'v': vendorsFile = new File(args[++i]); break; default: usage(); } } } } private List loadFile(File theFile, int numFields) { List<String[]> records = new ArrayList<String[]>(); try { String theLine = null; FileInputStream fis = new FileInputStream(theFile); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); while((theLine=br.readLine()) != null) { String[] theLineArray = theLine.split("#"); if (theLineArray.length != numFields) { System.out.println("Malformed line found in " + theFile.getPath()); System.out.println("Line was: '" + theLine); System.out.println("length found was: " + theLineArray.length); System.exit(-1); } records.add(theLineArray); } // Close the input stream handle fis.close(); } catch (FileNotFoundException e) { System.err.println(theFile.getPath() + " does not exist."); e.printStackTrace(); usage(); } catch (IOException e) { System.err.println("IO Exception: " + e.toString()); e.printStackTrace(); System.exit(-1); } return records; } protected ExampleDatabasePut() {} } </pre> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="dataaccessorclass.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="dpl_example.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="dpl_exampleinventoryread.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top">DataAccessor.java </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> ExampleInventoryRead.java</td> </tr> </table> </div> </body> </html>