1 8 9 package com.sleepycat.je.recovery; 10 11 import java.util.Comparator ; 12 import java.util.Hashtable ; 13 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 import com.sleepycat.je.config.EnvironmentParams; 25 import com.sleepycat.je.util.TestUtils; 26 27 public class RecoveryTest extends RecoveryTestBase { 28 29 32 public void testBasic() 33 throws Throwable { 34 35 doBasic(true); 36 } 37 38 41 public void testBasicRecoveryWithBtreeComparator() 42 throws Throwable { 43 44 btreeComparisonFunction = new BtreeComparator(true); 45 doBasic(true); 46 } 47 48 51 public void testDuplicateOverwrite() 52 throws Throwable { 53 54 createEnvAndDbs(1 << 10, false, NUM_DBS); 55 try { 56 Hashtable expectedData = new Hashtable (); 57 58 Transaction txn = env.beginTransaction(null, null); 59 DatabaseEntry key = new DatabaseEntry("aaaaa".getBytes()); 60 DatabaseEntry data1 = new DatabaseEntry("dddddddddd".getBytes()); 61 DatabaseEntry data2 = new DatabaseEntry("eeeeeeeeee".getBytes()); 62 DatabaseEntry data3 = new DatabaseEntry("ffffffffff".getBytes()); 63 Database db = dbs[0]; 64 assertEquals(OperationStatus.SUCCESS, 65 db.put(null, key, data1)); 66 addExpectedData(expectedData, 0, key, data1, true); 67 assertEquals(OperationStatus.SUCCESS, 68 db.put(null, key, data2)); 69 addExpectedData(expectedData, 0, key, data2, true); 70 assertEquals(OperationStatus.SUCCESS, 71 db.put(null, key, data3)); 72 addExpectedData(expectedData, 0, key, data3, true); 73 assertEquals(OperationStatus.SUCCESS, 74 db.put(null, key, data3)); 75 txn.commit(); 76 closeEnv(); 77 78 recoverAndVerify(expectedData, NUM_DBS); 79 } catch (Throwable t) { 80 t.printStackTrace(); 81 throw t; 82 } 83 } 84 85 88 public void testBasicFewerCheckpoints() 89 throws Throwable { 90 91 doBasic(false); 92 } 93 94 public void testSR8984Part1() 95 throws Throwable { 96 97 doTestSR8984Work(true); 98 } 99 100 public void testSR8984Part2() 101 throws Throwable { 102 103 doTestSR8984Work(false); 104 } 105 106 private void doTestSR8984Work(boolean sameKey) 107 throws DatabaseException { 108 109 final int NUM_EXTRA_DUPS = 150; 110 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 111 112 envConfig.setTransactional(false); 113 envConfig.setAllowCreate(true); 114 envConfig.setConfigParam(EnvironmentParams.ENV_CHECK_LEAKS.getName(), 115 "false"); 116 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6"); 117 envConfig.setConfigParam(EnvironmentParams.ENV_RUN_CLEANER.getName(), 118 "false"); 119 120 envConfig.setConfigParam 121 (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false"); 122 env = new Environment(envHome, envConfig); 123 124 DatabaseConfig dbConfig = new DatabaseConfig(); 125 dbConfig.setTransactional(false); 126 dbConfig.setAllowCreate(true); 127 dbConfig.setSortedDuplicates(true); 128 Database db = env.openDatabase(null, "testSR8984db", dbConfig); 129 130 DatabaseEntry key = new DatabaseEntry("k1".getBytes()); 131 DatabaseEntry data = new DatabaseEntry("d1".getBytes()); 132 assertEquals(OperationStatus.SUCCESS, db.put(null, key, data)); 133 assertEquals(OperationStatus.SUCCESS, db.delete(null, key)); 134 135 if (!sameKey) { 136 data.setData("d2".getBytes()); 137 } 138 139 assertEquals(OperationStatus.SUCCESS, db.put(null, key, data)); 140 for (int i = 3; i < NUM_EXTRA_DUPS; i++) { 141 data.setData(("d" + i).getBytes()); 142 assertEquals(OperationStatus.SUCCESS, db.put(null, key, data)); 143 } 144 145 data.setData("d1".getBytes()); 146 147 Cursor c = db.openCursor(null, null); 148 assertEquals(OperationStatus.SUCCESS, 149 c.getFirst(key, data, LockMode.DEFAULT)); 150 151 c.close(); 152 db.close(); 153 154 155 closeEnv(); 156 env = new Environment(envHome, envConfig); 157 db = env.openDatabase(null, "testSR8984db", dbConfig); 158 c = db.openCursor(null, null); 159 assertEquals(OperationStatus.SUCCESS, 160 c.getFirst(key, data, LockMode.DEFAULT)); 161 assertEquals(NUM_EXTRA_DUPS - 2, c.count()); 162 c.close(); 163 db.close(); 164 env.close(); 165 } 166 167 170 public void doBasic(boolean runCheckpointerDaemon) 171 throws Throwable { 172 173 createEnvAndDbs(1 << 20, runCheckpointerDaemon, NUM_DBS); 174 int numRecs = NUM_RECS; 175 176 try { 177 Hashtable expectedData = new Hashtable (); 179 180 Transaction txn = env.beginTransaction(null, null); 182 insertData(txn, 0, numRecs - 1, expectedData, 1, true, NUM_DBS); 183 txn.commit(); 184 185 txn = env.beginTransaction(null, null); 187 deleteData(txn, expectedData, false, true, NUM_DBS); 188 txn.commit(); 189 190 txn = env.beginTransaction(null, null); 192 modifyData(txn, NUM_RECS/2, expectedData, 1, true, NUM_DBS); 193 txn.commit(); 194 195 closeEnv(); 196 recoverAndVerify(expectedData, NUM_DBS); 197 } catch (Throwable t) { 198 t.printStackTrace(); 200 throw t; 201 } 202 } 203 204 207 public void testBasicDeleteAll() 208 throws Throwable { 209 210 createEnvAndDbs(1024, true, NUM_DBS); 211 int numRecs = NUM_RECS; 212 try { 213 Hashtable expectedData = new Hashtable (); 215 216 Transaction txn = env.beginTransaction(null, null); 218 insertData(txn, 0, numRecs - 1, expectedData, 1, true, NUM_DBS); 219 txn.commit(); 220 221 txn = env.beginTransaction(null, null); 223 modifyData(txn, numRecs/2, expectedData, 1, true, NUM_DBS); 224 txn.commit(); 225 226 txn = env.beginTransaction(null, null); 228 deleteData(txn, expectedData, true, true, NUM_DBS); 229 txn.commit(); 230 231 closeEnv(); 232 233 recoverAndVerify(expectedData, NUM_DBS); 234 } catch (Throwable t) { 235 t.printStackTrace(); 237 throw t; 238 } 239 } 240 241 protected static class BtreeComparator implements Comparator { 242 protected boolean ascendingComparison = true; 243 244 public BtreeComparator() { 245 } 246 247 protected BtreeComparator(boolean ascendingComparison) { 248 this.ascendingComparison = ascendingComparison; 249 } 250 251 public int compare(Object o1, Object o2) { 252 byte[] arg1; 253 byte[] arg2; 254 if (ascendingComparison) { 255 arg1 = (byte[]) o1; 256 arg2 = (byte[]) o2; 257 } else { 258 arg1 = (byte[]) o2; 259 arg2 = (byte[]) o1; 260 } 261 int a1Len = arg1.length; 262 int a2Len = arg2.length; 263 264 int limit = Math.min(a1Len, a2Len); 265 266 for (int i = 0; i < limit; i++) { 267 byte b1 = arg1[i]; 268 byte b2 = arg2[i]; 269 if (b1 == b2) { 270 continue; 271 } else { 272 274 short s1 = (short) (b1 & 0x7F); 275 short s2 = (short) (b2 & 0x7F); 276 if (b1 < 0) { 277 s1 |= 0x80; 278 } 279 if (b2 < 0) { 280 s2 |= 0x80; 281 } 282 return (s1 - s2); 283 } 284 } 285 286 return (a1Len - a2Len); 287 } 288 } 289 } 290 | Popular Tags |