2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
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;
23 import java.util.Map.Entry;
25 import org.opendaylight.yangtools.concepts.WritableObject;
26 import org.opendaylight.yangtools.concepts.WritableObjects;
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;
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;
40 this.closedTransactions = ImmutableMap.copyOf(closedTransactions);
41 this.purgedTransactions = ImmutableRangeSet.copyOf(purgedTransactions);
44 public long getHistoryId() {
48 public long getCookie() {
52 public boolean isClosed() {
56 public Map<UnsignedLong, Boolean> getClosedTransactions() {
57 return closedTransactions;
60 public RangeSet<UnsignedLong> getPurgedTransactions() {
61 return purgedTransactions;
65 public void writeTo(final DataOutput out) throws IOException {
66 WritableObjects.writeLongs(out, historyId, cookie);
67 out.writeBoolean(closed);
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());
75 for (Range<UnsignedLong> r : purgedRanges) {
76 WritableObjects.writeLongs(out, r.lowerEndpoint().longValue(), r.upperEndpoint().longValue());
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();
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;
91 ls = WritableObjects.readSecondLong(in, header);
92 Verify.verify(ls >= 0 && ls <= Integer.MAX_VALUE);
93 final int psize = (int) ls;
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);
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));
109 return new FrontendHistoryMetadata(historyId, cookie, closed, closedTransactions, purgedTransactions);
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();