1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: ReplicationStatus.java,v 12.11 2008/01/17 05:04:53 mjc Exp $
7 */
8
9package com.sleepycat.db;
10
11import com.sleepycat.db.internal.DbConstants;
12import com.sleepycat.db.internal.DbEnv;
13
14/** The return status from processing a replication message. */
15public final class ReplicationStatus {
16    static final ReplicationStatus SUCCESS =
17        new ReplicationStatus("SUCCESS", 0);
18
19    private int errCode;
20    private DatabaseEntry cdata;
21    private int envid;
22    private LogSequenceNumber lsn;
23
24    /* For toString */
25    private String statusName;
26
27    private ReplicationStatus(final String statusName,
28                              final int errCode,
29                              final DatabaseEntry cdata,
30                              final int envid,
31                              final LogSequenceNumber lsn) {
32        this.statusName = statusName;
33        this.errCode = errCode;
34        this.cdata = cdata;
35        this.envid = envid;
36        this.lsn = lsn;
37    }
38
39    private ReplicationStatus(final String statusName, final int errCode) {
40        this(statusName, errCode, null, 0, null);
41    }
42
43    /**
44    The operation succeeded.
45    */
46    public boolean isSuccess() {
47        return errCode == 0;
48    }
49
50    /**
51    This message cannot be processed.
52    This is an indication that this message is irrelevant to the current
53    replication state (for example, an old message from a previous
54    generation arrives and is processed late).
55    **/
56    public boolean isIgnore() {
57        return errCode == DbConstants.DB_REP_IGNORE;
58    }
59
60    /**
61    Processing this message resulted in the processing of records that
62    are permanent.  The maximum LSN of the permanent records stored is
63    available from the getLSN method.
64    */
65    public boolean isPermanent() {
66        return errCode == DbConstants.DB_REP_ISPERM;
67    }
68
69    /**
70    The system received contact information from a new environment.  A
71    copy of the opaque data specified in the cdata parameter to the
72    {@link com.sleepycat.db.Environment#startReplication Environment.startReplication} is available from the
73    getCDAta method.  The application should take whatever action is
74    needed to establish a communication channel with this new
75    environment.
76    */
77    public boolean isNewSite() {
78        return errCode == DbConstants.DB_REP_NEWSITE;
79    }
80
81    /**
82    A message carrying a DB_REP_PERMANENT flag was processed successfully,
83    but was not written to disk.  The LSN of this record is available from
84    the getLSN method.  The application should take whatever action is
85    deemed necessary to retain its recoverability characteristics.
86    */
87    public boolean isNotPermanent() {
88        return errCode == DbConstants.DB_REP_NOTPERM;
89    }
90
91    /**
92    Whenever the system receives contact information from a new
93    environment, a copy of the opaque data specified in the cdata
94    parameter to the {@link com.sleepycat.db.Environment#startReplication Environment.startReplication} is available
95    from the getCDAta method.  The application should take whatever
96    action is needed to establish a communication channel with this new
97    environment.
98    */
99    public DatabaseEntry getCData() {
100        return cdata;
101    }
102
103    /**
104    Return the environment ID associated with the operation.  In most cases,
105    this is the same as the environment ID passed to {@link com.sleepycat.db.Environment#processReplicationMessage Environment.processReplicationMessage}.  However, if a new master is elected, this
106    method returns the environment ID of the new master.  It is the
107    application's responsibility to ensure that the matching node begins acting
108    as the master environment.
109    */
110    public int getEnvID() {
111        return envid;
112    }
113
114    /**
115    Whenever processing a messages results in the processing of messages
116    that are permanent, or a message carrying a DB_REP_PERMANENT flag
117    was processed successfully, but was not written to disk, the LSN of
118    the record is available from the getLSN method.  The application
119    should take whatever action is deemed necessary to retain its
120    recoverability characteristics.
121    */
122    public LogSequenceNumber getLSN() {
123        return lsn;
124    }
125
126    /** {@inheritDoc} */
127    public String toString() {
128        return "ReplicationStatus." + statusName;
129    }
130
131    /* package */
132    static ReplicationStatus getStatus(final int errCode,
133                                       final DatabaseEntry cdata,
134                                       final int envid,
135                                       final LogSequenceNumber lsn) {
136        switch(errCode) {
137        case 0:
138            return SUCCESS;
139        case DbConstants.DB_REP_IGNORE:
140            return IGNORE;
141        case DbConstants.DB_REP_ISPERM:
142            return new ReplicationStatus("ISPERM", errCode, cdata, envid, lsn);
143        case DbConstants.DB_REP_NEWSITE:
144            return new ReplicationStatus("NEWSITE", errCode, cdata, envid, lsn);
145        case DbConstants.DB_REP_NOTPERM:
146            return new ReplicationStatus("NOTPERM", errCode, cdata, envid, lsn);
147        default:
148            throw new IllegalArgumentException(
149                "Unknown error code: " + DbEnv.strerror(errCode));
150        }
151    }
152
153    private static final ReplicationStatus IGNORE =
154        new ReplicationStatus("IGNORE", DbConstants.DB_REP_IGNORE);
155}
156