Search in sources :

Example 1 with BitsArray

use of org.apache.rocketmq.filter.util.BitsArray in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BitsArrayTest method testOr.

@Test
public void testOr() {
    BitsArray b1 = BitsArray.create(new byte[] { (byte) 0xff, 0x00 });
    BitsArray b2 = BitsArray.create(new byte[] { 0x00, (byte) 0xff });
    b1.or(b2);
    for (int i = 0; i < b1.bitLength(); i++) {
        assertThat(b1.getBit(i)).isTrue();
    }
}
Also used : BitsArray(org.apache.rocketmq.filter.util.BitsArray) Test(org.junit.Test)

Example 2 with BitsArray

use of org.apache.rocketmq.filter.util.BitsArray in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BitsArrayTest method testConstructor.

@Test
public void testConstructor() {
    BitsArray bitsArray = BitsArray.create(8);
    assertThat(bitsArray.byteLength() == 1 && bitsArray.bitLength() == 8).isTrue();
    bitsArray = BitsArray.create(9);
    assertThat(bitsArray.byteLength() == 2 && bitsArray.bitLength() == 9).isTrue();
    bitsArray = BitsArray.create(7);
    assertThat(bitsArray.byteLength() == 1 && bitsArray.bitLength() == 7).isTrue();
}
Also used : BitsArray(org.apache.rocketmq.filter.util.BitsArray) Test(org.junit.Test)

Example 3 with BitsArray

use of org.apache.rocketmq.filter.util.BitsArray 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();
    }
}
Also used : BitsArray(org.apache.rocketmq.filter.util.BitsArray) BloomFilter(org.apache.rocketmq.filter.util.BloomFilter) Test(org.junit.Test)

Example 4 with BitsArray

use of org.apache.rocketmq.filter.util.BitsArray 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();
}
Also used : BitsArray(org.apache.rocketmq.filter.util.BitsArray) BloomFilter(org.apache.rocketmq.filter.util.BloomFilter) Test(org.junit.Test)

Example 5 with BitsArray

use of org.apache.rocketmq.filter.util.BitsArray 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);
}
Also used : Random(java.util.Random) BitsArray(org.apache.rocketmq.filter.util.BitsArray) BloomFilter(org.apache.rocketmq.filter.util.BloomFilter) Test(org.junit.Test)

Aggregations

BitsArray (org.apache.rocketmq.filter.util.BitsArray)26 Test (org.junit.Test)22 BloomFilter (org.apache.rocketmq.filter.util.BloomFilter)8 HashMap (java.util.HashMap)4 BrokerConfig (org.apache.rocketmq.common.BrokerConfig)4 DispatchRequest (org.apache.rocketmq.store.DispatchRequest)4 Random (java.util.Random)2