-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_zset.cpp
251 lines (232 loc) · 7.75 KB
/
db_zset.cpp
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
#include "lightdb.h"
namespace lightdb{
// ZAdd adds the specified member with the specified score to the sorted set stored at key.
Status LightDB::ZAdd(const std::string& key, double score,const std::string& member, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
suc = false;
return Status::OK();
}
double old_score;
bool exist = sortedSetIdx.indexes->ZScore(key, member, old_score);
if(exist){
suc = false;
return Status::OK();
}
sortedSetIdx.indexes->ZAdd(key, score, member);
std::string extra = to_string(score);
Entry* e = Entry::NewEntryNow(key, member, extra, ZSet, ZSetZAdd);
s = store(e);
if(!s.ok()){
return s;
}
suc = true;
return Status::OK();
}
// ZScore returns the score of member in the sorted set at key.
bool LightDB::ZScore(const std::string& key, const std::string& member, double& score){
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return false;
}
return sortedSetIdx.indexes->ZScore(key, member, score);
}
// ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at key.
int LightDB::ZCard(const std::string& key){
Status s;
if(!s.ok()){
return 0;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return 0;
}
return sortedSetIdx.indexes->ZCard(key);
}
// ZRank returns the rank of member in the sorted set stored at key, with the scores ordered from low to high.
// The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.
int LightDB::ZRank(const std::string& key, const std::string& member){
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
return -1;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return -1;
}
return sortedSetIdx.indexes->ZRank(key, member);
}
int LightDB::ZRevRank(const std::string& key, const std::string& member) {
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
return -1;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return -1;
}
return sortedSetIdx.indexes->ZRevRank(key, member);
}
// ZIncrBy increments the score of member in the sorted set stored at key by increment.
// If member does not exist in the sorted set, it is added with increment as its score (as if its previous score was 0.0).
// If key does not exist, a new sorted set with the specified member as its sole member is created.
Status LightDB::ZIncrBy(const std::string& key, double increment, const std::string& member, double& res){
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
return s;
}
CheckExpired(key, ZSet);
res = sortedSetIdx.indexes->ZIncrBy(key, increment, member);
std::string extra = to_string(increment);
Entry* e = Entry::NewEntryNow(key, member, extra, ZSet, ZSetZAdd);
s = store(e);
if(s.ok()){
return s;
}
return Status::OK();
}
// ZRange returns the specified range of elements in the sorted set stored at key.
void LightDB::ZRange(const std::string& key, int start, int end, std::vector<std::string>& vals){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return;
}
sortedSetIdx.indexes->ZRange(key, start, end,vals);
}
void LightDB::ZRevRange(const std::string& key, int start, int end, std::vector<std::string>& vals){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return;
}
sortedSetIdx.indexes->ZRevRange(key, start, end,vals);
}
// ZRem removes the specified members from the sorted set stored at key. Non existing members are ignored.
// An error is returned when key exists and does not hold a sorted set.
Status LightDB::ZRem(const std::string& key, const std::string& member, bool& suc){
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
suc = false;
return Status::OK();
}
bool expired = CheckExpired(key, ZSet);
if(expired){
suc = false;
return Status::OK();
}
suc = sortedSetIdx.indexes->ZRem(key, member);
Entry* e = Entry::NewEntryNoExtra(key, member, ZSet, ZSetZRem);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// ZGetByRank get the member at key by rank, the rank is ordered from lowest to highest.
// The rank of lowest is 0 and so on.
bool LightDB::ZGetByRank(const std::string& key, int rank, std::string& member){
bool expired = CheckExpired(key, ZSet);
if(expired){
return false;
}
return sortedSetIdx.indexes->ZGetByRank(key, rank, member);
}
bool LightDB::ZRevGetByRank(const std::string& key, int rank, std::string& member){
bool expired = CheckExpired(key, ZSet);
if(expired){
return false;
}
return sortedSetIdx.indexes->ZRevGetByRank(key, rank, member);
}
// ZKeyExists check if the key exists in zset.
bool LightDB::ZKeyExist(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return false;
}
return sortedSetIdx.indexes->ZKeyExist(key);
}
// ZClear clear the specified key in zset.
Status LightDB::ZClear(const std::string& key, int& count){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
count = 0;
return Status::OK();
}
bool expired = CheckExpired(key, ZSet);
if(expired){
count = 0;
return Status::OK();
}
count = sortedSetIdx.indexes->ZClear(key);
return Status::OK();
}
// ZExpire set expired time for the key in zset.
Status LightDB::ZExpire(const std::string& key, int64_t duration, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
suc = false;
return Status::OK();
}
bool expired = CheckExpired(key, ZSet);
if(expired){
suc = false;
return Status::OK();
}
if(!ZKeyExist(key)){
suc = false;
return Status::OK();
}
uint64_t deadline = getCurrentTimeStamp() + duration * 1000;
Entry* e = Entry::NewEntryWithExpire(key, "", deadline, ZSet, ZSetZExpire);
s = store(e);
if(!s.ok()){
suc = false;
return s;
}
expires[ZSet][key] = deadline;
suc = true;
return Status::OK();
}
int64_t LightDB::ZTTL(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return -2;
}
bool expired = CheckExpired(key, ZSet);
if(expired){
return -2;
}
if(!ZKeyExist(key)){
return -2;
}
return (expires[ZSet][key] - getCurrentTimeStamp()) / 1000;
}
}