1 8 9 package com.sleepycat.je.cleaner; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.bind.tuple.IntegerBinding; 17 import com.sleepycat.je.CheckpointConfig; 18 import com.sleepycat.je.Database; 19 import com.sleepycat.je.DatabaseConfig; 20 import com.sleepycat.je.DatabaseEntry; 21 import com.sleepycat.je.DatabaseException; 22 import com.sleepycat.je.DbInternal; 23 import com.sleepycat.je.Environment; 24 import com.sleepycat.je.EnvironmentConfig; 25 import com.sleepycat.je.LockMode; 26 import com.sleepycat.je.OperationStatus; 27 import com.sleepycat.je.Transaction; 28 import com.sleepycat.je.log.FileManager; 29 import com.sleepycat.je.util.TestUtils; 30 31 35 public class RMWLockingTest extends TestCase { 36 37 private static final int NUM_RECS = 5; 38 39 private File envHome; 40 private Environment env; 41 private Database db; 42 private DatabaseEntry key; 43 private DatabaseEntry data; 44 45 public RMWLockingTest() { 46 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 47 } 48 49 public void setUp() 50 throws IOException , DatabaseException { 51 52 TestUtils.removeLogFiles("Setup", envHome, false); 53 TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX); 54 } 55 56 public void tearDown() 57 throws IOException , DatabaseException { 58 59 try { 60 if (db != null) { 61 db.close(); 62 } 63 if (env != null) { 64 env.close(); 65 } 66 } catch (Throwable e) { 67 System.out.println("tearDown: " + e); 68 } 69 70 try { 71 TestUtils.removeLogFiles("tearDown", envHome, true); 72 TestUtils.removeFiles("tearDown", envHome, FileManager.DEL_SUFFIX); 73 } catch (Throwable e) { 74 System.out.println("tearDown: " + e); 75 } 76 77 db = null; 78 env = null; 79 envHome = null; 80 } 81 82 public void testBasic() 83 throws DatabaseException { 84 85 init(); 86 insertRecords(); 87 rmwModify(); 88 89 UtilizationProfile up = 90 DbInternal.envGetEnvironmentImpl(env).getUtilizationProfile(); 91 92 96 CheckpointConfig ckptConfig = new CheckpointConfig(); 97 ckptConfig.setForce(true); 98 env.checkpoint(ckptConfig); 99 100 assertTrue(up.verifyFileSummaryDatabase()); 101 } 102 103 109 public void testBadLog() 110 throws DatabaseException, IOException { 111 112 113 String resName = "rmw_bad_offsets.jdb"; 114 TestUtils.loadLog(getClass(), resName, envHome); 115 116 117 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 118 envConfig.setAllowCreate(false); 119 envConfig.setReadOnly(true); 120 env = new Environment(envHome, envConfig); 121 122 127 UtilizationProfile up = 128 DbInternal.envGetEnvironmentImpl(env).getUtilizationProfile(); 129 assertTrue(up.verifyFileSummaryDatabase()); 130 131 env.close(); 132 env = null; 133 } 134 135 private void init() 136 throws DatabaseException { 137 138 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 139 envConfig.setAllowCreate(true); 140 envConfig.setTransactional(true); 141 env = new Environment(envHome, envConfig); 142 143 DatabaseConfig dbConfig = new DatabaseConfig(); 144 dbConfig.setAllowCreate(true); 145 dbConfig.setTransactional(true); 146 db = env.openDatabase(null, "foo", dbConfig); 147 } 148 149 150 private void insertRecords() 151 throws DatabaseException { 152 153 key = new DatabaseEntry(); 154 data = new DatabaseEntry(); 155 156 IntegerBinding.intToEntry(100, data); 157 158 for (int i = 0; i < NUM_RECS; i++) { 159 IntegerBinding.intToEntry(i, key); 160 assertEquals(OperationStatus.SUCCESS, db.put(null, key, data)); 161 } 162 } 163 164 165 private void rmwModify() 166 throws DatabaseException { 167 168 Transaction txn = env.beginTransaction(null, null); 169 IntegerBinding.intToEntry(0, key); 170 assertEquals(OperationStatus.SUCCESS, 171 db.get(txn, key, data, LockMode.RMW)); 172 IntegerBinding.intToEntry(1, key); 173 assertEquals(OperationStatus.SUCCESS, 174 db.get(txn, key, data, LockMode.RMW)); 175 176 IntegerBinding.intToEntry(200, data); 177 assertEquals(OperationStatus.SUCCESS, 178 db.put(txn, key, data)); 179 txn.commit(); 180 } 181 } 182 | Popular Tags |