-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.cpp
256 lines (243 loc) · 7.98 KB
/
API.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
#include "minisql.h"
#include "IndexManager.h"
#include "Interpreter.h"
#include "vector"
#include "string"
#include "API.h"
#include "algorithm"
int FetchAllFlag;
void API_Create_Table(string tablename,FieldTree *T)
{
// try{
//catalog meta data
Table tbl(tablename);
tbl.initializeTable(T);
tbl.createTable();
string primaryIdxName=tablename+"PRIMARY_INDEX";
Index newIndex(primaryIdxName);
IndexStruct Idx;
Idx.attr_name = tbl.getPrimaryKey();
Idx.index_name = primaryIdxName;
Idx.table_name = tablename;
newIndex.initializeIndex(&Idx);
newIndex.createIndex();
//End of catalog do
//IndexManager do
IndexInfo idxInfo;
idxInfo.indexname = Idx.table_name + "PRIMARY_INDEX";
idxInfo.attr_size=tbl.getPrimaryKeySize();
idxInfo.maxKeyNum=(BLOCKSIZE-INDEX_BLOCK_INFO-4)/(4+idxInfo.attr_size);
Create_Index_Init(idxInfo);
//End of IndexManager do
/* }
catch(...){
cout<<"Failed to Create Table."<<endl;
}*/
}
void API_Drop_Table(Table &tbl)
{
vector<string> idxList;
for(int i=0;i<tbl.getAttrNum();i++){
string tempstring = tbl.getAttrIndex(tbl.getAttr(i).attr_name);
if (tempstring.empty()){
continue;
}
Index droppedIndex(tempstring);
droppedIndex.dropIndex();
}
tbl.dropTable();
}
extern vector<TupleIndex_number> TupleList_IDX;
void API_Create_Index(IndexStruct I)
{
Index idx(I.index_name);
idx.initializeIndex(&I);
idx.createIndex();
Table tb(I.table_name);
tb.getTableInfo();
//Catalog do
Tuple tpl(I.table_name);
Attribute at = tb.getAttr(I.attr_name);
tpl.FetchAll(at.attr_id);
int listSize = TupleList_IDX.size();
IndexInfo idxInfo;
idxInfo.attr_size = at.attr_len;
idxInfo.indexname = I.index_name;
idxInfo.maxKeyNum = (4096 - 4 - INDEX_BLOCK_INFO) / (4+at.attr_len);
Create_Index(idxInfo);
cout << "Query OK, " << listSize;
if (listSize!=1)cout << " rows affected." << endl;
else cout << " row affected." << endl;
//Index do
}
void API_Drop_Index(string indexname)
{
Index idx(indexname);
idx.dropIndex();
}
void API_Select(Table tbl,list<Condition> &clist)
{
if (!tbl.isTableExist()){
//表不存在
throw Table_Index_Error("table", tbl.getTableName());
}
int flag=0;
if (clist.empty())flag = 1;
list<Condition> Have_Index_Condition;
list<Condition> No_Index_Condition;
list<Condition>::iterator it;
string IndexNameList[100];
int Attribute_Size[100];
int i=0;
for(it=clist.begin();it!=clist.end();it++){
Attribute_Size[i] = tbl.getAttr(it->compare_attr).attr_len;
IndexNameList[i++] = tbl.getAttrIndex(it->compare_attr);
if(!IndexNameList[i-1].empty()&&it->compare_type!=NOT_EQUAL){
Have_Index_Condition.push_back(*it);
}
else{
i--;
No_Index_Condition.push_back(*it);
}
}
i=0;
vector<int> finalOfstList;
vector<int> tempOfstList;
vector<int> temp2;
vector<int>::iterator endOfList;
for(it=Have_Index_Condition.begin();it!=Have_Index_Condition.end();it++){
if (it == Have_Index_Condition.begin()){
//cout << IndexNameList[i] << ' ' << Attribute_Size[i] << ' ' << tbl.getAttr((*it).compare_attr).attr_type << endl;
Index_Select_Position(*it, IndexNameList[i], finalOfstList, Attribute_Size[i], tbl.getAttr((*it).compare_attr).attr_type);
//Index_Select_Position(*it, IndexNameList[i], tempOfstList, Attribute_Size[i], tbl.getAttr((*it).compare_attr).attr_type);
i++;
}
else{
cout << IndexNameList[i] << ' ' << Attribute_Size[i] << ' ' << tbl.getAttr((*it).compare_attr).attr_type << endl;
Index_Select_Position(*it, IndexNameList[i], tempOfstList, Attribute_Size[i], tbl.getAttr((*it).compare_attr).attr_type);
i++;
sort(finalOfstList.begin(), finalOfstList.end());
sort(tempOfstList.begin(), tempOfstList.end());
endOfList = set_intersection(finalOfstList.begin(), finalOfstList.end(), tempOfstList.begin(), tempOfstList.end(), finalOfstList.begin());
finalOfstList.resize(endOfList - finalOfstList.begin());
sort(finalOfstList.begin(), finalOfstList.end());
finalOfstList.erase(unique(finalOfstList.begin(), finalOfstList.end()), finalOfstList.end());
}
if (finalOfstList.empty()){
cout << "Empty set." << endl;
return;
}
Block::flush_all_blocks();
}
Tuple selectTuple(tbl.getTableName());
if (flag)finalOfstList.push_back(0);//clist为空
else if (Have_Index_Condition.empty())finalOfstList.push_back(1);//clist不为空,且无索引信息
else if (finalOfstList.empty())finalOfstList.push_back(2);//正常,但是索引找到的list为空
else finalOfstList.push_back(3);//正常
selectTuple.Select(finalOfstList,No_Index_Condition);
selectTuple.printSelectResult();
}
void API_Insert(Table tbl,Tuple Element)
{
INT32 position;
if(Element.Insert(position)==false){
throw Conflict_Error(tbl.getTableName());
return;
}
//record do
for(int i=0;i<tbl.getAttrNum();i++){
IndexInfo idxInfo;
idxInfo.indexname = tbl.getAttrIndex(tbl.getAttr(i).attr_name);
if(!idxInfo.indexname.empty()){
idxInfo.attr_size=tbl.getAttr(i).attr_len;
idxInfo.maxKeyNum=(BLOCKSIZE-INDEX_BLOCK_INFO-4)/(4+idxInfo.attr_size);
API_Insert_Key_To_Index(idxInfo,Element.getTupleValue(i),position,tbl.getAttr(i).attr_type);
//index manager do
}
}
}
void API_Insert_Key_To_Index(IndexInfo idx,string Key,int position,int type)
{
TupleIndex Ti;
Ti.key = Key;
Ti.position = position;
Ti.type = type;
Insert_Key_To_Index(idx,Ti);
}
void API_Delete(Table tbl, list<Condition> &clist)
{
int flag = 0;
if (clist.empty())flag = 1;
list<Condition> Have_Index_Condition;
list<Condition> No_Index_Condition;
list<Condition>::iterator it;
string IndexNameList[100];
int Attribute_Size[100];
int i=0;
for(it=clist.begin();it!=clist.end();it++){
Attribute_Size[i] = tbl.getAttr(it->compare_attr).attr_len;
IndexNameList[i++] = tbl.getAttrIndex(it->compare_attr);
if(!IndexNameList[i-1].empty()&&it->compare_type!=NOT_EQUAL){
Have_Index_Condition.push_back(*it);
}
else{
i--;
No_Index_Condition.push_back(*it);
}
}
i=0;
vector<int> finalOfstList;
vector<int> tempOfstList;
vector<int>::iterator endOfList;
for(it=Have_Index_Condition.begin();it!=Have_Index_Condition.end();it++){
if (it == Have_Index_Condition.begin()){
Index_Select_Position(*it, IndexNameList[i], finalOfstList, Attribute_Size[i], tbl.getAttr((*it).compare_attr).attr_type);
i++;
}
else{
Index_Select_Position(*it,IndexNameList[i],tempOfstList,Attribute_Size[i],tbl.getAttr((*it).compare_attr).attr_type);
i++;
sort(finalOfstList.begin(), finalOfstList.end());
sort(tempOfstList.begin(), tempOfstList.end());
endOfList = set_intersection(finalOfstList.begin(), finalOfstList.end(), tempOfstList.begin(), tempOfstList.end(), finalOfstList.begin());
finalOfstList.resize(endOfList - finalOfstList.begin());
sort(finalOfstList.begin(), finalOfstList.end());
finalOfstList.erase(unique(finalOfstList.begin(), finalOfstList.end()), finalOfstList.end());
}
if(finalOfstList.empty()){
cout << "Query OK, 0 rows affected." << endl;
return;
}
}
Tuple selectTuple(tbl.getTableName());
if (flag)finalOfstList.push_back(0);//clist为空
else if (Have_Index_Condition.empty())finalOfstList.push_back(1);//clist不为空,且无索引信息
else if (finalOfstList.empty())finalOfstList.push_back(2);//正常,但是索引找到的list为空
else finalOfstList.push_back(3);//正常
int deleteNum=selectTuple.Delete(finalOfstList, No_Index_Condition);
cout << "Query OK, "<< deleteNum <<" rows affected." << endl;
}
void Index_Select_Position(Condition cond,string indexname,vector<int> &offsetList,int AttrSize,int type)
{
IndexInfo idxInfo;
idxInfo.indexname=indexname;
idxInfo.attr_size=AttrSize;
idxInfo.maxKeyNum=(BLOCKSIZE-INDEX_BLOCK_INFO-4)/(4+idxInfo.attr_size);
if(type ==INT){
int key;
key=atoi(cond.compare_value.c_str());
Find_Key_Offset_Index(idxInfo,key,offsetList,cond.compare_type);
}
else if(type==FLOAT){
float key;
key = atof(cond.compare_value.c_str());
Find_Key_Offset_Index(idxInfo,key,offsetList,cond.compare_type);
}
else if(type==CHAR){
char temp[256];
memset(temp, 0, 256);
memcpy(&temp, cond.compare_value.c_str(), AttrSize);
string key(temp);
Find_Key_Offset_Index(idxInfo,key,offsetList,cond.compare_type);
}
}