1 4 package je.gettingStarted; 5 6 import java.io.BufferedReader ; 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileNotFoundException ; 10 import java.io.IOException ; 11 import java.io.InputStreamReader ; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 15 import com.sleepycat.bind.EntryBinding; 16 import com.sleepycat.bind.serial.SerialBinding; 17 import com.sleepycat.bind.tuple.TupleBinding; 18 import com.sleepycat.je.DatabaseEntry; 19 import com.sleepycat.je.DatabaseException; 20 import com.sleepycat.je.Transaction; 21 22 public class ExampleDatabasePut { 23 24 private static File myDbEnvPath = new File ("/tmp/JEDB"); 25 private static File inventoryFile = new File ("./inventory.txt"); 26 private static File vendorsFile = new File ("./vendors.txt"); 27 28 private static DatabaseEntry theKey = new DatabaseEntry(); 30 private static DatabaseEntry theData = new DatabaseEntry(); 31 32 private static MyDbEnv myDbEnv = new MyDbEnv(); 34 35 private static void usage() { 36 System.out.println("ExampleDatabasePut [-h <env directory>]"); 37 System.out.println(" [-s <selections file>] [-v <vendors file>]"); 38 System.exit(-1); 39 } 40 41 42 public static void main(String args[]) { 43 ExampleDatabasePut edp = new ExampleDatabasePut(); 44 try { 45 edp.run(args); 46 } catch (DatabaseException dbe) { 47 System.err.println("ExampleDatabasePut: " + dbe.toString()); 48 dbe.printStackTrace(); 49 dbe.printStackTrace(); 50 } catch (Exception e) { 51 System.out.println("Exception: " + e.toString()); 52 e.printStackTrace(); 53 } finally { 54 myDbEnv.close(); 55 } 56 System.out.println("All done."); 57 } 58 59 60 private void run(String args[]) 61 throws DatabaseException { 62 parseArgs(args); 64 65 myDbEnv.setup(myDbEnvPath, false); 68 System.out.println("loading vendors db...."); 69 loadVendorsDb(); 70 71 System.out.println("loading inventory db...."); 72 loadInventoryDb(); 73 } 74 75 76 private void loadVendorsDb() 77 throws DatabaseException { 78 79 List vendors = loadFile(vendorsFile, 8); 84 85 88 EntryBinding dataBinding = 90 new SerialBinding(myDbEnv.getClassCatalog(), Vendor.class); 91 92 for (int i = 0; i < vendors.size(); i++) { 93 String [] sArray = (String [])vendors.get(i); 94 Vendor theVendor = new Vendor(); 95 theVendor.setVendorName(sArray[0]); 96 theVendor.setAddress(sArray[1]); 97 theVendor.setCity(sArray[2]); 98 theVendor.setState(sArray[3]); 99 theVendor.setZipcode(sArray[4]); 100 theVendor.setBusinessPhoneNumber(sArray[5]); 101 theVendor.setRepName(sArray[6]); 102 theVendor.setRepPhoneNumber(sArray[7]); 103 104 String vendorName = theVendor.getVendorName(); 107 try { 108 theKey = new DatabaseEntry(vendorName.getBytes("UTF-8")); 109 } catch (IOException willNeverOccur) {} 110 111 dataBinding.objectToEntry(theVendor, theData); 114 115 myDbEnv.getVendorDB().put(null, theKey, theData); 118 } 119 } 120 121 private void loadInventoryDb() 122 throws DatabaseException { 123 124 List inventoryArray = loadFile(inventoryFile, 6); 129 130 133 TupleBinding inventoryBinding = new InventoryBinding(); 135 136 Transaction txn = myDbEnv.getEnv().beginTransaction(null, null); 139 140 for (int i = 0; i < inventoryArray.size(); i++) { 141 String [] sArray = (String [])inventoryArray.get(i); 142 String sku = sArray[1]; 143 try { 144 theKey = new DatabaseEntry(sku.getBytes("UTF-8")); 145 } catch (IOException willNeverOccur) {} 146 147 Inventory theInventory = new Inventory(); 148 theInventory.setItemName(sArray[0]); 149 theInventory.setSku(sArray[1]); 150 theInventory.setVendorPrice((new Float (sArray[2])).floatValue()); 151 theInventory.setVendorInventory((new Integer (sArray[3])).intValue()); 152 theInventory.setCategory(sArray[4]); 153 theInventory.setVendor(sArray[5]); 154 155 inventoryBinding.objectToEntry(theInventory, theData); 158 159 try { 162 myDbEnv.getInventoryDB().put(txn, theKey, theData); 163 } catch (DatabaseException dbe) { 164 try { 165 System.out.println("Error putting entry " + 166 sku.getBytes("UTF-8")); 167 } catch (IOException willNeverOccur) {} 168 txn.abort(); 169 throw dbe; 170 } 171 } 172 txn.commit(); 175 } 176 177 178 private static void parseArgs(String args[]) { 179 for(int i = 0; i < args.length; ++i) { 180 if (args[i].startsWith("-")) { 181 switch(args[i].charAt(1)) { 182 case 'h': 183 myDbEnvPath = new File (args[++i]); 184 break; 185 case 'i': 186 inventoryFile = new File (args[++i]); 187 break; 188 case 'v': 189 vendorsFile = new File (args[++i]); 190 break; 191 default: 192 usage(); 193 } 194 } 195 } 196 } 197 198 private List loadFile(File theFile, int numFields) { 199 List records = new ArrayList (); 200 try { 201 String theLine = null; 202 FileInputStream fis = new FileInputStream (theFile); 203 BufferedReader br = new BufferedReader (new InputStreamReader (fis)); 204 while((theLine=br.readLine()) != null) { 205 String [] theLineArray = theLine.split("#"); 206 if (theLineArray.length != numFields) { 207 System.out.println("Malformed line found in " + theFile.getPath()); 208 System.out.println("Line was: '" + theLine); 209 System.out.println("length found was: " + theLineArray.length); 210 System.exit(-1); 211 } 212 records.add(theLineArray); 213 } 214 fis.close(); 216 } catch (FileNotFoundException e) { 217 System.err.println(theFile.getPath() + " does not exist."); 218 e.printStackTrace(); 219 usage(); 220 } catch (IOException e) { 221 System.err.println("IO Exception: " + e.toString()); 222 e.printStackTrace(); 223 System.exit(-1); 224 } 225 return records; 226 } 227 228 protected ExampleDatabasePut() {} 229 } 230 | Popular Tags |