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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.Verify;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.primitives.UnsignedLong;
16 import java.io.DataInput;
17 import java.io.DataOutput;
18 import java.io.IOException;
19 import java.util.HashMap;
21 import java.util.Map.Entry;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
24 import org.opendaylight.yangtools.concepts.WritableObject;
25 import org.opendaylight.yangtools.concepts.WritableObjects;
27 public final class FrontendHistoryMetadata implements WritableObject {
28 private final @NonNull ImmutableUnsignedLongSet purgedTransactions;
29 private final @NonNull ImmutableMap<UnsignedLong, Boolean> closedTransactions;
30 private final long historyId;
31 private final long cookie;
32 private final boolean closed;
34 public FrontendHistoryMetadata(final long historyId, final long cookie, final boolean closed,
35 final Map<UnsignedLong, Boolean> closedTransactions, final ImmutableUnsignedLongSet purgedTransactions) {
36 this.historyId = historyId;
39 this.closedTransactions = ImmutableMap.copyOf(closedTransactions);
40 this.purgedTransactions = requireNonNull(purgedTransactions);
43 public long getHistoryId() {
47 public long getCookie() {
51 public boolean isClosed() {
55 public ImmutableMap<UnsignedLong, Boolean> getClosedTransactions() {
56 return closedTransactions;
59 public ImmutableUnsignedLongSet getPurgedTransactions() {
60 return purgedTransactions;
64 public void writeTo(final DataOutput out) throws IOException {
65 WritableObjects.writeLongs(out, historyId, cookie);
66 out.writeBoolean(closed);
68 final int purgedSize = purgedTransactions.size();
69 WritableObjects.writeLongs(out, closedTransactions.size(), purgedSize);
70 for (Entry<UnsignedLong, Boolean> e : closedTransactions.entrySet()) {
71 WritableObjects.writeLong(out, e.getKey().longValue());
72 out.writeBoolean(e.getValue());
74 purgedTransactions.writeRangesTo(out, purgedSize);
77 public static FrontendHistoryMetadata readFrom(final DataInput in) throws IOException {
78 byte header = WritableObjects.readLongHeader(in);
79 final long historyId = WritableObjects.readFirstLong(in, header);
80 final long cookie = WritableObjects.readSecondLong(in, header);
81 final boolean closed = in.readBoolean();
83 header = WritableObjects.readLongHeader(in);
84 long ls = WritableObjects.readFirstLong(in, header);
85 Verify.verify(ls >= 0 && ls <= Integer.MAX_VALUE);
86 final int csize = (int) ls;
88 ls = WritableObjects.readSecondLong(in, header);
89 Verify.verify(ls >= 0 && ls <= Integer.MAX_VALUE);
90 final int psize = (int) ls;
92 final Map<UnsignedLong, Boolean> closedTransactions = new HashMap<>(csize);
93 for (int i = 0; i < csize; ++i) {
94 final UnsignedLong key = UnsignedLong.fromLongBits(WritableObjects.readLong(in));
95 final Boolean value = in.readBoolean();
96 closedTransactions.put(key, value);
98 final var purgedTransactions = ImmutableUnsignedLongSet.readFrom(in, psize);
100 return new FrontendHistoryMetadata(historyId, cookie, closed, closedTransactions, purgedTransactions);
104 public String toString() {
105 return MoreObjects.toStringHelper(FrontendHistoryMetadata.class).add("historyId", historyId)
106 .add("cookie", cookie).add("closed", closed).add("closedTransactions", closedTransactions)
107 .add("purgedTransactions", purgedTransactions).toString();