Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / CT.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.datastore.persisted;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.StreamCorruptedException;
18 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload.Chunked;
19 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload.Simple;
20 import org.opendaylight.controller.cluster.io.ChunkedByteArray;
21
22 /**
23  * Serialization proxy for {@link CommitTransactionPayload}.
24  */
25 final class CT implements Externalizable {
26     @java.io.Serial
27     private static final long serialVersionUID = 1L;
28
29     private CommitTransactionPayload payload;
30
31     @SuppressWarnings("checkstyle:RedundantModifier")
32     public CT() {
33         // For Externalizable
34     }
35
36     CT(final CommitTransactionPayload payload) {
37         this.payload = requireNonNull(payload);
38     }
39
40     @Override
41     public void writeExternal(final ObjectOutput out) throws IOException {
42         out.writeInt(payload.size());
43         payload.writeBytes(out);
44     }
45
46     @Override
47     public void readExternal(final ObjectInput in) throws IOException {
48         final int length = in.readInt();
49         if (length < 0) {
50             throw new StreamCorruptedException("Invalid payload length " + length);
51         } else if (length < CommitTransactionPayload.MAX_ARRAY_SIZE) {
52             final byte[] serialized = new byte[length];
53             in.readFully(serialized);
54             payload = new Simple(serialized);
55         } else {
56             payload = new Chunked(ChunkedByteArray.readFrom(in, length, CommitTransactionPayload.MAX_ARRAY_SIZE));
57         }
58     }
59
60     @java.io.Serial
61     private Object readResolve() {
62         return verifyNotNull(payload);
63     }
64 }