KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.je.dbi;
10
11 import java.util.Comparator JavaDoc;
12 import java.util.Hashtable JavaDoc;
13
14 import com.sleepycat.je.DbInternal;
15 import com.sleepycat.je.Cursor;
16 import com.sleepycat.je.DatabaseException;
17 import com.sleepycat.je.LockMode;
18 import com.sleepycat.je.OperationStatus;
19 import com.sleepycat.je.util.StringDbt;
20 import com.sleepycat.je.util.TestUtils;
21
22 /**
23  * Various unit tests for CursorImpl.
24  */

25 public class DbCursorTest extends DbCursorTestBase {
26
27     public DbCursorTest()
28         throws DatabaseException {
29
30         super();
31     }
32
33     /**
34      * Put a small number of data items into the database in a specific order
35      * and ensure that they read back in ascending order.
36      */

37     public void testSimpleGetPut()
38         throws Throwable JavaDoc {
39
40         try {
41             initEnv(false);
42             doSimpleCursorPuts();
43
44             DataWalker dw = new DataWalker(simpleDataMap) {
45                     void perData(String JavaDoc foundKey, String JavaDoc foundData) {
46                         assertTrue(foundKey.compareTo(prevKey) >= 0);
47                         prevKey = foundKey;
48                     }
49                 };
50             dw.walkData();
51             assertTrue(dw.nEntries == simpleKeyStrings.length);
52         } catch (Throwable JavaDoc t) {
53             t.printStackTrace();
54             throw t;
55         }
56     }
57
58     /**
59      * Test the internal Cursor.advanceCursor() entrypoint.
60      */

61     public void testCursorAdvance()
62         throws Throwable JavaDoc {
63
64         try {
65             initEnv(false);
66             doSimpleCursorPuts();
67
68         StringDbt foundKey = new StringDbt();
69         StringDbt foundData = new StringDbt();
70         String JavaDoc prevKey = "";
71
72         OperationStatus status = cursor.getFirst(foundKey, foundData,
73                              LockMode.DEFAULT);
74
75         /*
76          * Advance forward and then back to the first. Rest of scan
77          * should be as normal.
78          */

79         DbInternal.advanceCursor(cursor, foundKey, foundData);
80         DbInternal.retrieveNext
81         (cursor, foundKey, foundData, LockMode.DEFAULT, GetMode.PREV);
82         int nEntries = 0;
83         while (status == OperationStatus.SUCCESS) {
84                 String JavaDoc foundKeyString = foundKey.getString();
85                 String JavaDoc foundDataString = foundData.getString();
86
87         assertTrue(foundKeyString.compareTo(prevKey) >= 0);
88         prevKey = foundKeyString;
89                 nEntries++;
90
91         status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
92         }
93
94             assertTrue(nEntries == simpleKeyStrings.length);
95         } catch (Throwable JavaDoc t) {
96             t.printStackTrace();
97             throw t;
98         }
99     }
100
101     /**
102      * Put a small number of data items into the database in a specific order
103      * and ensure that they read back in descending order.
104      */

105     public void testSimpleGetPutBackwards()
106         throws Throwable JavaDoc {
107
108         try {
109             initEnv(false);
110             doSimpleCursorPuts();
111
112             DataWalker dw = new BackwardsDataWalker(simpleDataMap) {
113                     void perData(String JavaDoc foundKey, String JavaDoc foundData) {
114                         if (!prevKey.equals("")) {
115                             assertTrue(foundKey.compareTo(prevKey) <= 0);
116                         }
117                         prevKey = foundKey;
118                     }
119
120                     OperationStatus getFirst(StringDbt foundKey,
121                          StringDbt foundData)
122                         throws DatabaseException {
123
124                         return cursor.getLast(foundKey, foundData,
125                                               LockMode.DEFAULT);
126                     }
127
128                     OperationStatus getData(StringDbt foundKey,
129                         StringDbt foundData)
130                         throws DatabaseException {
131
132                         return cursor.getPrev(foundKey, foundData,
133                                               LockMode.DEFAULT);
134                     }
135                 };
136             dw.walkData();
137             assertTrue(dw.nEntries == simpleKeyStrings.length);
138         } catch (Throwable JavaDoc t) {
139             t.printStackTrace();
140             throw t;
141         }
142     }
143
144     /**
145      * Put a small number of data items into the database in a specific order
146      * and ensure that they read back in descending order. When "quux" is
147      * found, insert "fub" into the database and make sure that it is also read
148      * back in the cursor.
149      */

150     public void testSimpleGetPut2()
151         throws Throwable JavaDoc {
152
153         try {
154             initEnv(false);
155             doSimpleGetPut2("quux", "fub");
156         } catch (Throwable JavaDoc t) {
157             t.printStackTrace();
158             throw t;
159         }
160     }
161
162     public void doSimpleGetPut2(String JavaDoc whenFoundDoInsert,
163                                 String JavaDoc newKey)
164         throws DatabaseException {
165
166         doSimpleCursorPuts();
167
168         DataWalker dw =
169             new BackwardsDataWalker(whenFoundDoInsert, newKey, simpleDataMap) {
170                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
171                     throws DatabaseException {
172
173                     if (foundKey.equals(whenFoundDoInsert)) {
174                         putAndVerifyCursor(cursor2, new StringDbt(newKey),
175                                            new StringDbt("ten"), true);
176                         simpleDataMap.put(newKey, "ten");
177                     }
178                 }
179
180                 OperationStatus getFirst(StringDbt foundKey,
181                      StringDbt foundData)
182                     throws DatabaseException {
183
184                     return cursor.getLast(foundKey, foundData,
185                                           LockMode.DEFAULT);
186                 }
187
188                 OperationStatus getData(StringDbt foundKey,
189                     StringDbt foundData)
190                     throws DatabaseException {
191
192                     return cursor.getPrev(foundKey, foundData,
193                                           LockMode.DEFAULT);
194                 }
195             };
196         dw.walkData();
197         assertTrue(dw.nEntries == simpleKeyStrings.length + 1);
198     }
199
200     /**
201      * Iterate through each of the keys in the list of "simple keys". For each
202      * one, create the database afresh, iterate through the entries in
203      * ascending order, and when the key being tested is found, insert the next
204      * highest key into the database. Make sure that the key just inserted is
205      * retrieved during the cursor walk. Lather, rinse, repeat.
206      */

207     public void testSimpleGetPutNextKeyForwardTraverse()
208         throws Throwable JavaDoc {
209
210         try {
211             tearDown();
212             for (int i = 0; i < simpleKeyStrings.length; i++) {
213                 setUp();
214                 initEnv(false);
215                 doSimpleGetPut(true,
216                                simpleKeyStrings[i],
217                                nextKey(simpleKeyStrings[i]),
218                                1);
219                 tearDown();
220             }
221         } catch (Throwable JavaDoc t) {
222             t.printStackTrace();
223             throw t;
224         }
225     }
226
227     /**
228      * Iterate through each of the keys in the list of "simple keys". For each
229      * one, create the database afresh, iterate through the entries in
230      * ascending order, and when the key being tested is found, insert the next
231      * lowest key into the database. Make sure that the key just inserted is
232      * not retrieved during the cursor walk. Lather, rinse, repeat.
233      */

234     public void testSimpleGetPutPrevKeyForwardTraverse()
235         throws Throwable JavaDoc {
236
237         try {
238             tearDown();
239             for (int i = 0; i < simpleKeyStrings.length; i++) {
240                 setUp();
241                 initEnv(false);
242                 doSimpleGetPut(true, simpleKeyStrings[i],
243                                prevKey(simpleKeyStrings[i]), 0);
244                 tearDown();
245             }
246         } catch (Throwable JavaDoc t) {
247             t.printStackTrace();
248             throw t;
249         }
250     }
251
252     /**
253      * Iterate through each of the keys in the list of "simple keys". For each
254      * one, create the database afresh, iterate through the entries in
255      * descending order, and when the key being tested is found, insert the
256      * next lowest key into the database. Make sure that the key just inserted
257      * is retrieved during the cursor walk. Lather, rinse, repeat.
258      */

259     public void testSimpleGetPutPrevKeyBackwardsTraverse()
260         throws Throwable JavaDoc {
261
262         try {
263             tearDown();
264             for (int i = 0; i < simpleKeyStrings.length; i++) {
265                 setUp();
266                 initEnv(false);
267                 doSimpleGetPut(false, simpleKeyStrings[i],
268                                prevKey(simpleKeyStrings[i]), 1);
269                 tearDown();
270             }
271         } catch (Throwable JavaDoc t) {
272             t.printStackTrace();
273         }
274     }
275
276     /**
277      * Iterate through each of the keys in the list of "simple keys". For each
278      * one, create the database afresh, iterate through the entries in
279      * descending order, and when the key being tested is found, insert the
280      * next highest key into the database. Make sure that the key just
281      * inserted is not retrieved during the cursor walk. Lather, rinse,
282      * repeat.
283      */

284     public void testSimpleGetPutNextKeyBackwardsTraverse()
285         throws Throwable JavaDoc {
286
287         try {
288             tearDown();
289             for (int i = 0; i < simpleKeyStrings.length; i++) {
290                 setUp();
291                 initEnv(false);
292                 doSimpleGetPut(true, simpleKeyStrings[i],
293                                prevKey(simpleKeyStrings[i]), 0);
294                 tearDown();
295             }
296         } catch (Throwable JavaDoc t) {
297             t.printStackTrace();
298             throw t;
299         }
300     }
301
302     /**
303      * Helper routine for the above four tests.
304      */

305     private void doSimpleGetPut(boolean forward,
306                                 String JavaDoc whenFoundDoInsert,
307                                 String JavaDoc newKey,
308                                 int additionalEntries)
309         throws DatabaseException {
310
311         doSimpleCursorPuts();
312
313         DataWalker dw;
314         if (forward) {
315             dw = new DataWalker(whenFoundDoInsert, newKey, simpleDataMap) {
316                     void perData(String JavaDoc foundKey, String JavaDoc foundData)
317                         throws DatabaseException {
318
319                         if (foundKey.equals(whenFoundDoInsert)) {
320                             putAndVerifyCursor(cursor2, new StringDbt(newKey),
321                                                new StringDbt("ten"), true);
322                             simpleDataMap.put(newKey, "ten");
323                         }
324                     }
325                 };
326         } else {
327             dw = new BackwardsDataWalker(whenFoundDoInsert,
328                      newKey,
329                                          simpleDataMap) {
330             void perData(String JavaDoc foundKey, String JavaDoc foundData)
331             throws DatabaseException {
332
333             if (foundKey.equals(whenFoundDoInsert)) {
334                 putAndVerifyCursor(cursor2, new StringDbt(newKey),
335                            new StringDbt("ten"), true);
336                 simpleDataMap.put(newKey, "ten");
337             }
338             }
339
340             OperationStatus getFirst(StringDbt foundKey,
341                          StringDbt foundData)
342             throws DatabaseException {
343
344             return cursor.getLast(foundKey, foundData,
345                           LockMode.DEFAULT);
346             }
347
348             OperationStatus getData(StringDbt foundKey,
349                         StringDbt foundData)
350             throws DatabaseException {
351
352             return cursor.getPrev(foundKey, foundData,
353                           LockMode.DEFAULT);
354             }
355                 };
356         }
357         dw.walkData();
358         assertEquals(simpleKeyStrings.length + additionalEntries, dw.nEntries);
359     }
360
361     /**
362      * Put a small number of data items into the database in a specific order
363      * and ensure that they read back in descending order. Replace the data
364      * portion for each one, then read back again and make sure it was replaced
365      * properly.
366      */

367     public void testSimpleReplace()
368         throws Throwable JavaDoc {
369
370         try {
371             initEnv(false);
372             doSimpleReplace();
373         } catch (Throwable JavaDoc t) {
374             t.printStackTrace();
375             throw t;
376         }
377     }
378
379     public void doSimpleReplace()
380         throws DatabaseException {
381
382         doSimpleCursorPuts();
383
384         DataWalker dw =
385             new DataWalker(simpleDataMap) {
386                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
387                     throws DatabaseException {
388
389                     String JavaDoc newData = foundData + "x";
390                     cursor.putCurrent(new StringDbt(newData));
391                     simpleDataMap.put(foundKey, newData);
392                 }
393             };
394         dw.walkData();
395         dw = new DataWalker(simpleDataMap) {
396                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
397                     throws DatabaseException {
398
399                     assertTrue(foundData.equals(simpleDataMap.get(foundKey)));
400                 }
401             };
402         dw.walkData();
403     }
404
405     /**
406      * Insert N_KEYS data items into a tree. Iterate through the tree in
407      * ascending order. After each element is retrieved, insert the next
408      * lowest key into the tree. Ensure that the element just inserted is not
409      * returned by the cursor. Ensure that the elements are returned in
410      * ascending order. Lather, rinse, repeat.
411      */

412     public void testLargeGetPutPrevKeyForwardTraverse()
413         throws Throwable JavaDoc {
414
415         try {
416             initEnv(false);
417             doLargeGetPutPrevKeyForwardTraverse();
418         } catch (Throwable JavaDoc t) {
419             t.printStackTrace();
420             throw t;
421         }
422     }
423
424     /**
425      * Helper routine for above.
426      */

427     private void doLargeGetPutPrevKeyForwardTraverse()
428         throws DatabaseException {
429
430         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
431         doLargePut(dataMap, N_KEYS);
432
433         DataWalker dw = new DataWalker(dataMap) {
434                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
435                     throws DatabaseException {
436
437                     assertTrue(foundKey.compareTo(prevKey) >= 0);
438                     putAndVerifyCursor(cursor2,
439                                        new StringDbt(prevKey(foundKey)),
440                                        new StringDbt
441                                        (Integer.toString(dataMap.size() +
442                                                          nEntries)),
443                                        true);
444                     prevKey = foundKey;
445                     assertTrue(dataMap.get(foundKey) != null);
446                     dataMap.remove(foundKey);
447                 }
448             };
449         dw.walkData();
450         if (dataMap.size() > 0) {
451             fail("dataMap still has entries");
452         }
453         assertTrue(dw.nEntries == N_KEYS);
454     }
455
456     /**
457      * Insert N_KEYS data items into a tree. Iterate through the tree
458      * in ascending order. Ensure that count() always returns 1 for each
459      * data item returned.
460      */

461     public void testLargeCount()
462         throws Throwable JavaDoc {
463
464         try {
465             initEnv(false);
466             doLargeCount();
467         } catch (Throwable JavaDoc t) {
468             t.printStackTrace();
469             throw t;
470         }
471     }
472
473     /**
474      * Helper routine for above.
475      */

476     private void doLargeCount()
477         throws DatabaseException {
478
479         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
480         doLargePut(dataMap, N_KEYS);
481
482         DataWalker dw = new DataWalker(dataMap) {
483                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
484                     throws DatabaseException {
485
486                     assertTrue(cursor.count() == 1);
487                     assertTrue(foundKey.compareTo(prevKey) >= 0);
488                     prevKey = foundKey;
489                     assertTrue(dataMap.get(foundKey) != null);
490                     dataMap.remove(foundKey);
491                 }
492             };
493         dw.walkData();
494         if (dataMap.size() > 0) {
495             fail("dataMap still has entries");
496         }
497         assertTrue(dw.nEntries == N_KEYS);
498     }
499
500     public void xxtestGetPerf()
501         throws Throwable JavaDoc {
502
503         try {
504             initEnv(false);
505             final int N = 50000;
506             int count = 0;
507             doLargePutPerf(N);
508
509             StringDbt foundKey = new StringDbt();
510             StringDbt foundData = new StringDbt();
511             OperationStatus status;
512             status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT);
513
514             while (status == OperationStatus.SUCCESS) {
515                 status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
516                 count++;
517             }
518         
519             assertTrue(count == N);
520         } catch (Throwable JavaDoc t) {
521             t.printStackTrace();
522             throw t;
523         }
524     }
525
526     /**
527      * Insert a bunch of key/data pairs. Read them back and replace each of
528      * the data. Read the pairs back again and make sure the replace did the
529      * right thing.
530      */

