Improve segmented journal actor metrics
[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.Serializable;
13 import org.apache.commons.lang3.SerializationUtils;
14 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
15 import org.opendaylight.controller.cluster.raft.messages.Payload;
16
17 /**
18  * A {@link ReplicatedLogEntry} implementation.
19  *
20  * @author Thomas Pantelis
21  */
22 public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
23     @java.io.Serial
24     private static final long serialVersionUID = 1L;
25     // Estimate to how big the proxy is. Note this includes object stream overhead, so it is a bit conservative.
26     private static final int PROXY_SIZE = SerializationUtils.serialize(new LE((Void) null)).length;
27
28     private final long index;
29     private final long term;
30     private final Payload payload;
31     private boolean persistencePending;
32
33     /**
34      * Constructs an instance.
35      *
36      * @param index the index
37      * @param term the term
38      * @param payload the payload
39      */
40     public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
41         this.index = index;
42         this.term = term;
43         this.payload = requireNonNull(payload);
44     }
45
46     @Override
47     public Payload getData() {
48         return payload;
49     }
50
51     @Override
52     public long getTerm() {
53         return term;
54     }
55
56     @Override
57     public long getIndex() {
58         return index;
59     }
60
61     @Override
62     public int size() {
63         return payload.size();
64     }
65
66     @Override
67     public int serializedSize() {
68         return PROXY_SIZE + payload.serializedSize();
69     }
70
71     @Override
72     public boolean isPersistencePending() {
73         return persistencePending;
74     }
75
76     @Override
77     public void setPersistencePending(final boolean pending) {
78         persistencePending = pending;
79     }
80
81     @Override
82     public int hashCode() {
83         final int prime = 31;
84         int result = 1;
85         result = prime * result + payload.hashCode();
86         result = prime * result + (int) (index ^ index >>> 32);
87         result = prime * result + (int) (term ^ term >>> 32);
88         return result;
89     }
90
91     @Override
92     public boolean equals(final Object obj) {
93         return this == obj || obj instanceof SimpleReplicatedLogEntry other && index == other.index
94             && term == other.term && payload.equals(other.payload);
95     }
96
97     @Override
98     public String toString() {
99         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
100     }
101
102     @java.io.Serial
103     private Object writeReplace() {
104         return new LE(this);
105     }
106 }