-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_hash.cpp
305 lines (280 loc) · 8.83 KB
/
db_hash.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "lightdb.h"
namespace lightdb{
// HSet sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created and return 1.
// If field already exists in the hash, it is overwritten and return 0.
Status LightDB::HSet(const std::string& key, const std::string& field, const std::string& value, int& res){
Status s;
s = CheckKeyValue(key, field);
if(!s.ok()){
res = 0;
return s;
}
s = CheckKeyValue(key, value);
if(!s.ok()){
res = 0;
return s;
}
std::string oldValue;
HGet(key, field, oldValue);
if(oldValue == value){
res = 1;
return Status::OK();
}
res = hashIdx.indexes->HSet(key, field, value);
Entry* e = Entry::NewEntryNow(key, value, field, Hash, HashHSet);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// HSetNx Sets field in the hash stored at key to value, only if field does not yet exist.
// If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.
// Return if the operation is successful.
Status LightDB::HSetNx(const std::string& key, const std::string& field, const std::string& value, bool& res){
Status s;
s = CheckKeyValue(key, field);
if(!s.ok()){
return s;
}
s = CheckKeyValue(key, value);
if(!s.ok()){
return s;
}
CheckExpired(key, Hash);
res = hashIdx.indexes->HSetNx(key, field, value);
if(!res){
return Status::OK();
}
Entry* e = Entry::NewEntryNow(key, value, field, Hash, HashHSet);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// HGet returns the value associated with field in the hash stored at key.
// if key and field exist, return true, else return false.
bool LightDB::HGet(const std::string& key, const std::string& field, std::string& value){
Status s;
bool res;
s = CheckKeyValue(key, field);
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return false;
}
res = hashIdx.indexes->HGet(key, field, value);
return res;
}
// HGetAll returns all fields and values of the hash stored at key.
// In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.
bool LightDB::HGetAll(const std::string& key, std::vector<std::string>& vals){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return false;
}
return hashIdx.indexes->HGetAll(key, vals);
}
// HMSet set multiple hash fields to multiple values
Status LightDB::HMSet(const std::string& key, std::vector<std::string> vals){
Status s;
s = CheckKeyValue(key, vals);
if(!s.ok()){
return s;
}
for(int i = 0; i<vals.size(); i+=2){
int res;
s = HSet(key, vals[i], vals[i+1], res);
if(!s.ok()){
return s;
}
}
return Status::OK();
}
// HDel removes the specified fields from the hash stored at key.
// Specified fields that do not exist within this hash are ignored.
// If key does not exist, it is treated as an empty hash and this command returns false.
Status LightDB::HDel(const std::string& key, const std::string& field, int& res){
Status s;
s = CheckKeyValue(key, field);
if(!s.ok()){
res = 0;
return Status::OK();
}
bool expired = CheckExpired(key, Hash);
if(!expired){
//if expired, delete nothing
res = 0;
return Status::OK();
}
bool deleted = hashIdx.indexes->HDel(key, field);
if(!deleted){
res = 0;
return Status::OK();
}
res = 1;
Entry* e = Entry::NewEntryNow(key, "", field, Hash, HashHDel);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// HMGet get the values of all the given hash fields
Status LightDB::HMGet(const std::string& key, const std::vector<std::string>& fields, std::vector<std::string>& vals, std::vector<bool>& sucs){
Status s;
s = CheckKeyValue(key, fields);
if(!s.ok()){
return s;
}
for(auto & field : fields){
std::string val;
bool suc = HGet(key, field, val);
if(suc){
vals.push_back(val);
sucs.push_back(true);
}else{
vals.emplace_back("");
sucs.push_back(false);
}
}
return Status::OK();
}
// HKeyExists returns if the key is existed in hash.
bool LightDB::HKeyExist(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return false;
}
return hashIdx.indexes->HKeyExist(key);
}
bool LightDB::HExist(const std::string& key, const std::string& field){
Status s;
s = CheckKeyValue(key, field);
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return false;
}
return hashIdx.indexes->HExist(key, field);
}
int LightDB::HLen(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return 0;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return 0;
}
return hashIdx.indexes->HLen(key);
}
// HKeys returns all field names in the hash stored at key.
// if keynot exist, return false, else return true
bool LightDB::HKeys(const std::string& key, std::vector<std::string>& keys){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return false;
}
if(!HKeyExist(key)){
return false;
}
hashIdx.indexes->HKeys(key, keys);
return true;
}
// HVals returns all values in the hash stored at key.
// if key does not exist, return false, else return true
bool LightDB::HVals(const std::string& key, std::vector<std::string>& vals){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
if(!HKeyExist(key)){
return false;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return false;
}
hashIdx.indexes->HVals(key, vals);
return true;
}
// HClear clear the key in hash.
Status LightDB::HClear(const std::string& key, int& res){
Status s;
bool expired = CheckExpired(key, Hash);
if(expired){
res = 0;
return Status::OK();
}
Entry* e = Entry::NewEntryNoExtra(key, "", Hash, HashHClear);
s = store(e);
if(!s.ok()){
return s;
}
res = hashIdx.indexes->HClear(key);
return Status::OK();
}
// HExpire set expired time for a hash key.
Status LightDB::HExpire(const std::string& key, uint64_t duration, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
suc = false;
return Status::OK();
}
if(!HKeyExist(key)){
suc = false;
return Status::OK();
}
uint64_t deadline;
deadline = getCurrentTimeStamp() + duration * 1000;
Entry* e = Entry::NewEntryWithExpire(key, "", deadline, Hash, HashHExpire);
s = store(e);
if(!s.ok()){
suc = false;
return s;
}
expires[Hash][key] = deadline;
suc = true;
return Status::OK();
}
// HTTL return time to live for the key.
int64_t LightDB::HTTL(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return -2;
}
bool expired = CheckExpired(key, Hash);
if(expired){
return -2;
}
if(expires[Hash].find(key) == expires[Hash].end()){
return -2;
}
return (expires[Hash][key] - getCurrentTimeStamp()) / 1000;
}
}// namespace lightdb