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


##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java:
##########
@@ -975,30 +1020,104 @@ private boolean addToBatchForRemoval(WriteBatch batch, 
byte[] key, long curRev,
     }
 
     /**
-     * Compacts all entries by the given key, removing all previous revisions 
and deleting the last entry if it is a tombstone.
+     * Compacts all entries by the given key, removing revision that are no 
longer needed.
+     * Last entry with a revision lesser or equal to the {@code 
minRevisionToKeep} and all consecutive entries will be preserved.
+     * If the first entry to keep is a tombstone, it will be removed.
+     * Example:
+     * <pre>
+     * Example 1:
+     *     put entry1: revision 5
+     *     put entry2: revision 7
+     *
+     *     do compaction: revision 6
+     *
+     *     entry1: exists
+     *     entry2: exists
+     *
+     * Example 2:
+     *     put entry1: revision 5
+     *     put entry2: revision 7
+     *
+     *     do compaction: revision 7
+     *
+     *     entry1: doesn't exist
+     *     entry2: exists
+     * </pre>
      *
      * @param batch Write batch.
      * @param key   Target key.
      * @param revs  Revisions.
+     * @param minRevisionToKeep Minimum revision that should be kept.
      * @throws RocksDBException If failed.
      */
-    private void compactForKey(WriteBatch batch, byte[] key, long[] revs) 
throws RocksDBException {
-        long lastRev = lastRevision(revs);
+    private void compactForKey(WriteBatch batch, byte[] key, long[] revs, long 
minRevisionToKeep) throws RocksDBException {
+        if (revs.length < 2) {
+            // If we have less than two revisions, there is no point in 
compaction.
+            return;
+        }
+
+        // Index of the first revision we will be keeping in the array of 
revisions.
+        int idxToKeepFrom = 0;
+
+        // Whether there is an entry with the minRevisionToKeep.
+        boolean hasMinRevision = false;
+
+        // Traverse revisions, looking for the first revision that needs to be 
kept.
+        for (long rev : revs) {
+            if (rev >= minRevisionToKeep) {
+                if (rev == minRevisionToKeep) {
+                    hasMinRevision = true;
+                }
+                break;
+            }
+
+            idxToKeepFrom++;
+        }

Review Comment:
   It's just a "can it be done?" question.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to