zeromicro/zero-examples

bloom bug?

hr3685930 opened this issue · 3 comments

package main

import (
    "fmt"
    "github.com/spf13/cast"

    "github.com/tal-tech/go-zero/core/bloom"
    "github.com/tal-tech/go-zero/core/stores/redis"
)

func main() {
    store := redis.NewRedis("localhost:6379", "node")
    filter := bloom.New(store, "testbloom", 64)

    for i := 0; i < 10; i++ {
        filter.Add([]byte("kevin" + cast.ToString(i)))
    }
    fmt.Println(filter.Exists([]byte("kevin")))
    fmt.Println(filter.Exists([]byte("wan")))
    fmt.Println(filter.Exists([]byte("nothing")))
}

result:

false <nil>
false <nil>
true <nil>

This does not seem to meet expectations

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.

You're using only 64 bits.

After adding all the items in the for loop, the bloom bits is:

1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 

For the string "nothing", the bits to check if with value 1 is below:

0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

So, the bloom filter works correctly. But you need to know that bloom filter is not accurate.

If it tells not contain, that's definitely correct, but if it tells containing the item, it's probably correct.