Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/10883#discussion_r50630113
--- Diff:
common/sketch/src/main/java/org/apache/spark/util/sketch/DefaultBloomFilter.java
---
@@ -0,0 +1,220 @@
+/*
+ * 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.spark.util.sketch;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+
+public class DefaultBloomFilter extends BloomFilter {
+
+ private final int numHashFunctions;
+ private final BitArray bits;
+
+ public DefaultBloomFilter(int numHashFunctions, long numBits) {
+ this.numHashFunctions = numHashFunctions;
+ this.bits = new BitArray(numBits);
+ }
+
+ @Override
+ public double expectedFpp() {
+ return Math.pow((double) bits.bitCount() / bits.bitSize(),
numHashFunctions);
+ }
+
+ @Override
+ public long bitSize() {
+ return bits.bitSize();
+ }
+
+ private static long hashObjectToLong(Object item) {
+ if (item instanceof String) {
+ try {
+ byte[] bytes = ((String) item).getBytes("utf-8");
+ return hashBytesToLong(bytes);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("Only support utf-8 string", e);
+ }
+ } else {
+ long longValue;
+
+ if (item instanceof Long) {
+ longValue = (Long) item;
+ } else if (item instanceof Integer) {
+ longValue = ((Integer) item).longValue();
+ } else if (item instanceof Short) {
+ longValue = ((Short) item).longValue();
+ } else if (item instanceof Byte) {
+ longValue = ((Byte) item).longValue();
+ } else {
+ throw new IllegalArgumentException(
+ "Support for " + item.getClass().getName() + " not implemented"
+ );
+ }
+
+ int h1 = Murmur3_x86_32.hashLong(longValue, 0);
+ int h2 = Murmur3_x86_32.hashLong(longValue, h1);
+ return (((long) h1) << 32) | (h2 & 0xFFFFFFFFL);
+ }
+ }
+
+ private static long hashBytesToLong(byte[] bytes) {
+ int h1 = Murmur3_x86_32.hashUnsafeBytes(bytes,
Platform.BYTE_ARRAY_OFFSET, bytes.length, 0);
+ int h2 = Murmur3_x86_32.hashUnsafeBytes(bytes,
Platform.BYTE_ARRAY_OFFSET, bytes.length, h1);
+ return (((long) h1) << 32) | (h2 & 0xFFFFFFFFL);
+ }
+
+ @Override
+ public boolean put(Object item) {
+ long bitSize = bits.bitSize();
+ long hash64 = hashObjectToLong(item);
+ int h1 = (int) (hash64 >> 32);
+ int h2 = (int) hash64;
+
+ boolean bitsChanged = false;
+ for (int i = 1; i <= numHashFunctions; i++) {
+ int combinedHash = h1 + (i * h2);
+ // Flip all the bits if it's negative (guaranteed positive number)
+ if (combinedHash < 0) {
+ combinedHash = ~combinedHash;
+ }
+ bitsChanged |= bits.set(combinedHash % bitSize);
+ }
+ return bitsChanged;
+ }
+
+ @Override
+ public boolean mightContain(Object item) {
+ long bitSize = bits.bitSize();
+ long hash64 = hashObjectToLong(item);
+ int h1 = (int) (hash64 >> 32);
+ int h2 = (int) hash64;
+
+ for (int i = 1; i <= numHashFunctions; i++) {
+ int combinedHash = h1 + (i * h2);
+ // Flip all the bits if it's negative (guaranteed positive number)
+ if (combinedHash < 0) {
+ combinedHash = ~combinedHash;
+ }
+ if (!bits.get(combinedHash % bitSize)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public boolean isCompatible(BloomFilter other) {
+ if (other == null) {
+ return false;
+ }
+
+ if (!(other instanceof DefaultBloomFilter)) {
+ return false;
+ }
+
+ DefaultBloomFilter that = (DefaultBloomFilter) other;
+ return this.bitSize() == that.bitSize() && this.numHashFunctions ==
that.numHashFunctions;
+ }
+
+ @Override
+ public BloomFilter mergeInPlace(BloomFilter other) {
+ if (!isCompatible(other)) {
+ throw new IllegalArgumentException("Can't merge incompatible bloom
filter");
+ }
+
+ DefaultBloomFilter that = (DefaultBloomFilter) other;
+ this.bits.putAll(that.bits);
+ return this;
+ }
+
+ static final class BitArray {
--- End diff --
I'd move this out of this file and just have a BitSet class. We should have
a test suite for this too.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]