Add support for reusable streaming
[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.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Payload persisted when a transaction commits. It contains the transaction identifier and the
32  * {@link DataTreeCandidate}
33  *
34  * @author Robert Varga
35  */
36 @Beta
37 public final class CommitTransactionPayload extends Payload implements Serializable {
38     private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
39
40     private static final class Proxy implements Externalizable {
41         private static final long serialVersionUID = 1L;
42         private byte[] serialized;
43
44         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
45         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
46         @SuppressWarnings("checkstyle:RedundantModifier")
47         public Proxy() {
48             // For Externalizable
49         }
50
51         Proxy(final byte[] serialized) {
52             this.serialized = Preconditions.checkNotNull(serialized);
53         }
54
55         @Override
56         public void writeExternal(final ObjectOutput out) throws IOException {
57             out.writeInt(serialized.length);
58             out.write(serialized);
59         }
60
61         @Override
62         public void readExternal(final ObjectInput in) throws IOException {
63             final int length = in.readInt();
64             serialized = new byte[length];
65             in.readFully(serialized);
66         }
67
68         private Object readResolve() {
69             return new CommitTransactionPayload(serialized);
70         }
71     }
72
73     private static final long serialVersionUID = 1L;
74
75     private final byte[] serialized;
76
77     CommitTransactionPayload(final byte[] serialized) {
78         this.serialized = Preconditions.checkNotNull(serialized);
79     }
80
81     public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
82             final DataTreeCandidate candidate, final int initialSerializedBufferCapacity) throws IOException {
83         final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
84         transactionId.writeTo(out);
85         DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
86         final byte[] serialized = out.toByteArray();
87
88         LOG.debug("Initial buffer capacity {}, actual serialized size {}",
89                 initialSerializedBufferCapacity, serialized.length);
90
91         return new CommitTransactionPayload(serialized);
92     }
93
94     @VisibleForTesting
95     public static CommitTransactionPayload create(final TransactionIdentifier transactionId,
96             final DataTreeCandidate candidate) throws IOException {
97         return create(transactionId, candidate, 512);
98     }
99
100     public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate() throws IOException {
101         return getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
102     }
103
104     public Entry<TransactionIdentifier, DataTreeCandidate> getCandidate(
105             final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
106         final DataInput in = ByteStreams.newDataInput(serialized);
107         return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
108                 DataTreeCandidateInputOutput.readDataTreeCandidate(in, writer));
109     }
110
111     @Override
112     public int size() {
113         return serialized.length;
114     }
115
116     private Object writeReplace() {
117         return new Proxy(serialized);
118     }
119 }