531     public void testLargeReplace()
532         throws Throwable JavaDoc {
533
534         try {
535             initEnv(false);
536             Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
537             doLargePut(dataMap, N_KEYS);
538
539             DataWalker dw = new DataWalker(dataMap) {
540                     void perData(String JavaDoc foundKey, String JavaDoc foundData)
541                         throws DatabaseException {
542
543                         String JavaDoc newData = foundData + "x";
544                         cursor.putCurrent(new StringDbt(newData));
545                         dataMap.put(foundKey, newData);
546                     }
547                 };
548             dw.walkData();
549             dw = new DataWalker(dataMap) {
550                     void perData(String JavaDoc foundKey, String JavaDoc foundData)
551                         throws DatabaseException {
552
553                         assertTrue(foundData.equals(dataMap.get(foundKey)));
554                         dataMap.remove(foundKey);
555                     }
556                 };
557             dw.walkData();
558             assertTrue(dw.nEntries == N_KEYS);
559             assertTrue(dataMap.size() == 0);
560         } catch (Throwable JavaDoc t) {
561             t.printStackTrace();
562             throw t;
563         }
564     }
565
566     /**
567      * Insert N_KEYS data items into a tree. Iterate through the tree in
568      * descending order. After each element is retrieved, insert the next
569      * highest key into the tree. Ensure that the element just inserted is not
570      * returned by the cursor. Ensure that the elements are returned in
571      * descending order. Lather, rinse, repeat.
572      */

