forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_set.go
268 lines (208 loc) · 7.19 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package nutsdb
import (
"errors"
"time"
"github.com/xujiajun/nutsdb/ds/set"
)
func (tx *Tx) sPut(bucket string, key []byte, dataFlag uint16, items ...[]byte) error {
for _, item := range items {
err := tx.put(bucket, key, item, Persistent, dataFlag, uint64(time.Now().Unix()), DataStructureSet)
if err != nil {
return err
}
}
return nil
}
// SAdd adds the specified members to the set stored int the bucket at given bucket,key and items.
func (tx *Tx) SAdd(bucket string, key []byte, items ...[]byte) error {
return tx.sPut(bucket, key, DataSetFlag, items...)
}
// SRem removes the specified members from the set stored int the bucket at given bucket,key and items.
func (tx *Tx) SRem(bucket string, key []byte, items ...[]byte) error {
return tx.sPut(bucket, key, DataDeleteFlag, items...)
}
// SAreMembers returns if the specified members are the member of the set int the bucket at given bucket,key and items.
func (tx *Tx) SAreMembers(bucket string, key []byte, items ...[]byte) (bool, error) {
if err := tx.checkTxIsClosed(); err != nil {
return false, err
}
if sets, ok := tx.db.SetIdx[bucket]; ok {
return sets.SAreMembers(string(key), items...)
}
return false, ErrBucketAndKey(bucket, key)
}
// SIsMember returns if member is a member of the set stored int the bucket at given bucket,key and item.
func (tx *Tx) SIsMember(bucket string, key, item []byte) (bool, error) {
if err := tx.checkTxIsClosed(); err != nil {
return false, err
}
if set, ok := tx.db.SetIdx[bucket]; ok {
if !set.SIsMember(string(key), item) {
return false, ErrBucketAndKey(bucket, key)
}
return true, nil
}
return false, ErrBucketAndKey(bucket, key)
}
// SMembers returns all the members of the set value stored int the bucket at given bucket and key.
func (tx *Tx) SMembers(bucket string, key []byte) (list [][]byte, err error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if set, ok := tx.db.SetIdx[bucket]; ok {
return set.SMembers(string(key))
}
return nil, ErrBucketAndKey(bucket, key)
}
// SHasKey returns if the set in the bucket at given bucket and key.
func (tx *Tx) SHasKey(bucket string, key []byte) (bool, error) {
if err := tx.checkTxIsClosed(); err != nil {
return false, err
}
if set, ok := tx.db.SetIdx[bucket]; ok {
return set.SHasKey(string(key)), nil
}
return false, ErrBucketAndKey(bucket, key)
}
// SPop removes and returns one or more random elements from the set value store in the bucket at given bucket and key.
func (tx *Tx) SPop(bucket string, key []byte) ([]byte, error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if _, ok := tx.db.SetIdx[bucket]; ok {
for item := range tx.db.SetIdx[bucket].M[string(key)] {
return []byte(item), tx.sPut(bucket, key, DataDeleteFlag, []byte(item))
}
}
return nil, ErrBucketAndKey(bucket, key)
}
// SCard returns the set cardinality (number of elements) of the set stored in the bucket at given bucket and key.
func (tx *Tx) SCard(bucket string, key []byte) (int, error) {
if err := tx.checkTxIsClosed(); err != nil {
return 0, err
}
if set, ok := tx.db.SetIdx[bucket]; ok {
return set.SCard(string(key)), nil
}
return 0, ErrBucketAndKey(bucket, key)
}
// SDiffByOneBucket returns the members of the set resulting from the difference
// between the first set and all the successive sets in one bucket.
func (tx *Tx) SDiffByOneBucket(bucket string, key1, key2 []byte) (list [][]byte, err error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if set, ok := tx.db.SetIdx[bucket]; ok {
return set.SDiff(string(key1), string(key2))
}
return nil, ErrBucketAndKey(bucket, key1)
}
// SDiffByTwoBuckets returns the members of the set resulting from the difference
// between the first set and all the successive sets in two buckets.
func (tx *Tx) SDiffByTwoBuckets(bucket1 string, key1 []byte, bucket2 string, key2 []byte) (list [][]byte, err error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
var (
set1, set2 *set.Set
ok bool
)
if set1, ok = tx.db.SetIdx[bucket1]; !ok {
return nil, ErrBucketAndKey(bucket1, key1)
}
if set2, ok = tx.db.SetIdx[bucket2]; !ok {
return nil, ErrBucketAndKey(bucket2, key2)
}
for item1 := range set1.M[string(key1)] {
if _, ok := set2.M[string(key2)][item1]; !ok {
list = append(list, []byte(item1))
}
}
return
}
// SMoveByOneBucket moves member from the set at source to the set at destination in one bucket.
func (tx *Tx) SMoveByOneBucket(bucket string, key1, key2, item []byte) (bool, error) {
if err := tx.checkTxIsClosed(); err != nil {
return false, err
}
if set, ok := tx.db.SetIdx[bucket]; ok {
return set.SMove(string(key1), string(key2), item)
}
return false, ErrBucket
}
// SMoveByTwoBuckets moves member from the set at source to the set at destination in two buckets.
func (tx *Tx) SMoveByTwoBuckets(bucket1 string, key1 []byte, bucket2 string, key2, item []byte) (bool, error) {
if err := tx.checkTxIsClosed(); err != nil {
return false, err
}
var (
set1, set2 *set.Set
ok bool
)
if set1, ok = tx.db.SetIdx[bucket1]; !ok {
return false, ErrBucketAndKey(bucket1, key1)
}
if set2, ok = tx.db.SetIdx[bucket2]; !ok {
return false, ErrBucketAndKey(bucket2, key1)
}
if !set1.SHasKey(string(key1)) {
return false, ErrNotFoundKeyInBucket(bucket1, key1)
}
if !set2.SHasKey(string(key2)) {
return false, ErrNotFoundKeyInBucket(bucket2, key2)
}
if _, ok := set2.M[string(key2)][string(item)]; !ok {
set2.SAdd(string(key2), item)
}
set1.SRem(string(key1), item)
return true, nil
}
// SUnionByOneBucket the members of the set resulting from the union of all the given sets in one bucket.
func (tx *Tx) SUnionByOneBucket(bucket string, key1, key2 []byte) (list [][]byte, err error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if set, ok := tx.db.SetIdx[bucket]; ok {
return set.SUnion(string(key1), string(key2))
}
return nil, ErrBucket
}
// SUnionByTwoBuckets the members of the set resulting from the union of all the given sets in two buckets.
func (tx *Tx) SUnionByTwoBuckets(bucket1 string, key1 []byte, bucket2 string, key2 []byte) (list [][]byte, err error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
var (
set1, set2 *set.Set
ok bool
)
if set1, ok = tx.db.SetIdx[bucket1]; !ok {
return nil, ErrBucketAndKey(bucket1, key1)
}
if set2, ok = tx.db.SetIdx[bucket2]; !ok {
return nil, ErrBucketAndKey(bucket2, key1)
}
if !set1.SHasKey(string(key1)) {
return nil, ErrNotFoundKeyInBucket(bucket1, key1)
}
if !set2.SHasKey(string(key2)) {
return nil, ErrNotFoundKeyInBucket(bucket2, key2)
}
for item1 := range set1.M[string(key1)] {
list = append(list, []byte(item1))
}
for item2 := range set2.M[string(key2)] {
if _, ok := set1.M[string(key1)][item2]; !ok {
list = append(list, []byte(item2))
}
}
return
}
// ErrBucketAndKey returns when bucket or key not found.
func ErrBucketAndKey(bucket string, key []byte) error {
return errors.New("not found bucket:" + bucket + ",key:" + string(key))
}
// ErrNotFoundKeyInBucket returns when key not in the bucket.
func ErrNotFoundKeyInBucket(bucket string, key []byte) error {
return errors.New(string(key) + " is not in the" + bucket)
}