You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "dcapwell (via GitHub)" <gi...@apache.org> on 2023/06/12 20:02:37 UTC

[GitHub] [cassandra-accord] dcapwell commented on a diff in pull request #50: State Eviction

dcapwell commented on code in PR #50:
URL: https://github.com/apache/cassandra-accord/pull/50#discussion_r1227176609


##########
accord-core/src/main/java/accord/local/CommandStores.java:
##########
@@ -203,53 +202,48 @@ public RangesForEpoch withRanges(long epoch, Ranges ranges)
             if (fromInclusive == toInclusive)
                 return allAt(fromInclusive);
 
-            int i = Arrays.binarySearch(epochs, fromInclusive);
-            if (i < 0) i = -2 - i;
-            if (i < 0) i = 0;
-
-            int j = Arrays.binarySearch(epochs, toInclusive);
-            if (j < 0) j = -2 - j;
-            if (i > j) return Ranges.EMPTY;
-
-            Ranges result = ranges[i++];
-            while (i <= j)
-                result = result.with(ranges[i++]);
-
-            return result;
+            return allInternal(floorIndex(fromInclusive), 1 + floorIndex(toInclusive));
         }
 
         public @Nonnull Ranges allSince(long fromInclusive)
         {
-            int i = Arrays.binarySearch(epochs, fromInclusive);
-            if (i < 0) i = Math.max(0, -2 -i);
-
-            Ranges result = ranges[i++];
-            while (i < ranges.length)
-                result = ranges[i++].with(result);
-
-            return result;
+            return allInternal(floorIndex(fromInclusive), ranges.length);
         }
 
         public @Nonnull Ranges all()
         {
-            if (ranges.length == 0)
-                return Ranges.EMPTY;
+            return allInternal(0, ranges.length);
+        }
 
-            Ranges result = ranges[0];
-            for (int i = 1; i < ranges.length ; ++i)
-                result = ranges[i].with(result);
+        public @Nonnull Ranges allBefore(long toExclusive)
+        {
+            return allInternal(0, ceilIndex(toExclusive));
+        }
 
-            return result;
+        public @Nonnull Ranges allUntil(long toInclusive)
+        {
+            return allInternal(0, 1 + floorIndex(toInclusive));
         }
 
-        public @Nonnull Ranges allBefore(long toExclusive)
+        private int floorIndex(long epoch)
         {
-            int limit = Arrays.binarySearch(epochs, toExclusive);
-            if (limit < 0) limit = Math.max(0, -1 -limit);
-            if (limit == 0) return Ranges.EMPTY;
+            int i = Arrays.binarySearch(epochs, epoch);
+            if (i < 0) i = -2 - i;

Review Comment:
   when `I = -1` you get `-2 - -1 = -1`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org