573     public void testLargeGetPutNextKeyBackwardsTraverse()
574         throws Throwable JavaDoc {
575
576         try {
577             tearDown();
578             for (int i = 0; i < N_ITERS; i++) {
579                 setUp();
580                 initEnv(false);
581                 doLargeGetPutNextKeyBackwardsTraverse();
582                 tearDown();
583             }
584         } catch (Throwable JavaDoc t) {
585             t.printStackTrace();
586             throw t;
587         }
588     }
589
590     /**
591      * Helper routine for above.
592      */

593     private void doLargeGetPutNextKeyBackwardsTraverse()
594         throws DatabaseException {
595
596         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
597         doLargePut(dataMap, N_KEYS);
598
599         DataWalker dw = new BackwardsDataWalker(dataMap) {
600                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
601                     throws DatabaseException {
602
603                     if (!prevKey.equals("")) {
604                         assertTrue(foundKey.compareTo(prevKey) <= 0);
605                     }
606                     putAndVerifyCursor(cursor2,
607                                        new StringDbt(nextKey(foundKey)),
608                                        new StringDbt
609                                        (Integer.toString(dataMap.size() +
610                                                          nEntries)),
611                                        true);
612                     prevKey = foundKey;
613                     assertTrue(dataMap.get(foundKey) != null);
614                     dataMap.remove(foundKey);
615                 }
616
617                 OperationStatus getFirst(StringDbt foundKey,
618                      StringDbt foundData)
619                     throws DatabaseException {
620
621                     return cursor.getLast(foundKey, foundData,
622                                           LockMode.DEFAULT);
623                 }
624
625                 OperationStatus getData(StringDbt foundKey,
626                     StringDbt foundData)
627                     throws DatabaseException {
628
629                     return cursor.getPrev(foundKey, foundData,
630                                           LockMode.DEFAULT);
631                 }
632             };
633         dw.walkData();
634         if (dataMap.size() > 0) {
635             fail("dataMap still has entries");
636         }
637         assertTrue(dw.nEntries == N_KEYS);
638     }
639
640     /**
641      * Insert N_KEYS data items into a tree. Iterate through the tree in
642      * ascending order. After each element is retrieved, insert the next
643      * highest key into the tree. Ensure that the element just inserted is
644      * returned by the cursor. Ensure that the elements are returned in
645      * ascending order. Lather, rinse, repeat.
646      */

