-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcatalog.cpp
More file actions
194 lines (178 loc) · 4.99 KB
/
catalog.cpp
File metadata and controls
194 lines (178 loc) · 4.99 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
//
#include "catalog.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <pthread.h>
//
bool iicompare_pred(unsigned char a, unsigned char b)
{
return std::tolower(a) == std::tolower(b);
}
bool iicompare(std::string const& a, std::string const& b)
{
if (a.length()==b.length()) {
return std::equal(b.begin(), b.end(),
a.begin(), iicompare_pred);
}
else {
return false;
}
}
Catalog::Catalog() {
pthread_mutex_init(&m, NULL);
numOfTables = 0;
}
Catalog::~Catalog() {
pthread_mutex_destroy(&m);
}
bool Catalog::addTable(Table* newTable){
for (const auto& kv : tables){
if (iicompare(kv.first, newTable->getTableName())){
// there is an existing table
return false;
}
}
tables.insert(std::make_pair(newTable->getTableName(), newTable));
if (numOfTables == tables.size()) {
// failed to insert because duplicate exists
return false;
} else {
numOfTables = tables.size();
return true;
}
}
void Catalog::dropTable(std::string tn){
delete findTable(tn);
tables.erase(tn);
}
void Catalog::loadFromFile(std::string fileName){
// check if file is valid table schemas
printf("Loading catalog from file\n");
std::string line;
std::ifstream file (fileName);
if (file.is_open()){
while (getline(file, line)){
// process >> 6 << lines at a time
// if a line fails to be parsed correctly, the catalog is formatted wrong or contains wrong syntax (watch out for empty lines)
// a correctly formatted file has 6 lines in the following order.
// tablename=?
// columns=C1:INT,C2,CHAR(10)
// primary key=C1
// recordsize=?
// totalsize=?
// records=?
// case-sensitive
//std::cout << line.substr(10) << "111\n";
if (line.substr(0, 10) == "tablename="){
std::string nTableName = line.substr(10);
if (getline(file, line)){
if (line.substr(0, 8) == "columns="){
// columns=C1:INT,C2,CHAR(10)
// first take everything after the =
// split this string by ','
// into a vector
std::stringstream allColumns(line.substr(8));
if (getline(file, line)){
if (line.substr(0, 12) == "primary key="){
std::string nPrimaryKey = line.substr(12);
if (getline(file, line)){
if (line.substr(0, 11) == "recordsize="){
std::string nRecordSize = line.substr(11);
if (getline(file, line)){
if (line.substr(0, 10) == "totalsize="){
std::string nTotalSize = line.substr(10);
if (getline(file, line)){
if (line.substr(0, 8) == "records="){
std::string nRecords = line.substr(8);
// now build the table, and insert it into the catalog
Table* pTable = new Table(nTableName);
pTable->setPrimaryKey(nPrimaryKey);
// add columns
std::string segment;
while(std::getline(allColumns, segment, ',')){
pTable->addColumn(segment);
}
pTable->setRecordSize(std::stoi(nRecordSize));
pTable->setTotalSize(std::stoi(nTotalSize));
pTable->setNumOfRecords(std::stoi(nRecords));
if (addTable(pTable)){
//??? can't fail
} else {
delete pTable;
}
//pTable->print();
//delete pTable;
}
}
}
}
}
}
}
}
}
}
}
}
file.close();
}
}
void Catalog::writeToFile(std::string fileName){
// write the contents of each table in catalog to file in a specific format
// ignore tables that have the temporary and dropped flags
// format dictated in project specifiction
// copied below
// tableName=T
// columns=C1:INT,C2:CHAR(5),C3:INT
// primarykey=C1
// recordsize=13
// totalsize=65
// records=5
printf("Writing catalog to file\n");
std::ofstream ofs(fileName);
if (ofs.is_open()){
// for each table in the map
// print the schema of the table if the temporary and dropped flags are both false
for (const auto& kv : tables){
if (!kv.second->isTemporary() && !kv.second->isDropped()){
ofs << kv.second->getFormattedMetaData();
}
}
}
ofs.close();
}
bool Catalog::incrementRecordsInTable(std::string tn){
bool result=false;
pthread_mutex_lock(&m);
for (const auto& kv : tables)
if(kv.first == tn){
kv.second->incrementRecords();
result=true;
break;
}
pthread_mutex_unlock(&m);
return result;
}
void Catalog::showTable(std::string t) {
std::cout << t << std::endl;
for (const auto& kv : tables)
if(kv.first == t)
if (!kv.second->isDropped()) {
kv.second->print();
return;
}
std::cout << "No table found." << std::endl;
}
void Catalog::showTables() {
for (const auto& kv : tables)
if (!kv.second->isTemporary() && !kv.second->isDropped())
kv.second->print();
}
Table* Catalog::findTable(std::string t) {
auto it = tables.find(t);
if (it == tables.end())
return NULL;
return it->second;
}