/*
* 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 io.snappydata.util.com.clearspring.analytics.stream.membership;

import java.io.UnsupportedEncodingException;

import java.lang.reflect.Method;

import io.snappydata.util.com.clearspring.analytics.hash.MurmurHash;

public abstract class Filter {

    int hashCount;

    public int getHashCount() {
        return hashCount;
    }

    public int[] getHashBuckets(String key) {
        return Filter.getHashBuckets(key, hashCount, buckets());
    }

    public int[] getHashBuckets(byte[] key) {
        return Filter.getHashBuckets(key, hashCount, buckets());
    }


    abstract int buckets();

    public abstract void add(String key);

    public abstract boolean isPresent(String key);

    // for testing
    abstract int emptyBuckets();

    @SuppressWarnings("unchecked")
    ICompactSerializer<Filter> getSerializer() {
        Method method = null;
        try {
            method = getClass().getMethod("serializer");
            return (ICompactSerializer<Filter>) method.invoke(null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Murmur is faster than an SHA-based approach and provides as-good collision
    // resistance.  The combinatorial generation approach described in
    // http://www.eecs.harvard.edu/~kirsch/pubs/bbbf/esa06.pdf
    // does prove to work in actual tests, and is obviously faster
    // than performing further iterations of murmur.
    public static int[] getHashBuckets(String key, int hashCount, int max) {
        byte[] b;
        try {
            b = key.getBytes("UTF-16");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return getHashBuckets(b, hashCount, max);
    }

    static int[] getHashBuckets(byte[] b, int hashCount, int max) {
        int[] result = new int[hashCount];
        int hash1 = MurmurHash.hash(b, b.length, 0);
        int hash2 = MurmurHash.hash(b, b.length, hash1);
        for (int i = 0; i < hashCount; i++) {
            result[i] = Math.abs((hash1 + i * hash2) % max);
        }
        return result;
    }
}
@@ -1881,24 +1881,107 @@
    %7D%0A    %7D%0A%0A
+    static final int seed1 = 0x3c074a61;%0A    static final int seed2 = 0xf7ca7fd2;%0A%0A
     // Murmu
@@ -2050,16 +2050,16 @@
 llision%0A
-
     // r
@@ -2451,10 +2451,9 @@
 UTF-
-16
+8
 %22);%0A
@@ -2773,17 +2773,21 @@
 length, 
-0
+seed1
 );%0A     
@@ -2835,16 +2835,24 @@
 h, hash1
+ * seed2
 );%0A     

Fix from Sumedh for better hash computation- he did some benchmarking and found this one to be better than the previous one.