647     public void testLargeGetPutNextKeyForwardTraverse()
648         throws Throwable JavaDoc {
649
650         try {
651             initEnv(false);
652             doLargeGetPutNextKeyForwardTraverse(N_KEYS);
653         } catch (Throwable JavaDoc t) {
654             t.printStackTrace();
655             throw t;
656         }
657     }
658
659     /**
660      * Helper routine for above.
661      */

662     private void doLargeGetPutNextKeyForwardTraverse(int nKeys)
663         throws DatabaseException {
664
665         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
666         Hashtable JavaDoc addedDataMap = new Hashtable JavaDoc();
667         doLargePut(dataMap, nKeys);
668
669         DataWalker dw = new DataWalker(dataMap, addedDataMap) {
670                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
671                     throws DatabaseException {
672
673                     assertTrue(foundKey.compareTo(prevKey) >= 0);
674                     if (addedDataMap.get(foundKey) == null) {
675                         String JavaDoc newKey = nextKey(foundKey);
676                         String JavaDoc newData =
677                             Integer.toString(dataMap.size() + nEntries);
678                         putAndVerifyCursor(cursor2,
679                                            new StringDbt(newKey),
680                                            new StringDbt(newData),
681                                            true);
682                         addedDataMap.put(newKey, newData);
683                         prevKey = foundKey;
684                         assertTrue(dataMap.get(foundKey) != null);
685                         dataMap.remove(foundKey);
686                     } else {
687                         addedDataMap.remove(foundKey);
688                     }
689                 }
690             };
691         dw.walkData();
692         if (dataMap.size() > 0) {
693             fail("dataMap still has entries");
694         }
695         if (addedDataMap.size() > 0) {
696             System.out.println(addedDataMap);
697             fail("addedDataMap still has entries");
698         }
699         assertTrue(dw.nEntries == nKeys * 2);
700     }
701
702     /**
703      * Insert N_KEYS data items into a tree. Iterate through the tree in
704      * descending order. After each element is retrieved, insert the next
705      * lowest key into the tree. Ensure that the element just inserted is
706      * returned by the cursor. Ensure that the elements are returned in
707      * descending order. Lather, rinse, repeat.
708      */

709     public void testLargeGetPutPrevKeyBackwardsTraverse()
710         throws Throwable JavaDoc {
711
712         try {
713             initEnv(false);
714             doLargeGetPutPrevKeyBackwardsTraverse(N_KEYS);
715         } catch (Throwable JavaDoc t) {
716             t.printStackTrace();
717             throw t;
718         }
719     }
720
721     /**
722      * Helper routine for above.
723      */

724     private void doLargeGetPutPrevKeyBackwardsTraverse(int nKeys)
725         throws DatabaseException {
726
727         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
728         Hashtable JavaDoc addedDataMap = new Hashtable JavaDoc();
729         doLargePut(dataMap, nKeys);
730
731         DataWalker dw = new BackwardsDataWalker(dataMap, addedDataMap) {
732                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
733                     throws DatabaseException {
734
735             if (!prevKey.equals("")) {
736                         assertTrue(foundKey.compareTo(prevKey) <= 0);
737                     }
738                     if (addedDataMap.get(foundKey) == null) {
739                         String JavaDoc newKey = prevKey(foundKey);
740                         String JavaDoc newData =
741                             Integer.toString(dataMap.size() + nEntries);
742                         putAndVerifyCursor(cursor2,
743                                            new StringDbt(newKey),
744                                            new StringDbt(newData),
745                                            true);
746                         addedDataMap.put(newKey, newData);
747                         prevKey = foundKey;
748                         assertTrue(dataMap.get(foundKey) != null);
749                         dataMap.remove(foundKey);
750                     } else {
751                         addedDataMap.remove(foundKey);
752                     }
753                 }
754
755                 OperationStatus getFirst(StringDbt foundKey,
756                      StringDbt foundData)
757                     throws DatabaseException {
758
759                     return cursor.getLast(foundKey, foundData,
760                                           LockMode.DEFAULT);
761                 }
762
763                 OperationStatus getData(StringDbt foundKey,
764                     StringDbt foundData)
765                     throws DatabaseException {
766
767                     return cursor.getPrev(foundKey, foundData,
768                                           LockMode.DEFAULT);
769                 }
770             };
771         dw.walkData();
772         if (dataMap.size() > 0) {
773             fail("dataMap still has entries");
774         }
775         if (addedDataMap.size() > 0) {
776             System.out.println(addedDataMap);
777             fail("addedDataMap still has entries");
778         }
779         assertTrue(dw.nEntries == nKeys * 2);
780     }
781
782     /**
783      * Insert N_KEYS data items into a tree. Iterate through the tree in
784      * ascending order. After each element is retrieved, insert the next
785      * highest and next lowest keys into the tree. Ensure that the next
786      * highest element just inserted is returned by the cursor. Ensure that
787      * the elements are returned in ascending order. Lather, rinse, repeat.
788      */

