AlexanderSaydakov commented on code in PR #558:
URL: https://github.com/apache/datasketches-java/pull/558#discussion_r1605593242


##########
src/main/java/org/apache/datasketches/filters/quotientfilter/QuotientFilterBuilder.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.datasketches.filters.quotientfilter;
+import java.util.concurrent.ThreadLocalRandom;
+
+import org.apache.datasketches.common.SketchesArgumentException;
+
+/**
+ * This class provides methods to help estimate the correct parameters when
+ * creating a Quotient filter, and methods to create the filter using those 
values.
+ *
+ * The underlying math is described in the
+ *
+ * Wikipedia article on Quotient filters.
+ */
+public final class QuotientFilterBuilder {
+
+    /*
+    This function is used to suggest the number of bits per entry for a given 
number of entries.
+    The fingerprint length is related to the targetFalsePositiveProb roughly 
by 2^(-fingerprint_length).
+    Hence, the length of the fingerprint can be stored in at most 8 bits.
+    This, after rounding up, is the same as the more sophisticated expression 
which involves the capacity
+    from 
https://en.wikipedia.org/wiki/Quotient_filter#Probability_of_false_positives.
+    * @param targetFalsePositiveProb A desired false positive probability per 
item
+    * @return The suggested fingerprint length in bits
+     */
+    public static byte suggestFingerprintLength(double 
targetFalsePositiveProb) {
+        if (targetFalsePositiveProb <= 0. || targetFalsePositiveProb >= 1.) {
+
+            throw new SketchesArgumentException("targetFalsePositiveProb must 
be a valid probability and strictly greater than 0");
+        }
+        return (byte) Math.ceil(-Math.log(targetFalsePositiveProb) / 
Math.log(2));
+    }
+
+    /**
+     * This method suggests the number of slots in the filter for a given 
input size, assuming 90% capacity.
+     * There is no load factor checking internally within the filter, so this 
method is used to map between the
+     * number of items we insert into a sketch and the number of slots we need 
to allocate.
+     * A design feature of Niv's implementation is that 2^j +2*j slots are 
allocated. This asymptotically approaches
+     * 2^j slots as j grows, and the canonical number of slots is 2^j. 
Therefore, we will only check against
+     * 0.9*2^j slots.
+     * The load factor is 0.9 to get some space-utility advantages over the 
bloom filter.
+     * @param maxDistinctItems The maximum number of distinct items that can 
be inserted into the filter.
+     * @return The log-base-2 of the number of slots in the filter.
+     */
+    public static byte suggestLgNumSlots(long maxDistinctItems) {
+        if (maxDistinctItems <= 0) {
+            throw new SketchesArgumentException("maxDistinctItems must be 
strictly positive");
+        }
+        byte result = (byte) Math.ceil(Math.log(maxDistinctItems / 0.9) / 
Math.log(2));

Review Comment:
   I would suggest to avoid unnamed magic numbers.
   static final double loadFactor = 0.9



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