-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_list.cpp
302 lines (276 loc) · 9.49 KB
/
db_list.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
#include "lightdb.h"
namespace lightdb{
// LPush insert all the specified values at the head of the list stored at key.
// If key does not exist, it is created as empty list before performing the push operations.
Status LightDB::LPush(const std::string& key, const std::string& value, int& length){
//store
Status s;
s = CheckKeyValue(key, value);
if(!s.ok()){
return s;
}
CheckExpired(key, List);
//update index
listIdx.indexes->LPush(key,value);
//store
Entry* e = Entry::NewEntryNoExtra(key, value, List, ListLPush);
s = store(e);
if(!s.ok()){
return s;
}
length = LLen(key);
return Status::OK();
}
// RPush insert all the specified values at the tail of the list stored at key.
// If key does not exist, it is created as empty list before performing the push operation.
Status LightDB::RPush(const std::string& key, const std::string& value, int& length){
Status s;
s = CheckKeyValue(key, value);
if(!s.ok()){
return s;
}
CheckExpired(key, List);
//update index
length = listIdx.indexes->RPush(key, value);
//store;
Entry* e = Entry::NewEntryNoExtra(key, value, List, ListRPush);
s = store(e);
if(!s.ok()){
return s;
}
//update index
length = LLen(key);
return Status::OK();
}
// LPop removes and returns the first elements of the list stored at key.
Status LightDB::LPop(const std::string& key, std::string& val, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return s;
}
CheckExpired(key, List);
suc = listIdx.indexes->LPop(key, val);
//store;
Entry* e = Entry::NewEntryNoExtra(key, val, List, ListLPop);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// Removes and returns the last elements of the list stored at key.
Status LightDB::RPop(const std::string& key, std::string& val, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return s;
}
CheckExpired(key, List);
suc = listIdx.indexes->RPop(key, val);
//store
Entry* e = Entry::NewEntryNoExtra(key, val, List, ListRPop);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// LIndex returns the element at index in the list stored at key.
// The index is zero-based, so 0 means the first element, 1 the second element and so on.
bool LightDB::LIndex(const std::string& key, uint32_t index, std::string& val){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
bool expired = CheckExpired(key, List);
if(expired){
return false;
}
return listIdx.indexes->LIndex(key, index, val);
}
// LRem removes the first count occurrences of elements equal to element from the list stored at key.
// The count argument influences the operation in the following ways:
// count > 0: Remove elements equal to element moving from head to tail.
// count < 0: Remove elements equal to element moving from tail to head.
// count = 0: Remove all elements equal to element.
Status LightDB::LRem(const std::string& key, const std::string& value, int count, int& removed){
Status s;
s = CheckKeyValue(key, value);
if(!s.ok()){
return s;
}
bool expired = CheckExpired(key, List);
if(expired){
removed = 0;
return Status::OK();
}
removed = listIdx.indexes->LRem(key, value, count);
//store;
Entry* e = Entry::NewEntryNow(key, value, to_string(count), List, ListLRem);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// LInsert inserts element in the list stored at key either before or after the reference value pivot.
Status LightDB::LInsert(const std::string& key, InsertOption option, const std::string& pivot, const std::string& val, int& count){
Status s;
s = CheckKeyValue(key, pivot);
if(!s.ok()){
return s;
}
s = CheckKeyValue(key, val);
if(!s.ok()){
return s;
}
bool expired = CheckExpired(key, List);
if(expired){
count = 0;
return Status::OK();
}
count = listIdx.indexes->LInsert(key, option, pivot, val);
//store;
std::string extra;
extra.append(pivot);
extra.append(ExtraSeparator);
extra.append(to_string(option));
Entry* e = Entry::NewEntryNow(key, val, extra, List, ListLInsert);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// LSet sets the list element at index to element.
// returns whether is successful.
Status LightDB::LSet(const std::string& key, int idx, const std::string& value, bool& suc){
Status s;
s = CheckKeyValue(key, value);
if(!s.ok()){
return s;
}
bool expired = CheckExpired(key, List);
if(expired){
suc = false;
return Status::OK();
}
suc = listIdx.indexes->LSet(key, idx, value);
if(!suc){
return Status::OK();
}
std::string extra = to_string(idx);
Entry* e = Entry::NewEntryNow(key, value, extra, List, ListLSet);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// LTrim trim an existing list so that it will contain only the specified range of elements specified.
// Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.
Status LightDB::LTrim(const std::string& key, int start, int end, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return Status::OK();
}
bool expired = CheckExpired(key, List);
if(expired){
return Status::OK();
}
suc = listIdx.indexes->LTrim(key, start, end);
std::string extra;
extra.append(to_string(start));
extra.append(ExtraSeparator);
extra.append(to_string(end));
Entry* e = Entry::NewEntryNow(key, "", extra, List, ListLTrim);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// LRange returns the specified elements of the list stored at key.
// The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on.
// These offsets can also be negative numbers indicating offsets starting at the end of the list.
// For example, -1 is the last element of the list, -2 the penultimate, and so on.
Status LightDB::LRange(const std::string& key, int start, int end, std::vector<std::string>& vals, bool& suc){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return s;
}
suc = listIdx.indexes->LRange(key, start, end, vals);
return Status::OK();
}
bool LightDB::LKeyExist(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return false;
}
return listIdx.indexes->LKeyExist(key);
}
int LightDB::LLen(const std::string& key){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
return 0;
}
return listIdx.indexes->LLen(key);
}
// LClear clear a specified key for List.
// return the number of eliment of the list in this operation
Status LightDB::LClear(const std::string& key, int& count){
Status s;
s = CheckKeyValue(key, "");
if(!s.ok()){
count = 0;
return Status::OK();
}
bool expired = CheckExpired(key, List);
if(expired){
count = 0;
return Status::OK();
}
count = listIdx.indexes->LClear(key);
Entry* e = Entry::NewEntryNoExtra(key, "", List, ListLClear);
s = store(e);
if(!s.ok()){
return s;
}
return Status::OK();
}
// LExpire set expired time for a specified key of List.
Status LightDB::LExpire(const std::string& key, uint64_t duration, bool& suc){
Status s;
if(!LKeyExist(key)){
suc = false;
return Status::KeyNotExist();
}
uint64_t deadline = getCurrentTimeStamp() + duration * 1000;
Entry* e = Entry::NewEntryWithExpire(key, "", deadline, List, ListLExpire);
s = store(e);
if(!s.ok()){
return s;
}
expires[List][key] = deadline;
suc = true;
return Status::OK();
}
// LTTL return time to live.
int64_t LightDB::LTTL(const std::string& key){
bool expired = CheckExpired(key, List);
if(expired){
return -2;
}
if(expires[List].find(key) == expires[List].end()){
return -2;
}
uint64_t deadline = expires[List][key];
return (deadline - getCurrentTimeStamp()) / 1000;
}
}//namespace lightdb