789     public void testLargeGetPutBothKeyForwardTraverse()
790         throws Throwable JavaDoc {
791
792         try {
793             initEnv(false);
794             doLargeGetPutBothKeyForwardTraverse(N_KEYS);
795         } catch (Throwable JavaDoc t) {
796             t.printStackTrace();
797             throw t;
798         }
799     }
800
801     /**
802      * Helper routine for above.
803      */

804     private void doLargeGetPutBothKeyForwardTraverse(int nKeys)
805         throws DatabaseException {
806
807         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
808         Hashtable JavaDoc addedDataMap = new Hashtable JavaDoc();
809         doLargePut(dataMap, nKeys);
810
811         DataWalker dw = new DataWalker(dataMap, addedDataMap) {
812                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
813                     throws DatabaseException {
814
815                     assertTrue(foundKey.compareTo(prevKey) >= 0);
816                     if (addedDataMap.get(foundKey) == null) {
817                         String JavaDoc newKey = nextKey(foundKey);
818                         String JavaDoc newData =
819                             Integer.toString(dataMap.size() + nEntries);
820                         putAndVerifyCursor(cursor2,
821                                            new StringDbt(newKey),
822                                            new StringDbt(newData),
823                                            true);
824                         addedDataMap.put(newKey, newData);
825                         newKey = prevKey(foundKey);
826                         newData = Integer.toString(dataMap.size() + nEntries);
827                         putAndVerifyCursor(cursor2,
828                                            new StringDbt(newKey),
829                                            new StringDbt(newData),
830                                            true);
831                         prevKey = foundKey;
832                         assertTrue(dataMap.get(foundKey) != null);
833                         dataMap.remove(foundKey);
834                     } else {
835                         addedDataMap.remove(foundKey);
836                     }
837                 }
838             };
839         dw.walkData();
840         if (dataMap.size() > 0) {
841             fail("dataMap still has entries");
842         }
843         if (addedDataMap.size() > 0) {
844             System.out.println(addedDataMap);
845             fail("addedDataMap still has entries");
846         }
847         assertTrue(dw.nEntries == nKeys * 2);
848     }
849
850     /**
851      * Insert N_KEYS data items into a tree. Iterate through the tree in
852      * descending order. After each element is retrieved, insert the next
853      * highest and next lowest keys into the tree. Ensure that the next lowest
854      * element just inserted is returned by the cursor. Ensure that the
855      * elements are returned in descending order. Lather, rinse, repeat.
856      */

857     public void testLargeGetPutBothKeyBackwardsTraverse()
858         throws Throwable JavaDoc {
859
860         try {
861             initEnv(false);
862             doLargeGetPutBothKeyBackwardsTraverse(N_KEYS);
863         } catch (Throwable JavaDoc t) {
864             t.printStackTrace();
865             throw t;
866         }
867     }
868
869     /**
870      * Helper routine for above.
871      */

872     private void doLargeGetPutBothKeyBackwardsTraverse(int nKeys)
873         throws DatabaseException {
874
875         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
876         Hashtable JavaDoc addedDataMap = new Hashtable JavaDoc();
877         doLargePut(dataMap, nKeys);
878
879         DataWalker dw = new BackwardsDataWalker(dataMap, addedDataMap) {
880                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
881                     throws DatabaseException {
882
883                     if (!prevKey.equals("")) {
884                         assertTrue(foundKey.compareTo(prevKey) <= 0);
885                     }
886                     if (addedDataMap.get(foundKey) == null) {
887                         String JavaDoc newKey = nextKey(foundKey);
888                         String JavaDoc newData =
889                             Integer.toString(dataMap.size() + nEntries);
890                         putAndVerifyCursor(cursor2,
891                                            new StringDbt(newKey),
892                                            new StringDbt(newData),
893                                            true);
894                         newKey = prevKey(foundKey);
895                         newData = Integer.toString(dataMap.size() + nEntries);
896                         putAndVerifyCursor(cursor2,
897                                            new StringDbt(newKey),
898                                            new StringDbt(newData),
899                                            true);
900                         addedDataMap.put(newKey, newData);
901                         prevKey = foundKey;
902                         assertTrue(dataMap.get(foundKey) != null);
903                         dataMap.remove(foundKey);
904                     } else {
905                         addedDataMap.remove(foundKey);
906                     }
907                 }
908
909                 OperationStatus getFirst(StringDbt foundKey,
910                      StringDbt foundData)
911                     throws DatabaseException {
912
913                     return cursor.getLast(foundKey, foundData,
914                                           LockMode.DEFAULT);
915                 }
916
917                 OperationStatus getData(StringDbt foundKey,
918                     StringDbt foundData)
919                     throws DatabaseException {
920
921                     return cursor.getPrev(foundKey, foundData,
922                                           LockMode.DEFAULT);
923                 }
924             };
925         dw.walkData();
926         if (dataMap.size() > 0) {
927             fail("dataMap still has entries");
928         }
929         if (addedDataMap.size() > 0) {
930             System.out.println(addedDataMap);
931             fail("addedDataMap still has entries");
932         }
933         assertTrue(dw.nEntries == nKeys * 2);
934     }
935
936     /**
937      * Insert N_KEYS data items into a tree. Iterate through the tree in
938      * ascending order. After each element is retrieved, insert a new random
939      * key/data pair into the tree. Ensure that the element just inserted is
940      * returned by the cursor if it is greater than the current element.
941      * Ensure that the elements are returned in ascending order. Lather,
942      * rinse, repeat.
943      */

