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);
}
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);
}
}
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);
}
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);
}
}
Aggregations