summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java
blob: 04cc87d293875cb149404096a5526e5136147470 (plain)
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
/*
 * Copyright 2006-2012 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.carbonado.repo.sleepycat;

import java.io.File;
import java.io.PrintStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amazon.carbonado.ConfigurationException;
import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.IsolationLevel;
import com.amazon.carbonado.MalformedArgumentException;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.Transaction;
import com.amazon.carbonado.TriggerFactory;

import com.amazon.carbonado.capability.Capability;
import com.amazon.carbonado.capability.IndexInfo;
import com.amazon.carbonado.capability.IndexInfoCapability;
import com.amazon.carbonado.capability.ShutdownCapability;
import com.amazon.carbonado.capability.StorableInfoCapability;

import com.amazon.carbonado.info.StorableIntrospector;

import com.amazon.carbonado.layout.Layout;
import com.amazon.carbonado.layout.LayoutCapability;
import com.amazon.carbonado.layout.LayoutFactory;

import com.amazon.carbonado.qe.RepositoryAccess;
import com.amazon.carbonado.qe.StorageAccess;

import com.amazon.carbonado.raw.StorableCodecFactory;

import com.amazon.carbonado.sequence.SequenceCapability;
import com.amazon.carbonado.sequence.SequenceValueGenerator;
import com.amazon.carbonado.sequence.SequenceValueProducer;

import com.amazon.carbonado.spi.AbstractRepository;
import com.amazon.carbonado.spi.ExceptionTransformer;
import com.amazon.carbonado.spi.LobEngine;

import com.amazon.carbonado.txn.TransactionManager;
import com.amazon.carbonado.txn.TransactionScope;

/**
 * Repository implementation backed by a Berkeley DB. Data is encoded in the
 * BDB in a specialized format, and so this repository should not be used to
 * open arbitrary Berkeley databases. BDBRepository has total schema ownership,
 * and so it updates type definitions in the storage layer automatically.
 *
 * @author Brian S O'Neill
 * @author Vidya Iyer
 * @author Nicole Deflaux
 * @author bcastill
 */
