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.base.MoreObjects;
15 import com.google.common.collect.BoundType;
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableRangeSet;
18 import com.google.common.collect.Range;
19 import com.google.common.collect.RangeSet;
20 import com.google.common.primitives.UnsignedLong;
21 import java.util.Iterator;
22 import java.util.TreeSet;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.yangtools.concepts.Mutable;
27 * A class holding an equivalent of {@code Set<UnsignedLong>}. It is geared towards efficiently tracking ranges of
28 * objects, similar to what a {@link RangeSet} would do.
31 * Unlike a {@code RangeSet}, though, this class takes advantage of knowing that an unsigned long is a discrete unit
32 * and can be stored in a simple {@code long}.
34 * @author Robert Varga
37 public final class UnsignedLongSet implements Mutable {
38 private static final class Entry implements Comparable<Entry>, Mutable {
39 // Note: mutable to allow efficient merges.
43 private Entry(final long lowerBits, final long upperBits) {
44 this.lowerBits = lowerBits;
45 this.upperBits = upperBits;
48 static Entry of(final long longBits) {
49 return of(longBits, longBits);
52 static Entry of(final long lowerBits, final long upperBits) {
53 return new Entry(lowerBits, upperBits);
56 static Entry of(final Range<UnsignedLong> range) {
57 verify(range.lowerBoundType() == BoundType.CLOSED && range.upperBoundType() == BoundType.OPEN,
58 "Unexpected range %s", range);
59 return of(range.lowerEndpoint().longValue(), range.upperEndpoint().longValue() - 1);
62 boolean contains(final long longBits) {
63 return Long.compareUnsigned(lowerBits, longBits) <= 0 && Long.compareUnsigned(upperBits, longBits) >= 0;
67 return new Entry(lowerBits, upperBits);
70 // Provides compatibility with RangeSet<UnsignedLong> using [lower, upper + 1)
71 Range<UnsignedLong> toUnsigned() {
72 return Range.closedOpen(UnsignedLong.fromLongBits(lowerBits), UnsignedLong.fromLongBits(upperBits + 1));
76 @SuppressWarnings("checkstyle:parameterName")
77 public int compareTo(final Entry o) {
78 return Long.compareUnsigned(lowerBits, o.lowerBits);
82 public int hashCode() {
83 return Long.hashCode(lowerBits) * 31 + Long.hashCode(upperBits);
87 public boolean equals(final Object obj) {
91 if (!(obj instanceof Entry)) {
94 final var other = (Entry) obj;
95 return lowerBits == other.lowerBits && upperBits == other.upperBits;
99 public String toString() {
100 return "[" + Long.toUnsignedString(lowerBits) + ".." + Long.toUnsignedString(upperBits) + "]";
104 // The idea is rather simple, we track a TreeSet of range entries, ordered by their lower bound. This means that
105 // for a contains() operation we just need the first headSet() entry. For insert operations we just update either
106 // the lower bound or the upper bound of an existing entry. When we do, we also look at prev/next entry and if they
107 // are contiguous with the updated entry, we adjust the entry once more and remove the prev/next entry.
108 private final TreeSet<Entry> ranges;
110 private UnsignedLongSet(final TreeSet<Entry> ranges) {
111 this.ranges = requireNonNull(ranges);
114 private UnsignedLongSet(final RangeSet<UnsignedLong> rangeSet) {
115 ranges = new TreeSet<>();
116 for (var range : rangeSet.asRanges()) {
117 ranges.add(Entry.of(range));
121 public static @NonNull UnsignedLongSet of() {
122 return new UnsignedLongSet(new TreeSet<>());
125 public static @NonNull UnsignedLongSet of(final RangeSet<UnsignedLong> rangeSet) {
126 return new UnsignedLongSet(rangeSet);
129 public void add(final long longBits) {
130 final var range = Entry.of(longBits);
132 final var headIt = headIter(range);
133 if (headIt.hasNext()) {
134 final var head = headIt.next();
135 if (head.contains(longBits)) {
138 if (head.upperBits + 1 == longBits) {
139 head.upperBits = longBits;
140 final var tailSet = ranges.tailSet(range);
141 if (!tailSet.isEmpty()) {
142 final var tail = tailSet.first();
143 if (tail.lowerBits - 1 == longBits) {
144 tail.lowerBits = head.lowerBits;
152 final var tailSet = ranges.tailSet(range);
153 if (!tailSet.isEmpty()) {
154 final var tail = tailSet.first();
155 if (tail.lowerBits - 1 == longBits) {
156 tail.lowerBits = longBits;
164 public boolean contains(final long longBits) {
165 final var headIt = headIter(Entry.of(longBits));
166 return headIt.hasNext() && headIt.next().contains(longBits);
169 public UnsignedLongSet copy() {
170 return new UnsignedLongSet(new TreeSet<>(Collections2.transform(ranges, Entry::copy)));
173 public ImmutableRangeSet<UnsignedLong> toRangeSet() {
174 return ImmutableRangeSet.copyOf(Collections2.transform(ranges, Entry::toUnsigned));
178 public int hashCode() {
179 return ranges.hashCode();
183 public boolean equals(final Object obj) {
184 return obj == this || obj instanceof UnsignedLongSet && ranges.equals(((UnsignedLongSet) obj).ranges);
188 public String toString() {
189 final var helper = MoreObjects.toStringHelper(this);
191 final int size = ranges.size();
196 helper.add("span", ranges.first());
199 helper.add("span", Entry.of(ranges.first().lowerBits, ranges.last().upperBits));
202 return helper.add("size", size).toString();
205 private Iterator<Entry> headIter(final Entry range) {
206 return ranges.headSet(range, true).descendingIterator();