1 8 9 package com.sleepycat.je.tree; 10 11 import java.io.File ; 12 import java.util.Random ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.je.DatabaseConfig; 17 import com.sleepycat.je.DatabaseException; 18 import com.sleepycat.je.EnvironmentConfig; 19 import com.sleepycat.je.config.EnvironmentParams; 20 import com.sleepycat.je.dbi.DatabaseId; 21 import com.sleepycat.je.dbi.DatabaseImpl; 22 import com.sleepycat.je.dbi.EnvironmentImpl; 23 import com.sleepycat.je.util.TestUtils; 24 import com.sleepycat.je.utilint.DbLsn; 25 26 public class INTest extends TestCase { 27 static private final int N_BYTES_IN_KEY = 3; 28 private int initialINCapacity; 29 private DatabaseImpl db = null; 30 static private long FAKE_LSN = DbLsn.makeLsn(0, 0); 31 private EnvironmentImpl noLogEnv; 32 private File envHome; 33 34 public INTest() 35 throws DatabaseException { 36 37 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 38 } 39 40 public void setUp() 41 throws DatabaseException { 42 43 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 44 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6"); 45 envConfig.setAllowCreate(true); 46 noLogEnv = new EnvironmentImpl(envHome, envConfig); 47 initialINCapacity = 48 noLogEnv.getConfigManager().getInt(EnvironmentParams.NODE_MAX); 49 db = new DatabaseImpl("foo", new DatabaseId(11), noLogEnv, 50 new DatabaseConfig()); 51 } 52 53 public void tearDown() 54 throws DatabaseException { 55 56 noLogEnv.close(); 57 } 58 59 public void testFindEntry() 60 throws DatabaseException { 61 62 IN in = new IN(db, new byte[0], initialINCapacity, 7); 63 in.latch(); 64 65 byte[] zeroBytes = new byte[N_BYTES_IN_KEY]; 66 for (int i = 0; i < N_BYTES_IN_KEY; i++) { 67 zeroBytes[i] = 0x00; 68 } 69 70 byte[] maxBytes = new byte[N_BYTES_IN_KEY]; 71 for (int i = 0; i < N_BYTES_IN_KEY; i++) { 72 75 maxBytes[i] = (byte) 0xFF; 76 } 77 78 assertTrue(in.findEntry(zeroBytes, false, false) == -1); 79 assertTrue(in.findEntry(maxBytes, false, false) == -1); 80 assertTrue(in.findEntry(zeroBytes, false, true) == -1); 81 assertTrue(in.findEntry(maxBytes, false, true) == -1); 82 assertTrue(in.findEntry(zeroBytes, true, false) == -1); 83 assertTrue(in.findEntry(maxBytes, true, false) == -1); 84 assertTrue(in.findEntry(zeroBytes, true, true) == -1); 85 assertTrue(in.findEntry(maxBytes, true, true) == -1); 86 for (int i = 0; i < initialINCapacity; i++) { 87 90 byte[] keyBytes = new byte[N_BYTES_IN_KEY]; 91 byte[] nextKeyBytes = new byte[N_BYTES_IN_KEY]; 92 byte[] prevKeyBytes = new byte[N_BYTES_IN_KEY]; 93 nextKeyBytes[0] = prevKeyBytes[0] = keyBytes[0] = 0x01; 94 nextKeyBytes[1] = prevKeyBytes[1] = keyBytes[1] = (byte) i; 95 nextKeyBytes[2] = prevKeyBytes[2] = keyBytes[2] = 0x10; 96 nextKeyBytes[2]++; 97 prevKeyBytes[2]--; 98 in.setEntry(i, null, keyBytes, FAKE_LSN, (byte) 0); 99 assertTrue(in.findEntry(zeroBytes, false, false) == 0); 100 assertTrue(in.findEntry(maxBytes, false, false) == i); 101 assertTrue(in.findEntry(zeroBytes, false, true) == -1); 102 assertTrue(in.findEntry(maxBytes, false, true) == -1); 103 assertTrue(in.findEntry(zeroBytes, true, false) == -1); 104 assertTrue(in.findEntry(maxBytes, true, false) == i); 105 assertTrue(in.findEntry(zeroBytes, true, true) == -1); 106 assertTrue(in.findEntry(maxBytes, true, true) == -1); 107 for (int j = 1; j < in.getNEntries(); j++) { assertTrue(in.findEntry(in.getKey(j), false, false) 109 == j); 110 assertTrue(in.findEntry(in.getKey(j), false, true) 111 == j); 112 assertTrue(in.findEntry(in.getKey(j), true, false) == 113 (j | IN.EXACT_MATCH)); 114 assertTrue(in.findEntry(in.getKey(j), true, true) == 115 (j | IN.EXACT_MATCH)); 116 assertTrue(in.findEntry(nextKeyBytes, false, false) == i); 117 assertTrue(in.findEntry(prevKeyBytes, false, false) == i - 1); 118 assertTrue(in.findEntry(nextKeyBytes, false, true) == -1); 119 assertTrue(in.findEntry(prevKeyBytes, false, true) == -1); 120 } 121 } 122 in.releaseLatch(); 123 } 124 125 public void testInsertEntry() 126 throws DatabaseException { 127 128 for (int i = 0; i < 10; i++) { doInsertEntry(false); 130 doInsertEntry(true); 131 } 132 } 133 134 private void doInsertEntry(boolean withMinMax) 135 throws DatabaseException { 136 137 IN in = new IN(db, new byte[0], initialINCapacity, 7); 138 in.latch(); 139 140 byte[] zeroBytes = new byte[N_BYTES_IN_KEY]; 141 for (int i = 0; i < N_BYTES_IN_KEY; i++) { 142 zeroBytes[i] = 0x00; 143 } 144 145 byte[] maxBytes = new byte[N_BYTES_IN_KEY]; 146 for (int i = 0; i < N_BYTES_IN_KEY; i++) { 147 maxBytes[i] = (byte) 0xFF; 148 } 149 150 if (withMinMax) { 151 try { 152 in.insertEntry(new ChildReference(null, zeroBytes, FAKE_LSN)); 153 in.verify(null); 154 in.insertEntry(new ChildReference(null, maxBytes, FAKE_LSN)); 155 in.verify(null); 156 } catch (InconsistentNodeException INE) { 157 fail("caught InconsistentNodeException"); 158 } 159 160 assertTrue(in.findEntry(zeroBytes, false, false) == 0); 161 assertTrue(in.findEntry(maxBytes, false, false) == 1); 162 163 assertTrue(in.findEntry(zeroBytes, false, true) == 0); 164 assertTrue(in.findEntry(maxBytes, false, true) == 1); 165 166 assertTrue(in.findEntry(zeroBytes, true, false) == IN.EXACT_MATCH); 167 assertTrue(in.findEntry(maxBytes, true, false) == 168 (1 | IN.EXACT_MATCH)); 169 170 assertTrue(in.findEntry(zeroBytes, true, true) == IN.EXACT_MATCH); 171 assertTrue(in.findEntry(maxBytes, true, true) == 172 (1 | IN.EXACT_MATCH)); 173 } 174 175 Random rnd = new Random (); 176 177 try { 178 for (int i = 0; 179 i < initialINCapacity - (withMinMax ? 2 : 0); 180 i++) { 181 184 byte[] keyBytes = new byte[N_BYTES_IN_KEY]; 185 186 188 while (true) { 189 rnd.nextBytes(keyBytes); 190 int index = in.findEntry(keyBytes, true, false); 191 if ((index & IN.EXACT_MATCH) != 0 && 192 index >= 0) { 193 continue; 194 } 195 break; 196 } 197 198 in.insertEntry(new ChildReference(null, keyBytes, FAKE_LSN)); 199 try { 200 in.verify(null); 201 } catch (InconsistentNodeException INE) { 202 Key.DUMP_BINARY = true; 203 in.dump(0); 204 } 205 206 if (withMinMax) { 207 assertTrue(in.findEntry(zeroBytes, false, false) == 0); 208 assertTrue(in.findEntry(maxBytes, false, false) == 209 in.getNEntries() - 1); 210 211 assertTrue(in.findEntry(zeroBytes, false, true) == 0); 212 assertTrue(in.findEntry(maxBytes, false, true) == 213 in.getNEntries() - 1); 214 215 assertTrue(in.findEntry(zeroBytes, true, false) == 216 IN.EXACT_MATCH); 217 assertTrue(in.findEntry(maxBytes, true, false) == 218 ((in.getNEntries() - 1) | IN.EXACT_MATCH)); 219 220 assertTrue(in.findEntry(zeroBytes, true, true) == 221 IN.EXACT_MATCH); 222 assertTrue(in.findEntry(maxBytes, true, true) == 223 ((in.getNEntries() - 1) | IN.EXACT_MATCH)); 224 } else { 225 assertTrue(in.findEntry(zeroBytes, false, false) == 0); 226 assertTrue(in.findEntry(maxBytes, false, false) == 227 in.getNEntries() - 1); 228 assertTrue(in.findEntry(zeroBytes, false, true) == -1); 229 assertTrue(in.findEntry(maxBytes, false, true) == -1); 230 231 assertTrue(in.findEntry(zeroBytes, true, false) == -1); 232 assertTrue(in.findEntry(maxBytes, true, false) == 233 in.getNEntries() - 1); 234 } 235 236 for (int j = 1; j < in.getNEntries(); j++) { 237 assertTrue(in.findEntry(in.getKey(j), false, false) == j); 238 assertTrue(in.findEntry(in.getKey(j), false, true) == j); 239 240 assertTrue(in.findEntry(in.getKey(j), false, true) == j); 241 assertTrue(in.findEntry(in.getKey(j), true, false) == 242 (j | IN.EXACT_MATCH)); 243 } 244 } 245 } catch (InconsistentNodeException INE) { 246 fail("caught InconsistentNodeException"); 247 } 248 249 250 byte[] keyBytes = new byte[N_BYTES_IN_KEY]; 251 rnd.nextBytes(keyBytes); 252 253 try { 254 in.insertEntry(new ChildReference(null, keyBytes, FAKE_LSN)); 255 fail("should have caught InconsistentNodeException, but didn't"); 256 } catch (InconsistentNodeException INE) { 257 } 258 in.releaseLatch(); 259 } 260 261 public void testDeleteEntry() 262 throws DatabaseException { 263 264 for (int i = 0; i < 10; i++) { doDeleteEntry(true); 266 doDeleteEntry(false); 267 } 268 } 269 270 private void doDeleteEntry(boolean withMinMax) 271 throws DatabaseException { 272 273 IN in = new IN(db, new byte[0], initialINCapacity, 7); 274 in.latch(); 275 276 byte[] zeroBytes = new byte[N_BYTES_IN_KEY]; 277 for (int i = 0; i < N_BYTES_IN_KEY; i++) { 278 zeroBytes[i] = 0x00; 279 } 280 281 byte[] maxBytes = new byte[N_BYTES_IN_KEY]; 282 for (int i = 0; i < N_BYTES_IN_KEY; i++) { 283 maxBytes[i] = (byte) 0xFF; 284 } 285 286 if (withMinMax) { 287 try { 288 in.insertEntry(new ChildReference(null, zeroBytes, FAKE_LSN)); 289 in.verify(null); 290 in.insertEntry(new ChildReference(null, maxBytes, FAKE_LSN)); 291 in.verify(null); 292 } catch (InconsistentNodeException INE) { 293 fail("caught InconsistentNodeException"); 294 } 295 296 assertTrue(in.findEntry(zeroBytes, false, false) == 0); 297 assertTrue(in.findEntry(maxBytes, false, false) == 1); 298 299 assertTrue(in.findEntry(zeroBytes, false, true) == 0); 300 assertTrue(in.findEntry(maxBytes, false, true) == 1); 301 302 assertTrue(in.findEntry(zeroBytes, true, false) == IN.EXACT_MATCH); 303 assertTrue(in.findEntry(maxBytes, true, false) == 304 (1 | IN.EXACT_MATCH)); 305 306 assertTrue(in.findEntry(zeroBytes, true, true) == IN.EXACT_MATCH); 307 assertTrue(in.findEntry(maxBytes, true, true) == 308 (1 | IN.EXACT_MATCH)); 309 } 310 311 Random rnd = new Random (); 312 313 try { 314 315 for (int i = 0; 316 i < initialINCapacity - (withMinMax ? 2 : 0); 317 i++) { 318 321 byte[] keyBytes = new byte[N_BYTES_IN_KEY]; 322 323 325 while (true) { 326 rnd.nextBytes(keyBytes); 327 int index = in.findEntry(keyBytes, true, false); 328 if ((index & IN.EXACT_MATCH) != 0 && 329 index >= 0) { 330 continue; 331 } 332 break; 333 } 334 335 in.insertEntry(new ChildReference(null, keyBytes, FAKE_LSN)); 336 } 337 338 if (withMinMax) { 339 assertTrue(in.findEntry(zeroBytes, false, false) == 0); 340 assertTrue(in.findEntry(maxBytes, false, false) == 341 in.getNEntries() - 1); 342 344 assertTrue(in.findEntry(zeroBytes, false, true) == 0); 345 assertTrue(in.findEntry(maxBytes, false, true) == 346 in.getNEntries() - 1); 347 348 assertTrue(in.findEntry(zeroBytes, false, true) == 0); 349 assertTrue(in.findEntry(maxBytes, false, true) == 350 in.getNEntries() - 1); 351 assertTrue(in.findEntry(zeroBytes, true, false) == IN.EXACT_MATCH); 352 assertTrue(in.findEntry(maxBytes, true, false) == 353 ((in.getNEntries() - 1) | IN.EXACT_MATCH)); 354 } 355 356 while (in.getNEntries() > 1) { 357 int i = rnd.nextInt(in.getNEntries() - 1) + 1; 358 assertTrue(in.deleteEntry(in.getKey(i), false)); 359 } 360 361 365 assertEquals(withMinMax, in.deleteEntry(zeroBytes, false)); 366 } catch (InconsistentNodeException INE) { 367 fail("caught InconsistentNodeException"); 368 } 369 in.releaseLatch(); 370 } 371 } 372 | Popular Tags |