bloom.go 3.2 KB

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