KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > dbi > DbCursorDeleteTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbCursorDeleteTest.java,v 1.36 2006/10/30 21:14:43 bostic Exp $
7  */

8
9 package com.sleepycat.je.dbi;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Hashtable JavaDoc;
13
14 import com.sleepycat.je.DatabaseException;
15 import com.sleepycat.je.LockMode;
16 import com.sleepycat.je.OperationStatus;
17 import com.sleepycat.je.util.StringDbt;
18
19 /**
20  * Various unit tests for CursorImpl.delete().
21  */

22 public class DbCursorDeleteTest extends DbCursorTestBase {
23     
24     public DbCursorDeleteTest()
25         throws DatabaseException {
26         super();
27     }
28
29     /**
30      * Put a small number of data items into the database in a specific order,
31      * delete all the ones beginning with 'f', and then make sure they were
32      * really deleted.
33      */

34     public void testSimpleDelete()
35     throws DatabaseException {
36
37         initEnv(false);
38     doSimpleCursorPuts();
39
40     int deletedEntries = 0;
41     DataWalker dw = new DataWalker(simpleDataMap) {
42         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
43             try {
44             if (foundKey.charAt(0) == 'f') {
45                 cursor.delete();
46                 deletedEntries++;
47             }
48             } catch (DatabaseException DBE) {
49             System.out.println("DBE " + DBE);
50             }
51         }
52         };
53     dw.walkData();
54     deletedEntries = dw.deletedEntries;
55     dw = new DataWalker(simpleDataMap) {
56         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
57             assertTrue(foundKey.compareTo(prevKey) >= 0);
58             assertTrue(foundKey.charAt(0) != 'f');
59             prevKey = foundKey;
60         }
61         };
62     dw.walkData();
63     assertTrue(dw.nEntries == simpleKeyStrings.length - deletedEntries);
64     }
65
66     /**
67      * Put a small number of data items into the database in a specific order.
68      * For each one: delete, getCurrent (make sure failure), reinsert
69      * (success), delete (success). Once iterated through all of them,
70      * reinsert and make sure successful.
71      */

72     public void testSimpleDeleteInsert()
73     throws DatabaseException {
74
75         initEnv(false);
76     doSimpleCursorPuts();
77     DataWalker dw = new DataWalker(simpleDataMap) {
78         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
79             try {
80             cursor.delete();
81             deletedEntries++;
82             assertEquals(OperationStatus.KEYEMPTY,
83                      cursor.getCurrent
84                      (new StringDbt(), new StringDbt(),
85                       LockMode.DEFAULT));
86             StringDbt newKey = new StringDbt(foundKey);
87             StringDbt newData = new StringDbt(foundData);
88             assertEquals(OperationStatus.SUCCESS,
89                                      cursor2.putNoOverwrite(newKey, newData));
90             assertEquals(OperationStatus.SUCCESS,
91                                      cursor2.delete());
92             } catch (DatabaseException DBE) {
93             System.out.println("DBE " + DBE);
94             }
95         }
96         };
97
98     dw.walkData();
99     doSimpleCursorPuts();
100
101     dw = new DataWalker(simpleDataMap) {
102         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
103             assertEquals(foundData,
104                  (String JavaDoc) simpleDataMap.get(foundKey));
105             simpleDataMap.remove(foundKey);
106         }
107         };
108     dw.walkData();
109     assertTrue(simpleDataMap.size() == 0);
110     }
111
112     /**
113      * Put a small number of data items into the database in a specific order.
114      * For each one: delete, getCurrent (make sure failure), reinsert
115      * (success), delete (success). Once iterated through all of them,
116      * reinsert and make sure successful.
117      */

118     public void testSimpleDeletePutCurrent()
119     throws DatabaseException {
120
121         initEnv(false);
122     doSimpleCursorPuts();
123     DataWalker dw = new DataWalker(simpleDataMap) {
124         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
125             try {
126             cursor.delete();
127             deletedEntries++;
128             assertEquals(OperationStatus.KEYEMPTY,
129                      cursor.getCurrent
130                      (new StringDbt(), new StringDbt(),
131                       LockMode.DEFAULT));
132             StringDbt newData = new StringDbt(foundData);
133             assertEquals(OperationStatus.NOTFOUND,
134                                      cursor.putCurrent(newData));
135             } catch (DatabaseException DBE) {
136             System.out.println("DBE " + DBE);
137             }
138         }
139         };
140
141     dw.walkData();
142     doSimpleCursorPuts();
143
144     dw = new DataWalker(simpleDataMap) {
145         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
146             assertEquals(foundData,
147                  (String JavaDoc) simpleDataMap.get(foundKey));
148             simpleDataMap.remove(foundKey);
149         }
150         };
151     dw.walkData();
152     assertTrue(simpleDataMap.size() == 0);
153     }
154
155     /**
156      * Similar to above test, but there was some question about whether this
157      * tests new functionality or not. Insert k1/d1 and d1/k1. Iterate
158      * through the data and delete k1/d1. Reinsert k1/d1 and make sure it
159      * inserts ok.
160      */

