1 8 9 package je; 10 11 import java.io.File ; 12 13 import com.sleepycat.bind.tuple.IntegerBinding; 14 import com.sleepycat.je.Cursor; 15 import com.sleepycat.je.Database; 16 import com.sleepycat.je.DatabaseConfig; 17 import com.sleepycat.je.DatabaseEntry; 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.Environment; 20 import com.sleepycat.je.EnvironmentConfig; 21 import com.sleepycat.je.LockMode; 22 import com.sleepycat.je.OperationStatus; 23 import com.sleepycat.je.Transaction; 24 25 29 class SimpleExample { 30 private static final int EXIT_SUCCESS = 0; 31 private static final int EXIT_FAILURE = 1; 32 33 private int numRecords; private int offset; private boolean doInsert; private File envDir; 37 38 public SimpleExample(int numRecords, 39 boolean doInsert, 40 File envDir, 41 int offset) { 42 this.numRecords = numRecords; 43 this.doInsert = doInsert; 44 this.envDir = envDir; 45 this.offset = offset; 46 } 47 48 51 public static void usage() { 52 System.out.println("usage: java " + 53 "je.SimpleExample " + 54 "<dbEnvHomeDirectory> " + 55 "<insert|retrieve> <numRecords> [offset]"); 56 System.exit(EXIT_FAILURE); 57 } 58 59 62 public static void main(String argv[]) { 63 64 if (argv.length < 2) { 65 usage(); 66 return; 67 } 68 File envHomeDirectory = new File (argv[0]); 69 70 boolean doInsertArg = false; 71 if (argv[1].equalsIgnoreCase("insert")) { 72 doInsertArg = true; 73 } else if (argv[1].equalsIgnoreCase("retrieve")) { 74 doInsertArg = false; 75 } else { 76 usage(); 77 } 78 79 int startOffset = 0; 80 int numRecordsVal = 0; 81 82 if (doInsertArg) { 83 84 if (argv.length > 2) { 85 numRecordsVal = Integer.parseInt(argv[2]); 86 } else { 87 usage(); 88 return; 89 } 90 91 if (argv.length > 3) { 92 startOffset = Integer.parseInt(argv[3]); 93 } 94 } 95 96 try { 97 SimpleExample app = new SimpleExample(numRecordsVal, 98 doInsertArg, 99 envHomeDirectory, 100 startOffset); 101 app.run(); 102 } catch (DatabaseException e) { 103 e.printStackTrace(); 104 System.exit(EXIT_FAILURE); 105 } 106 System.exit(EXIT_SUCCESS); 107 } 108 109 112 public void run() throws DatabaseException { 113 114 EnvironmentConfig envConfig = new EnvironmentConfig(); 115 envConfig.setTransactional(true); 116 envConfig.setAllowCreate(true); 117 Environment exampleEnv = new Environment(envDir, envConfig); 118 119 131 Transaction txn = exampleEnv.beginTransaction(null, null); 132 DatabaseConfig dbConfig = new DatabaseConfig(); 133 dbConfig.setTransactional(true); 134 dbConfig.setAllowCreate(true); 135 dbConfig.setSortedDuplicates(true); 136 Database exampleDb = exampleEnv.openDatabase(txn, 137 "simpleDb", 138 dbConfig); 139 txn.commit(); 140 141 145 146 147 DatabaseEntry keyEntry = new DatabaseEntry(); 148 DatabaseEntry dataEntry = new DatabaseEntry(); 149 150 if (doInsert) { 151 152 153 for (int i = offset; i < numRecords + offset; i++) { 154 159 txn = exampleEnv.beginTransaction(null, null); 160 161 162 163 IntegerBinding.intToEntry(i, keyEntry); 164 IntegerBinding.intToEntry(i+1, dataEntry); 165 OperationStatus status = 166 exampleDb.put(txn, keyEntry, dataEntry); 167 168 177 if (status != OperationStatus.SUCCESS) { 178 throw new DatabaseException("Data insertion got status " + 179 status); 180 } 181 txn.commit(); 182 } 183 } else { 184 185 Cursor cursor = exampleDb.openCursor(null, null); 186 187 while (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == 188 OperationStatus.SUCCESS) { 189 System.out.println("key=" + 190 IntegerBinding.entryToInt(keyEntry) + 191 " data=" + 192 IntegerBinding.entryToInt(dataEntry)); 193 194 } 195 cursor.close(); 196 } 197 198 exampleDb.close(); 199 exampleEnv.close(); 200 201 } 202 } 203 | Popular Tags |