-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom.go
50 lines (39 loc) · 1.86 KB
/
bloom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package redisson
import (
"context"
"github.com/redis/rueidis/rueidisprob"
)
// BloomFilter based on Redis Bitmaps.
// BloomFilter uses 128-bit murmur3 hash function.
type BloomFilter interface {
// Add adds an item to the Bloom filter.
Add(ctx context.Context, key string) error
// AddMulti adds one or more items to the Bloom filter.
// NOTE: If keys are too many, it can block the Redis server for a long time.
AddMulti(ctx context.Context, keys []string) error
// Exists checks if an item is in the Bloom filter.
Exists(ctx context.Context, key string) (bool, error)
// ExistsMulti checks if one or more items are in the Bloom filter.
// Returns a slice of bool values where each bool indicates whether the corresponding key was found.
ExistsMulti(ctx context.Context, keys []string) ([]bool, error)
// Reset resets the Bloom filter.
Reset(ctx context.Context) error
// Delete deletes the Bloom filter.
Delete(ctx context.Context) error
// Count returns count of items in Bloom filter.
Count(ctx context.Context) (uint64, error)
}
const bloomFilterROVersion = "7.0.0"
// newBloomFilter 新建一个布隆过滤器
func newBloomFilter(c *client, name string, expectedNumberOfItems uint, falsePositiveRate float64, opts ...BloomOption) (BloomFilter, error) {
// 校验版本
if c.version.LessThan(mustNewSemVersion(bloomFilterROVersion)) {
opts = append(opts, WithBloomOptionEnableReadOperation(false))
}
cc := newBloomOptions(opts...)
return rueidisprob.NewBloomFilter(c.cmd, name, expectedNumberOfItems, falsePositiveRate, rueidisprob.WithEnableReadOperation(cc.GetEnableReadOperation()))
}
// NewBloomFilter 新建一个布隆过滤器
func (c *client) NewBloomFilter(name string, expectedNumberOfItems uint, falsePositiveRate float64, opts ...BloomOption) (BloomFilter, error) {
return newBloomFilter(c, name, expectedNumberOfItems, falsePositiveRate, opts...)
}