161     public void testSimpleInsertDeleteInsert()
162     throws DatabaseException {
163
164         initEnv(true);
165     StringDbt key = new StringDbt("k1");
166     StringDbt data1 = new StringDbt("d1");
167
168     assertEquals(OperationStatus.SUCCESS,
169                      putAndVerifyCursor(cursor, key, data1, true));
170     assertEquals(OperationStatus.SUCCESS,
171                      putAndVerifyCursor(cursor, data1, key, true));
172
173     DataWalker dw = new DataWalker(null) {
174         void perData(String JavaDoc foundKey, String JavaDoc foundData)
175             throws DatabaseException {
176
177             if (foundKey.equals("k1")) {
178             if (cursor.delete() == OperationStatus.SUCCESS) {
179                 deletedEntries++;
180             }
181             }
182         }
183         };
184     dw.setIgnoreDataMap(true);
185     dw.walkData();
186
187     assertEquals(OperationStatus.SUCCESS,
188                      putAndVerifyCursor(cursor, key, data1, true));
189     }
190
191     /**
192      * Put a small number of data items into the database in a specific order,
193      * delete all of them and then make sure they were really deleted.
194      */

195     public void testSimpleDeleteAll()
196     throws DatabaseException {
197
198         initEnv(false);
199     doSimpleCursorPuts();
200
201     int deletedEntries = 0;
202     DataWalker dw = new DataWalker(simpleDataMap) {
203         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
204             try {
205             cursor.delete();
206             deletedEntries++;
207             assertEquals(OperationStatus.KEYEMPTY,
208                      cursor.getCurrent
209                      (new StringDbt(), new StringDbt(),
210                       LockMode.DEFAULT));
211             } catch (DatabaseException DBE) {
212             System.out.println("DBE " + DBE);
213             }
214         }
215         };
216     dw.walkData();
217     deletedEntries = dw.deletedEntries;
218     dw = new DataWalker(simpleDataMap) {
219         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
220             fail("didn't delete everything");
221         }
222         };
223     dw.walkData();
224     assertTrue(dw.nEntries == 0);
225     assertTrue(simpleKeyStrings.length == deletedEntries);
226     }
227
228     /**
229      * Insert N_KEYS data items into a tree. Iterate through the tree in
230      * ascending order deleting anything that has 'F' as the second character.
231      * Iterate through the tree again and make sure they are all correctly
232      * deleted.
233      */

234     public void testLargeDelete()
235         throws IOException JavaDoc, DatabaseException {
236
237         tearDown();
238     for (int i = 0; i < N_ITERS; i++) {
239         setUp();
240             initEnv(false);
241         doLargeDelete();
242             tearDown();
243     }
244     }
245
246     /**
247      * Helper routine for above.
248      */

249     private void doLargeDeleteAll()
250     throws DatabaseException {
251
252     Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
253     int n_keys = 2000;
254     doLargePut(dataMap, /* N_KEYS */ n_keys);
255
256     int deletedEntries = 0;
257     DataWalker dw = new DataWalker(dataMap) {
258         void perData(String JavaDoc foundKey, String JavaDoc foundData)
259                     throws DatabaseException {
260             cursor.delete();
261             deletedEntries++;
262             assertEquals(OperationStatus.KEYEMPTY,
263                  cursor.getCurrent
264                  (new StringDbt(), new StringDbt(),
265                   LockMode.DEFAULT));
266         }
267         };
268     dw.walkData();
269     deletedEntries = dw.deletedEntries;
270     dw = new DataWalker(dataMap) {
271         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
272             fail("didn't delete everything");
273         }
274         };
275     dw.walkData();
276     assertTrue(dw.nEntries == 0);
277     assertTrue(/* N_KEYS */ n_keys == deletedEntries);
278     }
279
280     /**
281      * Insert N_KEYS data items into a tree. Iterate through the tree in
282      * ascending order deleting all entries. Iterate through the tree again
283      * and make sure they are all correctly deleted.
284      */

