https://github.com/ensteinjun/go-bloomfilter
bloomfilter for golang
https://github.com/ensteinjun/go-bloomfilter
bloomfilter go golang
Last synced: 6 months ago
JSON representation
bloomfilter for golang
- Host: GitHub
- URL: https://github.com/ensteinjun/go-bloomfilter
- Owner: Ensteinjun
- License: mit
- Created: 2023-02-10T11:01:59.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-17T11:26:39.000Z (over 3 years ago)
- Last Synced: 2024-06-21T15:36:48.945Z (about 2 years ago)
- Topics: bloomfilter, go, golang
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-bloomfilter
[](https://pkg.go.dev/github.com/Ensteinjun/go-bloomfilter)
bloomfilter for golang
## Containers
```golang
// Implement this interface and make a custom container
bloomfilter.BFContainer
// Support
bloomfilter.NewMemoryBloomFilter
bloomfilter.NewRedisContainer
bloomfilter.NewRedisClusterContainer
```
## Example
```golang
// option 1: memorty bloom filter
bf := bloomfilter.NewMemoryBloomFilter(100000, 0.001)
// option 2: redis bloom filter
rdc := container.NewRedisContainer(&redis.Options{}, "bf_demo", 2)
bf := bloomfilter.NewBloomFilter(100000, 0.001, rdc)
// option 3: redis bloom filter
rdc1 := container.NewRedisClusterContainer(&redis.ClusterOptions{}, "bf_demo", 2)
bf := bloomfilter.NewBloomFilter(100000, 0.001, rdc1)
bf.Add([]byte("hello"))
bf.Add([]byte("world"))
fmt.Printf("Capacity: %d\n", bf.Capacity())
fmt.Printf("Size: %d\n", bf.Size())
fmt.Printf("Error: %f\n", bf.Error())
fmt.Printf("Exists[hello]: %v\n", bf.Contains([]byte("hello")))
fmt.Printf("Exists[world]: %v\n", bf.Contains([]byte("world")))
fmt.Printf("Exists[golang]: %v\n", bf.Contains([]byte("golang")))
// save bloomfilter to file
writer, err := os.OpenFile("./test.bf", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("write failed failed: %v", err)
}
bf.Save(writer)
/*
Load bloomfilter from File
It is necessary to ensure that the two values โโof hashFunction and container.GetMaxBitSize() cannot be changed, otherwise the bloomfilter of load cannot work correctly
*/
fmt.Println("Load Bloom Filter")
bf2, err := bloomfilter.LoadBloomFilter("./test.bf", container.NewMemoryContainer(), nil)
if err != nil {
log.Fatalf("load bloom filter failed: %v", err)
}
fmt.Printf("Exists[hello]: %v\n", bf2.Contains([]byte("hello")))
fmt.Printf("Exists[world]: %v\n", bf2.Contains([]byte("world")))
fmt.Printf("Exists[golang]: %v\n", bf2.Contains([]byte("golang")))
fmt.Printf("Capacity: %d\n", bf2.Capacity())
fmt.Printf("Size: %d\n", bf2.Size())
fmt.Printf("Error: %f\n", bf2.Error())
```
## Run Test
```shell
go test
```
## License
[MIT License Copyright (c) 2023 coderjun](http://opensource.org/licenses/MIT)