Search in sources :

Example 1 with FilterKey

use of org.apache.cassandra.utils.IFilter.FilterKey in project cassandra by apache.

the class BitSetTest method compareBitSets.

/**
     * Test bitsets in a "real-world" environment, i.e., bloom filters
     */
@Test
public void compareBitSets() {
    BloomFilter bf2 = (BloomFilter) FilterFactory.getFilter(FilterTestHelper.ELEMENTS / 2, FilterTestHelper.MAX_FAILURE_RATE, false);
    BloomFilter bf3 = (BloomFilter) FilterFactory.getFilter(FilterTestHelper.ELEMENTS / 2, FilterTestHelper.MAX_FAILURE_RATE, true);
    RandomStringGenerator gen1 = new KeyGenerator.RandomStringGenerator(new Random().nextInt(), FilterTestHelper.ELEMENTS);
    // make sure both bitsets are empty.
    compare(bf2.bitset, bf3.bitset);
    while (gen1.hasNext()) {
        FilterKey key = FilterTestHelper.wrap(gen1.next());
        bf2.add(key);
        bf3.add(key);
    }
    compare(bf2.bitset, bf3.bitset);
}
Also used : RandomStringGenerator(org.apache.cassandra.utils.KeyGenerator.RandomStringGenerator) Random(java.util.Random) FilterKey(org.apache.cassandra.utils.IFilter.FilterKey) Test(org.junit.Test)

Example 2 with FilterKey

use of org.apache.cassandra.utils.IFilter.FilterKey in project cassandra by apache.

the class BloomFilterTest method testMurmur3FilterHash.

@Test
public void testMurmur3FilterHash() {
    IPartitioner partitioner = new Murmur3Partitioner();
    Iterator<ByteBuffer> gen = new KeyGenerator.RandomStringGenerator(new Random().nextInt(), FilterTestHelper.ELEMENTS);
    long[] expected = new long[2];
    long[] actual = new long[2];
    while (gen.hasNext()) {
        expected[0] = 1;
        expected[1] = 2;
        actual[0] = 3;
        actual[1] = 4;
        ByteBuffer key = gen.next();
        FilterKey expectedKey = FilterTestHelper.wrap(key);
        FilterKey actualKey = partitioner.decorateKey(key);
        actualKey.filterHash(actual);
        expectedKey.filterHash(expected);
        Assert.assertArrayEquals(expected, actual);
    }
}
Also used : RandomStringGenerator(org.apache.cassandra.utils.KeyGenerator.RandomStringGenerator) Random(java.util.Random) Murmur3Partitioner(org.apache.cassandra.dht.Murmur3Partitioner) ByteBuffer(java.nio.ByteBuffer) FilterKey(org.apache.cassandra.utils.IFilter.FilterKey) IPartitioner(org.apache.cassandra.dht.IPartitioner)

Example 3 with FilterKey

use of org.apache.cassandra.utils.IFilter.FilterKey in project cassandra by apache.

the class BloomFilterTest method testManyRandom.

private static void testManyRandom(Iterator<ByteBuffer> keys) {
    int MAX_HASH_COUNT = 128;
    Set<Long> hashes = new HashSet<>();
    long collisions = 0;
    while (keys.hasNext()) {
        hashes.clear();
        FilterKey buf = FilterTestHelper.wrap(keys.next());
        BloomFilter bf = (BloomFilter) FilterFactory.getFilter(10, 1);
        for (long hashIndex : bf.getHashBuckets(buf, MAX_HASH_COUNT, 1024 * 1024)) {
            hashes.add(hashIndex);
        }
        collisions += (MAX_HASH_COUNT - hashes.size());
        bf.close();
    }
    Assert.assertTrue("collisions=" + collisions, collisions <= 100);
}
Also used : FilterKey(org.apache.cassandra.utils.IFilter.FilterKey) HashSet(java.util.HashSet)

Example 4 with FilterKey

use of org.apache.cassandra.utils.IFilter.FilterKey in project cassandra by apache.

the class BloomFilterTest method compareCachedKey.

@Test
public void compareCachedKey() {
    try (BloomFilter bf1 = (BloomFilter) FilterFactory.getFilter(FilterTestHelper.ELEMENTS / 2, FilterTestHelper.MAX_FAILURE_RATE);
        BloomFilter bf2 = (BloomFilter) FilterFactory.getFilter(FilterTestHelper.ELEMENTS / 2, FilterTestHelper.MAX_FAILURE_RATE);
        BloomFilter bf3 = (BloomFilter) FilterFactory.getFilter(FilterTestHelper.ELEMENTS / 2, FilterTestHelper.MAX_FAILURE_RATE)) {
        RandomStringGenerator gen1 = new KeyGenerator.RandomStringGenerator(new Random().nextInt(), FilterTestHelper.ELEMENTS);
        // make sure all bitsets are empty.
        compare(bf1.bitset, bf2.bitset);
        compare(bf1.bitset, bf3.bitset);
        while (gen1.hasNext()) {
            ByteBuffer key = gen1.next();
            FilterKey cached = FilterTestHelper.wrapCached(key);
            bf1.add(FilterTestHelper.wrap(key));
            bf2.add(cached);
            bf3.add(cached);
        }
        compare(bf1.bitset, bf2.bitset);
        compare(bf1.bitset, bf3.bitset);
    }
}
Also used : RandomStringGenerator(org.apache.cassandra.utils.KeyGenerator.RandomStringGenerator) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) FilterKey(org.apache.cassandra.utils.IFilter.FilterKey)

Aggregations

FilterKey (org.apache.cassandra.utils.IFilter.FilterKey)4 Random (java.util.Random)3 RandomStringGenerator (org.apache.cassandra.utils.KeyGenerator.RandomStringGenerator)3 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)1 IPartitioner (org.apache.cassandra.dht.IPartitioner)1 Murmur3Partitioner (org.apache.cassandra.dht.Murmur3Partitioner)1 Test (org.junit.Test)1