bloom.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package bloom
  2. import (
  3. "errors"
  4. "strconv"
  5. "github.com/zeromicro/go-zero/core/hash"
  6. "github.com/zeromicro/go-zero/core/stores/redis"
  7. )
  8. const (
  9. // for detailed error rate table, see http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html
  10. // maps as k in the error rate table
  11. maps = 14
  12. setScript = `
  13. for _, offset in ipairs(ARGV) do
  14. redis.call("setbit", KEYS[1], offset, 1)
  15. end
  16. `
  17. testScript = `
  18. for _, offset in ipairs(ARGV) do
  19. if tonumber(redis.call("getbit", KEYS[1], offset)) == 0 then
  20. return false
  21. end
  22. end
  23. return true
  24. `
  25. )
  26. // ErrTooLargeOffset indicates the offset is too large in bitset.
  27. var ErrTooLargeOffset = errors.New("too large offset")
  28. type (
  29. // A Filter is a bloom filter.
  30. Filter struct {
  31. bits uint
  32. bitSet bitSetProvider
  33. }
  34. bitSetProvider interface {
  35. check([]uint) (bool, error)
  36. set([]uint) error
  37. }
  38. )
  39. // New create a Filter, store is the backed redis, key is the key for the bloom filter,
  40. // bits is how many bits will be used, maps is how many hashes for each addition.
  41. // best practices:
  42. // elements - means how many actual elements
  43. // when maps = 14, formula: 0.7*(bits/maps), bits = 20*elements, the error rate is 0.000067 < 1e-4
  44. // for detailed error rate table, see http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html
  45. func New(store *redis.Redis, key string, bits uint) *Filter {
  46. return &Filter{
  47. bits: bits,
  48. bitSet: newRedisBitSet(store, key, bits),
  49. }
  50. }
  51. // Add adds data into f.
  52. func (f *Filter) Add(data []byte) error {
  53. locations := f.getLocations(data)
  54. return f.bitSet.set(locations)
  55. }
  56. // Exists checks if data is in f.
  57. func (f *Filter) Exists(data []byte) (bool, error) {
  58. locations := f.getLocations(data)
  59. isSet, err := f.bitSet.check(locations)
  60. if err != nil {
  61. return false, err
  62. }
  63. return isSet, nil
  64. }
  65. func (f *Filter) getLocations(data []byte) []uint {
  66. locations := make([]uint, maps)
  67. for i := uint(0); i < maps; i++ {
  68. hashValue := hash.Hash(append(data, byte(i)))
  69. locations[i] = uint(hashValue % uint64(f.bits))
  70. }
  71. return locations
  72. }
  73. type redisBitSet struct {
  74. store *redis.Redis
  75. key string
  76. bits uint
  77. }
  78. func newRedisBitSet(store *redis.Redis, key string, bits uint) *redisBitSet {
  79. return &redisBitSet{
  80. store: store,
  81. key: key,
  82. bits: bits,
  83. }
  84. }
  85. func (r *redisBitSet) buildOffsetArgs(offsets []uint) ([]string, error) {
  86. var args []string
  87. for _, offset := range offsets {
  88. if offset >= r.bits {
  89. return nil, ErrTooLargeOffset
  90. }
  91. args = append(args, strconv.FormatUint(uint64(offset), 10))
  92. }
  93. return args, nil
  94. }
  95. func (r *redisBitSet) check(offsets []uint) (bool, error) {
  96. args, err := r.buildOffsetArgs(offsets)
  97. if err != nil {
  98. return false, err
  99. }
  100. resp, err := r.store.Eval(testScript, []string{r.key}, args)
  101. if err == redis.Nil {
  102. return false, nil
  103. } else if err != nil {
  104. return false, err
  105. }
  106. exists, ok := resp.(int64)
  107. if !ok {
  108. return false, nil
  109. }
  110. return exists == 1, nil
  111. }
  112. func (r *redisBitSet) del() error {
  113. _, err := r.store.Del(r.key)
  114. return err
  115. }
  116. func (r *redisBitSet) expire(seconds int) error {
  117. return r.store.Expire(r.key, seconds)
  118. }
  119. func (r *redisBitSet) set(offsets []uint) error {
  120. args, err := r.buildOffsetArgs(offsets)
  121. if err != nil {
  122. return err
  123. }
  124. _, err = r.store.Eval(setScript, []string{r.key}, args)
  125. if err == redis.Nil {
  126. return nil
  127. }
  128. return err
  129. }