-
Notifications
You must be signed in to change notification settings - Fork 29
/
tx_set.go
196 lines (170 loc) · 4.55 KB
/
tx_set.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package flashdb
import (
"time"
)
// SAdd adds one or more members to the set stored at key. If a member exists at
// key, it is skipped.
func (tx *Tx) SAdd(key string, members ...string) (err error) {
for _, m := range members {
exist := tx.db.setStore.SIsMember(key, m)
if !exist {
e := newRecord([]byte(key), []byte(m), SetRecord, SetSAdd)
tx.addRecord(e)
}
}
return
}
// SIsMember checks the member is a member of set stored at key. If the key has
// expired, the key is evicted.
func (tx *Tx) SIsMember(key string, member string) bool {
if tx.db.hasExpired(key, Set) {
tx.db.evict(key, Set)
return false
}
return tx.db.setStore.SIsMember(key, member)
}
// SRandMember returns random elements stored at key. If the key has expired,
// the key is evicted.
func (tx *Tx) SRandMember(key string, count int) (values []string) {
if tx.db.hasExpired(key, Set) {
tx.db.evict(key, Set)
return nil
}
vals := tx.db.setStore.SRandMember(key, count)
for _, v := range vals {
values = append(values, toString(v))
}
return
}
// SRem removes one or more members from the set stored at key. It returns
// number of removed members from the set. If the key has expired, the key
// is evicted.
func (tx *Tx) SRem(key string, members ...string) (res int, err error) {
if tx.db.hasExpired(key, Set) {
tx.db.evict(key, Set)
return
}
for _, m := range members {
e := newRecord([]byte(key), []byte(m), SetRecord, SetSRem)
tx.addRecord(e)
res++
}
return
}
// SMove moves a member from src to dst. If both keys have expired, the key is
// evicted.
func (tx *Tx) SMove(src, dst string, member string) error {
if tx.db.hasExpired(src, Set) {
tx.db.evict(src, Hash)
return ErrExpiredKey
}
if tx.db.hasExpired(dst, Set) {
tx.db.evict(dst, Hash)
return ErrExpiredKey
}
ok := tx.db.setStore.SMove(src, dst, member)
if ok {
e := newRecordWithValue([]byte(src), []byte(member), []byte(dst), SetRecord, SetSMove)
tx.addRecord(e)
}
return nil
}
// SCard returns the cardinality of the set stored at key. If the key has expired,
// the key is evicted.
func (tx *Tx) SCard(key string) int {
if tx.db.hasExpired(key, Set) {
tx.db.evict(key, Set)
return 0
}
return tx.db.setStore.SCard(key)
}
// SMembers returns the members stored at key. If the key has expired, the key
// is evicted.
func (tx *Tx) SMembers(key string) (values []string) {
if tx.db.hasExpired(key, Set) {
tx.db.evict(key, Set)
return
}
vals := tx.db.setStore.SMembers(key)
for _, v := range vals {
values = append(values, toString(v))
}
return
}
// SUnion returns the members of the set resulting from union of all the given
// keys. The members' type is string. If any key has expired, the key is evicted.
func (tx *Tx) SUnion(keys ...string) (values []string) {
var activeKeys []string
for _, k := range keys {
if tx.db.hasExpired(k, Set) {
tx.db.evict(k, Hash)
continue
}
activeKeys = append(activeKeys, k)
}
vals := tx.db.setStore.SUnion(activeKeys...)
for _, v := range vals {
values = append(values, toString(v))
}
return
}
// SDiff returns the members if the set resulting from difference between the
// first and all the remaining keys. If any key has expired, the key is evicted.
func (tx *Tx) SDiff(keys ...string) (values []string) {
var activeKeys []string
for _, k := range keys {
if tx.db.hasExpired(k, Set) {
tx.db.evict(k, Hash)
continue
}
activeKeys = append(activeKeys, k)
}
vals := tx.db.setStore.SDiff(activeKeys...)
for _, v := range vals {
values = append(values, toString(v))
}
return
}
// SKeyExists returns if the key exists.
func (tx *Tx) SKeyExists(key string) (ok bool) {
if tx.db.hasExpired(key, Set) {
tx.db.evict(key, Set)
return
}
ok = tx.db.setStore.SKeyExists(key)
return
}
// SClear clear the specified key in set.
func (tx *Tx) SClear(key string) (err error) {
if !tx.SKeyExists(key) {
return ErrInvalidKey
}
e := newRecord([]byte(key), nil, SetRecord, SetSClear)
tx.addRecord(e)
return
}
// SExpire set expired time for the key in set.
func (tx *Tx) SExpire(key string, duration int64) (err error) {
if duration <= 0 {
return ErrInvalidTTL
}
if !tx.SKeyExists(key) {
return ErrInvalidKey
}
ttl := time.Now().Unix() + duration
e := newRecordWithExpire([]byte(key), nil, ttl, SetRecord, SetSExpire)
tx.addRecord(e)
return
}
// STTL return time to live for the key in set.
func (tx *Tx) STTL(key string) (ttl int64) {
if tx.db.hasExpired(key, Set) {
tx.db.evict(key, Set)
return
}
deadline := tx.db.getTTL(Set, key)
if deadline == nil {
return
}
return deadline.(int64) - time.Now().Unix()
}