Fix followerDistributedDataStore tear down
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / FrontendHistoryMetadata.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.base.MoreObjects;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableRangeSet;
14 import com.google.common.collect.Range;
15 import com.google.common.collect.RangeSet;
16 import com.google.common.collect.TreeRangeSet;
17 import com.google.common.primitives.UnsignedLong;
18 import java.io.DataInput;
19 import java.io.DataOutput;
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
25 import org.opendaylight.yangtools.concepts.WritableObject;
26 import org.opendaylight.yangtools.concepts.WritableObjects;
27
28 public final class FrontendHistoryMetadata implements WritableObject {
29     private final RangeSet<UnsignedLong> purgedTransactions;
30     private final Map<UnsignedLong, Boolean> closedTransactions;
31     private final long historyId;
32     private final long cookie;
33     private final boolean closed;
34
35     public FrontendHistoryMetadata(final long historyId, final long cookie, final boolean closed,
36             final Map<UnsignedLong, Boolean> closedTransactions, final RangeSet<UnsignedLong> purgedTransactions) {
37         this.historyId = historyId;
38         this.cookie = cookie;
39         this.closed = closed;
40         this.closedTransactions = ImmutableMap.copyOf(closedTransactions);
41         this.purgedTransactions = ImmutableRangeSet.copyOf(purgedTransactions);
42     }
43
44     public long getHistoryId() {
45         return historyId;
46     }
47
48     public long getCookie() {
49         return cookie;
50     }
51
52     public boolean isClosed() {
53         return closed;
54     }
55
56     public Map<UnsignedLong, Boolean> getClosedTransactions() {
57         return closedTransactions;
58     }
59
60     public RangeSet<UnsignedLong> getPurgedTransactions() {
61         return purgedTransactions;
62     }
63
64     @Override
65     public void writeTo(final DataOutput out) throws IOException {
66         WritableObjects.writeLongs(out, historyId, cookie);
67         out.writeBoolean(closed);
68
69         final Set<Range<UnsignedLong>> purgedRanges = purgedTransactions.asRanges();
70         WritableObjects.writeLongs(out, closedTransactions.size(), purgedRanges.size());
71         for (Entry<UnsignedLong, Boolean> e : closedTransactions.entrySet()) {
72             WritableObjects.writeLong(out, e.getKey().longValue());
73             out.writeBoolean(e.getValue().booleanValue());
74         }
75         for (Range<UnsignedLong> r : purgedRanges) {
76             WritableObjects.writeLongs(out, r.lowerEndpoint().longValue(), r.upperEndpoint().longValue());
77         }
78     }
79
80     public static FrontendHistoryMetadata readFrom(final DataInput in) throws IOException {
81         byte header = WritableObjects.readLongHeader(in);
82         final long historyId = WritableObjects.readFirstLong(in, header);
83         final long cookie = WritableObjects.readSecondLong(in, header);
84         final boolean closed = in.readBoolean();
85
86         header = WritableObjects.readLongHeader(in);
87         long ls = WritableObjects.readFirstLong(in, header);
88         Verify.verify(ls >= 0 && ls <= Integer.MAX_VALUE);
89         final int csize = (int) ls;
90
91         ls = WritableObjects.readSecondLong(in, header);
92         Verify.verify(ls >= 0 && ls <= Integer.MAX_VALUE);
93         final int psize = (int) ls;
94
95         final Map<UnsignedLong, Boolean> closedTransactions = new HashMap<>(csize);
96         for (int i = 0; i < csize; ++i) {
97             final UnsignedLong key = UnsignedLong.fromLongBits(WritableObjects.readLong(in));
98             final Boolean value = Boolean.valueOf(in.readBoolean());
99             closedTransactions.put(key, value);
100         }
101         final RangeSet<UnsignedLong> purgedTransactions = TreeRangeSet.create();
102         for (int i = 0; i < psize; ++i) {
103             final byte h = WritableObjects.readLongHeader(in);
104             final UnsignedLong l = UnsignedLong.fromLongBits(WritableObjects.readFirstLong(in, h));
105             final UnsignedLong u = UnsignedLong.fromLongBits(WritableObjects.readSecondLong(in, h));
106             purgedTransactions.add(Range.closed(l, u));
107         }
108
109         return new FrontendHistoryMetadata(historyId, cookie, closed, closedTransactions, purgedTransactions);
110     }
111
112     @Override
113     public String toString() {
114         return MoreObjects.toStringHelper(FrontendHistoryMetadata.class).add("historyId", historyId)
115                 .add("cookie", cookie).add("closed", closed).add("closedTransactions", closedTransactions)
116                 .add("purgedTransactions", purgedTransactions).toString();
117     }
118 }