-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_set.cpp
282 lines (260 loc) · 8.37 KB
/
db_set.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "lightdb.h"
namespace lightdb{
//static const int Set = 3;
// SAdd add the specified members to the set stored at key.
// Specified members that are already a member of this set are ignored.
// If key does not exist, a new set is created before adding the specified members.
Status LightDB::SAdd(const std::string& key, const std::string& member, int& count){
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
return s;
}
bool expired = CheckExpired(key, Set);
bool isMember = setIdx.indexes->SIsMember(key, member);
count = setIdx.indexes->SAdd(key, member);
if(isMember){
return Status::OK();
}
Entry* e = Entry::NewEntryNoExtra(key, member, Set, SetSAdd);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// SPop removes and returns one or more random members from the set value store at key.
Status LightDB::SPop(const std::string& key, std::string& val, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return s;
}
bool expired = CheckExpired(key, Set);
if(expired){
suc = false;
return Status::OK();
}
suc = setIdx.indexes->SPop(key, val);
Entry* e = Entry::NewEntryNoExtra(key, val, Set, SetSRem);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// SIsMember Returns if member is a member of the set stored at key.
bool LightDB::SIsMember(const std::string& key, const std::string& member){
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Set);
if(expired){
return false;
}
bool exist = setIdx.indexes->SIsMember(key, member);
return exist;
}
// SRem Remove the specified members from the set stored at key.
// Specified members that are not a member of this set are ignored.
// If key does not exist, it is treated as an empty set and this command returns 0.
Status LightDB::SRem(const std::string& key, const std::string& member, bool& suc){
Status s;
s = CheckKeyValue(key, member);
if(!s.ok()){
return s;
}
bool expired = CheckExpired(key, Set);
if(expired){
suc = false;
return Status::OK();
}
suc = setIdx.indexes->SRem(key, member);
Entry* e = Entry::NewEntryNoExtra(key, member, Set, SetSRem);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// SMove move member from the set at source to the set at destination.
Status LightDB::SMove(const std::string& src, const std::string& dst, const std::string& member, bool& suc){
Status s;
s = CheckKeyValue(src, "");
if(!s.ok()){
suc = false;
return Status::OK();
}
s = CheckKeyValue(dst, member);
if(!s.ok()){
suc = false;
return Status::OK();
}
bool srcExpired = CheckExpired(src, Set);
if(srcExpired){
suc = false;
return Status::OK();
}
bool dstExpired = CheckExpired(dst, Set);
suc = setIdx.indexes->SMove(src, dst, member);
Entry* e = Entry::NewEntryNow(src, member, dst, Set, SetSMove);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// SCard returns the set cardinality (number of elements) of the set stored at key.
int LightDB::SCard(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return 0;
}
bool expired = CheckExpired(key, Set);
if(expired){
return 0;
}
return setIdx.indexes->SCard(key);
}
// SMembers returns all the members of the set value stored at key.
bool LightDB::SMembers(const std::string& key, std::vector<std::string>& members){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Set);
if(expired){
return false;
}
return setIdx.indexes->SMembers(key, members);
}
// SUnion returns the members of the set resulting from the union of all the given sets.
void LightDB::SUnion(const std::vector<std::string>& keys, std::vector<std::string>& vals){
Status s;
std::vector<std::string> validKeys;
for(auto & key : keys){
s = CheckKeyValue(key, "");
if(s.ok()){
validKeys.push_back(key);
}
}
std::vector<std::string> keysNotExpired;
for(auto & validKey : validKeys){
if(!CheckExpired(validKey, Set)){
keysNotExpired.push_back(validKey);
}
}
setIdx.indexes->SUnion(keysNotExpired, vals);
}
void LightDB::SDiff(const std::string& key1, const std::vector<std::string>& succ_keys, std::vector<std::string>& vals) {
Status s;
s = CheckKeyValue(key1, "");
if(!s.ok()){
return;
}
if(CheckExpired(key1, Set)){
return;
}
std::vector<std::string> validKeys;
for(auto & succ_key : succ_keys){
s = CheckKeyValue(succ_key, "");
if(s.ok()){
validKeys.push_back(succ_key);
}
}
std::vector<std::string> keysNotExpired;
for(auto & validKey : validKeys){
if(!CheckExpired(validKey, Set)){
keysNotExpired.push_back(validKey);
}
}
setIdx.indexes->SDiff(key1, keysNotExpired, vals);
}
// SKeyExists returns if the key exists.
bool LightDB::SKeyExist(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Set);
if(expired){
return false;
}
return setIdx.indexes->SKeyExist(key);
}
// SClear clear the specified key in set.
Status LightDB::SClear(const std::string& key, int& count){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
count = 0;
return Status::OK();
}
if(!SKeyExist(key)){
count = 0;
return Status::OK();
}
bool expired = CheckExpired(key, Set);
if(expired){
count = 0;
return Status::OK();
}
Entry* e = Entry::NewEntryNoExtra(key, "", Set, SetSClear);
s = store(e);
if(!s.ok()){
return s;
}
count = setIdx.indexes->SClear(key);
return Status::OK();
}
// SExpire set expired time for the key in set.
// if the key does not exist, suc = false, else suc = true
Status LightDB::SExpire(const std::string& key, uint64_t duration, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
suc = false;
return Status::OK();
}
bool expired = CheckExpired(key, Set);
if(expired){
suc = false;
return Status::OK();
}
if(!SKeyExist(key)){
suc = false;
return Status::OK();
}
uint64_t deadline = getCurrentTimeStamp() + duration * 1000;
Entry* e = Entry::NewEntryWithExpire(key, "", deadline, Set, SetSExpire);
s = store(e);
if(!s.ok()){
suc = false;
return s;
}
expires[Set][key] == deadline;
suc = true;
return Status::OK();
}
// STTL return time to live for the key in set.
int64_t LightDB::STTL(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return -2;
}
bool expired = CheckExpired(key, Set);
if(expired){
return -2;
}
if(expires[Set].find(key) == expires[static_cast<const uint16_t>(Set)].end()){
return -2;
}
return (expires[Set][key] - getCurrentTimeStamp()) / 1000;
}
}//namespace lightdb