bloom.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package bloom
  2. import (
  3. "errors"
  4. "strconv"
  5. "github.com/tal-tech/go-zero/core/hash"
  6. "github.com/tal-tech/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. var ErrTooLargeOffset = errors.New("too large offset")
  27. type (
  28. BitSetProvider interface {
  29. check([]uint) (bool, error)
  30. set([]uint) error
  31. }
  32. BloomFilter struct {
  33. bits uint
  34. bitSet BitSetProvider
  35. }
  36. )
  37. // New create a BloomFilter, store is the backed redis, key is the key for the bloom filter,
  38. // bits is how many bits will be used, maps is how many hashes for each addition.
  39. // best practices:
  40. // elements - means how many actual elements
  41. // when maps = 14, formula: 0.7*(bits/maps), bits = 20*elements, the error rate is 0.000067 < 1e-4
  42. // for detailed error rate table, see http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html
  43. func New(store *redis.Redis, key string, bits uint) *BloomFilter {
  44. return &BloomFilter{
  45. bits: bits,
  46. bitSet: newRedisBitSet(store, key, bits),
  47. }
  48. }
  49. func (f *BloomFilter) Add(data []byte) error {
  50. locations := f.getLocations(data)
  51. err := f.bitSet.set(locations)
  52. if err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. func (f *BloomFilter) 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. if !isSet {
  64. return false, nil
  65. }
  66. return true, nil
  67. }
  68. func (f *BloomFilter) getLocations(data []byte) []uint {
  69. locations := make([]uint, maps)
  70. for i := uint(0); i < maps; i++ {
  71. hashValue := hash.Hash(append(data, byte(i)))
  72. locations[i] = uint(hashValue % uint64(f.bits))
  73. }
  74. return locations
  75. }
  76. type redisBitSet struct {
  77. store *redis.Redis
  78. key string
  79. bits uint
  80. }
  81. func newRedisBitSet(store *redis.Redis, key string, bits uint) *redisBitSet {
  82. return &redisBitSet{
  83. store: store,
  84. key: key,
  85. bits: bits,
  86. }
  87. }
  88. func (r *redisBitSet) buildOffsetArgs(offsets []uint) ([]string, error) {
  89. var args []string
  90. for _, offset := range offsets {
  91. if offset >= r.bits {
  92. return nil, ErrTooLargeOffset
  93. }
  94. args = append(args, strconv.FormatUint(uint64(offset), 10))
  95. }
  96. return args, nil
  97. }
  98. func (r *redisBitSet) check(offsets []uint) (bool, error) {
  99. args, err := r.buildOffsetArgs(offsets)
  100. if err != nil {
  101. return false, err
  102. }
  103. resp, err := r.store.Eval(testScript, []string{r.key}, args)
  104. if err == redis.Nil {
  105. return false, nil
  106. } else if err != nil {
  107. return false, err
  108. }
  109. if exists, ok := resp.(int64); !ok {
  110. return false, nil
  111. } else {
  112. return exists == 1, nil
  113. }
  114. }
  115. func (r *redisBitSet) del() error {
  116. _, err := r.store.Del(r.key)
  117. return err
  118. }
  119. func (r *redisBitSet) expire(seconds int) error {
  120. return r.store.Expire(r.key, seconds)
  121. }
  122. func (r *redisBitSet) set(offsets []uint) error {
  123. args, err := r.buildOffsetArgs(offsets)
  124. if err != nil {
  125. return err
  126. }
  127. _, err = r.store.Eval(setScript, []string{r.key}, args)
  128. if err == redis.Nil {
  129. return nil
  130. } else {
  131. return err
  132. }
  133. }