-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexManager.h
More file actions
executable file
·310 lines (238 loc) · 8.9 KB
/
IndexManager.h
File metadata and controls
executable file
·310 lines (238 loc) · 8.9 KB
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
306
307
308
309
310
// B+ Tree
// Bohan Li
#ifndef B_PLUS_TREE
#define B_PLUS_TREE
#include <iostream>
#include <cstdlib>
#include <vector>
#include <stack>
#include <iterator>
#include <algorithm>
#include <string>
#include <map>
#include "recorder.hpp"
#define BLOCK_SIZE 4096
#define TEMP_FANOUT 100
class Block {
public:
int pos;
char* data;
std::string filename;
Block() {
data = (char*)malloc(BLOCK_SIZE);
};
Block(int _pos) { pos = _pos; }
void setPos(int _pos) { pos = _pos; }
int getPos() { return pos; }
};
class BufferManager {
public:
recorder* r;
std::string fname;
void setRecorder(recorder* _r) { r = _r; }
void getBlock(std::string filename, int offset, Block& blk){
std::vector<int> offsetV;
offsetV.push_back(offset);
conditionJudge cJ;
std::cout << "getBlock" << " Offset = " << offset/BLOCK_SIZE << std::endl;
vector<void*> rst = r->selectWhereIndex(filename, offsetV, cJ);
char* mem = (char*)rst[0];
memcpy(blk.data, mem, BLOCK_SIZE);
blk.pos = offset;
}
Block newBlock(std::string filename){
char* nulldata = (char*)malloc(BLOCK_SIZE);
for( int i = 0; i < BLOCK_SIZE; i++ ) {
nulldata[i] = 'A';
}
long pos = r->insert(nulldata, filename);
Block blk;
blk.pos = pos;
blk.filename = filename;
return blk;
};
void writeBlock(Block& block) {
std::cout << "writeBlock -- block.pos" << block.pos/BLOCK_SIZE << std::endl;
char* mem = block.data;
r->updateWhere(fname, block.pos, block.data);
}
void deleteBlock(Block block) {
conditionJudge cJ;
r->deleteWhereIndex(block.filename, block.pos, cJ);
}
recorder* getRecorder() { return r; }
};
class AttrVal {
public:
int typeId;
int datai;
float dataf;
std::string datas;
AttrVal() {}
AttrVal(int i): datai(i), typeId(0) {}
AttrVal(float f): dataf(f), typeId(1) {}
AttrVal(char s[]): datas(std::string(s)), typeId(2) {}
AttrVal(std::string s): datas(s), typeId(2) {}
friend bool operator<(const AttrVal &op1, const AttrVal &op2) {
if( op1.typeId == 0 ) return op1.datai < op2.datai;
if( op1.typeId == 1 ) return op1.dataf < op2.dataf;
if( op1.typeId == 2 ) return op1.datas < op2.datas;
return true;
}
friend bool operator==(const AttrVal &op1, const AttrVal &op2) {
if( op1.typeId == 0 ) return op1.datai == op2.datai;
if( op1.typeId == 1 ) return op1.dataf == op2.dataf;
if( op1.typeId == 2 ) return op1.datas == op2.datas;
return true;
}
friend bool operator>(const AttrVal &op1, const AttrVal &op2) { return !(op1 == op2 || op1 < op2); }
friend bool operator<=(const AttrVal &op1, const AttrVal &op2) { return !( op1 > op2); }
friend bool operator>=(const AttrVal &op1, const AttrVal &op2) { return !( op1 < op2); }
friend bool operator!=(const AttrVal &op1, const AttrVal &op2) { return !( op1 == op2); }
};
class Node {
private:
bool isroot, isleaf;
int readi(char *&mem);
float readf(char *&mem);
std::string reads(char *&mem, int strLen);
void writei(int i, char *&mem);
void writef(float f, char *&mem);
void writes(std::string s, int strLen, char *&mem );
public:
std::vector<int> P;
std::vector<AttrVal> K;
void resetByBlock(const Block &blk);
Node();
Node(bool _isroot, bool _isleaf);
Node(const Block &blk);
void write2Blk(Block &blk, int typeId, int strLen, BufferManager* bufferManager);
bool isRoot();
bool isLeaf();
void setRoot(bool _isroot) {isroot = _isroot; }
void setLeaf(bool _isleaf) {isleaf = _isleaf; }
void appendK(const AttrVal &_k);
void appendP(int _p);
void popK();
void popP();
int getKSize();
int getPSize();
AttrVal getK(int i);
int getP(int i);
void insert(const AttrVal &k, int p, int i);
void remove(const AttrVal &k, int p, int i);
};
class BPlusTree {
public:
std::string filename; // {tableName}_{indexName}.idx
Node rootNode;
int rootPos;
int fanout;
int typeId, strLen;
std::stack<int> stk;
//BufferManager bufferManager;
int _findLeaf(const AttrVal &k);
void _insertNewBlk(Block &blk1, const AttrVal &k, Block &blk2);
void _remove(Block blk, AttrVal k);
BufferManager* bufferManager;
BPlusTree();
BPlusTree(const std::string &_filename, int _typeId = 0, int _strLen = 0) : filename(_filename), typeId(_typeId), strLen(_strLen) {
//
// fanout = TEMP_FANOUT;
fanout = (BLOCK_SIZE - 2 - 4 * 4 - 4) / (4+4) - 10;
if( _typeId == 2 )
fanout = (BLOCK_SIZE - 2 - 4 * 4 - 4) / (_strLen+1+4) - 10;
}
void setBufferManager(BufferManager* _bufferManager) { bufferManager = _bufferManager; }
void setFilename(std::string _filename) { filename = _filename; }
void setTypeId(int i) { typeId = i; };
void setStrLen(int i) { strLen = i; };
void setRootNode(Node n);
void setRootPos(int p);
int getTypeId() { return typeId; }
int getStrLen() { return strLen; }
int find(const AttrVal &k);
void insert(const AttrVal &k, int p);
void remove(const AttrVal &k);
// std::vector<int> findLeft(const AttrVal &k, bool (*cmp)(const AttrVal &a,const AttrVal &b));
// std::vector<int> findRight(const AttrVal &k, bool (*cmp)(const AttrVal &a,const AttrVal &b));
std::vector<int> findLeft(const AttrVal &k);
std::vector<int> findRight(const AttrVal &k);
};
class IndexManager {
public:
//BufferManager bfManager;
BPlusTree btree;
std::string filename;
BufferManager bufferManager;
IndexManager(recorder* r) {
bufferManager.setRecorder(r);
btree.filename = "_";
btree.rootPos = -1;
}
int createIndex(std::string filename, int typeId = 0, int strLen = 0) {
recorder* r;
r = bufferManager.getRecorder();
r->createTable(filename, BLOCK_SIZE);
this->filename = filename;
bufferManager.fname = filename;
btree.setBufferManager(&bufferManager);
btree.setFilename(filename);
Node rootNode(true, true);
Block rootBlock = bufferManager.newBlock(filename);
btree.setTypeId(typeId);
btree.setStrLen(strLen);
btree.setRootNode(rootNode);
btree.setRootPos(rootBlock.pos);
rootNode.write2Blk(rootBlock, btree.getTypeId(), btree.getStrLen(), &bufferManager);
return rootBlock.pos;
}
int getIndex(std::string filename, int rootPos, int typeId = 0, int strLen = 0){
recorder* r;
r = bufferManager.getRecorder();
if( btree.filename == filename && btree.rootPos == rootPos )
return rootPos;
this->filename = filename;
btree.setBufferManager(&bufferManager);
btree.setFilename(filename);
Block rootBlock;
bufferManager.getBlock(filename, rootPos, rootBlock);
Node rootNode(rootBlock);
btree.setTypeId(typeId);
btree.setStrLen(strLen);
btree.setRootNode(rootNode);
btree.setRootPos(rootBlock.pos);
return rootBlock.pos;
}
void insertIntoIndex(std::map<AttrVal, int> datalist) {
std::map<AttrVal, int>::iterator it;
for( it = datalist.begin(); it != datalist.end(); it++ ) {
std::cout << "==>insertIntoIndex" <<it->first.datai << " " << it->second << std::endl;
btree.insert(it->first, it->second);
//system("pause");
}
}
std::vector<int> findInIndex(std::vector<AttrVal> datalist) {
std::vector<int> rst;
std::vector<AttrVal>::iterator it;
for( it = datalist.begin(); it != datalist.end(); it++ ) {
int tmpRst;
//tmpRst = btree._findLeaf(*it);
tmpRst = btree.find(*it);
//std::cout << (*it).datai << "-->" << tmpRst << std::endl;
rst.push_back(tmpRst);
}
return rst;
}
void deleteFromIndex(std::vector<AttrVal> datalist) {
std::vector<int> rst;
std::vector<AttrVal>::iterator it;
for( it = datalist.begin(); it != datalist.end(); it++ ) {
btree.remove(*it);
}
}
void dropIndex(std::string filename) {
bufferManager.r->dropTable(filename);
}
};
#endif