Package orestes.bloomfilter

Examples of orestes.bloomfilter.FilterBuilder


        int m = root.get("size").getAsInt();
        int k = root.get("hashes").getAsInt();
        String hashMethod = root.get("HashMethod").getAsString();
        byte[] bits = Base64.getDecoder().decode(root.get("bits").getAsString());

        FilterBuilder builder = new FilterBuilder(m, k)
                .hashFunction(HashMethod.valueOf(hashMethod));

        BloomFilter<T> filter = builder.buildBloomFilter();
        filter.getBitSet().and(BitSet.valueOf(bits));

        return filter;
    }
View Full Code Here


    }


    public FilterBuilder persistConfig(RedisPool pool, FilterBuilder builder) {
        return pool.safelyReturn(jedis -> {
            FilterBuilder newConfig = null;
            //Retry on concurrent changes
            while (newConfig == null) {
                if (!builder.overwriteIfExists() && jedis.exists(builder.name())) {
                    newConfig = this.parseConfigHash(jedis.hgetAll(builder.name()), builder.name());
                } else {
View Full Code Here

        map.put(HASH_METHOD_KEY, config.hashMethod().name());
        return map;
    }

    public FilterBuilder parseConfigHash(Map<String, String> map, String name) {
        FilterBuilder config = new FilterBuilder();
        config.name(name);
        config.redisBacked(true);
        config.falsePositiveProbability(Double.valueOf(map.get(P_KEY)));
        config.size(Integer.valueOf(map.get(M_KEY)));
        config.hashes(Integer.valueOf(map.get(K_KEY)));
        config.expectedElements(Integer.valueOf(map.get(N_KEY)));
        config.countingBits(Integer.valueOf(map.get(C_KEY)));
        config.hashFunction(HashMethod.valueOf(map.get(HASH_METHOD_KEY)));
        config.complete();
        return config;
    }
View Full Code Here

        testFalsePositives(inserts, fpp, Randoms.RANDWORDS);
    }

    public static void testFalsePositives(int inserts, double p, Randoms mode) {
        BloomFilter<String> dummy = new FilterBuilder()
                .expectedElements(inserts)
                .falsePositiveProbability(p)
                .buildBloomFilter();
        System.out.println(dummy);
        System.out.println("<table><tr><th>Hash function</th><th>Speed  (ms)</th><th>f during insert (%)</th><th>f final (%)</th></tr>");
View Full Code Here

        System.out.println("</table>");
    }

    public static void testFP(HashMethod hm, Randoms mode, int n, double p) {
        FilterBuilder builder = new FilterBuilder()
                .expectedElements(n)
                .falsePositiveProbability(p)
                .hashFunction(hm);

        List<byte[]> hashData = mode.generate(n, 1).get(0);
        List<byte[]> probeData = mode.generate(n * 3, 1).get(0);

        BloomFilter<String> b = builder.buildBloomFilter();
        int inserts = hashData.size();
        int fps = 0;
        Set<String> seen = new HashSet<>();
        long start = System.nanoTime();
        for (byte[] current : hashData) {
View Full Code Here

        System.out.printf("Time to complete %s sets: %s ms\n", rounds, stop - start);
    }

    public static void regularBF() {
        //Create a Bloom filter that has a false positive rate of 0.1 when containing 1000 elements
        BloomFilter<String> bf = new FilterBuilder(1000, 0.1).buildBloomFilter();

        //Add a few elements
        bf.add("Just");
        bf.add("a");
        bf.add("test.");

        //Test if they are contained
        print(bf.contains("Just")); //true
        print(bf.contains("a")); //true
        print(bf.contains("test.")); //true

        //Test with a non-existing element
        print(bf.contains("WingDangDoodel")); //false

        //Add 300 elements
        for (int i = 0; i < 300; i++) {
            String element = "Element " + i;
            bf.add(element);
        }

        //test for false positives
        for (int i = 300; i < 1000; i++) {
            String element = "Element " + i;
            if (bf.contains(element)) {
                print(element); //two elements: 440, 669
            }
        }
        //Compare with the expected amount
        print(bf.getFalsePositiveProbability(303) * 700); //1.74

        //Estimate cardinality/population
        print(bf.getEstimatedPopulation()); //303.6759147801151

        //Clone the Bloom filter
        bf.clone();
        //Reset it
        bf.clear();
        //add in Bulk
        List<String> bulk = Arrays.asList("one", "two", "three");
        bf.addAll(bulk);
        print(bf.containsAll(bulk)); //true

        //Create a more customized Bloom filter
        BloomFilter<Integer> bf2 = new FilterBuilder()
                .expectedElements(6666) //elements
                .size(10000) //bits to use
                .hashFunction(HashMethod.Murmur3) //our hash
                .buildBloomFilter();

        print("#Hashes:" + bf2.getHashes()); //2
        print(FilterBuilder.optimalK(6666, 10000)); //you can also do these calculations yourself

        //Create two Bloom filters with equal parameters
        BloomFilter<String> one = new FilterBuilder(100, 0.1).buildBloomFilter();
        BloomFilter<String> other = new FilterBuilder(100, 0.1).buildBloomFilter();
        one.add("this");
        other.add("that");
        one.union(other);
        print(one.contains("this")); //true
        print(one.contains("that")); //true
View Full Code Here

    }

    public static void countingBF() throws MalformedURLException {
        //Create a Counting Bloom filter that has a FP rate of 0.01 when 1000 are inserted
        //and uses 4 bits for counting
        CountingBloomFilter<String> cbf = new FilterBuilder(1000, 0.01).buildCountingBloomFilter();
        cbf.add("http://google.com");
        cbf.add("http://twitter.com");
        print(cbf.contains("http://google.com")); //true
        print(cbf.contains("http://twitter.com")); //true

        //What only the Counting Bloom filter can do:
        cbf.remove("http://google.com");
        print(cbf.contains("http://google.com")); //false

        //Use the Memory Bloom filter explicitly:
        FilterBuilder fb = new FilterBuilder(1000, 0.01).countingBits(4);
        CountingBloomFilterMemory<String> filter = new CountingBloomFilterMemory<>(fb);
        filter.setOverflowHandler(() -> print("ups"));

        for (int i = 1; i < 20; i++) {
            print("Round " + i);
            filter.add("http://example.com"); //Causes onOverflow() in Round >= 16
        }

        //See the inner structure
        CountingBloomFilter<String> small = new FilterBuilder(3, 0.2)
                .countingBits(4)
                .buildCountingBloomFilter();
        small.add("One");
        small.add("Two");
        small.add("Three");
View Full Code Here

            r.nextBytes(b);
            nonExistingElements.add(new String(b));
        }

        //Mine
        BloomFilter<String> bf = new FilterBuilder(elementCount, 0.001).buildBloomFilter();
//        bf.setCryptographicHashFunction("SHA-512");
        //Counting
//        CountingBloomFilter<String> bf = new CountingBloomFilter<String>(elementCount, 0.001, 4);
//        bf.setHashFunction("SHA-512");
        //Skjegstad's
View Full Code Here

    public static void redisBF() {
        String host = "localhost";
        int port = 6379;
        String filterName = "normalbloomfilter";
        //Open a Redis-backed Bloom filter
        BloomFilter<String> bfr = new FilterBuilder(1000, 0.01)
                .name(filterName) //use a distinct name
                .redisBacked(true)
                .redisHost(host) //Default is localhost
                .redisPort(port) //Default is standard 6379
                .buildBloomFilter();
        bfr.add("cow");

        //Open the same Bloom filter from anywhere else
        BloomFilter<String> bfr2 = new FilterBuilder(1000, 0.01)
                .name(filterName) //load the same filter
                .redisBacked(true)
                .buildBloomFilter();
        bfr2.add("bison");
View Full Code Here

        print(bfr.contains("bison")); //true
    }

    public static void redisCBF() {
        //Open a Redis-backed Counting Bloom filter
        CountingBloomFilter<String> cbfr = new FilterBuilder(10000, 0.01)
                .name("myfilter")
                .overwriteIfExists(true) //instead of loading it, overwrite it if it's already there
                .redisBacked(true)
                .buildCountingBloomFilter();
        cbfr.add("cow");

        //Open a second Redis-backed Bloom filter with a new connection
        CountingBloomFilter<String> bfr2 = new FilterBuilder(10000, 0.01)
                .name("myfilter") //this time it will be load it
                .redisBacked(true)
                .buildCountingBloomFilter();
        bfr2.add("bison");
        bfr2.remove("cow");
View Full Code Here

TOP

Related Classes of orestes.bloomfilter.FilterBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.