944     public void testLargeGetPutRandomKeyForwardTraverse()
945         throws Throwable JavaDoc {
946
947         try {
948             initEnv(false);
949             doLargeGetPutRandomKeyForwardTraverse(N_KEYS);
950         } catch (Throwable JavaDoc t) {
951             t.printStackTrace();
952             throw t;
953         }
954     }
955
956     /**
957      * Helper routine for above.
958      */

959     private void doLargeGetPutRandomKeyForwardTraverse(int nKeys)
960         throws DatabaseException {
961
962         Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
963         Hashtable JavaDoc addedDataMap = new Hashtable JavaDoc();
964         doLargePut(dataMap, nKeys);
965
966         DataWalker dw = new DataWalker(dataMap, addedDataMap) {
967                 void perData(String JavaDoc foundKey, String JavaDoc foundData)
968                     throws DatabaseException {
969
970                     assertTrue(foundKey.compareTo(prevKey) >= 0);
971                     byte[] key = new byte[N_KEY_BYTES];
972                     TestUtils.generateRandomAlphaBytes(key);
973                     String JavaDoc newKey = new String JavaDoc(key);
974                     String JavaDoc newData =
975                         Integer.toString(dataMap.size() + nEntries);
976                     putAndVerifyCursor(cursor2,
977                                        new StringDbt(newKey),
978                                        new StringDbt(newData),
979                                        true);
980                     if (newKey.compareTo(foundKey) > 0) {
981                         addedDataMap.put(newKey, newData);
982                         extraVisibleEntries++;
983                     }
984                     if (addedDataMap.get(foundKey) == null) {
985                         prevKey = foundKey;
986                         assertTrue(dataMap.get(foundKey) != null);
987                         dataMap.remove(foundKey);
988                     } else {
989                         if (addedDataMap.remove(foundKey) == null) {
990                             fail(foundKey + " not found in either datamap");
991                         }
992                     }
993                 }
994             };
995         dw.walkData();
996         if (dataMap.size() > 0) {
997             fail("dataMap still has entries");
998         }
999         if (addedDataMap.size() > 0) {
1000            System.out.println(addedDataMap);
1001            fail("addedDataMap still has entries");
1002        }
1003        assertTrue(dw.nEntries == nKeys + dw.extraVisibleEntries);
1004    }
1005
1006    /**
1007     * Insert N_KEYS data items into a tree. Iterate through the tree in
1008     * descending order. After each element is retrieved, insert a new random
1009     * key/data pair into the tree. Ensure that the element just inserted is
1010     * returned by the cursor if it is less than the current element. Ensure
1011     * that the elements are returned in descending order. Lather, rinse,
1012     * repeat.
1013     */

1014    public void testLargeGetPutRandomKeyBackwardsTraverse()
1015        throws Throwable JavaDoc {
1016
1017        try {
1018            initEnv(false);
1019            doLargeGetPutRandomKeyBackwardsTraverse(N_KEYS);
1020        } catch (Throwable JavaDoc t) {
1021            t.printStackTrace();
1022            throw t;
1023        }
1024    }
1025
1026    /**
1027     * Helper routine for above.
1028     */

1029    private void doLargeGetPutRandomKeyBackwardsTraverse(int nKeys)
1030        throws DatabaseException {
1031
1032        Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
1033        Hashtable JavaDoc addedDataMap = new Hashtable JavaDoc();
1034        doLargePut(dataMap, nKeys);
1035
1036        DataWalker dw = new BackwardsDataWalker(dataMap, addedDataMap) {
1037                void perData(String JavaDoc foundKey, String JavaDoc foundData)
1038                    throws DatabaseException {
1039
1040                    if (!prevKey.equals("")) {
1041                        assertTrue(foundKey.compareTo(prevKey) <= 0);
1042                    }
1043                    byte[] key = new byte[N_KEY_BYTES];
1044                    TestUtils.generateRandomAlphaBytes(key);
1045                    String JavaDoc newKey = new String JavaDoc(key);
1046                    String JavaDoc newData =
1047                        Integer.toString(dataMap.size() + nEntries);
1048                    putAndVerifyCursor(cursor2,
1049                                       new StringDbt(newKey),
1050                                       new StringDbt(newData),
1051                                       true);
1052                    if (newKey.compareTo(foundKey) < 0) {
1053                        addedDataMap.put(newKey, newData);
1054                        extraVisibleEntries++;
1055                    }
1056                    if (addedDataMap.get(foundKey) == null) {
1057                        prevKey = foundKey;
1058                        assertTrue(dataMap.get(foundKey) != null);
1059                        dataMap.remove(foundKey);
1060                    } else {
1061                        if (addedDataMap.remove(foundKey) == null) {
1062                            fail(foundKey + " not found in either datamap");
1063                        }
1064                    }
1065                }
1066
1067                OperationStatus getFirst(StringDbt foundKey,
1068                     StringDbt foundData)
1069                    throws DatabaseException {
1070
1071                    return cursor.getLast(foundKey, foundData,
1072                                          LockMode.DEFAULT);
1073                }
1074
1075                OperationStatus getData(StringDbt foundKey,
1076                    StringDbt foundData)
1077                    throws DatabaseException {
1078
1079                    return cursor.getPrev(foundKey, foundData,
1080                                          LockMode.DEFAULT);
1081                }
1082            };
1083        dw.walkData();
1084        if (dataMap.size() > 0) {
1085            fail("dataMap still has entries");
1086        }
1087        if (addedDataMap.size() > 0) {
1088            System.out.println(addedDataMap);
1089            fail("addedDataMap still has entries");
1090        }
1091        assertTrue(dw.nEntries == nKeys + dw.extraVisibleEntries);
1092    }
1093
1094    /**
1095     * Insert N_KEYS data items into a tree. Set a btreeComparison function.
1096     * Iterate through the tree in ascending order. Ensure that the elements
1097     * are returned in ascending order.
1098     */

