Define efficient serialization proxies
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ST.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 com.google.common.io.ByteStreams;
14 import java.io.IOException;
15 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
16 import org.opendaylight.controller.cluster.datastore.persisted.AbstractIdentifiablePayload.SerialForm;
17 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
18
19 /**
20  * Serialization proxy for {@link SkipTransactionsPayload}.
21  */
22 final class ST implements SerialForm {
23     private static final long serialVersionUID = 1L;
24
25     private ImmutableUnsignedLongSet transactionIds;
26     private LocalHistoryIdentifier identifier;
27     private byte[] bytes;
28
29     @SuppressWarnings("checkstyle:RedundantModifier")
30     public ST() {
31         // For Externalizable
32     }
33
34     ST(final byte[] bytes) {
35         this.bytes = requireNonNull(bytes);
36     }
37
38     @Override
39     public byte[] bytes() {
40         return bytes;
41     }
42
43     @Override
44     public void readExternal(final byte[] newBytes) throws IOException {
45         bytes = requireNonNull(newBytes);
46
47         final var in = ByteStreams.newDataInput(newBytes);
48         identifier = LocalHistoryIdentifier.readFrom(in);
49         transactionIds = verifyNotNull(ImmutableUnsignedLongSet.readFrom(in));
50     }
51
52     @Override
53     public Object readResolve() {
54         return new SkipTransactionsPayload(identifier, bytes, transactionIds);
55     }
56 }