-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecordManager.cpp
477 lines (425 loc) · 20 KB
/
RecordManager.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//
// RecordManager.cpp
// MiniSQL
//
// Created by 顾秀烨 on 10/20/15.
// Copyright © 2015 laoreja. All rights reserved.
//
#include <fcntl.h>
#include <sys/stat.h>
#include "BufferManager.hpp"
#include "RecordManager.hpp"
#include "sqlstruct.h"
using namespace sqlstruct;
void RecordManager::createTableFile(string tableName){
int fd;
if((fd = open(tableName.c_str(), O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH)) == -1){
fprintf(stderr, "create file error: %s", tableName.c_str());
throw createFileError;
}
if (close(fd)) {
fprintf(stderr, "close file error: %s", tableName.c_str());
throw closeFileError;
}
// file head write
unsigned int recordCount = 0;
recordPointer emptyListHead(0, recordStartPos);
bm.writeBuffer(tableName, 0, &recordCount, RCPos, sizeof(unsigned int));
bm.writeBuffer(tableName, 0, &emptyListHead, ELHeadPos, sizeof(recordPointer));
}
void RecordManager::deleteTableFile(string tableName){
bm.deleteFileFromBuffer(tableName);
if (remove(tableName.c_str())) {
fprintf(stderr, "remove file error: %s", tableName.c_str());
throw removeFileError;
}
}
vector<indexPair> RecordManager::returnIndexInfo(string tableName, int attr_pos, int attrType, int recordContentSize){
int recordSizeInFile = recordContentSize + recordPrefixLen;
vector<indexPair> res;
unsigned int recordCount;
bm.constReadBuffer(tableName, 0, &recordCount, RCPos, sizeof(unsigned int));
recordPointer currentPointer(0, recordStartPos);
short deleteBit;
bm.constReadBuffer(tableName, currentPointer.blockNum, &deleteBit, currentPointer.offset+deleteBitOffset, sizeof(deleteBit));
while (deleteBit) {
currentPointer.offset += recordSizeInFile;
if (currentPointer.offset >= BUFFERSIZE) {
currentPointer.blockNum++;
currentPointer.offset = 0;
}
bm.constReadBuffer(tableName, currentPointer.blockNum, &deleteBit, currentPointer.offset+deleteBitOffset, sizeof(deleteBit));
}
switch (attrType) {
case INTNUM:
{
int tmpValue;
for (int i = 0; i < recordCount; i++) {
bm.constReadBuffer(tableName, currentPointer.blockNum, &tmpValue, currentPointer.offset+recordPrefixLen+attr_pos, sizeof(int));
res.push_back(indexPair(to_string(tmpValue), currentPointer));
bm.constReadBuffer(tableName, currentPointer.blockNum, ¤tPointer, currentPointer.offset+nextOffset, sizeof(recordPointer));
}
break;
}
case FLOATNUM:
{
float tmpValue;
for (int i = 0; i < recordCount; i++) {
bm.constReadBuffer(tableName, currentPointer.blockNum, &tmpValue, currentPointer.offset+recordPrefixLen+attr_pos, sizeof(float));
res.push_back(indexPair(to_string(tmpValue), currentPointer));
bm.constReadBuffer(tableName, currentPointer.blockNum, ¤tPointer, currentPointer.offset+nextOffset, sizeof(recordPointer));
}
break;
}
default:
{
char* tmpValue = new char[attrType - CHAR];
for (int i = 0; i < recordCount; i++) {
bm.constReadBuffer(tableName, currentPointer.blockNum, tmpValue, currentPointer.offset+recordPrefixLen+attr_pos, attrType-CHAR);
res.push_back(indexPair(string(tmpValue), currentPointer));
bm.constReadBuffer(tableName, currentPointer.blockNum, ¤tPointer, currentPointer.offset+nextOffset, sizeof(recordPointer));
}
delete[] tmpValue;
break;
}
}
return res;
}
recordPointer RecordManager::insertRecords(string tableName, vector<insertitem>& recordContent, int recordSize){
recordPointer next;
recordPointer last;
recordPointer current;
short tempDeleteBit; // 1: deleted
recordSize += recordPrefixLen;
//read file head
unsigned int recordCount;
recordPointer emptyListHead;
bm.constReadBuffer(tableName, 0, &recordCount, RCPos, sizeof(unsigned int));
bm.constReadBuffer(tableName, 0, &emptyListHead, ELHeadPos, sizeof(recordPointer));
current.blockNum = emptyListHead.blockNum;
current.offset = emptyListHead.offset;
//set the new empty list head
bm.constReadBuffer(tableName, current.blockNum, &tempDeleteBit, current.offset+deleteBitOffset, sizeof(short));
if (tempDeleteBit){
bm.constReadBuffer(tableName, current.blockNum, &emptyListHead, current.offset+nextOffset, sizeof(recordPointer));
}else {
//current node is the first node of the empty list.
// and is the last node in record list
emptyListHead.blockNum = current.blockNum;
emptyListHead.offset = current.offset + recordSize;
if (emptyListHead.offset + recordSize >= BUFFERSIZE) {
emptyListHead.blockNum += 1;
emptyListHead.offset = 0;
}
}
bm.writeBuffer(tableName, 0, &emptyListHead, ELHeadPos, sizeof(recordPointer)); //store the new empty list head
if (recordCount == 0) {
last = current;
next = current;
} else {
if (tempDeleteBit) {
next = current;
do{
next.offset += recordSize;
if (next.offset >= BUFFERSIZE) {
next.blockNum += 1;
next.offset = 0;
}
bm.constReadBuffer(tableName, next.blockNum, &tempDeleteBit, next.offset+deleteBitOffset, sizeof(short));
}while(tempDeleteBit);
} else{
next.blockNum = current.blockNum;
next.offset = current.offset + recordSize;
if (next.offset >= BUFFERSIZE) {
next.blockNum++;
next.offset = 0;
}
}
bm.constReadBuffer(tableName, next.blockNum, &last, next.offset+lastOffset, sizeof(recordPointer));
if (last.blockNum == 0 && last.offset == 0) {
//the next node is wrong, need to search from the start.
next.blockNum = 0; next.offset = recordStartPos;
bm.constReadBuffer(tableName, next.blockNum, &tempDeleteBit, next.offset+deleteBitOffset, sizeof(short));
while (tempDeleteBit) {
next.offset += recordSize;
if (next.offset >= BUFFERSIZE) {
next.blockNum ++;
next.offset = 0;
}
bm.constReadBuffer(tableName, next.blockNum, &tempDeleteBit, next.offset+deleteBitOffset, sizeof(short));
}
bm.constReadBuffer(tableName, next.blockNum, &last, next.offset+lastOffset, sizeof(recordPointer));
}
}
short deleteBit = 0;
bm.writeBuffer(tableName, current.blockNum, &deleteBit, current.offset+deleteBitOffset, sizeof(short));
int tempPos = recordPrefixLen;
for (int i = 0; i < recordContent.size(); i++) {
int valueSize = recordContent[i].size();
switch (recordContent[i].data_type) {
case INTNUM:
{
int iValue = recordContent[i].getInt();
bm.writeBuffer(tableName, current.blockNum, &iValue, current.offset+tempPos, sizeof(int));
break;
}
case FLOATNUM:
{
float fValue = recordContent[i].getFloat();
bm.writeBuffer(tableName, current.blockNum, &fValue, current.offset+tempPos, sizeof(float));
break;
}
default:
{
char* cNValue = new char[valueSize];
strcpy(cNValue, recordContent[i].getCharN());
bm.writeBuffer(tableName, current.blockNum, cNValue, current.offset+tempPos, valueSize);
delete[] cNValue;
break;
}
}
tempPos += valueSize;
}
bm.writeBuffer(tableName, current.blockNum, &next, current.offset+nextOffset, sizeof(recordPointer));
bm.writeBuffer(tableName, current.blockNum, &last, current.offset+lastOffset, sizeof(recordPointer));
bm.writeBuffer(tableName, last.blockNum, ¤t, last.offset+nextOffset, sizeof(recordPointer));
bm.writeBuffer(tableName, next.blockNum, ¤t, next.offset+lastOffset, sizeof(recordPointer));
recordCount++;
bm.writeBuffer(tableName, 0, &recordCount, RCPos, sizeof(unsigned int));
return current;
}
vector<vector<string> > RecordManager::deleteRecords(string tableName, int recordSize, const vector<condition>& conditions, const vector<int>& attrPositions, const vector<int>& attrTypes){
recordPointer next;
recordPointer last;
recordPointer ELHead;
unsigned int recordCount;
short deleteBit = 1;
vector<vector<string> > res;
vector<recordPointer> deleteList = select(tableName, recordSize, conditions, true, attrPositions, attrTypes, res);
bm.constReadBuffer(tableName, 0, &recordCount, RCPos, sizeof(unsigned int));
for (int i = 0; i < deleteList.size(); i++) {
recordCount--;
bm.writeBuffer(tableName, deleteList[i].blockNum, &deleteBit, deleteList[i].offset+deleteBitOffset, sizeof(short));
if (recordCount){
bm.constReadBuffer(tableName, deleteList[i].blockNum, &next, deleteList[i].offset+nextOffset, sizeof(recordPointer));
bm.constReadBuffer(tableName, deleteList[i].blockNum, &last, deleteList[i].offset+lastOffset, sizeof(recordPointer));
bm.writeBuffer(tableName, last.blockNum, &next, last.offset+nextOffset, sizeof(recordPointer));
bm.writeBuffer(tableName, next.blockNum, &last, next.offset+lastOffset, sizeof(recordPointer));
}
bm.constReadBuffer(tableName, 0, &ELHead, ELHeadPos, sizeof(recordPointer));
bm.writeBuffer(tableName, deleteList[i].blockNum, &ELHead, deleteList[i].offset+nextOffset, sizeof(recordPointer));
bm.writeBuffer(tableName, 0, &(deleteList[i]), ELHeadPos, sizeof(recordPointer));
}
bm.writeBuffer(tableName, 0, &recordCount, RCPos, sizeof(unsigned int));
return res;
}
vector<recordPointer> RecordManager::select(string tableName, int recordSize, const vector<condition>& conditions, bool returnDeleteKey, const vector<int>& attrPositions, const vector<int>& attrTypes, vector<vector<string> >& deleteKeys){
vector<recordPointer> res;
recordSize += recordPrefixLen;
recordPointer tmp(0, recordStartPos);
insertitem tmpValue;
unsigned int tableTotalSize;
bm.constReadBuffer(tableName, 0, &tableTotalSize, RCPos, sizeof(unsigned int));
short deleteBit;
bm.constReadBuffer(tableName, tmp.blockNum, &deleteBit, tmp.offset+deleteBitOffset, sizeof(short));
while (deleteBit) {
tmp.offset+=recordSize;
if (tmp.offset >= BUFFERSIZE) {
tmp.blockNum += 1;
tmp.offset = 0;
}
bm.constReadBuffer(tableName, tmp.blockNum, &deleteBit, tmp.offset+deleteBitOffset, sizeof(short));
}
if (conditions.size() == 0) {
for (int j = 0; j < tableTotalSize ; j++) {
res.push_back(tmp);
vector<string> oneLineDeleteKeys;
if (returnDeleteKey) {
for (int k = 0; k < attrPositions.size(); k++) {
int attrType = attrTypes[k];
int attrPosition = attrPositions[k];
switch (attrType) {
case INTNUM:
{
int tmpIntForDeleteKey;
bm.constReadBuffer(tableName, tmp.blockNum, &tmpIntForDeleteKey, tmp.offset+recordPrefixLen+attrPosition, sizeof(int));
oneLineDeleteKeys.push_back(to_string(tmpIntForDeleteKey));
break;
}
case FLOATNUM:{
float tmpFloatForDeleteKey;
bm.constReadBuffer(tableName, tmp.blockNum, &tmpFloatForDeleteKey, tmp.offset+recordPrefixLen+attrPosition, sizeof(float));
oneLineDeleteKeys.push_back(to_string(tmpFloatForDeleteKey));
break;
}
default:
{
char* tmpCharNForDeleteKey = new char[attrType - CHAR];
bm.constReadBuffer(tableName, tmp.blockNum, tmpCharNForDeleteKey, tmp.offset+recordPrefixLen+attrPosition, attrType-CHAR);
oneLineDeleteKeys.push_back(string(tmpCharNForDeleteKey));
break;
}
}
}
deleteKeys.push_back(oneLineDeleteKeys);
}
bm.constReadBuffer(tableName, tmp.blockNum, &tmp, tmp.offset, sizeof(recordPointer));
}
return res;
}
int i = 0;
for (int j = 0; j < tableTotalSize; j++) {
if (conditions.size() > 0) {
switch (conditions[i].value.data_type) {
case INTNUM:
{
int iValue;
bm.constReadBuffer(tableName, tmp.blockNum, &iValue, tmp.offset+recordPrefixLen+conditions[i].attr_startPos, sizeof(int));
tmpValue.data_type = INTNUM;
tmpValue.value = to_string(iValue);
break;
}
case FLOATNUM:
{
float fValue;
bm.constReadBuffer(tableName, tmp.blockNum, &fValue, tmp.offset+recordPrefixLen+conditions[i].attr_startPos, sizeof(float));
tmpValue.data_type = FLOATNUM;
tmpValue.value = to_string(fValue);
break;
}
default:
{
char* cValue = new char[conditions[i].value.size()];
bm.constReadBuffer(tableName, tmp.blockNum, cValue, tmp.offset+recordPrefixLen+conditions[i].attr_startPos, conditions[i].value.size());
tmpValue.data_type = CHAR + conditions[i].value.size();
tmpValue.value = string(cValue);
delete[] cValue;
break;
}
}
}
if (conditions.size() == 0 || tmpValue.operation(conditions[i].op, conditions[i].value)) {
res.push_back(tmp);
vector<string> oneLineDeleteKeys;
if (returnDeleteKey) {
for (int k = 0; k < attrPositions.size(); k++) {
int attrType = attrTypes[k];
int attrPosition = attrPositions[k];
switch (attrType) {
case INTNUM:
{
int tmpIntForDeleteKey;
bm.constReadBuffer(tableName, tmp.blockNum, &tmpIntForDeleteKey, tmp.offset+recordPrefixLen+attrPosition, sizeof(int));
oneLineDeleteKeys.push_back(to_string(tmpIntForDeleteKey));
break;
}
case FLOATNUM:{
float tmpFloatForDeleteKey;
bm.constReadBuffer(tableName, tmp.blockNum, &tmpFloatForDeleteKey, tmp.offset+recordPrefixLen+attrPosition, sizeof(float));
oneLineDeleteKeys.push_back(to_string(tmpFloatForDeleteKey));
break;
}
default:
{
char* tmpCharNForDeleteKey = new char[attrType - CHAR];
bm.constReadBuffer(tableName, tmp.blockNum, tmpCharNForDeleteKey, tmp.offset+recordPrefixLen+attrPosition, attrType-CHAR);
oneLineDeleteKeys.push_back(string(tmpCharNForDeleteKey));
break;
}
}
}
deleteKeys.push_back(oneLineDeleteKeys);
}
}
bm.constReadBuffer(tableName, tmp.blockNum, &tmp, tmp.offset+nextOffset, sizeof(recordPointer));
}
if (conditions.size() == 0) {
return res;
}
for (i = 1; i < conditions.size(); i++) {
vector<recordPointer>::iterator it = res.begin();
vector<vector<string> >::iterator deleteKeysIt = deleteKeys.begin();
int originalSize = (int)res.size();
for (int k = 0; k < originalSize; k++) {
switch (conditions[i].value.data_type) {
case INTNUM:
{
int iValue;
bm.constReadBuffer(tableName, it->blockNum, &iValue, it->offset+recordPrefixLen+conditions[i].attr_startPos, sizeof(int));
tmpValue.data_type = INTNUM;
tmpValue.value = to_string(iValue);
break;
}
case FLOATNUM:
{
float fValue;
bm.constReadBuffer(tableName, it->blockNum, &fValue, it->offset+recordPrefixLen+conditions[i].attr_startPos, sizeof(float));
tmpValue.data_type = FLOATNUM;
tmpValue.value = to_string(fValue);
break;
}
default:
{
char* cValue = new char[conditions[i].value.size()];
bm.constReadBuffer(tableName, it->blockNum, cValue, it->offset+recordPrefixLen+conditions[i].attr_startPos, conditions[i].value.size());
tmpValue.data_type = CHAR + conditions[i].value.size();
tmpValue.value = string(cValue);
delete[] cValue;
break;
}
}
if (!tmpValue.operation(conditions[i].op, conditions[i].value)) {
it = res.erase(it);
if (returnDeleteKey) {
deleteKeysIt = deleteKeys.erase(deleteKeysIt);
}
}else{
it++;
if (returnDeleteKey) {
deleteKeysIt++;
}
}
}
}
return res;
}
vector<vector<string> > RecordManager::selectRecords(string tablename, int recordSize, const vector<condition>& conditions, const vector<int>& attrTypes){
vector<vector<string> > res;
vector<vector<string> > noUseStrVecVec;
vector<recordPointer> resPointers = select(tablename, recordSize, conditions, false, vector<int>(), vector<int>(), noUseStrVecVec);
for (int i = 0; i < resPointers.size(); i++) {
vector<string> lineRes;
int tempPos = recordPrefixLen;
for (int j = 0; j < attrTypes.size(); j++) {
switch (attrTypes[j]) {
case INTNUM:
{
int iValue;
bm.constReadBuffer(tablename, resPointers[i].blockNum, &iValue, resPointers[i].offset+tempPos, sizeof(int));
lineRes.push_back(to_string(iValue));
tempPos += sizeof(int);
break;
}
case FLOATNUM:
{
float fValue;
bm.constReadBuffer(tablename, resPointers[i].blockNum, &fValue, resPointers[i].offset+tempPos, sizeof(float));
lineRes.push_back(to_string(fValue));
tempPos += sizeof(float);
break;
}
default:
{
char* cValue = new char[attrTypes[j] - CHAR];
bm.constReadBuffer(tablename, resPointers[i].blockNum, cValue, resPointers[i].offset+tempPos, attrTypes[j] - CHAR);
lineRes.push_back(string(cValue));
tempPos += attrTypes[j] - CHAR;
delete[] cValue;
break;
}
}
}
res.push_back(lineRes);
}
return res;
}