1099    public void testLargeGetForwardTraverseWithNormalComparisonFunction()
1100        throws Throwable JavaDoc {
1101
1102        try {
1103            tearDown();
1104            btreeComparisonFunction = btreeComparator;
1105            setUp();
1106            initEnv(false);
1107            doLargeGetForwardTraverseWithNormalComparisonFunction();
1108        } catch (Throwable JavaDoc t) {
1109            t.printStackTrace();
1110            throw t;
1111        }
1112    }
1113
1114    /**
1115     * Helper routine for above.
1116     */

1117    private void doLargeGetForwardTraverseWithNormalComparisonFunction()
1118        throws DatabaseException {
1119
1120        Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
1121        doLargePut(dataMap, N_KEYS);
1122
1123        DataWalker dw = new DataWalker(dataMap) {
1124                void perData(String JavaDoc foundKey, String JavaDoc foundData)
1125                    throws DatabaseException {
1126
1127                    assertTrue(foundKey.compareTo(prevKey) >= 0);
1128                    prevKey = foundKey;
1129                    assertTrue(dataMap.get(foundKey) != null);
1130                    dataMap.remove(foundKey);
1131                }
1132            };
1133        dw.walkData();
1134        if (dataMap.size() > 0) {
1135            fail("dataMap still has entries");
1136        }
1137        assertTrue(dw.nEntries == N_KEYS);
1138    }
1139
1140    /**
1141     * Insert N_KEYS data items into a tree. Set a reverse order
1142     * btreeComparison function. Iterate through the tree in ascending order.
1143     * Ensure that the elements are returned in ascending order.
1144     */

1145    public void testLargeGetForwardTraverseWithReverseComparisonFunction()
1146        throws Throwable JavaDoc {
1147
1148        try {
1149            tearDown();
1150            btreeComparisonFunction = reverseBtreeComparator;
1151            setUp();
1152            initEnv(false);
1153            doLargeGetForwardTraverseWithReverseComparisonFunction();
1154        } catch (Throwable JavaDoc t) {
1155            t.printStackTrace();
1156            throw t;
1157        }
1158    }
1159
1160    /**
1161     * Helper routine for above.
1162     */

