use of org.apache.rocketmq.filter.util.BloomFilter in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testEquals.
@Test
public void testEquals() {
BloomFilter a = BloomFilter.createByFn(10, 20);
BloomFilter b = BloomFilter.createByFn(10, 20);
BloomFilter c = BloomFilter.createByFn(12, 20);
BloomFilter d = BloomFilter.createByFn(10, 30);
assertThat(a).isEqualTo(b);
assertThat(a).isNotEqualTo(c);
assertThat(a).isNotEqualTo(d);
assertThat(d).isNotEqualTo(c);
assertThat(a.hashCode()).isEqualTo(b.hashCode());
assertThat(a.hashCode()).isNotEqualTo(c.hashCode());
assertThat(a.hashCode()).isNotEqualTo(d.hashCode());
assertThat(c.hashCode()).isNotEqualTo(d.hashCode());
}
use of org.apache.rocketmq.filter.util.BloomFilter in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testCalcBitPositions.
@Test
public void testCalcBitPositions() {
String cid = "CID_abc_efg";
BloomFilter bloomFilter = BloomFilter.createByFn(10, 20);
int[] bitPos = bloomFilter.calcBitPositions(cid);
assertThat(bitPos).isNotNull();
assertThat(bitPos.length).isEqualTo(bloomFilter.getK());
int[] bitPos2 = bloomFilter.calcBitPositions(cid);
assertThat(bitPos2).isNotNull();
assertThat(bitPos2.length).isEqualTo(bloomFilter.getK());
assertThat(bitPos).isEqualTo(bitPos2);
}
use of org.apache.rocketmq.filter.util.BloomFilter in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testHashTo.
@Test
public void testHashTo() {
String cid = "CID_abc_efg";
BloomFilter bloomFilter = BloomFilter.createByFn(10, 20);
BitsArray bits = BitsArray.create(bloomFilter.getM());
int[] bitPos = bloomFilter.calcBitPositions(cid);
bloomFilter.hashTo(cid, bits);
for (int bit : bitPos) {
assertThat(bits.getBit(bit)).isTrue();
}
}
use of org.apache.rocketmq.filter.util.BloomFilter in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testIsHit.
@Test
public void testIsHit() {
String cid = "CID_abc_efg";
String cid2 = "CID_abc_123";
BloomFilter bloomFilter = BloomFilter.createByFn(10, 20);
BitsArray bits = BitsArray.create(bloomFilter.getM());
bloomFilter.hashTo(cid, bits);
assertThat(bloomFilter.isHit(cid, bits)).isTrue();
assertThat(!bloomFilter.isHit(cid2, bits)).isTrue();
bloomFilter.hashTo(cid2, bits);
assertThat(bloomFilter.isHit(cid, bits)).isTrue();
assertThat(bloomFilter.isHit(cid2, bits)).isTrue();
}
use of org.apache.rocketmq.filter.util.BloomFilter in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class BloomFilterTest method testCheckFalseHit.
@Test
public void testCheckFalseHit() {
BloomFilter bloomFilter = BloomFilter.createByFn(1, 300);
BitsArray bits = BitsArray.create(bloomFilter.getM());
int falseHit = 0;
for (int i = 0; i < bloomFilter.getN(); i++) {
String str = randomString((new Random(System.nanoTime())).nextInt(127) + 10);
int[] bitPos = bloomFilter.calcBitPositions(str);
if (bloomFilter.checkFalseHit(bitPos, bits)) {
falseHit++;
}
bloomFilter.hashTo(bitPos, bits);
}
assertThat(falseHit).isLessThanOrEqualTo(bloomFilter.getF() * bloomFilter.getN() / 100);
}
Aggregations