File: INUtilizationTest.java

package info (click to toggle)
libdb-je-java 3.3.98-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,052 kB
  • sloc: java: 153,077; xml: 2,034; makefile: 3
file content (970 lines) | stat: -rw-r--r-- 33,006 bytes parent folder | download | duplicates (3)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: INUtilizationTest.java,v 1.27.2.2 2010/01/04 15:30:42 cwl Exp $
 */

package com.sleepycat.je.cleaner;

import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.je.CheckpointConfig;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.DatabaseImpl;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.log.FileManager;
import com.sleepycat.je.log.LogEntryType;
import com.sleepycat.je.log.SearchFileReader;
import com.sleepycat.je.tree.IN;
import com.sleepycat.je.util.TestUtils;
import com.sleepycat.je.utilint.CmdUtil;
import com.sleepycat.je.utilint.DbLsn;

/**
 * Test utilization counting of INs.
 */
public class INUtilizationTest extends TestCase {

    private static final String DB_NAME = "foo";

    private static final CheckpointConfig forceConfig = new CheckpointConfig();
    static {
        forceConfig.setForce(true);
    }

    private File envHome;
    private Environment env;
    private EnvironmentImpl envImpl;
    private Database db;
    private DatabaseImpl dbImpl;
    private Transaction txn;
    private Cursor cursor;
    private boolean dups = false;
    private DatabaseEntry keyEntry = new DatabaseEntry();
    private DatabaseEntry dataEntry = new DatabaseEntry();
    private boolean truncateOrRemoveDone;

    public INUtilizationTest() {
        envHome = new File(System.getProperty(TestUtils.DEST_DIR));
    }

