blambov commented on code in PR #2064:
URL: https://github.com/apache/cassandra/pull/2064#discussion_r1081342432


##########
src/java/org/apache/cassandra/io/sstable/format/FilterComponent.java:
##########
@@ -90,4 +97,63 @@ public static void saveOrDeleteCorrupted(Descriptor 
descriptor, IFilter filter)
             throw ex;
         }
     }
+
+    /**
+     * Optionally loads a Bloom filter. If the filter is not needed (FP chance 
is neglectable), it sets
+     * {@link AlwaysPresentFilter} as a filter in the builder. If the filter 
is expected to be recreated for various
+     * reasons, it leaves it {@code null} (unchanged). Otherwise, it attempts 
to load the filter, and if it succeeds,
+     * it is set in the builder. If a filter fails to load, it is left {@code 
null} (unchanged) meaning that it should
+     * be rebuilt.
+     */
+    public static IFilter maybeLoadBloomFilter(Descriptor descriptor, 
Set<Component> components, TableMetadata metadata, ValidationMetadata 
validationMetadata)
+    {
+        double currentFPChance = validationMetadata != null ? 
validationMetadata.bloomFilterFPChance : Double.NaN;
+        double desiredFPChance = metadata.params.bloomFilterFpChance;
+
+        IFilter filter = null;
+        if (!shouldUseBloomFilter(desiredFPChance))
+        {
+            if (logger.isTraceEnabled())
+                logger.trace("Bloom filter for {} will not be loaded because 
fpChance={} is neglectable", descriptor, desiredFPChance);
+
+            return FilterFactory.AlwaysPresent;
+        }
+        else if (!components.contains(Component.FILTER) || 
Double.isNaN(currentFPChance))
+        {
+            if (logger.isTraceEnabled())
+                logger.trace("Bloom filter for {} will not be loaded because 
filter component is missing or sstable lacks validation metadata", descriptor);
+
+            return null;
+        }
+        else if (!isFPChanceDiffNeglectable(desiredFPChance, currentFPChance) 
&& rebuildFilterOnFPChanceChange)
+        {
+            if (logger.isTraceEnabled())
+                logger.trace("Bloom filter for {} will not be loaded because 
fpChance has changed from {} to {} and the filter should be recreated", 
descriptor, currentFPChance, desiredFPChance);
+
+            return null;
+        }
+
+        try
+        {
+            filter = load(descriptor);
+            if (filter == null || filter instanceof AlwaysPresentFilter)
+                logger.info("Bloom filter for {} is missing or invalid", 
descriptor);
+        }
+        catch (IOException ex)
+        {
+            logger.info("Bloom filter for " + descriptor + " could not be 
deserialized", ex);
+        }
+
+        return filter;
+    }
+
+    static boolean shouldUseBloomFilter(double fpChance)
+    {
+        return !(Math.abs(1 - fpChance) <= filterFPChanceTolerance);
+    }
+
+    static boolean isFPChanceDiffNeglectable(double fpChance1, double 
fpChance2)

Review Comment:
   Nit: there's one "neglectable" left in the comment.



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