-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.cpp
More file actions
656 lines (575 loc) · 17.9 KB
/
API.cpp
File metadata and controls
656 lines (575 loc) · 17.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
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
#include "API.h"
#include "RecordManager.h"
#include "CatalogManager.h"
#include "IndexManager.h"
#define UNKNOWN_FILE 8
#define TABLE_FILE 9
#define INDEX_FILE 10
CatalogManager *cm;
IndexManager* im;
/**
*
* drop a table
* @param tableName: name of table
*/
void API::tableDrop(string tableName)
{
if (!tableExist(tableName)) return;
vector<string> indexNameVector;
//get all index in the table, and drop them all
indexNameListGet(tableName, &indexNameVector);
for (int i = 0; i < indexNameVector.size(); i++)
{
printf("%s", indexNameVector[i].c_str());
indexDrop(indexNameVector[i]);
}
//delete a table file
if(rm->tableDrop(tableName))
{
//delete a table information
cm->dropTable(tableName);
printf("Drop table %s successfully\n", tableName.c_str());
}
}
/**
*
* drop a index
* @param indexName: name of index
*/
void API::indexDrop(string indexName)
{
if (cm->findIndex(indexName) != INDEX_FILE)
{
printf("There is no index %s \n", indexName.c_str());
return;
}
//delete a index file
if (rm->indexDrop(indexName))
{
//get type of index
int indexType = cm->getIndexType(indexName);
if (indexType == -2)
{
printf("error\n");
return;
}
//delete a index information
cm->dropIndex(indexName);
//delete a index tree
im->dropIndex(rm->indexFileNameGet(indexName), indexType);
printf("Drop index %s successfully\n", indexName.c_str());
}
}
/**
*
* create a index
* @param indexName: name of index
* @param tableName: name of table
* @param attributeName: name of attribute in a table
*/
void API::indexCreate(string indexName, string tableName, string attributeName)
{
if (cm->findIndex(indexName) == INDEX_FILE)
{
cout << "There is index " << indexName << " already" << endl;
return;
}
if (!tableExist(tableName)) return;
vector<Attribute> attributeVector;
cm->attributeGet(tableName, &attributeVector);
int i;
int type = 0;
for (i = 0; i < attributeVector.size(); i++)
{
if (attributeName == attributeVector[i].name)
{
if (!attributeVector[i].ifUnique)
{
cout << "the attribute is not unique" << endl;
return;
}
type = attributeVector[i].type;
break;
}
}
if (i == attributeVector.size())
{
cout << "there is not this attribute in the table" << endl;
return;
}
//RecordManager to create a index file
if (rm->indexCreate(indexName))
{
//CatalogManager to add a index information
cm->addIndex(indexName, tableName, attributeName, type);
//get type of index
int indexType = cm->getIndexType(indexName);
if (indexType == -2)
{
cout << "error";
return;
}
//indexManager to create a index tress
im->createIndex(rm->indexFileNameGet(indexName), indexType);
//recordManager insert already record to index
rm->indexRecordAllAlreadyInsert(tableName, indexName);
printf("Create index %s successfully\n", indexName.c_str());
}
else
{
cout << "Create index " << indexName << " fail" << endl;
}
}
/**
*
* create a table
* @param tableName: name of table
* @param attributeVector: vector of attribute
* @param primaryKeyName: primary key of a table (default: "")
* @param primaryKeyLocation: the primary position in the table
*/
void API::tableCreate(string tableName, vector<Attribute>* attributeVector, string primaryKeyName,int primaryKeyLocation)
{
// cout << "=======api::tablecreate=======" << endl
// << "tableName: " << tableName << "; primaryKeyName: " << primaryKeyName << "; location: " << primaryKeyLocation << endl;
// for (int i = 0; i < (* attributeVector).size(); i++)
// {
// (* attributeVector)[i].print();
// }
if(cm->findTable(tableName) == TABLE_FILE)
{
cout << "There is a table " << tableName << " already" << endl;
return;
}
//RecordManager to create a table file
if(rm->tableCreate(tableName))
{
//CatalogManager to create a table information
cm->addTable(tableName, attributeVector, primaryKeyName, primaryKeyLocation);
printf("Create table %s successfully\n", tableName.c_str());
}
if (primaryKeyName != "")
{
//get a primary key
string indexName = primaryIndexNameGet(tableName);
indexCreate(indexName, tableName, primaryKeyName);
}
}
/**
*
* show all record of attribute in the table and the number of the record
* @param tableName: name of table
* @param attributeNameVector: vector of name of attribute
*/
void API::recordShow(string tableName, vector<string>* attributeNameVector)
{
vector<Condition> conditionVector;
recordShow(tableName, attributeNameVector, &conditionVector);
}
/**
*
* show the record matching the coditions of attribute in the table and the number of the record
* @param tableName: name of table
* @param attributeNameVector: vector of name of attribute
* @param conditionVector: vector of condition
*/
void API::recordShow(string tableName, vector<string>* attributeNameVector, vector<Condition>* conditionVector)
{
if (cm->findTable(tableName) == TABLE_FILE)
{
int num = 0;
vector<Attribute> attributeVector;
attributeGet(tableName, &attributeVector);
vector<string> allAttributeName;
if (attributeNameVector == NULL) {
for (Attribute attribute : attributeVector)
{
allAttributeName.insert(allAttributeName.end(), attribute.name);
}
attributeNameVector = &allAttributeName;
}
//print attribute name you want to show
tableAttributePrint(attributeNameVector);
for (string name : (*attributeNameVector))
{
int i = 0;
for (i = 0; i < attributeVector.size(); i++)
{
if (attributeVector[i].name == name)
{
break;
}
}
if (i == attributeVector.size())
{
cout << "the attribute which you want to print is not exist in the table" << endl;
return;
}
}
int blockOffset = -1;
if (conditionVector != NULL)
{
for (Condition condition : *conditionVector)
{
int i = 0;
for (i = 0; i < attributeVector.size(); i++)
{
if (attributeVector[i].name == condition.attributeName)
{
if (condition.operate == Condition::OPERATOR_EQUAL && attributeVector[i].index != "")
{
blockOffset = im->searchIndex(rm->indexFileNameGet(attributeVector[i].index), condition.value, attributeVector[i].type);
}
break;
}
}
if (i == attributeVector.size())
{
cout << "the attribute is not exist in the table" << endl;
return;
}
}
}
if (blockOffset == -1)
{
//cout << "if we con't find the block by index,we need to find all block" << endl;
num = rm->recordAllShow(tableName, attributeNameVector,conditionVector);
}
else
{
//find the block by index,search in the block
num = rm->recordBlockShow(tableName, attributeNameVector, conditionVector, blockOffset);
}
printf("%d records selected\n", num);
}
else
{
cout << "There is no table " << tableName << endl;
}
}
/**
*
* insert a record to a table
* @param tableName: name of table
* @param recordContent: Vector of these content of a record
*/
void API::recordInsert(string tableName, vector<string>* recordContent)
{
if (!tableExist(tableName)) return;
string indexName;
//deal if the record could be insert (if index is exist)
vector<Attribute> attributeVector;
vector<Condition> conditionVector;
attributeGet(tableName, &attributeVector);
for (int i = 0 ; i < attributeVector.size(); i++)
{
indexName = attributeVector[i].indexNameGet();
if (indexName != "")
{
//if the attribute has a index
int blockoffest = im->searchIndex(rm->indexFileNameGet(indexName), (*recordContent)[i], attributeVector[i].type);
if (blockoffest != -1)
{
//if the value has exist in index tree then fail to insert the record
cout << "insert fail because index value exist" << endl;
return;
}
}
else if (attributeVector[i].ifUnique)
{
//if the attribute is unique but not index
Condition condition(attributeVector[i].name, (*recordContent)[i], Condition::OPERATOR_EQUAL);
conditionVector.insert(conditionVector.end(), condition);
}
}
if (conditionVector.size() > 0)
{
for (int i = 0; i < conditionVector.size(); i++) {
vector<Condition> conditionTmp;
conditionTmp.insert(conditionTmp.begin(), conditionVector[i]);
int recordConflictNum = rm->recordAllFind(tableName, &conditionTmp);
if (recordConflictNum > 0) {
cout << "insert fail because unique value exist" << endl;
return;
}
}
}
char recordString[2000];
memset(recordString, 0, 2000);
//CatalogManager to get the record string
cm->recordStringGet(tableName, recordContent, recordString);
//RecordManager to insert the record into file; and get the position of block being insert
int recordSize = cm->calcuteLenth(tableName);
int blockOffset = rm->recordInsert(tableName, recordString, recordSize);
if(blockOffset >= 0)
{
recordIndexInsert(recordString, recordSize, &attributeVector, blockOffset);
cm->insertRecord(tableName, 1);
printf("insert record into table %s successful\n", tableName.c_str());
}
else
{
cout << "insert record into table " << tableName << " fail" << endl;
}
}
/**
*
* delete all record of table
* @param tableName: name of table
*/
void API::recordDelete(string tableName)
{
vector<Condition> conditionVector;
recordDelete(tableName, &conditionVector);
}
/**
*
* delete the record matching the coditions in the table
* @param tableName: name of table
* @param conditionVector: vector of condition
*/
void API::recordDelete(string tableName, vector<Condition>* conditionVector)
{
if (!tableExist(tableName)) return;
int num = 0;
vector<Attribute> attributeVector;
attributeGet(tableName, &attributeVector);
int blockOffset = -1;
if (conditionVector != NULL)
{
for (Condition condition : *conditionVector)
{
if (condition.operate == Condition::OPERATOR_EQUAL)
{
for (Attribute attribute : attributeVector)
{
if (attribute.index != "" && attribute.name == condition.attributeName)
{
blockOffset = im->searchIndex(rm->indexFileNameGet(attribute.index), condition.value, attribute.type);
}
}
}
}
}
if (blockOffset == -1)
{
//if we con't find the block by index,we need to find all block
num = rm->recordAllDelete(tableName, conditionVector);
}
else
{
//find the block by index,search in the block
num = rm->recordBlockDelete(tableName, conditionVector, blockOffset);
}
//delete the number of record in in the table
cm->deleteValue(tableName, num);
printf("delete %d record in table %s\n", num, tableName.c_str());
}
/**
*
* get the number of the records in table
* @param tableName: name of table
*/
int API::recordNumGet(string tableName)
{
if (!tableExist(tableName)) return 0;
return cm->getRecordNum(tableName);
}
/**
*
* get the size of a record in table
* @param tableName: name of table
*/
int API::recordSizeGet(string tableName)
{
if (!tableExist(tableName)) return 0;
return cm->calcuteLenth(tableName);
}
/**
*
* get the size of a type
* @param type: type of attribute
*/
int API::typeSizeGet(int type)
{
return cm->calcuteLenth2(type);
}
/**
*
* get the vector of a all name of index in the table
* @param tableName: name of table
* @param indexNameVector: a point to vector of indexName(which would change)
*/
int API::indexNameListGet(string tableName, vector<string>* indexNameVector)
{
if (!tableExist(tableName)) {
return 0;
}
return cm->indexNameListGet(tableName, indexNameVector);
}
/**
*
* get the vector of all name of index's file
* @param indexNameVector: will set all index's
*/
void API::allIndexAddressInfoGet(vector<IndexInfo> *indexNameVector)
{
cm->getAllIndex(indexNameVector);
for (int i = 0; i < (*indexNameVector).size(); i++)
{
(*indexNameVector)[i].indexName = rm->indexFileNameGet((*indexNameVector)[i].indexName);
}
}
/**
*
* get the vector of a attribute‘s type in a table
* @param tableName: name of table
* @param attributeNameVector: a point to vector of attributeType(which would change)
*/
int API::attributeGet(string tableName, vector<Attribute>* attributeVector)
{
if (!tableExist(tableName)) {
return 0;
}
return cm->attributeGet(tableName, attributeVector);
}
/**
*
* insert all index value of a record to index tree
* @param recordBegin: point to record begin
* @param recordSize: size of the record
* @param attributeVector: a point to vector of attributeType(which would change)
* @param blockOffset: the block offset num
*/
void API::recordIndexInsert(char* recordBegin,int recordSize, vector<Attribute>* attributeVector, int blockOffset)
{
char* contentBegin = recordBegin;
for (int i = 0; i < (*attributeVector).size() ; i++)
{
int type = (*attributeVector)[i].type;
int typeSize = typeSizeGet(type);
if ((*attributeVector)[i].index != "")
{
indexInsert((*attributeVector)[i].index, contentBegin, type, blockOffset);
}
contentBegin += typeSize;
}
}
/**
*
* insert a value to index tree
* @param indexName: name of index
* @param contentBegin: address of content
* @param type: the type of content
* @param blockOffset: the block offset num
*/
void API::indexInsert(string indexName, char* contentBegin, int type, int blockOffset)
{
string content= "";
stringstream tmp;//stringstream 主要是用在將一個字串分割
//if the attribute has index
///这里传*attributeVector)[i].index这个index的名字, blockOffset,还有值
if (type == Attribute::TYPE_INT)//0
{
int value = *((int*)contentBegin);//地址类型转换
tmp << value;//转化类型后地址,输进流
}
else if (type == Attribute::TYPE_FLOAT)//-1
{
float value = *((float* )contentBegin);
tmp << value;
}
else//正整数
{
char value[255];
memset(value, 0, 255);
memcpy(value, contentBegin, sizeof(type));//sizeof(type)?int的大小
string stringTmp = value;
tmp << stringTmp;
}
tmp >> content;
im->insertIndex(rm->indexFileNameGet(indexName), content, blockOffset, type);//content是key,key和value相映射
}
/**
*
* delete all index value of a record to index tree
* @param recordBegin: point to record begin
* @param recordSize: size of the record
* @param attributeVector: a point to vector of attributeType(which would change)
* @param blockOffset: the block offset num
*/
void API::recordIndexDelete(char* recordBegin,int recordSize, vector<Attribute>* attributeVector, int blockOffset)
{
char* contentBegin = recordBegin;
for (int i = 0; i < (*attributeVector).size() ; i++)
{
int type = (*attributeVector)[i].type;
int typeSize = typeSizeGet(type);
string content= "";
stringstream tmp;
if ((*attributeVector)[i].index != "")
{
//if the attribute has index
///这里传*attributeVector)[i].index这个index的名字, blockOffset,还有值
if (type == Attribute::TYPE_INT)
{
int value = *((int*)contentBegin);
tmp << value;
}
else if (type == Attribute::TYPE_FLOAT)
{
float value = *((float* )contentBegin);
tmp << value;
}
else
{
char value[255];
memset(value, 0, 255);
memcpy(value, contentBegin, sizeof(type));
string stringTmp = value;
tmp << stringTmp;
}
tmp >> content;
im->deleteIndexByKey(rm->indexFileNameGet((*attributeVector)[i].index), content, type);
}
contentBegin += typeSize;
}
}
/**
* get if the table
* @param tableName the name of the table
*/
int API::tableExist(string tableName)
{
if (cm->findTable(tableName) != TABLE_FILE)
{
cout << "There is no table " << tableName << endl;
return 0;
}
else
{
return 1;
}
}
/**
* get the primary index Name by table
* @param tableName : name of the table
*/
string API::primaryIndexNameGet(string tableName)
{
return "PRIMARY_" + tableName;
}
/**
* printe attribute name
* @param attributeNameVector: the vector of attribute's name
*/
void API::tableAttributePrint(vector<string>* attributeNameVector)
{
int i = 0;
for ( i = 0; i < (*attributeNameVector).size(); i++)
{
printf("%s ", (*attributeNameVector)[i].c_str());
}
if (i != 0)
printf("\n");
}