Specify initial serialization buffer capacity for Payloads
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / CommitTransactionPayload.java
1 /*
2  * Copyright (c) 2016 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.datastore.persisted;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.io.ByteArrayDataOutput;
14 import com.google.common.io.ByteStreams;
15 import java.io.DataInput;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectOutput;
20 import java.io.Serializable;
21 import java.util.AbstractMap.SimpleImmutableEntry;
22 import java.util.Map.Entry;
23 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
24 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Payload persisted when a transaction commits. It contains the transaction identifier and the
31  * {@link DataTreeCandidate}
32  *
33  * @author Robert Varga
34  */
35 @Beta
36 public final class CommitTransactionPayload extends Payload implements Serializable {
37     private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
38
39     private static final class Proxy implements Externalizable {
40         private static final long serialVersionUID = 1L;
41         private byte[] serialized;
42
43         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
44         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
45         @SuppressWarnings("checkstyle:RedundantModifier")
46         public Proxy() {
47             // For Externalizable
48         }
49
50         Proxy(final byte[] serialized) {
51             this.serialized = Preconditions.checkNotNull(serialized);
52         }
53
54         @Override
55         public void writeExternal(final ObjectOutput out) throws IOException {
56             out.writeInt(serialized.length);
57             out.write(serialized);
58         }
59
60         @Override
61         public void readExternal(final ObjectInput in) throws IOException {
62             final int length = in.readInt();
63             serialized = new byte[length];
64             in.readFully(serialized);
65         }
66
67         private Object readResolve() {
68             return new CommitTransactionPayload(serialized);
69         }
70     }
71
72     private static final long serialVersionUID = 1L;
73
74     private final byte[] serialized;
75
76     CommitTransactionPayload(final byte[] serialized) {
77         this.serialized = Preconditions.checkNotNull(serialized);
78     }
79
80     public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
81             final DataTreeCandidate candidate, final int initialSerializedBufferCapacity) throws IOException {
82         final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
83         transactionId.writeTo(out);
84         DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
85         final byte[] serialized = out.toByteArray();
86
87         LOG.info("Initial buffer capacity {}, actual serialized size {}",
88                 initialSerializedBufferCapacity, serialized.length);
89
90         return new CommitTransactionPayload(serialized);
91     }
92
93     @VisibleForTesting
94     public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
95             final DataTreeCandidate candidate) throws IOException {
96         return create(transactionId, candidate, 512);
97     }
98
99     public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
100         final DataInput in = ByteStreams.newDataInput(serialized);
101         return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
102                 DataTreeCandidateInputOutput.readDataTreeCandidate(in));
103     }
104
105     @Override
106     public int size() {
107         return serialized.length;
108     }
109
110     private Object writeReplace() {
111         return new Proxy(serialized);
112     }
113 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.