1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
5  *
6  * $Id$
7  */
8 
9 package com.sleepycat.db;
10 
11 import com.sleepycat.db.internal.DbConstants;
12 
13 /**
14 The recovery operation being performed when {@link com.sleepycat.db.LogRecordHandler#handleLogRecord LogRecordHandler.handleLogRecord} is called.
15 */
16 public final class RecoveryOperation {
17     /**
18     The log is being read backward to determine which transactions have
19     been committed and to abort those operations that were not; undo the
20     operation described by the log record.
21     */
22     public static final RecoveryOperation BACKWARD_ROLL =
23         new RecoveryOperation("BACKWARD_ROLL", DbConstants.DB_TXN_BACKWARD_ROLL);
24     /**
25     The log is being played forward; redo the operation described by the log
26     record.
27     <p>
28     The FORWARD_ROLL and APPLY operations frequently imply the same actions,
29     redoing changes that appear in the log record, although if a recovery
30     function is to be used on a replication client where reads may be taking
31     place concurrently with the processing of incoming messages, APPLY
32     operations should also perform appropriate locking.
33     */
34     public static final RecoveryOperation FORWARD_ROLL =
35         new RecoveryOperation("FORWARD_ROLL", DbConstants.DB_TXN_FORWARD_ROLL);
36     /**
37     The log is being read backward during a transaction abort; undo the
38     operation described by the log record.
39     */
40     public static final RecoveryOperation ABORT =
41         new RecoveryOperation("ABORT", DbConstants.DB_TXN_ABORT);
42     /**
43     The log is being applied on a replica site; redo the operation
44     described by the log record.
45     <p>
46     The FORWARD_ROLL and APPLY operations frequently imply the same actions,
47     redoing changes that appear in the log record, although if a recovery
48     function is to be used on a replication client where reads may be taking
49     place concurrently with the processing of incoming messages, APPLY
50     operations should also perform appropriate locking.
51     */
52     public static final RecoveryOperation APPLY =
53         new RecoveryOperation("APPLY", DbConstants.DB_TXN_APPLY);
54     /**
55     The log is being printed for debugging purposes; print the contents of
56     this log record in the desired format.
57     */
58     public static final RecoveryOperation PRINT =
59         new RecoveryOperation("PRINT", DbConstants.DB_TXN_PRINT);
60 
61     private String operationName;
62     private int flag;
63 
RecoveryOperation(String operationName, int flag)64     private RecoveryOperation(String operationName, int flag) {
65         this.operationName = operationName;
66         this.flag = flag;
67     }
68 
69     /** {@inheritDoc} */
toString()70     public String toString() {
71         return "RecoveryOperation." + operationName;
72     }
73 
74     /* This is public only so it can be called from internal/DbEnv.java. */
75     /**
76     Internal: this is public only so it can be called from an internal
77     package.
78      *
79     @param flag
80     the internal flag value to be wrapped in a RecoveryException object
81     */
fromFlag(int flag)82     public static RecoveryOperation fromFlag(int flag) {
83         switch (flag) {
84         case DbConstants.DB_TXN_BACKWARD_ROLL:
85             return BACKWARD_ROLL;
86         case DbConstants.DB_TXN_FORWARD_ROLL:
87             return FORWARD_ROLL;
88         case DbConstants.DB_TXN_ABORT:
89             return ABORT;
90         case DbConstants.DB_TXN_APPLY:
91             return APPLY;
92         case DbConstants.DB_TXN_PRINT:
93             return PRINT;
94         default:
95             throw new IllegalArgumentException(
96                 "Unknown recover operation: " + flag);
97         }
98     }
99 }
100