2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.utils;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.collect.Maps;
16 import com.google.common.primitives.UnsignedLong;
17 import java.io.DataInput;
18 import java.io.DataOutput;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Comparator;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.opendaylight.yangtools.concepts.Immutable;
28 import org.opendaylight.yangtools.concepts.WritableObjects;
31 * A more efficient equivalent of {@code ImmutableMap<UnsignedLong, Boolean>}.
34 public abstract class UnsignedLongBitmap implements Immutable {
36 static final class Regular extends UnsignedLongBitmap {
37 private final long[] keys;
38 private final boolean[] values;
40 Regular(final long[] keys, final boolean[] values) {
41 this.keys = requireNonNull(keys);
42 this.values = requireNonNull(values);
43 verify(keys.length == values.length);
47 public boolean isEmpty() {
48 return keys.length == 0;
57 void writeEntriesTo(final DataOutput out) throws IOException {
58 for (int i = 0; i < keys.length; ++i) {
59 writeEntry(out, keys[i], values[i]);
64 StringBuilder appendEntries(final StringBuilder sb) {
65 final int last = keys.length - 1;
66 for (int i = 0; i < last; ++i) {
67 appendEntry(sb, keys[i], values[i]).append(", ");
69 return appendEntry(sb, keys[last], values[last]);
73 void putEntries(final HashMap<UnsignedLong, Boolean> ret) {
74 for (int i = 0; i < keys.length; ++i) {
75 ret.put(UnsignedLong.fromLongBits(keys[i]), values[i]);
80 public int hashCode() {
81 return Arrays.hashCode(keys) ^ Arrays.hashCode(values);
85 public boolean equals(final Object obj) {
89 if (!(obj instanceof Regular)) {
92 final var other = (Regular) obj;
93 return Arrays.equals(keys, other.keys) && Arrays.equals(values, other.values);
97 private static final class Singleton extends UnsignedLongBitmap {
98 private final long key;
99 private final boolean value;
101 Singleton(final long key, final boolean value) {
107 public boolean isEmpty() {
117 void writeEntriesTo(final DataOutput out) throws IOException {
118 writeEntry(out, key, value);
122 StringBuilder appendEntries(final StringBuilder sb) {
123 return sb.append(Long.toUnsignedString(key)).append('=').append(value);
127 void putEntries(final HashMap<UnsignedLong, Boolean> ret) {
128 ret.put(UnsignedLong.fromLongBits(key), value);
132 public int hashCode() {
133 return Long.hashCode(key) ^ Boolean.hashCode(value);
137 public boolean equals(final Object obj) {
141 if (!(obj instanceof Singleton)) {
144 final var other = (Singleton) obj;
145 return key == other.key && value == other.value;
149 private static final @NonNull UnsignedLongBitmap EMPTY = new Regular(new long[0], new boolean[0]);
151 private UnsignedLongBitmap() {
155 public static @NonNull UnsignedLongBitmap of() {
159 public static @NonNull UnsignedLongBitmap of(final long keyBits, final boolean value) {
160 return new Singleton(keyBits, value);
163 public static @NonNull UnsignedLongBitmap copyOf(final Map<UnsignedLong, Boolean> map) {
164 final int size = map.size();
169 final var entry = map.entrySet().iterator().next();
170 return of(entry.getKey().longValue(), entry.getValue());
172 final var entries = new ArrayList<>(map.entrySet());
173 entries.sort(Comparator.comparing(Entry::getKey));
175 final var keys = new long[size];
176 final var values = new boolean[size];
179 for (var e : entries) {
180 keys[idx] = e.getKey().longValue();
181 values[idx] = e.getValue();
185 return new Regular(keys, values);
189 public abstract boolean isEmpty();
191 public abstract int size();
193 public final @NonNull HashMap<UnsignedLong, Boolean> mutableCopy() {
194 final int size = size();
197 return new HashMap<>();
199 final var ret = Maps.<UnsignedLong, Boolean>newHashMapWithExpectedSize(size);
205 public static @NonNull UnsignedLongBitmap readFrom(final @NonNull DataInput in, final int size) throws IOException {
210 return new Singleton(WritableObjects.readLong(in), in.readBoolean());
212 final var keys = new long[size];
213 final var values = new boolean[size];
214 for (int i = 0; i < size; ++i) {
215 keys[i] = WritableObjects.readLong(in);
216 values[i] = in.readBoolean();
219 // There should be no duplicates and the IDs need to be increasing
220 long prevKey = keys[0];
221 for (int i = 1; i < size; ++i) {
222 final long key = keys[i];
223 if (Long.compareUnsigned(prevKey, key) >= 0) {
224 throw new IOException("Key " + Long.toUnsignedString(key) + " may not be used after key "
225 + Long.toUnsignedString(prevKey));
230 return new Regular(keys, values);
234 public void writeEntriesTo(final @NonNull DataOutput out, final int size) throws IOException {
235 if (size != size()) {
236 throw new IOException("Mismatched size: expected " + size() + ", got " + size);
241 abstract void writeEntriesTo(@NonNull DataOutput out) throws IOException;
243 abstract StringBuilder appendEntries(StringBuilder sb);
245 abstract void putEntries(HashMap<UnsignedLong, Boolean> ret);
251 * Implementations of this method return a deterministic value.
254 public abstract int hashCode();
257 public abstract boolean equals(Object obj);
260 public final String toString() {
261 return isEmpty() ? "{}" : appendEntries(new StringBuilder().append('{')).append('}').toString();
264 private static StringBuilder appendEntry(final StringBuilder sb, final long key, final boolean value) {
265 return sb.append(Long.toUnsignedString(key)).append('=').append(value);
268 private static void writeEntry(final @NonNull DataOutput out, final long key, final boolean value)
270 // FIXME: This serialization format is what we inherited. We could do better by storing the boolean in
271 // writeLong()'s flags. On the other had, we could also be writing longs by twos, which might be
273 WritableObjects.writeLong(out, key);
274 out.writeBoolean(value);