tkalkirill commented on code in PR #4465:
URL: https://github.com/apache/ignite-3/pull/4465#discussion_r1778491732


##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/KeyValueStorageUtils.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.metastorage.server;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.util.function.LongPredicate;
+
+/** Helper class with useful methods and constants for {@link KeyValueStorage} 
implementations. */
+public class KeyValueStorageUtils {
+    /** Special value indicating that there are no key revisions that need to 
be compacted. */
+    public static final int NOTHING_TO_COMPACT_INDEX = -1;
+
+    /**
+     * Calculates the revision index in key revisions up to which compaction 
is needed, {@link #NOTHING_TO_COMPACT_INDEX} if nothing needs
+     * to be compacted.
+     *
+     * <p>If the returned index points to the last revision and if the last 
revision is <b>not</b> a tombstone, then the returned index is
+     * decremented by 1.</p>
+     *
+     * @param keyRevisions Metastorage key revisions in ascending order.
+     * @param compactionRevisionInclusive Revision up to which you need to 
compact (inclusive).
+     * @param tombstoneKeyRevisionPredicate Predicate to test whether a key 
revision is a tombstone.
+     */
+    public static int indexToCompact(long[] keyRevisions, long 
compactionRevisionInclusive, LongPredicate tombstoneKeyRevisionPredicate) {
+        int i = indexToCompact(keyRevisions, compactionRevisionInclusive);
+
+        if (i != NOTHING_TO_COMPACT_INDEX && i == keyRevisions.length - 1 && 
!tombstoneKeyRevisionPredicate.test(keyRevisions[i])) {
+            i--;
+        }
+
+        return i;
+    }
+
+    private static int indexToCompact(long[] keyRevisions, long 
compactionRevisionInclusive) {
+        int index = NOTHING_TO_COMPACT_INDEX;
+
+        for (int i = 0; i < keyRevisions.length; i++) {

Review Comment:
   Yep, thanks!



-- 
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: notifications-unsubscr...@ignite.apache.org

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

Reply via email to