1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2004-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9// File: ExampleDatabaseRead 10 11package db.GettingStarted; 12 13import com.sleepycat.bind.EntryBinding; 14import com.sleepycat.bind.serial.SerialBinding; 15import com.sleepycat.bind.tuple.TupleBinding; 16import com.sleepycat.db.Cursor; 17import com.sleepycat.db.DatabaseEntry; 18import com.sleepycat.db.DatabaseException; 19import com.sleepycat.db.LockMode; 20import com.sleepycat.db.OperationStatus; 21import com.sleepycat.db.SecondaryCursor; 22 23import java.io.IOException; 24 25public class ExampleDatabaseRead { 26 27 private static String myDbsPath = "./"; 28 29 // Encapsulates the database environment and databases. 30 private static MyDbs myDbs = new MyDbs(); 31 32 private static TupleBinding inventoryBinding; 33 private static EntryBinding vendorBinding; 34 35 // The item to locate if the -s switch is used 36 private static String locateItem; 37 38 private static void usage() { 39 System.out.println("ExampleDatabaseRead [-h <env directory>]" + 40 "[-s <item to locate>]"); 41 System.exit(-1); 42 } 43 44 public static void main(String args[]) { 45 ExampleDatabaseRead edr = new ExampleDatabaseRead(); 46 try { 47 edr.run(args); 48 } catch (DatabaseException dbe) { 49 System.err.println("ExampleDatabaseRead: " + dbe.toString()); 50 dbe.printStackTrace(); 51 } finally { 52 myDbs.close(); 53 } 54 System.out.println("All done."); 55 } 56 57 private void run(String args[]) 58 throws DatabaseException { 59 // Parse the arguments list 60 parseArgs(args); 61 62 myDbs.setup(myDbsPath); 63 64 // Setup our bindings. 65 inventoryBinding = new InventoryBinding(); 66 vendorBinding = 67 new SerialBinding(myDbs.getClassCatalog(), 68 Vendor.class); 69 70 if (locateItem != null) { 71 showItem(); 72 } else { 73 showAllInventory(); 74 } 75 } 76 77 private void showItem() throws DatabaseException { 78 79 SecondaryCursor secCursor = null; 80 try { 81 // searchKey is the key that we want to find in the 82 // secondary db. 83 DatabaseEntry searchKey = 84 new DatabaseEntry(locateItem.getBytes("UTF-8")); 85 86 // foundKey and foundData are populated from the primary 87 // entry that is associated with the secondary db key. 88 DatabaseEntry foundKey = new DatabaseEntry(); 89 DatabaseEntry foundData = new DatabaseEntry(); 90 91 // open a secondary cursor 92 secCursor = 93 myDbs.getNameIndexDB().openSecondaryCursor(null, null); 94 95 // Search for the secondary database entry. 96 OperationStatus retVal = 97 secCursor.getSearchKey(searchKey, foundKey, 98 foundData, LockMode.DEFAULT); 99 100 // Display the entry, if one is found. Repeat until no more 101 // secondary duplicate entries are found 102 while(retVal == OperationStatus.SUCCESS) { 103 Inventory theInventory = 104 (Inventory)inventoryBinding.entryToObject(foundData); 105 displayInventoryRecord(foundKey, theInventory); 106 retVal = secCursor.getNextDup(searchKey, foundKey, 107 foundData, LockMode.DEFAULT); 108 } 109 } catch (Exception e) { 110 System.err.println("Error on inventory secondary cursor:"); 111 System.err.println(e.toString()); 112 e.printStackTrace(); 113 } finally { 114 if (secCursor != null) { 115 secCursor.close(); 116 } 117 } 118 } 119 120 private void showAllInventory() 121 throws DatabaseException { 122 // Get a cursor 123 Cursor cursor = myDbs.getInventoryDB().openCursor(null, null); 124 125 // DatabaseEntry objects used for reading records 126 DatabaseEntry foundKey = new DatabaseEntry(); 127 DatabaseEntry foundData = new DatabaseEntry(); 128 129 try { // always want to make sure the cursor gets closed 130 while (cursor.getNext(foundKey, foundData, 131 LockMode.DEFAULT) == OperationStatus.SUCCESS) { 132 Inventory theInventory = 133 (Inventory)inventoryBinding.entryToObject(foundData); 134 displayInventoryRecord(foundKey, theInventory); 135 } 136 } catch (Exception e) { 137 System.err.println("Error on inventory cursor:"); 138 System.err.println(e.toString()); 139 e.printStackTrace(); 140 } finally { 141 cursor.close(); 142 } 143 } 144 145 private void displayInventoryRecord(DatabaseEntry theKey, 146 Inventory theInventory) 147 throws DatabaseException { 148 149 String theSKU = new String(theKey.getData()); 150 System.out.println(theSKU + ":"); 151 System.out.println("\t " + theInventory.getItemName()); 152 System.out.println("\t " + theInventory.getCategory()); 153 System.out.println("\t " + theInventory.getVendor()); 154 System.out.println("\t\tNumber in stock: " + 155 theInventory.getVendorInventory()); 156 System.out.println("\t\tPrice per unit: " + 157 theInventory.getVendorPrice()); 158 System.out.println("\t\tContact: "); 159 160 DatabaseEntry searchKey = null; 161 try { 162 searchKey = 163 new DatabaseEntry(theInventory.getVendor().getBytes("UTF-8")); 164 } catch (IOException willNeverOccur) {} 165 DatabaseEntry foundVendor = new DatabaseEntry(); 166 167 if (myDbs.getVendorDB().get(null, searchKey, foundVendor, 168 LockMode.DEFAULT) != OperationStatus.SUCCESS) { 169 System.out.println("Could not find vendor: " + 170 theInventory.getVendor() + "."); 171 System.exit(-1); 172 } else { 173 Vendor theVendor = 174 (Vendor)vendorBinding.entryToObject(foundVendor); 175 System.out.println("\t\t " + theVendor.getAddress()); 176 System.out.println("\t\t " + theVendor.getCity() + ", " + 177 theVendor.getState() + " " + theVendor.getZipcode()); 178 System.out.println("\t\t Business Phone: " + 179 theVendor.getBusinessPhoneNumber()); 180 System.out.println("\t\t Sales Rep: " + 181 theVendor.getRepName()); 182 System.out.println("\t\t " + 183 theVendor.getRepPhoneNumber()); 184 } 185 } 186 187 protected ExampleDatabaseRead() {} 188 189 private static void parseArgs(String args[]) { 190 for(int i = 0; i < args.length; ++i) { 191 if (args[i].startsWith("-")) { 192 switch(args[i].charAt(1)) { 193 case 'h': 194 myDbsPath = new String(args[++i]); 195 break; 196 case 's': 197 locateItem = new String(args[++i]); 198 break; 199 default: 200 usage(); 201 } 202 } 203 } 204 } 205} 206