-
Notifications
You must be signed in to change notification settings - Fork 0
/
Catalog.java
196 lines (179 loc) · 6.23 KB
/
Catalog.java
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
package simpledb;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
* The Catalog keeps track of all available tables in the database and their associated schemas. For now, this is a stub
* catalog that must be populated with tables by a user program before it can be used -- eventually, this should be
* converted to a catalog that reads a catalog table from disk.
*/
public class Catalog {
/**
* A map that associates the name of each table with the ID of that table.
*/
HashMap<String, Integer> name2tableID = new HashMap<String, Integer>();
/**
* A map that associates the ID of each table with the TupleDesc of that table.
*/
HashMap<Integer, TupleDesc> tableID2desc = new HashMap<Integer, TupleDesc>();
/**
* A map that associates the ID of each table with the DbFile of that table.
*/
HashMap<Integer, DbFile> tableID2dbFile = new HashMap<Integer, DbFile>();
/**
* Constructor. Creates a new, empty catalog.
*/
public Catalog() {
}
/**
* Add a new table to the catalog. This table's contents are stored in the specified DbFile.
*
* @param file
* the contents of the table to add; file.getId() is the identifier of this file/tupledesc param for the
* calls getTupleDesc and getFile
* @param name
* the name of the table -- may be an empty string. May not be null. If a name conflict exists, use the
* last table to be added as the table for a given name.
* @param pkeyField
* the name of the primary key field
*/
public void addTable(DbFile file, String name, String pkeyField) {
int tableID = file.getId();
name2tableID.put(name, tableID);
tableID2desc.put(tableID, file.getTupleDesc());
tableID2dbFile.put(tableID, file);
}
/**
* Add a new table to the catalog. This table's contents are stored in the specified DbFile.
*
* @param file
* the contents of the table to add; file.getId() is the identifier of this file/tupledesc param for the
* calls getTupleDesc and getFile
* @param name
* the name of the table -- may be an empty string. May not be null. If a name conflict exists, use the
* last table to be added as the table for a given name.
*/
public void addTable(DbFile file, String name) {
addTable(file, name, "");
}
/**
* Return the id of the table with a specified name,
*
* @throws NoSuchElementException
* if the table doesn't exist
*/
public int getTableId(String name) {
// some code goes here
return name2tableID.get(name);
//throw new UnsupportedOperationException("Implement this");
}
/**
* Returns the tuple descriptor (schema) of the specified table
*
* @param tableid
* The id of the table, as specified by the DbFile.getId() function passed to addTable
* @throws NoSuchElementException
* if the table doesn't exist
*/
public TupleDesc getTupleDesc(int tableid) throws NoSuchElementException {
// some code goes here
if(tableID2desc.containsKey(tableid)){ //check to see if the table exist
return tableID2desc.get(tableid);
}
throw new NoSuchElementException("Invalid table Id number");
//throw new UnsupportedOperationException("Implement this");
}
/**
* Returns the DbFile that can be used to read the contents of the specified table.
*
* @param tableid
* The id of the table, as specified by the DbFile.getId() function passed to addTable
* @throws NoSuchElementException
* if the table doesn't exist
*/
public DbFile getDbFile(int tableid) throws NoSuchElementException {
// some code goes here
if(tableID2dbFile.containsKey(tableid)){ //check to see if the table exists
return tableID2dbFile.get(tableid);
}
throw new NoSuchElementException("Invalid table ID number");
//throw new UnsupportedOperationException("Implement this");
}
/** Delete all tables from the catalog */
public void clear() {
name2tableID.clear();
tableID2desc.clear();
tableID2dbFile.clear();
}
public String getPrimaryKey(int tableid) {
// some code goes here
return null;
}
public Iterator<Integer> tableIdIterator() {
// some code goes here
return null;
}
public String getTableName(int id) {
// some code goes here
return null;
}
/**
* Reads the schema from a file and creates the appropriate tables in the database.
*
* @param catalogFile
*/
public void loadSchema(String catalogFile) {
String line = "";
try {
BufferedReader br = new BufferedReader(new FileReader(new File(catalogFile)));
try {
while ((line = br.readLine()) != null) {
// assume line is of the format name (field type, field type, ...)
String name = line.substring(0, line.indexOf("(")).trim();
// System.out.println("TABLE NAME: " + name);
String fields = line.substring(line.indexOf("(") + 1, line.indexOf(")")).trim();
String[] els = fields.split(",");
ArrayList<String> names = new ArrayList<String>();
ArrayList<Type> types = new ArrayList<Type>();
String primaryKey = "";
for (String e : els) {
String[] els2 = e.trim().split(" ");
names.add(els2[0].trim());
if (els2[1].trim().toLowerCase().equals("int"))
types.add(Type.INT_TYPE);
else if (els2[1].trim().toLowerCase().equals("string"))
types.add(Type.STRING_TYPE);
else {
System.out.println("Unknown type " + els2[1]);
System.exit(0);
}
if (els2.length == 3) {
if (els2[2].trim().equals("pk"))
primaryKey = els2[0].trim();
else {
System.out.println("Unknown annotation " + els2[2]);
System.exit(0);
}
}
}
Type[] typeAr = types.toArray(new Type[0]);
String[] namesAr = names.toArray(new String[0]);
TupleDesc t = new TupleDesc(typeAr, namesAr);
HeapFile tabHf = new HeapFile(new File(name + ".dat"), t);
addTable(tabHf, name, primaryKey);
System.out.println("Added table : " + name + " with schema " + t);
}
} finally {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
} catch (IndexOutOfBoundsException e) {
System.out.println("Invalid catalog entry : " + line);
System.exit(0);
}
}
}