1163    private void doLargeGetForwardTraverseWithReverseComparisonFunction()
1164        throws DatabaseException {
1165
1166        Hashtable JavaDoc dataMap = new Hashtable JavaDoc();
1167        doLargePut(dataMap, N_KEYS);
1168
1169        DataWalker dw = new DataWalker(dataMap) {
1170                void perData(String JavaDoc foundKey, String JavaDoc foundData)
1171                    throws DatabaseException {
1172
1173                    if (prevKey.length() != 0) {
1174                        assertTrue(foundKey.compareTo(prevKey) <= 0);
1175                    }
1176                    prevKey = foundKey;
1177                    assertTrue(dataMap.get(foundKey) != null);
1178                    dataMap.remove(foundKey);
1179                }
1180            };
1181        dw.walkData();
1182        if (dataMap.size() > 0) {
1183            fail("dataMap still has entries");
1184        }
1185        assertTrue(dw.nEntries == N_KEYS);
1186    }
1187
1188    public void testIllegalArgs()
1189        throws Throwable JavaDoc {
1190
1191        try {
1192            initEnv(false);
1193            /* Put some data so that we can get a cursor. */
1194            doSimpleCursorPuts();
1195
1196            DataWalker dw = new DataWalker(simpleDataMap) {
1197                    void perData(String JavaDoc foundKey, String JavaDoc foundData) {
1198
1199                        /* getCurrent() */
1200                        try {
1201                            cursor.getCurrent(new StringDbt(""),
1202                                              null,
1203                                              LockMode.DEFAULT);
1204                            fail("didn't throw NullPointerException");
1205                        } catch (NullPointerException JavaDoc IAE) {
1206                        } catch (DatabaseException DBE) {
1207                            fail("threw DatabaseException not " +
1208                 "NullPointerException");
1209                        }
1210
1211                        try {
1212                            cursor.getCurrent(null,
1213                                              new StringDbt(""),
1214                                              LockMode.DEFAULT);
1215                            fail("didn't throw NullPointerException");
1216                        } catch (NullPointerException JavaDoc IAE) {
1217                        } catch (DatabaseException DBE) {
1218                            fail("threw DatabaseException not " +
1219                 "NullPointerException");
1220                        }
1221
1222                        /* getFirst() */
1223                        try {
1224                            cursor.getFirst(new StringDbt(""),
1225                                            null,
1226                                            LockMode.DEFAULT);
1227                            fail("didn't throw NullPointerException");
1228                        } catch (NullPointerException JavaDoc IAE) {
1229                        } catch (DatabaseException DBE) {
1230                            fail("threw DatabaseException not " +
1231                 "NullPointerException");
1232                        }
1233
1234                        try {
1235                            cursor.getFirst(null,
1236                                            new StringDbt(""),
1237                                            LockMode.DEFAULT);
1238                            fail("didn't throw NullPointerException");
1239                        } catch (NullPointerException JavaDoc IAE) {
1240                        } catch (DatabaseException DBE) {
1241                            fail("threw DatabaseException not " +
1242                 "NullPointerException");
1243                        }
1244
1245                        /* getNext(), getPrev, getNextDup,
1246                           getNextNoDup, getPrevNoDup */

1247                        try {
1248                            cursor.getNext(new StringDbt(""),
1249                                           null,
1250                                           LockMode.DEFAULT);
1251                            fail("didn't throw NullPointerException");
1252                        } catch (NullPointerException JavaDoc IAE) {
1253                        } catch (DatabaseException DBE) {
1254                            fail("threw DatabaseException not " +
1255                 "NullPointerException");
1256                        }
1257
1258                        try {
1259                            cursor.getNext(null,
1260                                           new StringDbt(""),
1261                                           LockMode.DEFAULT);
1262                            fail("didn't throw NullPointerException");
1263                        } catch (NullPointerException JavaDoc IAE) {
1264                        } catch (DatabaseException DBE) {
1265                            fail("threw DatabaseException not " +
1266                 "NullPointerException");
1267                        }
1268
1269                        /* putXXX() */
1270                        try {
1271                            cursor.put(new StringDbt(""), null);
1272                            fail("didn't throw NullPointerException");
1273                        } catch (NullPointerException JavaDoc IAE) {
1274                        } catch (DatabaseException DBE) {
1275                            fail("threw DatabaseException not " +
1276                 "NullPointerException");
1277                        }
1278
1279                        try {
1280                            cursor.put(null, new StringDbt(""));
1281                            fail("didn't throw NullPointerException");
1282                        } catch (NullPointerException JavaDoc IAE) {
1283                        } catch (DatabaseException DBE) {
1284                            fail("threw DatabaseException not " +
1285                 "NullPointerException");
1286                        }
1287                    }
1288                };
1289            dw.walkData();
1290            assertTrue(dw.nEntries == simpleKeyStrings.length);
1291        } catch (Throwable JavaDoc t) {
1292            t.printStackTrace();
1293            throw t;
1294        }
1295    }
1296
1297    public void testCursorOutOfBoundsBackwards()
1298        throws Throwable JavaDoc {
1299
1300        try {
1301            initEnv(false);
1302            int count = 0;
1303            doSimpleCursorPuts();
1304
1305            StringDbt foundKey = new StringDbt();
1306            StringDbt foundData = new StringDbt();
1307            OperationStatus status;
1308            status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT);
1309
1310            assertEquals(OperationStatus.SUCCESS, status);
1311            assertEquals("aaa", foundKey.getString());
1312            assertEquals("four", foundData.getString());
1313
1314            status = cursor.getPrev(foundKey, foundData, LockMode.DEFAULT);
1315
1316            assertEquals(OperationStatus.NOTFOUND, status);
1317
1318            status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
1319
1320            assertEquals(OperationStatus.SUCCESS, status);
1321            assertEquals("bar", foundKey.getString());
1322            assertEquals("two", foundData.getString());
1323        } catch (Throwable JavaDoc t) {
1324            t.printStackTrace();
1325            throw t;
1326        }
1327    }
1328
1329    public void testCursorOutOfBoundsForwards()
1330        throws Throwable JavaDoc {
1331
1332        try {
1333            initEnv(false);
1334            int count = 0;
1335            doSimpleCursorPuts();
1336
1337            StringDbt foundKey = new StringDbt();
1338            StringDbt foundData = new StringDbt();
1339            OperationStatus status;
1340            status = cursor.getLast(foundKey, foundData, LockMode.DEFAULT);
1341
1342            assertEquals(OperationStatus.SUCCESS, status);
1343            assertEquals("quux", foundKey.getString());
1344            assertEquals("seven", foundData.getString());
1345
1346            status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
1347            assertEquals(OperationStatus.NOTFOUND, status);
1348
1349            status = cursor.getPrev(foundKey, foundData, LockMode.DEFAULT);
1350
1351            assertEquals(OperationStatus.SUCCESS, status);
1352            assertEquals("mumble", foundKey.getString());
1353            assertEquals("eight", foundData.getString());
1354        } catch (Throwable JavaDoc t) {
1355            t.printStackTrace();
1356            throw t;
1357        }
1358    }
1359
1360    public void testTwiceClosedCursor()
1361    throws Throwable JavaDoc {
1362
1363        try {
1364            initEnv(false);
1365            doSimpleCursorPuts();
1366            Cursor cursor = exampleDb.openCursor(null, null);
1367            cursor.close();
1368            try {
1369                cursor.close();
1370                fail("didn't catch DatabaseException for twice closed cursor");
1371            } catch (DatabaseException DBE) {
1372            }
1373            try {
1374                cursor.put
1375                    (new StringDbt("bogus"), new StringDbt("thingy"));
1376                fail("didn't catch DatabaseException for re-use of cursor");
1377            } catch (DatabaseException DBE) {
1378            }
1379        } catch (Throwable JavaDoc t) {
1380            t.printStackTrace();
1381            throw t;
1382        }
1383    }
1384
1385    public void testTreeSplittingWithDeletedIdKey()
1386    throws Throwable JavaDoc {
1387
1388    treeSplittingWithDeletedIdKeyWorker();
1389    }
1390
1391    public void testTreeSplittingWithDeletedIdKeyWithUserComparison()
1392    throws Throwable JavaDoc {
1393
1394    tearDown();
1395    btreeComparisonFunction = btreeComparator;
1396    setUp();
1397    treeSplittingWithDeletedIdKeyWorker();
1398    }
1399
1400    static private Comparator JavaDoc btreeComparator = new BtreeComparator();
1401
1402    static private Comparator JavaDoc reverseBtreeComparator =
1403        new ReverseBtreeComparator();
1404
1405    private void treeSplittingWithDeletedIdKeyWorker()
1406    throws Throwable JavaDoc {
1407
1408    initEnv(false);
1409    StringDbt data = new StringDbt("data");
1410
1411    Cursor cursor = exampleDb.openCursor(null, null);
1412    cursor.put(new StringDbt("AGPFX"), data);
1413    cursor.put(new StringDbt("AHHHH"), data);
1414    cursor.put(new StringDbt("AIIII"), data);
1415    cursor.put(new StringDbt("AAAAA"), data);
1416    cursor.put(new StringDbt("AABBB"), data);
1417    cursor.put(new StringDbt("AACCC"), data);
1418    cursor.close();
1419    exampleDb.delete(null, new StringDbt("AGPFX"));
1420    exampleEnv.compress();
1421    cursor = exampleDb.openCursor(null, null);
1422    cursor.put(new StringDbt("AAAAB"), data);
1423    cursor.put(new StringDbt("AAAAC"), data);
1424    cursor.close();
1425    validateDatabase();
1426    }
1427}
1428
Popular Tags