-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordManager.cpp
More file actions
661 lines (592 loc) · 19.6 KB
/
RecordManager.cpp
File metadata and controls
661 lines (592 loc) · 19.6 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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//
// RecordManager.cpp
// minisql
//
// Created by 邓永辉 on 14/11/5.
// Copyright (c) 2014年 邓永辉. All rights reserved.
//
#include <iostream>
#include "RecordManager.h"
#include <cstring>
#include "API.h"
/**
*
* create a table
* @param tableName: name of table
*/
int RecordManager::tableCreate(string tableName)//放table的空文件创建,1表示成功,0表示失败
{
string tableFileName = tableFileNameGet(tableName);//设置创建后放table的table's file name叫做“TABLE_FILE_**" ,**:tableName
FILE *fp;
fp = fopen(tableFileName.c_str(), "w+");//创建一个放表的文件
//tableFileName,string类型,tableFileName.c_str()指向tableFileName字符串内容,
if (fp == NULL)
{
return 0;
}
fclose(fp);
return 1;
}
/**
*
* drop a table
* @param tableName: name of table
*/
int RecordManager::tableDrop(string tableName)//删除表:删除buffermanager的file node and its block node?,删除磁盘文件,成功返回1,失败返回0
{
string tableFileName = tableFileNameGet(tableName);
bm.delete_fileNode(tableFileName.c_str());//BufferManager bm,delete the file node and its block node
if (remove(tableFileName.c_str()))//remove函数删除指定文件,int remove(const char *_Filename);参数Filename为指定的要删除的文件名,成功则返回0,失败则返回-1
{
return 0;
}
return 1;
}
/**
*
* create a index
* @param indexName: name of index
*/
int RecordManager::indexCreate(string indexName)////放index的空文件创建,1表示成功,0表示失败
{
string indexFileName = indexFileNameGet(indexName);//INDEX_FILE_"+indexName
FILE *fp;
fp = fopen(indexFileName.c_str(), "w+");
if (fp == NULL)
{
return 0;
}
fclose(fp);
return 1;
}
/**
*
* drop a index
* @param indexName: name of index
*/
int RecordManager::indexDrop(string indexName)
{
string indexFileName = indexFileNameGet(indexName);
bm.delete_fileNode(indexFileName.c_str());
if (remove(indexFileName.c_str()))
{
return 0;
}
return 1;
}
/**
*
* insert a record to table
* @param tableName: name of table
* @param record: value of record
* @param recordSize: size of the record
* @return the position of block in the file(-1 represent error)
*/
int RecordManager::recordInsert(string tableName,char* record, int recordSize)//找tableName对应文件有足够空间的block的对应地址,拷贝要插入的内容,成功return the position of block in the file,-1 represent error
{
fileNode *ftmp = bm.getFile(tableFileNameGet(tableName).c_str());//获得名叫tableName的文件节点
blockNode *btmp = bm.getBlockHead(ftmp);
while (true)
{
if (btmp == NULL)
{
return -1;
}
if (bm.get_usingSize(*btmp) <= bm.getBlockSize() - recordSize)
{
char* addressBegin;
addressBegin = bm.get_content(*btmp) + bm.get_usingSize(*btmp);//块节点开始指针+已使用的
memcpy(addressBegin, record, recordSize);//
bm.set_usingSize(*btmp, bm.get_usingSize(*btmp) + recordSize);//更新该块节点已使用的空间
bm.set_dirty(*btmp);//标记为脏节点,要重新写入文件
return btmp->offsetNum;
}
else
{
btmp = bm.getNextBlock(ftmp, btmp);
}
}
return -1;
}
/**
*
* print all record of a table meet requirement
* @param tableName: name of table
* @param attributeNameVector: the attribute list
* @param conditionVector: the conditions list
* @return int: the number of the record meet requirements(-1 represent error)
*/
int RecordManager::recordAllShow(string tableName, vector<string>* attributeNameVector, vector<Condition>* conditionVector)
{
fileNode *ftmp = bm.getFile(tableFileNameGet(tableName).c_str());
blockNode *btmp = bm.getBlockHead(ftmp);
int count = 0;//count为所有块的recordblocknum之和
while (true)
{
if (btmp == NULL)
{
return -1;
}
if (btmp->ifbottom)//如果文件最后一块
{
int recordBlockNum = recordBlockShow(tableName,attributeNameVector, conditionVector, btmp);//btmp-块节点
count += recordBlockNum;
return count;
}
else//否则块节点要连接到下一个
{
int recordBlockNum = recordBlockShow(tableName, attributeNameVector, conditionVector, btmp);
count += recordBlockNum;
btmp = bm.getNextBlock(ftmp, btmp);
}
}
return -1;
}
/**
*
* print record of a table in a block
* @param tableName: name of table
* @param attributeNameVector: the attribute list
* @param conditionVector: the conditions list
* @param blockOffset: the block's offsetNum
* @return int: the number of the record meet requirements in the block(-1 represent error)
*/
int RecordManager::recordBlockShow(string tableName, vector<string>* attributeNameVector, vector<Condition>* conditionVector, int blockOffset)//显示特定偏移的块节点的所有符合记录
{
fileNode *ftmp = bm.getFile(tableFileNameGet(tableName).c_str());
blockNode* block = bm.getBlockByOffset(ftmp, blockOffset);//获得偏移为blockOffset的块节点
if (block == NULL)
{
return -1;
}
else
{
return recordBlockShow(tableName, attributeNameVector, conditionVector, block);
}
}
/**
*
* print record of a table in a block
* @param tableName: name of table
* @param attributeNameVector: the attribute list
* @param conditionVector: the conditions list
* @param block: search record in the block
* @return int: the number of the record meet requirements in the block(-1 represent error)
*/
int RecordManager::recordBlockShow(string tableName, vector<string>* attributeNameVector, vector<Condition>* conditionVector, blockNode* block)//显示文件单块的符合条件的记录,返回记录数目,-1 represent error
{
//if block is null, return -1
if (block == NULL)
{
return -1;
}
int count = 0;
char* recordBegin = bm.get_content(*block);//开始等于blockBegin
vector<Attribute> attributeVector;
int recordSize = api->recordSizeGet(tableName);//获得一条记录的大小
api->attributeGet(tableName, &attributeVector);//属性向量里赋值标的属性
char* blockBegin = bm.get_content(*block);//
size_t usingSize = bm.get_usingSize(*block);
while (recordBegin - blockBegin < usingSize)//每次recordBegin会移到下一条记录
{
//if the recordBegin point to a record
if(recordConditionFit(recordBegin, recordSize, &attributeVector, conditionVector))//是否满足条件
{
count ++;
recordPrint(recordBegin, recordSize, &attributeVector, attributeNameVector);//attributeVector含一个表的所有属性,attributeNameVector,用户输进去的属性列
printf("\n");
}
recordBegin += recordSize;
}
return count;
}
/**
*
* find the number of all record of a table meet requirement
* @param tableName: name of table
* @param conditionVector: the conditions list
* @return int: the number of the record meet requirements(-1 represent error)
*/
int RecordManager::recordAllFind(string tableName, vector<Condition>* conditionVector)
{
fileNode *ftmp = bm.getFile(tableFileNameGet(tableName).c_str());
blockNode *btmp = bm.getBlockHead(ftmp);
int count = 0;
while (true)
{
if (btmp == NULL)
{
return -1;
}
if (btmp->ifbottom)
{
int recordBlockNum = recordBlockFind(tableName, conditionVector, btmp);
count += recordBlockNum;
return count;
}
else
{
int recordBlockNum = recordBlockFind(tableName, conditionVector, btmp);
count += recordBlockNum;
btmp = bm.getNextBlock(ftmp, btmp);
}
}
return -1;
}
/**
*
* find the number of record of a table in a block
* @param tableName: name of table
* @param block: search record in the block
* @param conditionVector: the conditions list
* @return int: the number of the record meet requirements in the block(-1 represent error)
*/
int RecordManager::recordBlockFind(string tableName, vector<Condition>* conditionVector, blockNode* block)
{
//if block is null, return -1
if (block == NULL)
{
return -1;
}
int count = 0;
char* recordBegin = bm.get_content(*block);
vector<Attribute> attributeVector;
int recordSize = api->recordSizeGet(tableName);
api->attributeGet(tableName, &attributeVector);
while (recordBegin - bm.get_content(*block) < bm.get_usingSize(*block))
{
//if the recordBegin point to a record
if(recordConditionFit(recordBegin, recordSize, &attributeVector, conditionVector))
{
count++;
}
recordBegin += recordSize;
}
return count;
}
/**
*
* delete all record of a table meet requirement
* @param tableName: name of table
* @param conditionVector: the conditions list
* @return int: the number of the record meet requirements(-1 represent error)
*/
int RecordManager::recordAllDelete(string tableName, vector<Condition>* conditionVector)
{
fileNode *ftmp = bm.getFile(tableFileNameGet(tableName).c_str());
blockNode *btmp = bm.getBlockHead(ftmp);
int count = 0;
while (true)
{
if (btmp == NULL)
{
return -1;
}
if (btmp->ifbottom)
{
int recordBlockNum = recordBlockDelete(tableName, conditionVector, btmp);
count += recordBlockNum;
return count;
}
else
{
int recordBlockNum = recordBlockDelete(tableName, conditionVector, btmp);
count += recordBlockNum;
btmp = bm.getNextBlock(ftmp, btmp);
}
}
return -1;
}
/**
*
* delete record of a table in a block
* @param tableName: name of table
* @param conditionVector: the conditions list
* @param blockOffset: the block's offsetNum
* @return int: the number of the record meet requirements in the block(-1 represent error)
*/
int RecordManager::recordBlockDelete(string tableName, vector<Condition>* conditionVector, int blockOffset)
{
fileNode *ftmp = bm.getFile(tableFileNameGet(tableName).c_str());
blockNode* block = bm.getBlockByOffset(ftmp, blockOffset);
if (block == NULL)
{
return -1;
}
else
{
return recordBlockDelete(tableName, conditionVector, block);
}
}
/**
*
* delete record of a table in a block
* @param tableName: name of table
* @param conditionVector: the conditions list
* @param block: search record in the block
* @return int: the number of the record meet requirements in the block(-1 represent error)
*/
int RecordManager::recordBlockDelete(string tableName, vector<Condition>* conditionVector, blockNode* block)
{
//if block is null, return -1
if (block == NULL)
{
return -1;
}
int count = 0;
char* recordBegin = bm.get_content(*block);
vector<Attribute> attributeVector;
int recordSize = api->recordSizeGet(tableName);
api->attributeGet(tableName, &attributeVector);
while (recordBegin - bm.get_content(*block) < bm.get_usingSize(*block))
{
//if the recordBegin point to a record
if(recordConditionFit(recordBegin, recordSize, &attributeVector, conditionVector))
{
count ++;
api->recordIndexDelete(recordBegin, recordSize, &attributeVector, block->offsetNum);//删除相应索引值
int i = 0;
for (i = 0; i + recordSize + recordBegin - bm.get_content(*block) < bm.get_usingSize(*block); i++)
{
recordBegin[i] = recordBegin[i + recordSize];//此时recordbegin后的所有记录向前挪一个
}
memset(recordBegin + i, 0, recordSize);//将最后一个置为零
//void *memset(void *s,int c,size_t n)将已开辟内存空间 s 的首 n 个字节的值设为值 c
bm.set_usingSize(*block, bm.get_usingSize(*block) - recordSize);
bm.set_dirty(*block);//标记为脏块
}
else
{
recordBegin += recordSize;
}
}
return count;
}
/**
*
* insert the index of all record of the table
* @param tableName: name of table
* @param indexName: name of index
* @return int: the number of the record meet requirements(-1 represent error)
*/
int RecordManager::indexRecordAllAlreadyInsert(string tableName,string indexName)
{
fileNode *ftmp = bm.getFile(tableFileNameGet(tableName).c_str());
blockNode *btmp = bm.getBlockHead(ftmp);
int count = 0;
while (true)
{
if (btmp == NULL)
{
return -1;
}
if (btmp->ifbottom)
{
int recordBlockNum = indexRecordBlockAlreadyInsert(tableName, indexName, btmp);
count += recordBlockNum;
return count;
}
else
{
int recordBlockNum = indexRecordBlockAlreadyInsert(tableName, indexName, btmp);
count += recordBlockNum;
btmp = bm.getNextBlock(ftmp, btmp);
}
}
return -1;
}
/**
*
* insert the index of a record of a table in a block
* @param tableName: name of table
* @param indexName: name of index
* @param block: search record in the block
* @return int: the number of the record meet requirements in the block(-1 represent error)
*/
int RecordManager::indexRecordBlockAlreadyInsert(string tableName,string indexName, blockNode* block)//在一个块节点里,表名为**的每条记录检查是否需要建立indexName的索引
{
//if block is null, return -1
if (block == NULL)
{
return -1;
}
int count = 0;
char* recordBegin = bm.get_content(*block);
vector<Attribute> attributeVector;
int recordSize = api->recordSizeGet(tableName);
api->attributeGet(tableName, &attributeVector);
int type;
int typeSize;
char * contentBegin;
while (recordBegin - bm.get_content(*block) < bm.get_usingSize(*block))//一条记录一条记录地找
{
contentBegin = recordBegin;
//if the recordBegin point to a record
for (int i = 0; i < attributeVector.size(); i++)//对一条记录的每个属性操作
{
type = attributeVector[i].type;
typeSize = api->typeSizeGet(type);
//find the index of the record, and insert it to index tree
if (attributeVector[i].index == indexName)//default value is "", representing no index
{
api->indexInsert(indexName, contentBegin, type, block->offsetNum);
count++;
}
contentBegin += typeSize;//下一条属性
}
recordBegin += recordSize;//下一条记录
}
return count;
}
/**
*
* judge if the record meet the requirement
* @param recordBegin: point to a record
* @param recordSize: size of the record
* @param attributeVector: the attribute list of the record
* @param conditionVector: the conditions
* @return bool: if the record fit the condition
*/
bool RecordManager::recordConditionFit(char* recordBegin,int recordSize, vector<Attribute>* attributeVector,vector<Condition>* conditionVector)
{
if (conditionVector == NULL) {
return true;
}
int type;
string attributeName;
int typeSize;
char content[255];
char *contentBegin = recordBegin;
for(int i = 0; i < attributeVector->size(); i++)//一个一个属性操作
{
type = (*attributeVector)[i].type;//-1、0、正整数
attributeName = (*attributeVector)[i].name;
typeSize = api->typeSizeGet(type);
//init content (when content is string , we can get a string easily)
memset(content, 0, 255);
memcpy(content, contentBegin, typeSize);//拷贝从contentBegin开始的地址的一块内容值
for(int j = 0; j < (*conditionVector).size(); j++)//对每一个属性一个条件一个条件判断
{
if ((*conditionVector)[j].attributeName == attributeName)
{
//if this attribute need to deal about the condition
if(!contentConditionFit(content, type, &(*conditionVector)[j]))//利用condition->ifRight(tmp)判断
{
//if this record is not fit the conditon
return false;
}
}
}
contentBegin += typeSize;
}
return true;
}
/**
*
* print value of record
* @param recordBegin: point to a record
* @param recordSize: size of the record
* @param attributeVector: the attribute list of the record
* @param attributeVector: the name list of all attribute you want to print
*/
void RecordManager::recordPrint(char* recordBegin, int recordSize, vector<Attribute>* attributeVector, vector<string> *attributeNameVector)
{
int type;
string attributeName;
int typeSize;
char content[255];
char *contentBegin = recordBegin;
for(int i = 0; i < attributeVector->size(); i++)//表的属性遍历,一条条属性来
{
type = (*attributeVector)[i].type;
typeSize = api->typeSizeGet(type);//每个类型固定大小
//init content (when content is string , we can get a string easily)
memset(content, 0, 255);
memcpy(content, contentBegin, typeSize);//获取地址从contentBegin开始的一块内容
for(int j = 0; j < (*attributeNameVector).size(); j++)//attributeVector含一个表的所有属性,attributeNameVector,用户输进去的属性列
{
if ((*attributeNameVector)[j] == (*attributeVector)[i].name)
{
contentPrint(content, type);//type是一个不同输出时if函数判断条件
break;
}
}
contentBegin += typeSize;
}
}
/**
*
* print value of content
* @param content: point to content
* @param type: type of content
*/
void RecordManager::contentPrint(char * content, int type)
{
if (type == Attribute::TYPE_INT)
{
//if the content is a int
int tmp = *((int *) content); //get content value by point
printf("%d ", tmp);
}
else if (type == Attribute::TYPE_FLOAT)
{
//if the content is a float
float tmp = *((float *) content); //get content value by point
printf("%f ", tmp);
}
else
{
//if the content is a string
string tmp = content;
printf("%s ", tmp.c_str());
}
}
/**
*
* judge if the content meet the requirement
* @param content: point to content
* @param type: type of content
* @param condition: condition
* @return bool: the content if meet
*/
bool RecordManager::contentConditionFit(char* content,int type,Condition* condition)
{
if (type == Attribute::TYPE_INT)
{
//if the content is a int
int tmp = *((int *) content); //get content value by point
return condition->ifRight(tmp);
}
else if (type == Attribute::TYPE_FLOAT)
{
//if the content is a float
float tmp = *((float *) content); //get content value by point
return condition->ifRight(tmp);
}
else
{
//if the content is a string
return condition->ifRight(content);
}
return true;
}
/**
*
* get a index's file name
* @param indexName: name of index
*/
string RecordManager::indexFileNameGet(string indexName)
{
string tmp = "";
return "INDEX_FILE_"+indexName;
}
/**
*
* get a table's file name
* @param tableName: name of table
*/
string RecordManager::tableFileNameGet(string tableName)
{
string tmp = "";
return tmp + "TABLE_FILE_" + tableName;
}