-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
424 lines (408 loc) · 9.07 KB
/
Table.cpp
File metadata and controls
424 lines (408 loc) · 9.07 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "Table.h"
#include <vector>
#include <algorithm>
#include <iostream>
#include <vector>
Table::Table(unsigned _metaAddr, BufferManager & _bm):RawTable(_metaAddr, _bm)
{
unsigned char* blk = _bm.read(_metaAddr);
unsigned char* p = blk + 28;
int stage = 1; //1:pk, 2:numCol, 3:col_1, col_2,..., 4:blk_1, blk_2, blk_3...
int count = 0;
while (stage < 4)
{
if (p == blk + *(unsigned*)blk)
{
blk = _bm.read(getNextAddr(blk));
p = blk + 8;
}
switch (stage)
{
case 1:
searchKey = *(unsigned*)p;
++stage;
break;
case 2:
count = *(unsigned*)p;
cols.push_back(0);
++stage;
break;
case 3:
cols.push_back(*(unsigned*)p);
if (--count == 0)
{
++stage;
count = numBlk;
}
break;
case 4:
index.push_back(*(unsigned*)p);
if (--count == 0)
{
++stage;
}
default:
break;
}
p += 4;
}
}
Table::Table(BufferManager & _bm, int _tupleSize, unsigned _searchKey, std::vector<unsigned> _cols)
:searchKey(_searchKey),cols(_cols), RawTable(_bm, _tupleSize)
{
index.push_back(startAddr);
}
Table::~Table()
{
delete[] cache;
}
void Table::save()
{
/*
从第28字节开始:
4字节unsigned:colPk
4字节unsigned:colNum
随后:每4个字节保存一列所占的字节数
随后的numBlk个4字节:存储对应的index
*/
RawTable::save();
unsigned addr = metaAddr;
unsigned char* blk = bm.read(metaAddr);
unsigned char* p = blk + 28;
int stage = 1; //1:pk, 2:numCol, 3:col_1, col_2,..., 4:blk_1, blk_2, blk_3...
int count = 0;
while (stage < 4)
{
if (p == blk + *(unsigned*)blk)
{
//1个磁盘不够用
if (-1 == getNextAddr(blk))
{
unsigned newAddr = getFreeBlockOnDisk();
//改下地址,写回
*(unsigned*)(blk + 4) = newAddr;
bm.write(addr);
//新申请一个磁盘
addr = newAddr;
blk = bm.get(addr);
*(unsigned*)blk = 8;
*(unsigned*)(blk + 4) = -1;
p = blk + 8;
}
else
{
//直接写回
bm.write(addr);
addr = getNextAddr(blk);
blk = bm.read(addr);
p = blk + 8;
}
}
switch (stage)
{
case 1:
*(unsigned*)p = searchKey;
++stage;
break;
case 2:
count = cols.size() - 1;
*(unsigned*)p = count;
++stage;
break;
case 3:
*(unsigned*)p = *(cols.end() - count);
if (--count == 0)
{
++stage;
count = numBlk;
}
break;
case 4:
*(unsigned*)p = *(index.end() - count);
if (--count == 0)
{
++stage;
}
default:
break;
}
p += 4;
}
bm.write(addr);
}
void Table::dropAll()
{
//清除数据磁盘块
for (auto i : index)
{
bm.drop(i);
}
//清除meta磁盘块
unsigned char* blk;
unsigned addr = metaAddr;
while (addr != -1)
{
blk = bm.read(addr);
unsigned newAddr = getNextAddr(blk);
bm.drop(addr);
addr = newAddr;
}
}
void Table::format(unsigned char * blk, std::map<unsigned, unsigned char*> args)
{
memset(blk, 0, tupleSize);
for (auto p : args)
{
memcpy(blk + cols[p.first - 1], p.second, cols[p.first] - cols[p.first - 1]);
}
}
SeqTable::SeqTable(unsigned _metaAddr, BufferManager & _bm):Table(_metaAddr, _bm)
{
unsigned char* blk = bm.read(_metaAddr);
cmp = *(bool*)(blk + 20);
}
SeqTable::SeqTable(BufferManager & _bm, int _tupleSize, unsigned _searchKey, std::vector<unsigned> _cols, bool _cmp)
:Table(_bm, _tupleSize, _searchKey, _cols), cmp(_cmp)
{
}
/*
返回值:
参数1:0:插入失败; -1:无新引入磁盘块;否则:新磁盘块的索引下标
参数2:新元组所在的磁盘块索引下标
*/
std::pair<unsigned, unsigned> SeqTable::insert(std::map<unsigned, unsigned char*> args, bool distinct)
{
if (args.find(searchKey) == args.end())
{
std::cerr << "INSERT ERROR: No SearchKey" << std::endl;
return { 0,0 };
}
//构造元组
format(cache, args);
//获取插入位置
auto pos = binarySearch(cache + cols[searchKey-1]);
//已存在相同的searchKey
if (distinct && pos[0] == 0)
{
std::cerr << "INSERT FAIL: cannot insert a tuple with exsiting key!" << std::endl;
return { 0,0 };
}
//使用rawAdd执行插入,并更新index(如果需要)
unsigned addr = pos[2], offset = pos[3];
auto rawAddResult = rawAdd(cache, addr, offset);
unsigned newTupleIndex = (rawAddResult.second == addr ? pos[1] : pos[1] + 1);
if (-1 != rawAddResult.first)
{
//更新第一个返回值为原磁盘块索引下标
index.insert(index.begin() + pos[1] + 1, rawAddResult.first);
return { pos[1] + 1, newTupleIndex };
}
return { -1, newTupleIndex };
}
int SeqTable::remove(std::map<unsigned, unsigned char*> args)
{
std::vector<std::pair<unsigned char*, std::pair<unsigned, unsigned>>> rmvArgs;
for (auto p : args)
{
rmvArgs.push_back({ p.second, {cols[p.first - 1], cols[p.first] - cols[p.first-1]} });
}
unsigned char* bufBlk;
if (args.find(searchKey) != args.end())
{
auto pos = binarySearch(args[searchKey]);
if (pos[0] != 0)
{
return 0;
}
std::vector<unsigned> candidate{ pos[1] };
for (unsigned i = pos[1] - 1; i != -1; --i)
{
unsigned addr = index[i];
auto linPos = linearSerach(args[searchKey], cols[searchKey - 1], cols[searchKey] - cols[searchKey - 1], addr, 1);
if (linPos.first != 0)
break;
candidate.push_back(i);
}
for (unsigned i = pos[1] + 1; i < index.size(); ++i)
{
unsigned addr = index[i];
auto linPos = linearSerach(args[searchKey], cols[searchKey - 1], cols[searchKey] - cols[searchKey - 1], addr, 1);
if (linPos.first != 0)
break;
candidate.push_back(i);
}
int res = 0;
for (auto addr : candidate)
{
res += doRemove(bufBlk, rmvArgs, index[addr]);
//磁盘块为空,回收
if (*(unsigned*)bufBlk == 8)
{
if (!addr)
{
if (numBlk == 1)
{
continue;
}
startAddr = *(unsigned*)(bufBlk + 4);
}
else
{
unsigned char* blk = bm.read(index[addr - 1]);
*(unsigned*)(blk + 4) = getNextAddr(bufBlk);
bm.write(index[addr - 1]);
}
index.erase(index.begin() + addr);
--numBlk;
}
}
return res;
}
else
{
int res = 0;
unsigned addr = 0;
do {
res += doRemove(bufBlk, rmvArgs, index[addr]);
//磁盘块为空,回收
if (*(unsigned*)bufBlk == 8)
{
if (!addr)
{
if (numBlk == 1)
{
continue;
}
startAddr = index[1];
}
else
{
unsigned char* blk = bm.read(index[addr - 1]);
*(unsigned*)(blk + 4) = getNextAddr(bufBlk);
bm.write(index[addr - 1]);
}
index.erase(index.begin() + addr);
--numBlk;
}
else
{
++addr;
}
} while (addr != index.size());
return res;
}
}
std::vector<unsigned char*> SeqTable::select(std::map<unsigned, unsigned char*> args)
{
std::vector<std::pair<unsigned char*, std::pair<unsigned, unsigned>>> selArgs;
for (auto p : args)
{
selArgs.push_back({ p.second,{ cols[p.first - 1], cols[p.first] - cols[p.first-1] } });
}
if (args.find(searchKey) != args.end())
{
std::vector<unsigned char*> ret;
auto pos = binarySearch(args[searchKey]);
if (pos[0] != 0)
{
return ret;
}
std::vector<unsigned> candidateBlks{ pos[2] };
for (unsigned i = pos[1] - 1; i != -1; --i)
{
unsigned addr = index[i];
auto linPos = linearSerach(args[searchKey], cols[searchKey - 1], cols[searchKey] - cols[searchKey - 1], addr, 1);
if (linPos.first != 0)
break;
candidateBlks.push_back(addr);
}
for (unsigned i = pos[1] + 1; i < index.size(); ++i)
{
unsigned addr = index[i];
auto linPos = linearSerach(args[searchKey], cols[searchKey - 1], cols[searchKey] - cols[searchKey - 1], addr, 1);
if (linPos.first != 0)
break;
candidateBlks.push_back(addr);
}
unsigned char* bufBlk;
for (auto i : candidateBlks)
{
auto tmp = doSelect(bufBlk, selArgs, i);
ret.insert(ret.end(), tmp.begin(), tmp.end());
}
return ret;
}
else
{
return rawSelect(selArgs);
}
}
void SeqTable::save()
{
Table::save();
unsigned char* blk = bm.read(metaAddr);
*(bool*)(blk + 20) = cmp;
bm.write(metaAddr);
}
std::pair<int, std::pair<unsigned, unsigned>> SeqTable::linearSerach(unsigned char* cond, unsigned beg, unsigned len, unsigned from, unsigned steps)
{
if (from == -1)
{
from = startAddr;
}
unsigned addr = from; //当前地址
unsigned char *p = nullptr; //当前块内指针
unsigned char* blk = nullptr; //当前块
for (unsigned i = 0; addr != -1 && i < steps; addr = getNextAddr(blk))
{
blk = bm.read(addr);
for (p = blk + 8; p != blk + *(unsigned*)blk; p += tupleSize)
{
//注意小端存储问题!!!!!
//int res = memcmp(cond, p + beg, end - beg);
int res = cmp ? memcmp(cond, p + beg, len) : *(unsigned*)cond - *(unsigned*)(p + beg);
if (res <= 0)
{
return { res,{ addr, p - blk } };
}
}
if (getNextAddr(blk) == -1 || ++i == steps)
break;
}
return { 1,{ addr, p - blk } };
}
std::vector<unsigned> SeqTable::binarySearch(unsigned char * cond)
{
unsigned first = 0;
unsigned last = index.size() - 1;
std::vector<unsigned> ret(4, -1);
while (first <= last)
{
unsigned mid = first + (last - first) / 2;
unsigned mid_addr = index[mid];
auto res = linearSerach(cond, cols[searchKey-1], cols[searchKey] - cols[searchKey - 1], mid_addr, 1);
ret[0] = res.first;
ret[1] = mid;
ret[2] = mid_addr;
ret[3] = res.second.second;
if (res.first == 0)
{
break;
}
if (res.first > 0)
{
first = mid + 1;
}
else if (res.second.second == 8)
{
if (mid == 0)
break;
last = mid - 1;
}
else
{
break;
}
}
return ret;
}