abstract class BDBRepository<Txn> extends AbstractRepository<Txn>
    implements Repository,
               RepositoryAccess,
               IndexInfoCapability,
               HotBackupCapability,
               CheckpointCapability,
               EnvironmentCapability,
               ShutdownCapability,
               StorableInfoCapability,
               SequenceCapability,
               LayoutCapability
{
    private final Log mLog = LogFactory.getLog(getClass());

    private final boolean mIsMaster;
    final Iterable<TriggerFactory> mTriggerFactories;
    private final AtomicReference<Repository> mRootRef;
    private final StorableCodecFactory mStorableCodecFactory;
    private final ExceptionTransformer mExTransformer;
    private final BDBTransactionManager<Txn> mTxnMgr;

    Checkpointer mCheckpointer;
    DeadlockDetector mDeadlockDetector;

    private final Runnable mPreShutdownHook;
    private final Runnable mPostShutdownHook;

    private final Object mInitialDBConfig;
    private final BDBRepositoryBuilder.DatabaseHook mDatabaseHook;
    private final Map<Class<?>, Integer> mDatabasePageSizes;

    final boolean mRunCheckpointer;
    final boolean mKeepOldLogFiles;
    final boolean mLogInMemory;
    final boolean mRunDeadlockDetector;

    final File mDataHome;
    final File mEnvHome;
    final String mSingleFileName;
    final Map<String, String> mFileNameMap;

    final Object mBackupLock = new Object();
    int mBackupCount = 0;
    int mIncrementalBackupCount = 0;

    private LayoutFactory mLayoutFactory;

    private LobEngine mLobEngine;

    /**
     * Subclass must call protected start method to fully initialize
     * BDBRepository.
     *
     * @param builder repository configuration
     * @param exTransformer transformer for exceptions
     * @throws IllegalArgumentException if name or environment home is null
     */
    @SuppressWarnings("unchecked")
    BDBRepository(AtomicReference<Repository> rootRef,
                  BDBRepositoryBuilder builder,
                  ExceptionTransformer exTransformer)
        throws ConfigurationException
    {
        super(builder.getName());

        builder.assertReady();

        if (exTransformer == null) {
            throw new IllegalArgumentException("Exception transformer must not be null");
        }

        mIsMaster = builder.isMaster();
        mTriggerFactories = builder.getTriggerFactories();
        mRootRef = rootRef;
        mExTransformer = exTransformer;
        mTxnMgr = new BDBTransactionManager<Txn>(mExTransformer, this);

        mRunCheckpointer = !builder.getReadOnly() && builder.getRunCheckpointer();
        mKeepOldLogFiles = builder.getKeepOldLogFiles();
        mLogInMemory = builder.getLogInMemory();
        mRunDeadlockDetector = builder.getRunDeadlockDetector();
        mStorableCodecFactory = builder.getStorableCodecFactory();
        mPreShutdownHook = builder.getPreShutdownHook();
        mPostShutdownHook = builder.getShutdownHook();
        mInitialDBConfig = builder.getInitialDatabaseConfig();
        mDatabaseHook = builder.getDatabaseHook();
        mDatabasePageSizes = builder.getDatabasePagesMap();
        mDataHome = builder.getDataHomeFile();
        mEnvHome = builder.getEnvironmentHomeFile();
        mSingleFileName = builder.getSingleFileName();
        mFileNameMap = builder.getFileNameMap();

        getLog().info("Opening repository \"" + getName() + '"');
    }

    public ExceptionTransformer getExceptionTransformer() {
        return mExTransformer;
    }
    
    public <S extends Storable> IndexInfo[] getIndexInfo(Class<S> storableType)
        throws RepositoryException
    {
        return ((BDBStorage) storageFor(storableType)).getIndexInfo();
    }

    public String[] getUserStorableTypeNames() throws RepositoryException {
        Repository metaRepo = getRootRepository();

        Cursor<StoredDatabaseInfo> cursor =
            metaRepo.storageFor(StoredDatabaseInfo.class)
            .query().orderBy("databaseName").fetch();

        try {
            ArrayList<String> names = new ArrayList<String>();
            while (cursor.hasNext()) {
                StoredDatabaseInfo info = cursor.next();
                // Ordinary user types support evolution.
                if (info.getEvolutionStrategy() != StoredDatabaseInfo.EVOLUTION_NONE) {
                    names.add(info.getDatabaseName());
                }
            }

            return names.toArray(new String[names.size()]);
        } finally {
            cursor.close();
        }
    }

    public boolean isSupported(Class<Storable> type) {
        if (type == null) {
            return false;
        }
        StorableIntrospector.examine(type);
        return true;
    }

    public boolean isPropertySupported(Class<Storable> type, String name) {
        if (type == null || name == null) {
            return false;
        }
        return StorableIntrospector.examine(type).getAllProperties().get(name) != null;
    }
    
    @Override 
    public Backup startBackup() throws RepositoryException {
        return startBackup(false);
    }

    @Override
    public Backup startBackup(boolean deleteOldLogFiles) throws RepositoryException {
        if (mLogInMemory) {
            throw new IllegalStateException
                ("Log files are only kept in memory and backups cannot be performed");
        }

        synchronized (mBackupLock) {
            int count = mBackupCount;
            if (count == 0) {
                try {
                    if (deleteOldLogFiles) {
                        // TODO: If backup rejects log deletion, queue up for later.
                        enterBackupMode(true);
                    } else {
                        // Call old API for backwards compatibility.
                        enterBackupMode();
                    }
                } catch (Exception e) {
                    throw mExTransformer.toRepositoryException(e);
                }
            }
            mBackupCount = count + 1;

            return new FullBackup();
        }
    }

    @Override 
    public Backup startIncrementalBackup(long lastLogNumber) 
        throws RepositoryException
    {
        return startIncrementalBackup(lastLogNumber, false);
    }

    @Override
    public Backup startIncrementalBackup(long lastLogNumber, boolean deleteOldLogFiles) 
        throws RepositoryException
    {
        if (mLogInMemory) {
            throw new IllegalStateException
                ("Log files are only kept in memory and incremental backup cannot be performed");
        }

        if (lastLogNumber < 0) {
            throw new IllegalArgumentException
                ("The number of the last backup cannot be negative: " + lastLogNumber);
        }
        synchronized (mBackupLock) {
            try {
                enterIncrementalBackupMode(lastLogNumber, deleteOldLogFiles);
                ++mIncrementalBackupCount;
            } catch (Exception e) {
                throw mExTransformer.toRepositoryException(e);
            }
        }
        return new IncrementalBackup(lastLogNumber);
    }

    /**
     * Suspend the checkpointer until the suspension time has expired or until
     * manually resumed. If a checkpoint is in progress, this method will block
     * until it is finished. If checkpointing is disabled, calling this method
     * has no effect.
     *
     * <p>Calling this method repeatedly resets the suspension time. This
     * technique should be used by hot backup processes to ensure that its
     * failure does not leave the checkpointer permanently suspended. Each
     * invocation of suspendCheckpointer is like a lease renewal or heartbeat.
     *
     * @param suspensionTime minimum length of suspension, in milliseconds,
     * unless checkpointer is manually resumed
     */
    public void suspendCheckpointer(long suspensionTime) {
        if (mCheckpointer != null) {
            mCheckpointer.suspendCheckpointer(suspensionTime);
        }
    }

    /**
     * Resumes the checkpointer if it was suspended. If checkpointing is
     * disabled or if not suspended, calling this method has no effect.
     */
    public void resumeCheckpointer() {
        if (mCheckpointer != null) {
            mCheckpointer.resumeCheckpointer();
        }
    }

    /**
     * Forces a checkpoint to run now, even if checkpointer is suspended or
     * disabled. If a checkpoint is in progress, then this method will block
     * until it is finished, and then run another checkpoint. This method does
     * not return until the requested checkpoint has finished.
     */
    public void forceCheckpoint() throws PersistException {
        if (mCheckpointer != null) {
            mCheckpointer.forceCheckpoint();
        } else {
            try {
                env_checkpoint();
            } catch (Exception e) {
                throw toPersistException(e);
            }
        }
    }

    public void sync() throws PersistException {
        try {
            env_sync();
        } catch (Exception e) {
            throw toPersistException(e);
        }
    }

    public Repository getRootRepository() {
        return mRootRef.get();
    }

    public <S extends Storable> StorageAccess<S> storageAccessFor(Class<S> type)
        throws RepositoryException
    {
        return (BDBStorage<Txn, S>) storageFor(type);
    }

    @Override
    public Layout layoutFor(Class<? extends Storable> type)
        throws FetchException, PersistException
    {
        try {
            return ((BDBStorage) storageFor(type)).getLayout(true, mStorableCodecFactory);
        } catch (PersistException e) {
            throw e;
        } catch (RepositoryException e) {
            throw e.toFetchException();
        }
    }

    @Override
    public Layout layoutFor(Class<? extends Storable> type, int generation)
        throws FetchException
    {
        return mLayoutFactory.layoutFor(type, generation);
    }

    @Override
    protected void finalize() {
        close();
    }

    @Override
    protected void shutdownHook() {
        // Run any external shutdown logic that needs to happen before the
        // databases and the environment are actually closed
        if (mPreShutdownHook != null) {
            mPreShutdownHook.run();
        }

        // Close database handles.
        for (Storage storage : allStorage()) {
            try {
                if (storage instanceof BDBStorage) {
                    ((BDBStorage) storage).close();
                }
            } catch (Throwable e) {
                getLog().error(null, e);
            }
        }

        // Wait for checkpointer to finish.
        if (mCheckpointer != null) {
            mCheckpointer.interrupt();
            try {
                mCheckpointer.join();
            } catch (InterruptedException e) {
            }
        }

        // Wait for deadlock detector to finish.
        if (mDeadlockDetector != null) {
            mDeadlockDetector.interrupt();
            try {
                mDeadlockDetector.join();
            } catch (InterruptedException e) {
            }
        }

        // Close environment.
        try {
            env_close();
        } catch (Throwable e) {
            getLog().error(null, e);
        }

        if (mPostShutdownHook != null) {
            mPostShutdownHook.run();
        }
    }

    @Override
    protected Log getLog() {
        return mLog;
    }

    @Override
    protected <S extends Storable> Storage createStorage(Class<S> type)
        throws RepositoryException
    {
        try {
            return createBDBStorage(type);
        } catch (MalformedArgumentException e) {
            throw e;
        } catch (Exception e) {
            throw toRepositoryException(e);
        }
    }

    @Override
    protected SequenceValueProducer createSequenceValueProducer(String name)
        throws RepositoryException
    {
        return new SequenceValueGenerator(BDBRepository.this, name);
    }

    /**
     * @see com.amazon.carbonado.spi.RepositoryBuilder#isMaster
     */
    boolean isMaster() {
        return mIsMaster;
    }

    String[] getAllDatabaseNames() throws RepositoryException {
        Repository metaRepo = getRootRepository();

        Cursor<StoredDatabaseInfo> cursor =
            metaRepo.storageFor(StoredDatabaseInfo.class)
            .query().orderBy("databaseName").fetch();

        ArrayList<String> names = new ArrayList<String>();
        // This one needs to manually added since it is the metadata db itself.
        names.add(StoredDatabaseInfo.class.getName());

        try {
            while (cursor.hasNext()) {
                names.add(cursor.next().getDatabaseName());
            }
        } finally {
            cursor.close();
        }

        return names.toArray(new String[names.size()]);
    }

    String getDatabaseFileName(final String dbName) {
        String singleFileName = mSingleFileName;
        if (singleFileName == null && mFileNameMap != null) {
            singleFileName = mFileNameMap.get(dbName);
            if (singleFileName == null && dbName != null) {
                singleFileName = mFileNameMap.get(null);
            }
        }

        String dbFileName = dbName;

        if (singleFileName == null) {
            if (mDatabaseHook != null) {
                dbFileName = mDatabaseHook.databaseName(dbName);
            }
        } else {
            dbFileName = singleFileName;
        }

        if (mDataHome != null && !mDataHome.equals(mEnvHome)) {
            dbFileName = new File(mDataHome, dbFileName).getPath();
        }

        return dbFileName;
    }

    /**
     * Returns null if name should not be used.
     */
    String getDatabaseName(String dbName) {
        if (mFileNameMap == null) {
            return null;
        }
        String name = mFileNameMap.get(dbName);
        if (name == null && dbName != null) {
            name = mFileNameMap.get(null);
        }
        if (name == null) {
            return null;
        }
        if (mDatabaseHook != null) {
            try {
                dbName = mDatabaseHook.databaseName(dbName);
            } catch (IncompatibleClassChangeError e) {
                // Method not implemented.
            }
        }
        return dbName;
    }

    StorableCodecFactory getStorableCodecFactory() {
        return mStorableCodecFactory;
    }

    LayoutFactory getLayoutFactory() throws RepositoryException {
        if (mLayoutFactory == null) {
            mLayoutFactory = new LayoutFactory(getRootRepository());
        }
        return mLayoutFactory;
    }

    LobEngine getLobEngine() throws RepositoryException {
        if (mLobEngine == null) {
            mLobEngine = new LobEngine(this, getRootRepository());
        }
        return mLobEngine;
    }

    /**
     * Returns the optional BDB specific database configuration to use
     * for all databases created.
     */
    public Object getInitialDatabaseConfig() {
        return mInitialDBConfig;
    }

    /**
     * Returns the desired page size for the given type, or null for default.
     */
    Integer getDatabasePageSize(Class<? extends Storable> type) {
        if (mDatabasePageSizes == null) {
            return null;
        }
        Integer size = mDatabasePageSizes.get(type);
        if (size == null && type != null) {
            size = mDatabasePageSizes.get(null);
        }
        return size;
    }

    void runDatabasePrepareForOpeningHook(Object database) throws RepositoryException {
        if (mDatabaseHook != null) {
            mDatabaseHook.prepareForOpening(database);
        }
    }

    /**
     * Start background tasks and enable auto shutdown.
     *
     * @param checkpointInterval how often to run checkpoints, in milliseconds,
     * or zero if never. Ignored if repository is read only or builder has
     * checkpoints disabled.
     * @param deadlockDetectorInterval how often to run deadlock detector, in
     * milliseconds, or zero if never. Ignored if builder has deadlock detector
     * disabled.
     *
     * @deprecated Overloaded for backwards compatiblity with older
     * CarbonadoSleepycat packages
     */
    void start(long checkpointInterval, long deadlockDetectorInterval) {
        getLog().info("Opened repository \"" + getName() + '"');

        if (mRunCheckpointer && checkpointInterval > 0) {
            mCheckpointer = new Checkpointer(this, checkpointInterval, 1024, 5);
            mCheckpointer.start();
        } else {
            mCheckpointer = null;
        }

        if (mRunDeadlockDetector && deadlockDetectorInterval > 0) {
            mDeadlockDetector = new DeadlockDetector(this, deadlockDetectorInterval);
            mDeadlockDetector.start();
        } else {
            mDeadlockDetector = null;
        }

        setAutoShutdownEnabled(true);
    }

    /**
     * Start background tasks and enable auto shutdown.
     *
     * @param checkpointInterval how often to run checkpoints, in milliseconds,
     * or zero if never. Ignored if repository is read only or builder has
     * checkpoints disabled.
     * @param deadlockDetectorInterval how often to run deadlock detector, in
     * milliseconds, or zero if never. Ignored if builder has deadlock detector
     * disabled.
     * @param builder containing additonal background task properties.
     */
    void start(long checkpointInterval, long deadlockDetectorInterval,
               BDBRepositoryBuilder builder) {
        getLog().info("Opened repository \"" + getName() + '"');

        if (mRunCheckpointer && checkpointInterval > 0) {
            mCheckpointer = new Checkpointer(this, checkpointInterval,
                                             builder.getCheckpointThresholdKB(),
                                             builder.getCheckpointThresholdMinutes());
            mCheckpointer.start();
        } else {
            mCheckpointer = null;
        }

        if (mRunDeadlockDetector && deadlockDetectorInterval > 0) {
            mDeadlockDetector = new DeadlockDetector(this, deadlockDetectorInterval);
            mDeadlockDetector.start();
        } else {
            mDeadlockDetector = null;
        }

        setAutoShutdownEnabled(true);
    }

    abstract boolean verify(PrintStream out) throws Exception;

    abstract IsolationLevel selectIsolationLevel(Transaction parent, IsolationLevel level);

    abstract Txn txn_begin(Txn parent, IsolationLevel level) throws Exception;

    // Subclass should override this method to actually apply the timeout
    Txn txn_begin(Txn parent, IsolationLevel level, int timeout, TimeUnit unit) throws Exception {
        return txn_begin(parent, level);
    }

    abstract Txn txn_begin_nowait(Txn parent, IsolationLevel level) throws Exception;

    abstract void txn_commit(Txn txn) throws Exception;

    abstract void txn_abort(Txn txn) throws Exception;

    /**
     * Force a checkpoint to run.
     */
    abstract void env_checkpoint() throws Exception;

    /**
     * Synchronously flush changes to stable storage.
     */
    abstract void env_sync() throws Exception;

    /**
     * @param kBytes run checkpoint if at least this many kilobytes in log
     * @param minutes run checkpoint if at least this many minutes passed since
     * last checkpoint
     */
    abstract void env_checkpoint(int kBytes, int minutes) throws Exception;

    /**
     * Run the deadlock detector.
     */
    abstract void env_detectDeadlocks() throws Exception;

    /**
     * Close the environment.
     */
    abstract void env_close() throws Exception;

    abstract <S extends Storable> BDBStorage<Txn, S> createBDBStorage(Class<S> type)
        throws Exception;

    /**
     * Called only the first time a backup is started. Old API is kept for
     * backwards compatibility.
     */
    void enterBackupMode() throws Exception {
        enterBackupMode(false);
    }

    /**
     * Called only the first time a backup is started.
     */
    abstract void enterBackupMode(boolean deleteOldLogFiles) throws Exception;

    /**
     * Called only after the last backup ends.
     */
    abstract void exitBackupMode() throws Exception;

    /**
     * Called only when an incremental backup is started.
     */
    abstract void enterIncrementalBackupMode(long lastLogNumber, boolean deleteOldLogFiles)
        throws Exception;

    /**
     * Called only after incremental backup ends.
     */
    abstract void exitIncrementalBackupMode() throws Exception;

    /**
     * Called only if in backup mode. Old API is kept for backwards
     * compatibility.
     */
    @Deprecated
    File[] backupFiles() throws Exception {
        return backupFiles(new long[1]);
    }

    @Deprecated
    File[] backupFiles(long[] newLastLogNum) throws Exception {
        throw new UnsupportedOperationException();
    }

    /**
     * Called only if in backup mode.
     */
    abstract File[] backupDataFiles() throws Exception;

    /**
     * Called only if in backup mode.
     *
     * @param newLastLogNum reference to last log number at [0]
     */
    abstract File[] backupLogFiles(long[] newLastLogNum) throws Exception;

    /**
     * Called only if in incremental backup mode.
     *
     * @param newLastLogNum reference to last log number at [0]
     */
    abstract File[] incrementalBackup(long lastLogNumber, long[] newLastLogNum) throws Exception;

    FetchException toFetchException(Throwable e) {
        return mExTransformer.toFetchException(e);
    }

    PersistException toPersistException(Throwable e) {
        return mExTransformer.toPersistException(e);
    }

    RepositoryException toRepositoryException(Throwable e) {
        return mExTransformer.toRepositoryException(e);
    }

    @Override
    protected final TransactionManager<Txn> transactionManager() {
        return mTxnMgr;
    }

    @Override
    protected final TransactionScope<Txn> localTransactionScope() {
        return mTxnMgr.localScope();
    }

    /**
     * Periodically runs checkpoints on the environment.
     */
    private static class Checkpointer extends Thread {
        private final WeakReference<BDBRepository> mRepository;
        private final long mSleepInterval;
        private final int mKBytes;
        private final int mMinutes;

        private boolean mInProgress;
        private long mSuspendUntil = Long.MIN_VALUE;

        /**
         *
         * @param repository outer class
         * @param sleepInterval milliseconds to sleep before running checkpoint
         * @param kBytes run checkpoint if at least this many kilobytes in log
         * @param minutes run checkpoint if at least this many minutes passed
         * since last checkpoint
         */
        Checkpointer(BDBRepository repository, long sleepInterval, int kBytes, int minutes) {
            super(repository.getClass().getSimpleName() + " checkpointer (" +
                  repository.getName() + ')');
            setDaemon(true);
            mRepository = new WeakReference<BDBRepository>(repository);
            mSleepInterval = sleepInterval;
            mKBytes = kBytes;
            mMinutes = minutes;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        if (!mInProgress) {
                            try {
                                wait(mSleepInterval);
                            } catch (InterruptedException e) {
                                break;
                            }
                        }
                    }

                    BDBRepository repository = mRepository.get();
                    if (repository == null) {
                        break;
                    }

                    long suspendUntil;
                    synchronized (this) {
                        suspendUntil = mSuspendUntil;
                    }
                    if (suspendUntil != Long.MIN_VALUE) {
                        if (System.currentTimeMillis() < suspendUntil) {
                            continue;
                        }
                    }

                    Log log = repository.getLog();

                    if (log.isDebugEnabled()) {
                        log.debug("Running checkpoint on repository \"" +
                                  repository.getName() + '"');
                    }

                    try {
                        synchronized (this) {
                            mInProgress = true;
                        }
                        repository.env_checkpoint(mKBytes, mMinutes);
                        if (log.isDebugEnabled()) {
                            log.debug("Finished running checkpoint on repository \"" +
                                      repository.getName() + '"');
                        }
                    } catch (ThreadDeath e) {
                        break;
                    } catch (Throwable e) {
                        log.error("Checkpoint failed", e);
                    } finally {
                        synchronized (this) {
                            mInProgress = false;
                            // Only wait condition is mInProgress, so okay to not call notifyAll.
                            notify();
                        }
                        repository = null;
                    }
                }
            } finally {
                synchronized (this) {
                    mInProgress = false;
                    // Only wait condition is mInProgress, so okay to not call notifyAll.
                    notify();
                }
            }
        }

        /**
         * Blocks until checkpoint has finished.
         */
        synchronized void suspendCheckpointer(long suspensionTime) {
            while (mInProgress) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }

            if (suspensionTime <= 0) {
                return;
            }

            long now = System.currentTimeMillis();
            long suspendUntil = now + suspensionTime;
            if (now >= 0 && suspendUntil < 0) {
                // Overflow.
                suspendUntil = Long.MAX_VALUE;
            }
            mSuspendUntil = suspendUntil;
        }

        synchronized void resumeCheckpointer() {
            mSuspendUntil = Long.MIN_VALUE;
        }

        /**
         * Blocks until checkpoint has finished.
         */
        synchronized void forceCheckpoint() throws PersistException {
            while (mInProgress) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    return;
                }
            }

            BDBRepository repository = mRepository.get();
            if (repository != null) {
                try {
                    repository.env_checkpoint();
                } catch (Exception e) {
                    throw repository.toPersistException(e);
                }
            }
        }
    }

    /**
     * Periodically runs deadlock detection on the environment.
     */
    private static class DeadlockDetector extends Thread {
        private final WeakReference<BDBRepository> mRepository;
        private final long mSleepInterval;

        /**
         * @param repository outer class
         * @param sleepInterval milliseconds to sleep before running deadlock detection
         */
        DeadlockDetector(BDBRepository repository, long sleepInterval) {
            super(repository.getClass().getSimpleName() + " deadlock detector (" +
                  repository.getName() + ')');
            setDaemon(true);
            mRepository = new WeakReference<BDBRepository>(repository);
            mSleepInterval = sleepInterval;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(mSleepInterval);
                } catch (InterruptedException e) {
                    break;
                }

                BDBRepository repository = mRepository.get();
                if (repository == null) {
                    break;
                }

                try {
                    repository.env_detectDeadlocks();
                } catch (ThreadDeath e) {
                    break;
                } catch (Throwable e) {
                    repository.getLog().error("Deadlock detection failed", e);
                } finally {
                    repository = null;
                }
            }
        }
    }

    abstract class AbstractBackup implements Backup {
        boolean mDone;
        long mFinalLogNumber;

        AbstractBackup() {
            mFinalLogNumber = -1;
        }

        @Override 
        public void endBackup() throws RepositoryException {
            synchronized (mBackupLock) {
                if (mDone) {
                    return;
                }
                mDone = true;
                finishBackup();
            }
        }

        @Override
        @Deprecated
        public File[] getFiles() throws RepositoryException {
            synchronized (mBackupLock) {
                File[] data = getDataFiles();
                File[] logs = getLogFiles();
                File[] all = new File[data.length + logs.length];
                System.arraycopy(data, 0, all, 0, data.length);
                System.arraycopy(logs, 0, all, data.length, logs.length);
                return all;
            }
        }
        
        @Override
        public File[] getDataFiles() throws RepositoryException {
            synchronized (mBackupLock) {
                if (mDone) {
                    throw new IllegalStateException("Backup has ended");
                }
                
                try {
                    return getDataBackupFiles();
                } catch (Exception e) {
                    throw mExTransformer.toRepositoryException(e);
                }
            }
        }

        @Override
        public File[] getLogFiles() throws RepositoryException {
            synchronized (mBackupLock) {
                if (mDone) {
                    throw new IllegalStateException("Backup has ended");
                }
                
                try {
                    long[] newLastLogNum = {-1}; 
                    File[] toReturn = getLogBackupFiles(newLastLogNum);
                    mFinalLogNumber = newLastLogNum[0];
                    return toReturn;
                } catch (Exception e) {
                    throw mExTransformer.toRepositoryException(e);
                }
            }
        }

        @Override
        public long getLastLogNumber() throws RepositoryException {
            if (mFinalLogNumber < 0) {
                throw new IllegalStateException
                    ("Must get files prior to retrieving the last log number");
            }
            return mFinalLogNumber;
        }       
        
        abstract void finishBackup() throws RepositoryException;

        abstract File[] getDataBackupFiles() throws Exception;

        abstract File[] getLogBackupFiles(long[] newLastLogNum) throws Exception;
    }

    class IncrementalBackup extends AbstractBackup {
        private final long mLastLogNumber;
        
        IncrementalBackup(long lastLogNumber) {
            super();
            mLastLogNumber = lastLogNumber;
        }

        @Override
        void finishBackup() throws RepositoryException {
            --mIncrementalBackupCount;
            
            try {
                exitIncrementalBackupMode();
            } catch (Exception e) {
                throw mExTransformer.toRepositoryException(e);
            }
        }
        
        @Override
        File[] getDataBackupFiles() throws Exception {
            return new File[0];
        }

        @Override
        File[] getLogBackupFiles(long[] newLastLogNum) throws Exception {
            return incrementalBackup(mLastLogNumber, newLastLogNum);
        }
    }

    class FullBackup extends AbstractBackup {
        @Override
        void finishBackup() throws RepositoryException {
            int count = mBackupCount - 1;
            try {
                if (count == 0) {
                    try {
                        exitBackupMode();
                    } catch (Exception e) {
                        throw mExTransformer.toRepositoryException(e);
                    }
                }
            } finally {
                mBackupCount = count;
            }
        }
        
        @Override
        File[] getDataBackupFiles() throws Exception {
            try {
                return backupDataFiles();
            } catch (AbstractMethodError e) {
                // Old API will be called for backwards compatibility in the
                // getLogBackupFiles method.
                return new File[0];
            }
        }

        @Override
        File[] getLogBackupFiles(long[] newLastLogNum) throws Exception {
            try {
                return backupLogFiles(newLastLogNum);
            } catch (AbstractMethodError e) {
                // Call old API for backwards compatibility.
                try {
                    return backupFiles(newLastLogNum);
                } catch (AbstractMethodError e2) {
                    // Call even older API for backwards compatibility.
                    return backupFiles();
                }
            }
        }
    }
}