Yukei7 commented on code in PR #3754:
URL: https://github.com/apache/cassandra/pull/3754#discussion_r1984281912


##########
src/java/org/apache/cassandra/db/lifecycle/View.java:
##########
@@ -353,4 +361,40 @@ public boolean apply(T t)
             }
         };
     }
+
+    // Match the SSTableReaders from the existing ones to the new one to be 
added (with same ranges)
+    // Returns the map of toRemove <-> toAdd. Return empty map if such 1-1 
replacement doesn't exist
+    private static Map<SSTableReader, SSTableReader> getReplacementMap(final 
Set<SSTableReader> remove, final Iterable<SSTableReader> add)
+    {
+        List<SSTableReader> toAdds = new ArrayList<>();
+        for (SSTableReader s : add)
+            toAdds.add(s);
+
+        if (remove.size() != toAdds.size())
+            return Collections.emptyMap();
+
+        List<SSTableReader> toRemoves = new ArrayList<>(remove);
+        // sort the SSTableReader list by (first, last, descriptor.id). The 
view is per cfs so id will be unique
+        Comparator<SSTableReader> comp = Comparator.comparing((SSTableReader 
s) -> s.first)
+                                                   .thenComparing(s -> s.last)
+                                                   
.thenComparing(SSTableReader.idComparator);
+        toRemoves.sort(comp);
+        toAdds.sort(comp);
+
+        Map<SSTableReader, SSTableReader> replacementMap = new HashMap<>();
+        // toAdd and toRemove have the same size
+        for (int i = 0; i < toAdds.size(); i++)
+        {
+            SSTableReader toRemove = toRemoves.get(i);
+            SSTableReader toAdd = toAdds.get(i);
+            // optimization: here we don't check the descriptor. If we're able 
to match those to be removed with those
+            // to be added, we ensure that the pairs have the same (first, 
last) range
+            if (toRemove.first.equals(toAdd.first) && 
toRemove.last.equals(toAdd.last))

Review Comment:
   IntervalTree only search with `searchInterval.min` and `searchInterval.max`, 
all sstables intersect with this will be returned. It's safe if we properly 
construct this map to ensure that toAdd and toRemove is 1-to-1 mapping with 
same [first,last], and in the replace method we do check on `toRemove` equals 
to the interval in the tree.
   
   e.g.
   Say T1, T2, T3 all share [1,4]
   We replace T2 with T2' also share with [1,4]
   toAdd = T2', toRemove=T2
   The map here will be {T2: T2'}
   
   When actually figuring the intervals to do replacement, we do check: 
`node.intersectsLeft.get(i).equals(intervalToRemove)`
   
   That we start checking from T1, but `!T1.equals(T2)` and we continue to the 
correct place T2.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to