Skip to content
This repository has been archived by the owner on Jul 7, 2020. It is now read-only.

I think should check value before set it to bits. #129

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static int getSizeForCount(int count) {
}

public void set(int position, int value) {
value = value & 0x1F; //make sure value would not longger than REGISTER_SIZE
int bucketPos = position / LOG2_BITS_PER_WORD;
int shift = REGISTER_SIZE * (position - (bucketPos * LOG2_BITS_PER_WORD));
this.M[bucketPos] = (this.M[bucketPos] & ~(0x1f << shift)) | (value << shift);
Expand All @@ -69,6 +70,7 @@ public int get(int position) {
}

public boolean updateIfGreater(int position, int value) {
value = value & 0x1F; //make sure value would not longger than REGISTER_SIZE
int bucket = position / LOG2_BITS_PER_WORD;
int shift = REGISTER_SIZE * (position - (bucket * LOG2_BITS_PER_WORD));
int mask = 0x1f << shift;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ public static BloomFilter deserialize(byte[] bytes) {

return filter;
}
/**
* this method return the cardinality estimation of current bloom filter
* base on article "cardinality estimation for dynamic bloomfilter"
* */
public long getCardinality(){
int nhash = this.getHashCount();
int mbits = this.buckets();

int bitsUsed = this.buckets() - this.emptyBuckets();
return Math.round(1.0/nhash * Math.log(1.0-1.0*bitsUsed/mbits)/Math.log(1-1.0/mbits));
}
}

class BloomFilterSerializer implements ICompactSerializer<BloomFilter> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ public class RegisterSetTest {
public void testGetAndSet() throws Exception {
RegisterSet rs = new RegisterSet((int) Math.pow(2, 4));
rs.set(0, 11);
rs.set(1, 11);
rs.set(2, 11);
assertEquals(11, rs.get(0));
assertEquals(11, rs.get(1));
assertEquals(11, rs.get(2));
rs.set(0, 0xFF);
assertEquals(11, rs.get(1));
assertEquals(11, rs.get(2));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;
import java.util.UUID;

import com.clearspring.analytics.TestUtils;
import com.clearspring.analytics.stream.membership.KeyGenerator.RandomStringGenerator;

import org.junit.Before;
Expand Down Expand Up @@ -56,7 +57,15 @@ public BloomFilterTest() {
public void clear() {
bf.clear();
}

@Test
public void testCardinality(){
Random r = new Random();
for(int i =0;i<7799;i++){
String str = Integer.toHexString(r.nextInt());
bf.add(str);
}
System.out.println(bf.getCardinality());
}
@Test
public void testOne() {
bf.add("a");
Expand Down