-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcatalog.cpp
More file actions
219 lines (202 loc) · 4.58 KB
/
catalog.cpp
File metadata and controls
219 lines (202 loc) · 4.58 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
#include "catalog.h"
Catalog::Catalog()
:numOfTables(0)
{}
Catalog::~Catalog()
{}
/***************************INDEX****************************/
int Catalog::cat_check_index(const char* table,const char* index,const char*column)
{
Table &tb=tables[table];
if(tb.numOfIndex==0)
return 0;
if(tb.index_name.find(index)!=tb.index_name.end()){
return -1;
}
std::map<std::string,std::string>::iterator it=tb.index_name.begin();
for(;it!=tb.index_name.end();++it){
if(column==NULL){
return -3;
}
if(it->second==column){
return -2;
}
}
}
int Catalog::cat_check_index_col(const char*table, const char*col)
{
Table &tb=tables[table];
std::map<std::string,std::string>::iterator it;
for(it=tb.index_name.begin();it!=tb.index_name.end();it++){
if(it->second==col)
return 1;
}
return 0;
}
int Catalog::cat_create_index(const char* table,const char* index, const char* column)
{
Table &tb=tables[table];
int num=tb.numOfIndex;
if(num==2)
return 0;
if(num<2){
tb.index_pos[index]=tb.getAttrsIndex(column);
tb.index_name[index]=column;
tb.numOfIndex++;
return 1;
}
//else
return 0;
}
int* Catalog::get_index_attr(const char* table, int numOfIndex)
{
Table &tb=tables[table];
int*dat=new int[numOfIndex];
std::map<std::string,int>::iterator it;
it=tb.index_pos.begin();
int i=0;
for(;it!=tb.index_pos.end();it++){
dat[i]=it->second;
i++;
}
return dat;
}
int Catalog::cat_drop_index(const char* table, const char* index)
{
Table &tb=tables[table];
int num=tb.numOfIndex;
if(num==0)
return 0;
if(num>0){
tb.index_name.erase(index);
tb.index_pos.erase(index);
tb.numOfIndex--;
return 1;
}
}
void Catalog::cat_print_index()
{
std::map<std::string, Table>::iterator it;
for(it=tables.begin();it!=tables.end();it++){
Table &table = it->second;
if(table.numOfIndex==0)
std::cout<<"table "<<table.name<<" has no index"<<std::endl;
//else
std::map<std::string,std::string>::iterator itt;
for(itt=table.index_name.begin();itt!=table.index_name.end();itt++)
std::cout<<"table "<<table.name<<", index "<<itt->first<<" on "<<itt->second<<std::endl;
}
}
/********************* Index function end ************************/
int Catalog::add_rec(const char* table_name, const id_list_t * attrlist)
{
int count=0;
const char * ptr=table_name;
while(*ptr){
ptr++;
count++;
}
if(count>3){
std::cout <<"Table name longer than 3!\n";
return 0;
}
if(tables.find(table_name) != tables.end())
{
std::cout << "Table with the same name already existed!\n";
return 0;
}
//else
if(numOfTables < MAX_TABLES)
{
tables[table_name] = Table(table_name, attrlist);
numOfTables++;
return 1;
}
return 0;
}
// return the insertion position of the new record
int Catalog::add_rec_count(const char* table_name)
{
Table &tb=tables[table_name];
int recSize=tb.numOfAttrs*sizeof(int);
int curBlocks=ceil(tb.insertPos/48.0);
int inBlockSize=tb.insertPos%48+recSize;
int newInsertPos=0;
if(inBlockSize>48){
newInsertPos=curBlocks*48;
}else{
newInsertPos=tb.insertPos;
}
tb.insertPos=newInsertPos+recSize;
tb.numOfRec++;
return newInsertPos;
}
/**
* Return an 1 if the table exist, 0 if not
**/
int Catalog::search_table(const char * table_name)
{
if(tables.find(table_name)==tables.end())
return 0;
else
return 1;
}
/**
* Return an index that indicates the position of the attribute, or -1
* the attributes does not exit
**/
int Catalog::search_attr1(const char *table_name, const char * attr_name)
{
return tables[table_name].getAttrsIndex(attr_name);
}
/**
* Return a list of indices of the positions of the attribute,
* or NULL if there is one or more non-exist attributes
**/
int Catalog::search_attr2(const char* table_name, const id_list_t * attrlist, int * loc)
{
Table &table = tables[table_name];
const id_list_t * tmp_lst = attrlist;
int i = 0;
do{
int p = table.getAttrsIndex(tmp_lst->id);
if(p == -1){
return 0;
}
*loc=p;
loc++;
tmp_lst = tmp_lst -> next;
}while(tmp_lst!=NULL);
return 1;
}
int Catalog::get_num_rec(const char * table)
{
return tables[table].numOfRec;
}
int Catalog::get_num_attrs(const char * table)
{
return tables[table].numOfAttrs;
}
/**
* Return 1 if successfully delete, 0 otherwise
**/
int Catalog::del_rec(char *table_name)
{
if(search_table(table_name))
{
tables.erase(tables.find(table_name));
numOfTables--;
return 1;
}
else
return 0;
}
void Catalog::print_catalog()
{
std::cout << "There are " << numOfTables << " table(s) in the DB\n";
std::map<std::string, Table>::iterator it;
for(it=tables.begin(); it!=tables.end();it++){
((*it).second).print_table();
std::cout<<"\n";
}
}