Claudenw commented on code in PR #402: URL: https://github.com/apache/commons-collections/pull/402#discussion_r1256479009
########## src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java: ########## @@ -0,0 +1,349 @@ +/* + * 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.commons.collections4.bloomfilter; + +import java.util.LinkedList; +import java.util.NoSuchElementException; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.function.Supplier; + +/** + * Implementation of the methods to manage the Layers in a Layered Bloom filter. + * <p> + * The manager comprises a list of Bloom filters that are managed based on + * various rules. The last filter in the list is known as the {@code target} and + * is the filter into which merges are performed. The Layered manager utilizes + * three methods to manage the list. + * </p> + * <ul> + * <li>ExtendCheck - A Predicate that if true causes a new Bloom filter to be + * created as the new target.</li> + * <li>FilterSupplier - A Supplier that produces empty Bloom filters to be used + * as a new target.</li> + * <li>Cleanup - A Consumer of a LinkedList of BloomFilter that removes any + * expired or out dated filters from the list.</li> + * </ul> + * <p> + * When extendCheck returns {@code true} the following steps are taken: + * </p> + * <ol> + * <li>If the current target is empty it is removed.</li> + * <li>{@code Cleanup} is called</li> + * <li>{@code FilterSuplier} is executed and the new filter added to the list as + * the {@code target} filter.</li> + * </ol> + * + * @since 4.5 + */ +public class LayerManager implements BloomFilterProducer { + + /** + * Static methods an variable for standard extend checks. + * + */ + public static class ExtendCheck { Review Comment: Done ########## src/main/java/org/apache/commons/collections4/bloomfilter/LayerManager.java: ########## @@ -0,0 +1,349 @@ +/* + * 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.commons.collections4.bloomfilter; + +import java.util.LinkedList; +import java.util.NoSuchElementException; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.function.Supplier; + +/** + * Implementation of the methods to manage the Layers in a Layered Bloom filter. + * <p> + * The manager comprises a list of Bloom filters that are managed based on + * various rules. The last filter in the list is known as the {@code target} and + * is the filter into which merges are performed. The Layered manager utilizes + * three methods to manage the list. + * </p> + * <ul> + * <li>ExtendCheck - A Predicate that if true causes a new Bloom filter to be + * created as the new target.</li> + * <li>FilterSupplier - A Supplier that produces empty Bloom filters to be used + * as a new target.</li> + * <li>Cleanup - A Consumer of a LinkedList of BloomFilter that removes any + * expired or out dated filters from the list.</li> + * </ul> + * <p> + * When extendCheck returns {@code true} the following steps are taken: + * </p> + * <ol> + * <li>If the current target is empty it is removed.</li> + * <li>{@code Cleanup} is called</li> + * <li>{@code FilterSuplier} is executed and the new filter added to the list as + * the {@code target} filter.</li> + * </ol> + * + * @since 4.5 + */ +public class LayerManager implements BloomFilterProducer { + + /** + * Static methods an variable for standard extend checks. + * + */ + public static class ExtendCheck { + private ExtendCheck() { + } + + /** + * Advances the target once a merge has been performed. + */ + public static final Predicate<LayerManager> ADVANCE_ON_POPULATED = lm -> { + return !lm.filters.isEmpty() && !lm.filters.peekLast().forEachBitMap(y -> y == 0); + }; + + /** + * Does not automatically advance the target. next() must be called directly to + * perform the advance. + */ + public static final Predicate<LayerManager> NEVER_ADVANCE = x -> false; + + /** + * Calculates the estimated number of Bloom filters (n) that have been merged + * into the target and compares that with the estimated maximum expected n based + * on the shape. If the target is full then a new target is created. + * + * @param shape The shape of the filters in the LayerManager. + * @return A Predicate suitable for the LayerManager extendCheck parameter. + */ + public static final Predicate<LayerManager> advanceOnCalculatedFull(Shape shape) { + return advanceOnSaturation(shape.estimateMaxN()); + } + + /** + * Creates a new target after a specific number of filters have been added to + * the current target. + * + * @param breakAt the number of filters to merge into each filter in the list. + * @return A Predicate suitable for the LayerManager extendCheck parameter. + */ + public static Predicate<LayerManager> advanceOnCount(int breakAt) { Review Comment: done -- 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]
