-
Notifications
You must be signed in to change notification settings - Fork 11
/
redis_bitset.go
84 lines (72 loc) · 1.77 KB
/
redis_bitset.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package bloom
import (
"fmt"
"github.com/garyburd/redigo/redis"
"strings"
)
const redisMaxLength = 8 * 512 * 1024 * 1024
type Connection interface {
Do(cmd string, args ...interface{}) (reply interface{}, err error)
Send(cmd string, args ...interface{}) error
Flush() error
}
type RedisBitSet struct {
keyPrefix string
conn Connection
m uint
}
func NewRedisBitSet(keyPrefix string, m uint, conn Connection) *RedisBitSet {
return &RedisBitSet{keyPrefix, conn, m}
}
func (r *RedisBitSet) Set(offsets []uint) error {
for _, offset := range offsets {
key, thisOffset := r.getKeyOffset(offset)
err := r.conn.Send("SETBIT", key, thisOffset, 1)
if err != nil {
return err
}
}
return r.conn.Flush()
}
func (r *RedisBitSet) Test(offsets []uint) (bool, error) {
for _, offset := range offsets {
key, thisOffset := r.getKeyOffset(offset)
bitValue, err := redis.Int(r.conn.Do("GETBIT", key, thisOffset))
if err != nil {
return false, err
}
if bitValue == 0 {
return false, nil
}
}
return true, nil
}
func (r *RedisBitSet) Expire(seconds uint) error {
n := uint(0)
for n <= uint(r.m/redisMaxLength) {
key := fmt.Sprintf("%s:%d", r.keyPrefix, n)
n = n + 1
err := r.conn.Send("EXPIRE", key, seconds)
if err != nil {
return err
}
}
return r.conn.Flush()
}
func (r *RedisBitSet) Delete() error {
n := uint(0)
keys := make([]string, 0)
for n <= uint(r.m/redisMaxLength) {
key := fmt.Sprintf("%s:%d", r.keyPrefix, n)
keys = append(keys, key)
n = n + 1
}
_, err := r.conn.Do("DEL", strings.Join(keys, " "))
return err
}
func (r *RedisBitSet) getKeyOffset(offset uint) (string, uint) {
n := uint(offset / redisMaxLength)
thisOffset := offset - n*redisMaxLength
key := fmt.Sprintf("%s:%d", r.keyPrefix, n)
return key, thisOffset
}