forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx_zset.go
252 lines (200 loc) · 7.9 KB
/
tx_zset.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
// Copyright 2019 The nutsdb Author. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nutsdb
import (
"bytes"
"errors"
"strconv"
"strings"
"time"
"github.com/xujiajun/nutsdb/ds/zset"
"github.com/xujiajun/utils/strconv2"
)
// SeparatorForZSetKey represents separator for zSet key.
const SeparatorForZSetKey = "|"
// ZAdd adds the specified member key with the specified score and specified val to the sorted set stored at bucket.
func (tx *Tx) ZAdd(bucket string, key []byte, score float64, val []byte) error {
var buffer bytes.Buffer
if strings.Contains(string(key), SeparatorForZSetKey) {
return ErrSeparatorForZSetKey()
}
buffer.Write(key)
buffer.Write([]byte(SeparatorForZSetKey))
scoreBytes := []byte(strconv.FormatFloat(score, 'f', -1, 64))
buffer.Write(scoreBytes)
newKey := buffer.Bytes()
return tx.put(bucket, newKey, val, Persistent, DataZAddFlag, uint64(time.Now().Unix()), DataStructureSortedSet)
}
// ZMembers returns all the members of the set value stored at bucket.
func (tx *Tx) ZMembers(bucket string) (map[string]*zset.SortedSetNode, error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return nil, ErrBucket
}
return tx.db.SortedSetIdx[bucket].Dict, nil
}
// ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at bucket.
func (tx *Tx) ZCard(bucket string) (int, error) {
members, err := tx.ZMembers(bucket)
if err != nil {
return 0, err
}
return len(members), nil
}
// ZCount returns the number of elements in the sorted set at bucket with a score between min and max and opts.
// opts includes the following parameters:
// Limit int // limit the max nodes to return
// ExcludeStart bool // exclude start value, so it search in interval (start, end] or (start, end)
// ExcludeEnd bool // exclude end value, so it search in interval [start, end) or (start, end)
func (tx *Tx) ZCount(bucket string, start, end float64, opts *zset.GetByScoreRangeOptions) (int, error) {
nodes, err := tx.ZRangeByScore(bucket, start, end, opts)
if err != nil {
return 0, err
}
return len(nodes), nil
}
// ZPopMax removes and returns the member with the highest score in the sorted set stored at bucket.
func (tx *Tx) ZPopMax(bucket string) (*zset.SortedSetNode, error) {
item, err := tx.ZPeekMax(bucket)
if err != nil {
return nil, err
}
return item, tx.put(bucket, []byte(" "), []byte(""), Persistent, DataZPopMaxFlag, uint64(time.Now().Unix()), DataStructureSortedSet)
}
// ZPopMin removes and returns the member with the lowest score in the sorted set stored at bucket.
func (tx *Tx) ZPopMin(bucket string) (*zset.SortedSetNode, error) {
item, err := tx.ZPeekMin(bucket)
if err != nil {
return nil, err
}
return item, tx.put(bucket, []byte(" "), []byte(""), Persistent, DataZPopMinFlag, uint64(time.Now().Unix()), DataStructureSortedSet)
}
// ZPeekMax returns the member with the highest score in the sorted set stored at bucket.
func (tx *Tx) ZPeekMax(bucket string) (*zset.SortedSetNode, error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return nil, ErrBucket
}
return tx.db.SortedSetIdx[bucket].PeekMax(), nil
}
// ZPeekMin returns the member with the lowest score in the sorted set stored at bucket.
func (tx *Tx) ZPeekMin(bucket string) (*zset.SortedSetNode, error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return nil, ErrBucket
}
return tx.db.SortedSetIdx[bucket].PeekMin(), nil
}
// ZRangeByScore returns all the elements in the sorted set at bucket with a score between min and max.
func (tx *Tx) ZRangeByScore(bucket string, start, end float64, opts *zset.GetByScoreRangeOptions) ([]*zset.SortedSetNode, error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return nil, ErrBucket
}
return tx.db.SortedSetIdx[bucket].GetByScoreRange(zset.SCORE(start), zset.SCORE(end), opts), nil
}
// ZRangeByRank returns all the elements in the sorted set in one bucket and key
// with a rank between start and end (including elements with rank equal to start or end).
func (tx *Tx) ZRangeByRank(bucket string, start, end int) ([]*zset.SortedSetNode, error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return nil, ErrBucket
}
return tx.db.SortedSetIdx[bucket].GetByRankRange(start, end, false), nil
}
// ZRem removes the specified members from the sorted set stored in one bucket at given bucket and key.
func (tx *Tx) ZRem(bucket, key string) error {
if err := tx.checkTxIsClosed(); err != nil {
return err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return ErrBucket
}
return tx.put(bucket, []byte(key), []byte(""), Persistent, DataZRemFlag, uint64(time.Now().Unix()), DataStructureSortedSet)
}
// ZRemRangeByRank removes all elements in the sorted set stored in one bucket at given bucket with rank between start and end.
// the rank is 1-based integer. Rank 1 means the first node; Rank -1 means the last node.
func (tx *Tx) ZRemRangeByRank(bucket string, start, end int) error {
if err := tx.checkTxIsClosed(); err != nil {
return err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return ErrBucket
}
newKey := strconv2.IntToStr(start)
newVal := strconv2.IntToStr(end)
return tx.put(bucket, []byte(newKey), []byte(newVal), Persistent, DataZRemRangeByRankFlag, uint64(time.Now().Unix()), DataStructureSortedSet)
}
// ZRank returns the rank of member in the sorted set stored in the bucket at given bucket and key,
// with the scores ordered from low to high.
func (tx *Tx) ZRank(bucket string, key []byte) (int, error) {
if err := tx.checkTxIsClosed(); err != nil {
return 0, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return 0, ErrBucket
}
return tx.db.SortedSetIdx[bucket].FindRank(string(key)), nil
}
// ZRevRank returns the rank of member in the sorted set stored in the bucket at given bucket and key,
// with the scores ordered from high to low.
func (tx *Tx) ZRevRank(bucket string, key []byte) (int, error) {
if err := tx.checkTxIsClosed(); err != nil {
return 0, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return 0, ErrBucket
}
return tx.db.SortedSetIdx[bucket].FindRevRank(string(key)), nil
}
// ZScore returns the score of member in the sorted set in the bucket at given bucket and key.
func (tx *Tx) ZScore(bucket string, key []byte) (float64, error) {
if err := tx.checkTxIsClosed(); err != nil {
return 0, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return 0, ErrBucket
}
if node := tx.db.SortedSetIdx[bucket].GetByKey(string(key)); node != nil {
return float64(node.Score()), nil
}
return 0, ErrNotFoundKey
}
// ZGetByKey returns node in the bucket at given bucket and key.
func (tx *Tx) ZGetByKey(bucket string, key []byte) (*zset.SortedSetNode, error) {
if err := tx.checkTxIsClosed(); err != nil {
return nil, err
}
if _, ok := tx.db.SortedSetIdx[bucket]; !ok {
return nil, ErrBucket
}
if node := tx.db.SortedSetIdx[bucket].GetByKey(string(key)); node != nil {
return node, nil
}
return nil, ErrNotFoundKey
}
// ErrSeparatorForZSetKey returns when zSet key contains the SeparatorForZSetKey flag.
func ErrSeparatorForZSetKey() error {
return errors.New("contain separator (" + SeparatorForZSetKey + ") for ZSet key")
}