1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: DbCursorDeleteTest.java,v 1.39.2.2 2010/01/04 15:30:43 cwl Exp $
*/
package com.sleepycat.je.dbi;
import java.io.IOException;
import java.util.Hashtable;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.util.StringDbt;
/**
* Various unit tests for CursorImpl.delete().
*/
public class DbCursorDeleteTest extends DbCursorTestBase {
public DbCursorDeleteTest()
throws DatabaseException {
super();
}
/**
* Put a small number of data items into the database in a specific order,
* delete all the ones beginning with 'f', and then make sure they were
* really deleted.
*/
public void testSimpleDelete()
throws DatabaseException {
initEnv(false);
doSimpleCursorPuts();
int deletedEntries = 0;
DataWalker dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
try {
if (foundKey.charAt(0) == 'f') {
cursor.delete();
deletedEntries++;
}
} catch (DatabaseException DBE) {
System.out.println("DBE " + DBE);
}
}
};
dw.walkData();
deletedEntries = dw.deletedEntries;
dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
assertTrue(foundKey.compareTo(prevKey) >= 0);
assertTrue(foundKey.charAt(0) != 'f');
prevKey = foundKey;
}
};
dw.walkData();
assertTrue(dw.nEntries == simpleKeyStrings.length - deletedEntries);
}
/**
* Put a small number of data items into the database in a specific order.
* For each one: delete, getCurrent (make sure failure), reinsert
* (success), delete (success). Once iterated through all of them,
* reinsert and make sure successful.
*/
public void testSimpleDeleteInsert()
throws DatabaseException {
initEnv(false);
doSimpleCursorPuts();
DataWalker dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
try {
cursor.delete();
deletedEntries++;
assertEquals(OperationStatus.KEYEMPTY,
cursor.getCurrent
(new StringDbt(), new StringDbt(),
LockMode.DEFAULT));
StringDbt newKey = new StringDbt(foundKey);
StringDbt newData = new StringDbt(foundData);
assertEquals(OperationStatus.SUCCESS,
cursor2.putNoOverwrite(newKey, newData));
assertEquals(OperationStatus.SUCCESS,
cursor2.delete());
} catch (DatabaseException DBE) {
System.out.println("DBE " + DBE);
}
}
};
dw.walkData();
doSimpleCursorPuts();
dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
assertEquals(foundData,
(String) simpleDataMap.get(foundKey));
simpleDataMap.remove(foundKey);
}
};
dw.walkData();
assertTrue(simpleDataMap.size() == 0);
}
/**
* Put a small number of data items into the database in a specific order.
* For each one: delete, getCurrent (make sure failure), reinsert
* (success), delete (success). Once iterated through all of them,
* reinsert and make sure successful.
*/
public void testSimpleDeletePutCurrent()
throws DatabaseException {
initEnv(false);
doSimpleCursorPuts();
DataWalker dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
try {
cursor.delete();
deletedEntries++;
assertEquals(OperationStatus.KEYEMPTY,
cursor.getCurrent
(new StringDbt(), new StringDbt(),
LockMode.DEFAULT));
StringDbt newData = new StringDbt(foundData);
assertEquals(OperationStatus.NOTFOUND,
cursor.putCurrent(newData));
} catch (DatabaseException DBE) {
System.out.println("DBE " + DBE);
}
}
};
dw.walkData();
doSimpleCursorPuts();
dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
assertEquals(foundData,
(String) simpleDataMap.get(foundKey));
simpleDataMap.remove(foundKey);
}
};
dw.walkData();
assertTrue(simpleDataMap.size() == 0);
}
/**
* Similar to above test, but there was some question about whether this
* tests new functionality or not. Insert k1/d1 and d1/k1. Iterate
* through the data and delete k1/d1. Reinsert k1/d1 and make sure it
* inserts ok.
*/
public void testSimpleInsertDeleteInsert()
throws DatabaseException {
initEnv(true);
StringDbt key = new StringDbt("k1");
StringDbt data1 = new StringDbt("d1");
assertEquals(OperationStatus.SUCCESS,
putAndVerifyCursor(cursor, key, data1, true));
assertEquals(OperationStatus.SUCCESS,
putAndVerifyCursor(cursor, data1, key, true));
DataWalker dw = new DataWalker(null) {
void perData(String foundKey, String foundData)
throws DatabaseException {
if (foundKey.equals("k1")) {
if (cursor.delete() == OperationStatus.SUCCESS) {
deletedEntries++;
}
}
}
};
dw.setIgnoreDataMap(true);
dw.walkData();
assertEquals(OperationStatus.SUCCESS,
putAndVerifyCursor(cursor, key, data1, true));
}
/**
* Put a small number of data items into the database in a specific order,
* delete all of them and then make sure they were really deleted.
*/
public void testSimpleDeleteAll()
throws DatabaseException {
initEnv(false);
doSimpleCursorPuts();
int deletedEntries = 0;
DataWalker dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
try {
cursor.delete();
deletedEntries++;
assertEquals(OperationStatus.KEYEMPTY,
cursor.getCurrent
(new StringDbt(), new StringDbt(),
LockMode.DEFAULT));
} catch (DatabaseException DBE) {
System.out.println("DBE " + DBE);
}
}
};
dw.walkData();
deletedEntries = dw.deletedEntries;
dw = new DataWalker(simpleDataMap) {
void perData(String foundKey, String foundData) {
fail("didn't delete everything");
}
};
dw.walkData();
assertTrue(dw.nEntries == 0);
assertTrue(simpleKeyStrings.length == deletedEntries);
}
/**
* Insert N_KEYS data items into a tree. Iterate through the tree in
* ascending order deleting anything that has 'F' as the second character.
* Iterate through the tree again and make sure they are all correctly
* deleted.
*/
public void testLargeDelete()
throws IOException, DatabaseException {
tearDown();
for (int i = 0; i < N_ITERS; i++) {
setUp();
initEnv(false);
doLargeDelete();
tearDown();
}
}
/**
* Helper routine for above.
*/
private void doLargeDeleteAll()
throws DatabaseException {
Hashtable dataMap = new Hashtable();
int n_keys = 2000;
doLargePut(dataMap, /* N_KEYS */ n_keys);
int deletedEntries = 0;
DataWalker dw = new DataWalker(dataMap) {
void perData(String foundKey, String foundData)
throws DatabaseException {
cursor.delete();
deletedEntries++;
assertEquals(OperationStatus.KEYEMPTY,
cursor.getCurrent
(new StringDbt(), new StringDbt(),
LockMode.DEFAULT));
}
};
dw.walkData();
deletedEntries = dw.deletedEntries;
dw = new DataWalker(dataMap) {
void perData(String foundKey, String foundData) {
fail("didn't delete everything");
}
};
dw.walkData();
assertTrue(dw.nEntries == 0);
assertTrue(/* N_KEYS */ n_keys == deletedEntries);
}
/**
* Insert N_KEYS data items into a tree. Iterate through the tree in
* ascending order deleting all entries. Iterate through the tree again
* and make sure they are all correctly deleted.
*/
public void testLargeDeleteAll()
throws IOException, DatabaseException {
tearDown();
for (int i = 0; i < N_ITERS; i++) {
setUp();
initEnv(false);
doLargeDeleteAll();
tearDown();
}
}
/**
* Helper routine for above.
*/
private void doLargeDelete()
throws DatabaseException {
Hashtable dataMap = new Hashtable();
doLargePut(dataMap, N_KEYS);
int deletedEntries = 0;
DataWalker dw = new DataWalker(dataMap) {
void perData(String foundKey, String foundData)
throws DatabaseException {
if (foundKey.charAt(1) == 'F') {
cursor.delete();
deletedEntries++;
}
}
};
dw.walkData();
deletedEntries = dw.deletedEntries;
dw = new DataWalker(dataMap) {
void perData(String foundKey, String foundData) {
assertTrue(foundKey.compareTo(prevKey) >= 0);
assertTrue(foundKey.charAt(1) != 'F');
prevKey = foundKey;
}
};
dw.walkData();
assertTrue(dw.nEntries == N_KEYS - deletedEntries);
}
/**
* Insert N_KEYS data items into a tree. Iterate through the tree in
* ascending order deleting the first entry. Iterate through the tree
* again and make sure only the first entry is deleted.
*/
public void testLargeDeleteFirst()
throws IOException, DatabaseException {
tearDown();
for (int i = 0; i < N_ITERS; i++) {
setUp();
initEnv(false);
doLargeDeleteFirst();
tearDown();
}
}
/**
* Helper routine for above.
*/
private void doLargeDeleteFirst()
throws DatabaseException {
Hashtable dataMap = new Hashtable();
doLargePut(dataMap, N_KEYS);
DataWalker dw = new DataWalker(dataMap) {
void perData(String foundKey, String foundData)
throws DatabaseException {
if (deletedEntry == null) {
deletedEntry = foundKey;
cursor.delete();
}
}
};
dw.walkData();
String deletedEntry = dw.deletedEntry;
dw = new DataWalker(dataMap) {
void perData(String foundKey, String foundData) {
assertFalse(deletedEntry.equals(foundKey));
}
};
dw.deletedEntry = deletedEntry;
dw.walkData();
assertTrue(dw.nEntries == N_KEYS - 1);
}
/**
* Insert N_KEYS data items into a tree. Iterate through the tree in
* ascending order deleting the last entry. Iterate through the tree again
* and make sure only the last entry is deleted.
*/
public void testLargeDeleteLast()
throws IOException, DatabaseException {
tearDown();
for (int i = 0; i < N_ITERS; i++) {
setUp();
initEnv(false);
doLargeDeleteLast();
tearDown();
}
}
/**
* Helper routine for above.
*/
private void doLargeDeleteLast()
throws DatabaseException {
Hashtable dataMap = new Hashtable();
doLargePut(dataMap, N_KEYS);
DataWalker dw = new BackwardsDataWalker(dataMap) {
void perData(String foundKey, String foundData)
throws DatabaseException {
if (deletedEntry == null) {
deletedEntry = foundKey;
cursor.delete();
}
}
};
dw.walkData();
String deletedEntry = dw.deletedEntry;
dw = new BackwardsDataWalker(dataMap) {
void perData(String foundKey, String foundData) {
assertFalse(deletedEntry.equals(foundKey));
}
};
dw.deletedEntry = deletedEntry;
dw.walkData();
assertTrue(dw.nEntries == N_KEYS - 1);
}
}
|