56c3f769ff18306f746496c48f387409697ecf10
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / SimpleReplicatedLogEntry.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.raft.persisted;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.Serializable;
17 import org.apache.commons.lang3.SerializationUtils;
18 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
19 import org.opendaylight.controller.cluster.raft.messages.Payload;
20
21 /**
22  * A {@link ReplicatedLogEntry} implementation.
23  *
24  * @author Thomas Pantelis
25  */
26 public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
27     @Deprecated(since = "7.0.0", forRemoval = true)
28     private static final class Proxy implements Externalizable {
29         @java.io.Serial
30         private static final long serialVersionUID = 1L;
31
32         private long index;
33         private long term;
34         private Payload data;
35
36         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
37         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
38         @SuppressWarnings("checkstyle:RedundantModifier")
39         public Proxy() {
40             // For Externalizable
41         }
42
43         Proxy(final SimpleReplicatedLogEntry replicatedLogEntry) {
44             index = replicatedLogEntry.getIndex();
45             term = replicatedLogEntry.getTerm();
46             data = replicatedLogEntry.getData();
47         }
48
49         @Override
50         public void writeExternal(final ObjectOutput out) throws IOException {
51             out.writeLong(index);
52             out.writeLong(term);
53             out.writeObject(data);
54         }
55
56         @Override
57         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
58             index = in.readLong();
59             term = in.readLong();
60             data = (Payload) in.readObject();
61         }
62
63         @java.io.Serial
64         private Object readResolve() {
65             return new SimpleReplicatedLogEntry(index, term, data);
66         }
67     }
68
69     @java.io.Serial
70     private static final long serialVersionUID = 1L;
71     // Estimate to how big the proxy is. Note this includes object stream overhead, so it is a bit conservative.
72     private static final int PROXY_SIZE = SerializationUtils.serialize(new LE((Void) null)).length;
73
74     private final long index;
75     private final long term;
76     private final Payload payload;
77     private boolean persistencePending;
78
79     /**
80      * Constructs an instance.
81      *
82      * @param index the index
83      * @param term the term
84      * @param payload the payload
85      */
86     public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
87         this.index = index;
88         this.term = term;
89         this.payload = requireNonNull(payload);
90     }
91
92     @Override
93     public Payload getData() {
94         return payload;
95     }
96
97     @Override
98     public long getTerm() {
99         return term;
100     }
101
102     @Override
103     public long getIndex() {
104         return index;
105     }
106
107     @Override
108     public int size() {
109         return payload.size();
110     }
111
112     @Override
113     public int serializedSize() {
114         return PROXY_SIZE + payload.serializedSize();
115     }
116
117     @Override
118     public boolean isPersistencePending() {
119         return persistencePending;
120     }
121
122     @Override
123     public void setPersistencePending(final boolean pending) {
124         persistencePending = pending;
125     }
126
127     private Object writeReplace() {
128         return new LE(this);
129     }
130
131     @Override
132     public int hashCode() {
133         final int prime = 31;
134         int result = 1;
135         result = prime * result + payload.hashCode();
136         result = prime * result + (int) (index ^ index >>> 32);
137         result = prime * result + (int) (term ^ term >>> 32);
138         return result;
139     }
140
141     @Override
142     public boolean equals(final Object obj) {
143         if (this == obj) {
144             return true;
145         }
146         if (obj == null || getClass() != obj.getClass()) {
147             return false;
148         }
149
150         var other = (SimpleReplicatedLogEntry) obj;
151         return index == other.index && term == other.term && payload.equals(other.payload);
152     }
153
154     @Override
155     public String toString() {
156         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
157     }
158 }