285     public void testLargeDeleteAll()
286         throws IOException JavaDoc, DatabaseException {
287
288         tearDown();
289     for (int i = 0; i < N_ITERS; i++) {
290         setUp();
291             initEnv(false);
292         doLargeDeleteAll();
293         tearDown();
294     }
295     }
296
297     /**
298      * Helper routine for above.
299      */

300     private void doLargeDelete()
301     throws DatabaseException {
302
303     Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
304     doLargePut(dataMap, N_KEYS);
305
306     int deletedEntries = 0;
307     DataWalker dw = new DataWalker(dataMap) {
308         void perData(String JavaDoc foundKey, String JavaDoc foundData)
309                     throws DatabaseException {
310             if (foundKey.charAt(1) == 'F') {
311             cursor.delete();
312             deletedEntries++;
313             }
314         }
315         };
316     dw.walkData();
317     deletedEntries = dw.deletedEntries;
318     dw = new DataWalker(dataMap) {
319         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
320             assertTrue(foundKey.compareTo(prevKey) >= 0);
321             assertTrue(foundKey.charAt(1) != 'F');
322             prevKey = foundKey;
323         }
324         };
325     dw.walkData();
326     assertTrue(dw.nEntries == N_KEYS - deletedEntries);
327     }
328
329     /**
330      * Insert N_KEYS data items into a tree. Iterate through the tree in
331      * ascending order deleting the first entry. Iterate through the tree
332      * again and make sure only the first entry is deleted.
333      */

334     public void testLargeDeleteFirst()
335         throws IOException JavaDoc, DatabaseException {
336
337         tearDown();
338     for (int i = 0; i < N_ITERS; i++) {
339         setUp();
340             initEnv(false);
341         doLargeDeleteFirst();
342         tearDown();
343     }
344     }
345
346     /**
347      * Helper routine for above.
348      */

349     private void doLargeDeleteFirst()
350     throws DatabaseException {
351
352     Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
353     doLargePut(dataMap, N_KEYS);
354
355     DataWalker dw = new DataWalker(dataMap) {
356         void perData(String JavaDoc foundKey, String JavaDoc foundData)
357                     throws DatabaseException {
358             if (deletedEntry == null) {
359             deletedEntry = foundKey;
360             cursor.delete();
361             }
362         }
363         };
364     dw.walkData();
365
366     String JavaDoc deletedEntry = dw.deletedEntry;
367
368     dw = new DataWalker(dataMap) {
369         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
370             assertFalse(deletedEntry.equals(foundKey));
371         }
372         };
373     dw.deletedEntry = deletedEntry;
374     dw.walkData();
375     assertTrue(dw.nEntries == N_KEYS - 1);
376     }
377
378     /**
379      * Insert N_KEYS data items into a tree. Iterate through the tree in
380      * ascending order deleting the last entry. Iterate through the tree again
381      * and make sure only the last entry is deleted.
382      */

383     public void testLargeDeleteLast()
384         throws IOException JavaDoc, DatabaseException {
385
386         tearDown();
387     for (int i = 0; i < N_ITERS; i++) {
388         setUp();
389             initEnv(false);
390         doLargeDeleteLast();
391         tearDown();
392     }
393     }
394
395     /**
396      * Helper routine for above.
397      */

398     private void doLargeDeleteLast()
399     throws DatabaseException {
400
401     Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
402     doLargePut(dataMap, N_KEYS);
403
404     DataWalker dw = new BackwardsDataWalker(dataMap) {
405         void perData(String JavaDoc foundKey, String JavaDoc foundData)
406                     throws DatabaseException {
407             if (deletedEntry == null) {
408             deletedEntry = foundKey;
409             cursor.delete();
410             }
411         }
412         };
413     dw.walkData();
414
415     String JavaDoc deletedEntry = dw.deletedEntry;
416
417     dw = new BackwardsDataWalker(dataMap) {
418         void perData(String JavaDoc foundKey, String JavaDoc foundData) {
419             assertFalse(deletedEntry.equals(foundKey));
420         }
421         };
422     dw.deletedEntry = deletedEntry;
423     dw.walkData();
424     assertTrue(dw.nEntries == N_KEYS - 1);
425     }
426 }
427
Popular Tags