Remove UnsignedLongSet.addImpl() 59/98359/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 8 Nov 2021 07:49:15 +0000 (08:49 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 8 Nov 2021 08:04:04 +0000 (09:04 +0100)
We now do not share utilities in UnsignedLongSet, hence relocate
implementation code to MutableUnsignedLongSet.add().

JIRA: CONTROLLER-2014
Change-Id: Id1578a8ea639ed512d55b2e6dca26de5abdf3ab0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/MutableUnsignedLongSet.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/UnsignedLongSet.java

index e455f90..6fdb04f 100644 (file)
@@ -31,7 +31,45 @@ public final class MutableUnsignedLongSet extends UnsignedLongSet implements Mut
     }
 
     public void add(final long longBits) {
-        addImpl(longBits);
+        final var ranges = trustedRanges();
+        final var range = Entry.of(longBits);
+
+        // We need Iterator.remove() to perform efficient merge below
+        final var headIt = ranges.headSet(range, true).descendingIterator();
+        if (headIt.hasNext()) {
+            final var head = headIt.next();
+            if (head.contains(longBits)) {
+                return;
+            }
+
+            // Merge into head entry if possible
+            if (head.upperBits + 1 == longBits) {
+                head.upperBits = longBits;
+
+                // Potentially merge head entry and tail entry
+                final var tail = ranges.higher(range);
+                if (tail != null) {
+                    if (tail.lowerBits - 1 == longBits) {
+                        // Expand tail, remove head
+                        tail.lowerBits = head.lowerBits;
+                        headIt.remove();
+                    }
+                }
+                return;
+            }
+        }
+
+        final var tail = ranges.higher(range);
+        if (tail != null) {
+            // Merge into tail entry if possible
+            if (tail.lowerBits - 1 == longBits) {
+                tail.lowerBits = longBits;
+                return;
+            }
+        }
+
+        // No luck, store a new entry
+        ranges.add(range);
     }
 
     public ImmutableRangeSet<UnsignedLong> toRangeSet() {
index ca45a3c..daf958a 100644 (file)
@@ -137,39 +137,6 @@ abstract class UnsignedLongSet {
         this.ranges = requireNonNull(ranges);
     }
 
-    final void addImpl(final long longBits) {
-        final var range = Entry.of(longBits);
-
-        final var headIt = ranges.headSet(range, true).descendingIterator();
-        if (headIt.hasNext()) {
-            final var head = headIt.next();
-            if (head.contains(longBits)) {
-                return;
-            }
-            if (head.upperBits + 1 == longBits) {
-                head.upperBits = longBits;
-                final var tail = ranges.higher(range);
-                if (tail != null) {
-                    if (tail.lowerBits - 1 == longBits) {
-                        tail.lowerBits = head.lowerBits;
-                        headIt.remove();
-                    }
-                }
-                return;
-            }
-        }
-
-        final var tail = ranges.higher(range);
-        if (tail != null) {
-            if (tail.lowerBits - 1 == longBits) {
-                tail.lowerBits = longBits;
-                return;
-            }
-        }
-
-        ranges.add(range);
-    }
-
     public final boolean contains(final long longBits) {
         final var head = ranges.floor(Entry.of(longBits));
         return head != null && head.contains(longBits);