    public void setUp()
        throws IOException, DatabaseException {

        TestUtils.removeLogFiles("Setup", envHome, false);
        TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX);
    }

    public void tearDown()
        throws IOException, DatabaseException {

        try {
            if (env != null) {
                //env.close();
            }
        } catch (Throwable e) {
            System.out.println("tearDown: " + e);
        }

        try {
            //*
            TestUtils.removeLogFiles("tearDown", envHome, true);
            TestUtils.removeFiles("tearDown", envHome, FileManager.DEL_SUFFIX);
            //*/
        } catch (Throwable e) {
            System.out.println("tearDown: " + e);
        }

        envHome = null;
        env = null;
        envImpl = null;
        db = null;
        dbImpl = null;
        txn = null;
        cursor = null;
        keyEntry = null;
        dataEntry = null;
    }

    /**
     * Opens the environment and database.
     */
    private void openEnv()
        throws DatabaseException {

        EnvironmentConfig config = TestUtils.initEnvConfig();
	DbInternal.disableParameterValidation(config);
        config.setTransactional(true);
        config.setTxnNoSync(true);
        config.setAllowCreate(true);
        /* Do not run the daemons. */
        config.setConfigParam
            (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
        config.setConfigParam
            (EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false");
        config.setConfigParam
	    (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
        config.setConfigParam
            (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false");
        /* Use a tiny log file size to write one node per file. */
        config.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(),
                              Integer.toString(64));
        env = new Environment(envHome, config);
        envImpl = DbInternal.envGetEnvironmentImpl(env);

        /* Speed up test that uses lots of very small files. */
        envImpl.getFileManager().setSyncAtFileEnd(false);

        openDb();
    }

    /**
     * Opens the database.
     */
    private void openDb()
        throws DatabaseException {

        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        dbConfig.setAllowCreate(true);
        dbConfig.setSortedDuplicates(dups);
        db = env.openDatabase(null, DB_NAME, dbConfig);
        dbImpl = DbInternal.dbGetDatabaseImpl(db);
    }

    private void closeEnv(boolean doCheckpoint)
        throws DatabaseException {

        closeEnv(doCheckpoint,
                 true,  // expectAccurateObsoleteLNCount
                 true); // expectAccurateObsoleteLNSize
    }

    private void closeEnv(boolean doCheckpoint,
                          boolean expectAccurateObsoleteLNCount)
        throws DatabaseException {

        closeEnv(doCheckpoint,
                 expectAccurateObsoleteLNCount,
                 expectAccurateObsoleteLNCount);
    }

    /**
     * Closes the environment and database.
     *
     * @param expectAccurateObsoleteLNCount should be false when a deleted LN
     * is not counted properly by recovery because its parent INs were flushed
     * and the obsolete LN was not found in the tree.
     *
     * @param expectAccurateObsoleteLNSize should be false when a tree walk is
     * performed for truncate/remove or an abortLsn is counted by recovery.
     */
    private void closeEnv(boolean doCheckpoint,
                          boolean expectAccurateObsoleteLNCount,
                          boolean expectAccurateObsoleteLNSize)
        throws DatabaseException {

        /*
         * We pass expectAccurateDbUtilization as false when
         * truncateOrRemoveDone, because the database utilization info for that
         * database is now gone.
         */
        VerifyUtils.verifyUtilization
            (envImpl, expectAccurateObsoleteLNCount,
             expectAccurateObsoleteLNSize,
             !truncateOrRemoveDone); // expectAccurateDbUtilization

        if (db != null) {
            db.close();
            db = null;
            dbImpl = null;
        }
        if (envImpl != null) {
            envImpl.close(doCheckpoint);
            envImpl = null;
            env = null;
        }
    }

    /**
     * Initial setup for all tests -- open env, put one record (or two for
     * dups) and sync.
     */
    private void openAndWriteDatabase()
        throws DatabaseException {

        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Put one record. */
        IntegerBinding.intToEntry(0, keyEntry);
        IntegerBinding.intToEntry(0, dataEntry);
        cursor.put(keyEntry, dataEntry);

        /* Add a duplicate. */
        if (dups) {
            IntegerBinding.intToEntry(1, dataEntry);
            cursor.put(keyEntry, dataEntry);
        }

        /* Commit the txn to avoid crossing the checkpoint boundary. */
        cursor.close();
        txn.commit();

        /* Checkpoint to the root so nothing is dirty. */
        env.sync();

        /* Open a txn and cursor for use by the test case. */
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* If we added a duplicate, move cursor back to the first record. */
        cursor.getFirst(keyEntry, dataEntry, null);

        /* Expect that BIN and parent IN files are not obsolete. */
        long binFile = getBINFile(cursor);
        long inFile = getINFile(cursor);
        expectObsolete(binFile, false);
        expectObsolete(inFile, false);
    }

    /**
     * Tests that BIN and IN utilization counting works.
     */
    public void testBasic()
        throws DatabaseException {

        openAndWriteDatabase();
        long binFile = getBINFile(cursor);
        long inFile = getINFile(cursor);

        /* Update to make BIN dirty. */
        cursor.put(keyEntry, dataEntry);

        /* Checkpoint */
        env.checkpoint(forceConfig);

        /* After checkpoint, expect BIN file is obsolete but not IN. */
        expectObsolete(binFile, true);
        expectObsolete(inFile, false);
        assertTrue(binFile != getBINFile(cursor));
        assertEquals(inFile, getINFile(cursor));

        /* After second checkpoint, IN file becomes obsolete also. */
        env.checkpoint(forceConfig);

        /* Both BIN and IN are obsolete. */
        expectObsolete(binFile, true);
        expectObsolete(inFile, true);
        assertTrue(binFile != getBINFile(cursor));
        assertTrue(inFile != getINFile(cursor));

        /* Expect that new files are not obsolete. */
        long binFile2 = getBINFile(cursor);
        long inFile2 = getINFile(cursor);
        expectObsolete(binFile2, false);
        expectObsolete(inFile2, false);

        cursor.close();
        txn.commit();
        closeEnv(true);
    }

    /**
     * Performs testBasic with duplicates.
     */
    public void testBasicDup()
        throws DatabaseException {

        dups = true;
        testBasic();
    }

    /**
     * Similar to testBasic, but logs INs explicitly and performs recovery to
     * ensure utilization recovery works.
     */
    public void testRecovery()
        throws DatabaseException {

        openAndWriteDatabase();
        long binFile = getBINFile(cursor);
        long inFile = getINFile(cursor);

        /* Close normally and reopen. */
        cursor.close();
        txn.commit();
        closeEnv(true);
        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Position cursor to load BIN and IN. */
        cursor.getSearchKey(keyEntry, dataEntry, null);

        /* Expect BIN and IN files have not changed. */
        assertEquals(binFile, getBINFile(cursor));
        assertEquals(inFile, getINFile(cursor));
        expectObsolete(binFile, false);
        expectObsolete(inFile, false);

        /*
         * Log explicitly since we have no way to do a partial checkpoint.
         * The BIN is logged provisionally and the IN non-provisionally.
         */
        TestUtils.logBINAndIN(env, cursor);

        /* Expect to obsolete the BIN and IN. */
        expectObsolete(binFile, true);
        expectObsolete(inFile, true);
        assertTrue(binFile != getBINFile(cursor));
        assertTrue(inFile != getINFile(cursor));

        /* Save current BIN and IN files. */
        long binFile2 = getBINFile(cursor);
        long inFile2 = getINFile(cursor);
        expectObsolete(binFile2, false);
        expectObsolete(inFile2, false);

        /* Shutdown without a checkpoint and reopen. */
        cursor.close();
        txn.commit();
        closeEnv(false);
        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Sync to make all INs non-dirty. */
        env.sync();

        /* Position cursor to load BIN and IN. */
        cursor.getSearchKey(keyEntry, dataEntry, null);

        /* Expect that recovery counts BIN and IN as obsolete. */
        expectObsolete(binFile, true);
        expectObsolete(inFile, true);
        assertTrue(binFile != getBINFile(cursor));
        assertTrue(inFile != getINFile(cursor));

        /*
         * Even though it is provisional, expect that current BIN is not
         * obsolete because it is not part of partial checkpoint.  This is
         * similar to what happens with a split.  The current IN is not
         * obsolete either (nor is it provisional).
         */
        assertTrue(binFile2 == getBINFile(cursor));
        assertTrue(inFile2 == getINFile(cursor));
        expectObsolete(binFile2, false);
        expectObsolete(inFile2, false);

        /* Update to make BIN dirty. */
        cursor.put(keyEntry, dataEntry);

        /* Check current BIN and IN files. */
        assertTrue(binFile2 == getBINFile(cursor));
        assertTrue(inFile2 == getINFile(cursor));
        expectObsolete(binFile2, false);
        expectObsolete(inFile2, false);

        /* Close normally and reopen to cause checkpoint of dirty BIN/IN. */
        cursor.close();
        txn.commit();
        closeEnv(true);
        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Position cursor to load BIN and IN. */
        cursor.getSearchKey(keyEntry, dataEntry, null);

        /* Expect BIN was checkpointed during close but not IN. */
        assertTrue(binFile2 != getBINFile(cursor));
        assertEquals(inFile2, getINFile(cursor));
        expectObsolete(binFile2, true);
        expectObsolete(inFile2, false);

        /* After second checkpoint, IN file becomes obsolete also. */
        env.checkpoint(forceConfig);

        /* Both BIN and IN are obsolete. */
        assertTrue(binFile2 != getBINFile(cursor));
        assertTrue(inFile2 != getINFile(cursor));
        expectObsolete(binFile2, true);
        expectObsolete(inFile2, true);

        cursor.close();
        txn.commit();
        closeEnv(true);
    }

    /**
     * Performs testRecovery with duplicates.
     */
    public void testRecoveryDup()
        throws DatabaseException {

        dups = true;
        testRecovery();
    }

    /**
     * Tests that in a partial checkpoint (CkptStart with no CkptEnd) all
     * provisional INs are counted as obsolete.
     */
    public void testPartialCheckpoint()
        throws DatabaseException, IOException {

        openAndWriteDatabase();
        long binFile = getBINFile(cursor);
        long inFile = getINFile(cursor);

        /* Close with partial checkpoint and reopen. */
        cursor.close();
        txn.commit();
        performPartialCheckpoint(true); // truncateUtilizationInfo
        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Position cursor to load BIN and IN. */
        cursor.getSearchKey(keyEntry, dataEntry, null);

        /* Expect BIN and IN files have not changed. */
        assertEquals(binFile, getBINFile(cursor));
        assertEquals(inFile, getINFile(cursor));
        expectObsolete(binFile, false);
        expectObsolete(inFile, false);

        /* Update to make BIN dirty. */
        cursor.put(keyEntry, dataEntry);

        /* Force IN dirty so that BIN is logged provisionally. */
        TestUtils.getIN(TestUtils.getBIN(cursor)).setDirty(true);

        /* Check current BIN and IN files. */
        assertTrue(binFile == getBINFile(cursor));
        assertTrue(inFile == getINFile(cursor));
        expectObsolete(binFile, false);
        expectObsolete(inFile, false);

        /* Close with partial checkpoint and reopen. */
        cursor.close();
        txn.commit();
        performPartialCheckpoint(true);  // truncateUtilizationInfo
        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Position cursor to load BIN and IN. */
        cursor.getSearchKey(keyEntry, dataEntry, null);

        /* Expect BIN and IN files are obsolete. */
        assertTrue(binFile != getBINFile(cursor));
        assertTrue(inFile != getINFile(cursor));
        expectObsolete(binFile, true);
        expectObsolete(inFile, true);

        /*
         * Expect that the current BIN is obsolete because it was provisional,
         * and provisional nodes following CkptStart are counted obsolete
         * even if that is sometimes incorrect.  The parent IN file is not
         * obsolete because it is not provisonal.
         */
        long binFile2 = getBINFile(cursor);
        long inFile2 = getINFile(cursor);
        expectObsolete(binFile2, true);
        expectObsolete(inFile2, false);

        /*
         * Now repeat the test above but do not truncate the FileSummaryLNs.
         * The counting will be accurate because the FileSummaryLNs override
         * what is counted manually during recovery.
         */

        /* Update to make BIN dirty. */
        cursor.put(keyEntry, dataEntry);

        /* Close with partial checkpoint and reopen. */
        cursor.close();
        txn.commit();
        performPartialCheckpoint(false,  // truncateUtilizationInfo
                                 true,   // expectAccurateObsoleteLNCount
                                 false); // expectAccurateObsoleteLNSize

        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Position cursor to load BIN and IN. */
        cursor.getSearchKey(keyEntry, dataEntry, null);

        /* The prior BIN file is now double-counted as obsolete. */
        assertTrue(binFile2 != getBINFile(cursor));
        assertTrue(inFile2 != getINFile(cursor));
        expectObsolete(binFile2, 2);
        expectObsolete(inFile2, 1);

        /* Expect current BIN and IN files are not obsolete. */
        binFile2 = getBINFile(cursor);
        inFile2 = getINFile(cursor);
        expectObsolete(binFile2, false);
        expectObsolete(inFile2, false);

        cursor.close();
        txn.commit();
        closeEnv(true,   // doCheckpoint
                 true,   // expectAccurateObsoleteLNCount
                 false); // expectAccurateObsoleteLNSize
    }

    /**
     * Performs testPartialCheckpoint with duplicates.
     */
    public void testPartialCheckpointDup()
        throws DatabaseException, IOException {

        dups = true;
        testPartialCheckpoint();
    }

    /**
     * Tests that deleting a subtree (by deleting the last LN in a BIN) is
     * counted correctly.
     */
    public void testDelete()
        throws DatabaseException, IOException {

        openAndWriteDatabase();
        long binFile = getBINFile(cursor);
        long inFile = getINFile(cursor);

        /* Close normally and reopen. */
        cursor.close();
        txn.commit();
        closeEnv(true);
        openEnv();
        txn = env.beginTransaction(null, null);
        cursor = db.openCursor(txn, null);

        /* Position cursor to load BIN and IN. */
        cursor.getSearchKey(keyEntry, dataEntry, null);

        /* Expect BIN and IN are still not obsolete. */
        assertEquals(binFile, getBINFile(cursor));
        assertEquals(inFile, getINFile(cursor));
        expectObsolete(binFile, false);
        expectObsolete(inFile, false);

        if (dups) {
            /* Delete both records. */
            OperationStatus status;
            status = cursor.delete();
            assertSame(OperationStatus.SUCCESS, status);
            cursor.getNext(keyEntry, dataEntry, null);
            status = cursor.delete();
            assertSame(OperationStatus.SUCCESS, status);
        } else {

            /*
             * Add records until we move to the next BIN, so that the
             * compressor would not need to delete the root in order to delete
             * the BIN (deleting the root is not configured by default).
             */
            int keyVal = 0;
            while (binFile == getBINFile(cursor)) {
                keyVal += 1;
                IntegerBinding.intToEntry(keyVal, keyEntry);
                cursor.put(keyEntry, dataEntry);
            }
            binFile = getBINFile(cursor);
            inFile = getINFile(cursor);

            /* Delete all records in the last BIN. */
            while (binFile == getBINFile(cursor)) {
                cursor.delete();
                cursor.getLast(keyEntry, dataEntry, null);
            }
        }

        /* Compressor daemon is not running -- they're not obsolete yet. */
        expectObsolete(binFile, false);
        expectObsolete(inFile, false);

        /* Close cursor and compress. */
        cursor.close();
        txn.commit();
        env.compress();

        /*
         * Now expect BIN and IN to be obsolete.
         */
        expectObsolete(binFile, true);
        expectObsolete(inFile, true);

        /* Close with partial checkpoint and reopen. */
        performPartialCheckpoint(true); // truncateUtilizationInfo
        openEnv();

        /*
         * Expect both files to be obsolete after recovery, because the
         * FileSummaryLN and MapLN was written prior to the checkpoint during
         * compression.
         */
        expectObsolete(binFile, true);
        expectObsolete(inFile, true);

        /*
         * expectAccurateObsoleteLNCount is false because the deleted LN is not
         * counted obsolete correctly as described in RecoveryManager
         * redoUtilizationInfo.
         */
        closeEnv(true,   // doCheckpoint
                 false); // expectAccurateObsoleteLNCount
    }

    /**
     * Performs testDelete with duplicates.
     */
    public void testDeleteDup()
        throws DatabaseException, IOException {

        dups = true;
        testDelete();
    }

    /**
     * Tests that truncating a database is counted correctly.
     * Tests recovery also.
     */
    public void testTruncate()
        throws DatabaseException, IOException {

        /* Expect inaccurate LN sizes only if we force a tree walk. */
        final boolean expectAccurateObsoleteLNSize =
            !DatabaseImpl.forceTreeWalkForTruncateAndRemove;

        openAndWriteDatabase();
        long binFile = getBINFile(cursor);
        long inFile = getINFile(cursor);

        /* Close normally and reopen. */
        cursor.close();
        txn.commit();
        closeEnv(true,   // doCheckpoint
                 true,   // expectAccurateObsoleteLNCount
                 expectAccurateObsoleteLNSize);
        openEnv();
        db.close();
        db = null;
        /* Truncate. */
        txn = env.beginTransaction(null, null);
        env.truncateDatabase(txn, DB_NAME, false /* returnCount */);
        truncateOrRemoveDone = true;
        txn.commit();

        /*
         * Expect BIN and IN are obsolete.  Do not check DbFileSummary when we
         * truncate/remove, since the old DatabaseImpl is gone.
         */
        expectObsolete(binFile, true, false /*checkDbFileSummary*/);
        expectObsolete(inFile, true, false /*checkDbFileSummary*/);

        /* Close with partial checkpoint and reopen. */
        performPartialCheckpoint(true,   // truncateUtilizationInfo
                                 true,   // expectAccurateObsoleteLNCount
                                 expectAccurateObsoleteLNSize);
        openEnv();

        /* Expect BIN and IN are counted obsolete during recovery. */
        expectObsolete(binFile, true, false /*checkDbFileSummary*/);
        expectObsolete(inFile, true, false /*checkDbFileSummary*/);

        /*
         * expectAccurateObsoleteLNSize is false because the size of the
         * deleted NameLN is not counted during recovery, as with other
         * abortLsns as described in RecoveryManager redoUtilizationInfo.
         */
        closeEnv(true,   // doCheckpoint
                 true,   // expectAccurateObsoleteLNCount
                 false); // expectAccurateObsoleteLNSize
    }

    /**
     * Tests that truncating a database is counted correctly.
     * Tests recovery also.
     */
    public void testRemove()
        throws DatabaseException, IOException {

        /* Expect inaccurate LN sizes only if we force a tree walk. */
        final boolean expectAccurateObsoleteLNSize =
            !DatabaseImpl.forceTreeWalkForTruncateAndRemove;

        openAndWriteDatabase();
        long binFile = getBINFile(cursor);
        long inFile = getINFile(cursor);

        /* Close normally and reopen. */
        cursor.close();
        txn.commit();
        closeEnv(true,   // doCheckpoint
                 true,   // expectAccurateObsoleteLNCount
                 expectAccurateObsoleteLNSize);
        openEnv();

        /* Remove. */
        db.close();
        db = null;
        txn = env.beginTransaction(null, null);
        env.removeDatabase(txn, DB_NAME);
        truncateOrRemoveDone = true;
        txn.commit();

        /*
         * Expect BIN and IN are obsolete.  Do not check DbFileSummary when we
         * truncate/remove, since the old DatabaseImpl is gone.
         */
        expectObsolete(binFile, true, false /*checkDbFileSummary*/);
        expectObsolete(inFile, true, false /*checkDbFileSummary*/);

        /* Close with partial checkpoint and reopen. */
        performPartialCheckpoint(true,   // truncateUtilizationInfo
                                 true,   // expectAccurateObsoleteLNCount
                                 expectAccurateObsoleteLNSize);
        openEnv();

        /* Expect BIN and IN are counted obsolete during recovery. */
        expectObsolete(binFile, true, false /*checkDbFileSummary*/);
        expectObsolete(inFile, true, false /*checkDbFileSummary*/);

        /*
         * expectAccurateObsoleteLNCount is false because the deleted NameLN is
         * not counted obsolete correctly as described in RecoveryManager
         * redoUtilizationInfo.
         */
        closeEnv(true,   // doCheckpoint
                 false); // expectAccurateObsoleteLNCount
    }

    /*
     * The xxxForceTreeWalk tests set the DatabaseImpl
     * forceTreeWalkForTruncateAndRemove field to true, which will force a walk
     * of the tree to count utilization during truncate/remove, rather than
     * using the per-database info.  This is used to test the "old technique"
     * for counting utilization, which is now used only if the database was
     * created prior to log version 6.
     */

    public void testTruncateForceTreeWalk()
        throws Exception {

        DatabaseImpl.forceTreeWalkForTruncateAndRemove = true;
        try {
            testTruncate();
        } finally {
            DatabaseImpl.forceTreeWalkForTruncateAndRemove = false;
        }
    }

    public void testRemoveForceTreeWalk()
        throws Exception {

        DatabaseImpl.forceTreeWalkForTruncateAndRemove = true;
        try {
            testRemove();
        } finally {
            DatabaseImpl.forceTreeWalkForTruncateAndRemove = false;
        }
    }

    private void expectObsolete(long file, boolean obsolete)
        throws DatabaseException {

        expectObsolete(file, obsolete, true /*checkDbFileSummary*/);
    }

    private void expectObsolete(long file,
                                boolean obsolete,
                                boolean checkDbFileSummary)
        throws DatabaseException {

        FileSummary fileSummary = getFileSummary(file);
        assertEquals("totalINCount",
                     1, fileSummary.totalINCount);
        assertEquals("obsoleteINCount",
                     obsolete ? 1 : 0, fileSummary.obsoleteINCount);

        if (checkDbFileSummary) {
            DbFileSummary dbFileSummary = getDbFileSummary(file);
            assertEquals("db totalINCount",
                         1, dbFileSummary.totalINCount);
            assertEquals("db obsoleteINCount",
                         obsolete ? 1 : 0, dbFileSummary.obsoleteINCount);
        }
    }

    private void expectObsolete(long file, int obsoleteCount)
        throws DatabaseException {

        FileSummary fileSummary = getFileSummary(file);
        assertEquals("totalINCount",
                     1, fileSummary.totalINCount);
        assertEquals("obsoleteINCount",
                     obsoleteCount, fileSummary.obsoleteINCount);

        DbFileSummary dbFileSummary = getDbFileSummary(file);
        assertEquals("db totalINCount",
                     1, dbFileSummary.totalINCount);
        assertEquals("db obsoleteINCount",
                     obsoleteCount, dbFileSummary.obsoleteINCount);
    }

    private long getINFile(Cursor cursor)
        throws DatabaseException {

        IN in = TestUtils.getIN(TestUtils.getBIN(cursor));
        long lsn = in.getLastFullVersion();
        assertTrue(lsn != DbLsn.NULL_LSN);
        return DbLsn.getFileNumber(lsn);
    }

    private long getBINFile(Cursor cursor)
        throws DatabaseException {

        long lsn = TestUtils.getBIN(cursor).getLastFullVersion();
        assertTrue(lsn != DbLsn.NULL_LSN);
        return DbLsn.getFileNumber(lsn);
    }

    /**
     * Returns the utilization summary for a given log file.
     */
    private FileSummary getFileSummary(long file)
        throws DatabaseException {

	return (FileSummary) envImpl.getUtilizationProfile()
                                    .getFileSummaryMap(true)
                                    .get(new Long(file));
    }

    /**
     * Returns the per-database utilization summary for a given log file.
     */
    private DbFileSummary getDbFileSummary(long file) {
        return dbImpl.getDbFileSummary
            (new Long(file), false /*willModify*/);
    }

    private void performPartialCheckpoint(boolean truncateUtilizationInfo)
        throws DatabaseException, IOException {

        performPartialCheckpoint(truncateUtilizationInfo,
                                 true,  // expectAccurateObsoleteLNCount
                                 true); // expectAccurateObsoleteLNSize
    }

    private void performPartialCheckpoint(boolean truncateUtilizationInfo,
                                          boolean
                                          expectAccurateObsoleteLNCount)
        throws DatabaseException, IOException {

        performPartialCheckpoint(truncateUtilizationInfo,
                                 expectAccurateObsoleteLNCount,
                                 expectAccurateObsoleteLNCount);
    }

    /**
     * Performs a checkpoint and truncates the log before the last CkptEnd.  If
     * truncateUtilizationInfo is true, truncates before the FileSummaryLNs
     * that appear at the end of the checkpoint.  The environment should be
     * open when this method is called, and it will be closed when it returns.
     */
    private void performPartialCheckpoint
                    (boolean truncateUtilizationInfo,
                     boolean expectAccurateObsoleteLNCount,
                     boolean expectAccurateObsoleteLNSize)
        throws DatabaseException, IOException {

        /* Do a normal checkpoint. */
        env.checkpoint(forceConfig);
        long eofLsn = envImpl.getFileManager().getNextLsn();
        long lastLsn = envImpl.getFileManager().getLastUsedLsn();
        long truncateLsn;

        /* Searching backward from end, find last CkptEnd. */
        SearchFileReader searcher =
            new SearchFileReader(envImpl, 1000, false, lastLsn, eofLsn,
                                 LogEntryType.LOG_CKPT_END);
        assertTrue(searcher.readNextEntry());
        long ckptEnd = searcher.getLastLsn();

        if (truncateUtilizationInfo) {

            /* Searching backward from CkptEnd, find last CkptStart. */
            searcher =
                new SearchFileReader(envImpl, 1000, false, ckptEnd, eofLsn,
                                     LogEntryType.LOG_CKPT_START);
            assertTrue(searcher.readNextEntry());
            long ckptStart = searcher.getLastLsn();

            /*
             * Searching forward from CkptStart, find first MapLN.  MapLNs are
             * written after writing root INs and before all FileSummaryLNs.
             * This will find the position at which to truncate all MapLNs and
             * FileSummaryLNs, but not INs below the mapping tree.
             */
            searcher =
                new SearchFileReader(envImpl, 1000, true, ckptStart, eofLsn,
                                     LogEntryType.LOG_MAPLN);
            assertTrue(searcher.readNextEntry());
            truncateLsn = searcher.getLastLsn();
        } else {
            truncateLsn = ckptEnd;
        }

        /*
         * Close without another checkpoint, although it doesn't matter since
         * we would truncate before it.
         */
        closeEnv(false, // doCheckpoint
                 expectAccurateObsoleteLNCount,
                 expectAccurateObsoleteLNSize);

        /* Truncate the log. */
        EnvironmentImpl cmdEnv =
	    CmdUtil.makeUtilityEnvironment(envHome, false);
        cmdEnv.getFileManager().truncateLog(DbLsn.getFileNumber(truncateLsn),
                                            DbLsn.getFileOffset(truncateLsn));
        cmdEnv.close(false);

        /* Delete files following the truncated file. */
        String[] fileNames = envHome.list();
        for (int i = 0; i < fileNames.length; i += 1) {
            String name = fileNames[i];
            if (name.endsWith(".jdb")) {
                String numStr = name.substring(0, name.length() - 4);
                long fileNum = Long.parseLong(numStr, 16);
                if (fileNum > DbLsn.getFileNumber(truncateLsn)) {
                    assertTrue(new File(envHome, name).delete());
                }
            }
        }
    }
}