-
Notifications
You must be signed in to change notification settings - Fork 3
/
idx.cpp
264 lines (235 loc) · 7.67 KB
/
idx.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
//
// Created by xiaolei on 2022/2/25.
//
#include "lightdb.h"
namespace lightdb{
/*
* 遍历所有类型的db文件,建立索引,每次打开数据库的时候都要进行一遍恢复索引的操作
*/
Status LightDB::loadIdxFromFiles(){
for(int dataType=0; dataType<DataStructureNum; dataType++){
std::vector<uint32_t> fileIds;
std::map<uint32_t, DBFile*> dbFiles;
//archived files
for(auto & it : archivedFiles[dataType]){
dbFiles.insert(std::make_pair(it.first, it.second));
fileIds.push_back(it.first);
}
//active files
DBFile* activeFile = getActiveFile(static_cast<DataType>(dataType));
dbFiles.insert(std::make_pair(activeFile->Id, activeFile));
fileIds.push_back(activeFile->Id);
sort(fileIds.begin(), fileIds.end());
for(unsigned int fid : fileIds){
DBFile* df = dbFiles[fid];
int64_t offset = 0;
for(; offset < config->blockSize; ){
Entry e;
Status s = df->Read(offset, e);
if(s.Code() == kEndOfFile){
break;
}
if(!s.ok()){
printf("a fatal err occurred, the db can not open, code:%d \n", s.Code());
s.Report();
exit(1);
}
Indexer* indexer = new Indexer();
indexer->fileId = fid;
indexer->offset = offset;
offset += e.Size();
df->SetOffset(offset);
if(!e.meta->key.empty()){
buildIndex(&e, indexer);
}
}
}
}
return Status::OK();
}
// 只在打开数据库,遍历所有数据文件进行索引恢复时使用,往db文件追加entry时,也会对entry建立索引,但是不会用这个函数,会使用strset系列函数
Status LightDB::buildIndex(Entry* entry, Indexer* indexer){
switch(entry->GetType()){
case String:
indexer->meta->key = entry->meta->key;
if (config->indexMode == KeyValueMemMode) {
indexer->meta->value = entry->meta->value;
}
buildStringIndex(indexer, entry);
break;
case List:
buildListIndex(entry);
break;
case Hash:
buildHashIndex(entry);
break;
case Set:
buildSetIndex(entry);
break;
case ZSet:
buildZSetIndex(entry);
break;
default:
break;
}
return Status::OK();
}
//build
void LightDB::buildStringIndex(Indexer* indexer,Entry* entry){
if(indexer== nullptr){
return;
}
switch (entry->GetMark()) {
case StringSet:
indexer->meta->value = entry->meta->value;
strIdx.indexes->put(indexer->meta->key, *indexer);
break;
case StringRem:
strIdx.indexes->erase(indexer->meta->key);
break;
case StringExpire:
if(entry->timeStamp<getCurrentTimeStamp()){
strIdx.indexes->erase(indexer->meta->key);
}else{
expires[String][indexer->meta->key] = entry->timeStamp;
}
default:
break;
}
}
void LightDB::buildListIndex(Entry *entry) {
if(entry == nullptr){
return;
}
std::string key = entry->meta->key;
std::string tmp;
std::vector<std::string> pivot_and_opt;
std::vector<std::string> start_and_end;
switch (entry->GetMark()) {
case ListLPush:
listIdx.indexes->LPush(key, entry->meta->value);
break;
case ListLPop:
listIdx.indexes->LPop(key, tmp);
break;
case ListRPush:
listIdx.indexes->RPush(key, entry->meta->value);
break;
case ListRPop:
listIdx.indexes->RPop(key, tmp);
break;
case ListLRem: {
int count;
count = std::stoi(entry->meta->extra);
listIdx.indexes->LRem(key, entry->meta->value, count);
break;
}
case ListLInsert: {
InsertOption opt;
pivot_and_opt = splitWithStl(entry->meta->extra, ExtraSeparator);
opt = static_cast<InsertOption>(std::stoi(pivot_and_opt[1]));
listIdx.indexes->LInsert(entry->meta->key, opt, pivot_and_opt[0], entry->meta->value);
break;
}
case ListLSet:
listIdx.indexes->LSet(entry->meta->key, std::stoi(entry->meta->extra), entry->meta->value);
break;
case ListLTrim:
start_and_end = splitWithStl(entry->meta->extra, ExtraSeparator);
listIdx.indexes->LTrim(entry->meta->key, std::stoi(start_and_end[0]), std::stoi(start_and_end[1]));
break;
case ListLExpire:
if(entry->timeStamp < getCurrentTimeStamp()){
listIdx.indexes->LClear(entry->meta->key);
}else{
expires[List][entry->meta->key] = entry->timeStamp;
}
break;
case ListLClear:
listIdx.indexes->LClear(entry->meta->key);
default:
break;
}
}
//build hashIdx, given a hash entry.
void LightDB::buildHashIndex(Entry *entry) {
if(entry == nullptr){
return;
}
std::string key = entry->meta->key;
switch (entry->GetMark()) {
case HashHSet:
hashIdx.indexes->HSet(entry->meta->key, entry->meta->extra, entry->meta->value);
break;
case HashHDel:
hashIdx.indexes->HDel(entry->meta->key, entry->meta->extra);
break;
case HashHClear:
hashIdx.indexes->HClear(entry->meta->key);
break;
case HashHExpire:
if(entry->timeStamp < getCurrentTimeStamp()){
hashIdx.indexes->HClear(entry->meta->key);
}else{
expires[Hash][entry->meta->key] = entry->timeStamp;
}
default:
break;
}
}
void LightDB::buildSetIndex(Entry *entry) {
if(entry == nullptr){
return;
}
std::string key = entry->meta->key;
switch (entry->GetMark()) {
case SetSAdd:
setIdx.indexes->SAdd(key, entry->meta->value);
break;
case SetSRem:
setIdx.indexes->SRem(key, entry->meta->value);
break;
case SetSMove:
setIdx.indexes->SMove(key, entry->meta->extra, entry->meta->value);
break;
case SetSClear:
setIdx.indexes->SClear(key);
break;
case SetSExpire:
if(entry->timeStamp < getCurrentTimeStamp()){
setIdx.indexes->SClear(key);
}else{
expires[Set][key] = entry->timeStamp;
}
default:
break;
}
}
void LightDB::buildZSetIndex(Entry *entry) {
if(entry == nullptr){
return;
}
std::string key = entry->meta->key;
double score;
switch (entry->GetMark()) {
case ZSetZAdd:
score = std::strtod(entry->meta->extra.c_str(), nullptr);
sortedSetIdx.indexes->ZAdd(key, score, entry->meta->value);
break;
case ZSetZRem:
sortedSetIdx.indexes->ZRem(key, entry->meta->value);
break;
case ZSetZClear:
sortedSetIdx.indexes->ZClear(key);
break;
case ZSetZExpire:
if(entry->timeStamp < getCurrentTimeStamp()){
sortedSetIdx.indexes->ZClear(key);
}else{
expires[ZSet][key] = entry->timeStamp;
}
default:
break;
}
}
}//namespace lightdb