-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleDb.java
More file actions
99 lines (90 loc) · 3.78 KB
/
SimpleDb.java
File metadata and controls
99 lines (90 loc) · 3.78 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
package simpledb;
import java.io.*;
public class SimpleDb {
public static void main (String args[])
throws DbException, TransactionAbortedException, IOException {
// convert a file
if(args[0].equals("convert")) {
try {
if (args.length<3 || args.length>5){
System.err.println("Unexpected number of arguments to convert ");
return;
}
File sourceTxtFile=new File(args[1]);
File targetDatFile=new File(args[1].replaceAll(".txt", ".dat"));
int numOfAttributes=Integer.parseInt(args[2]);
Type[] ts = new Type[numOfAttributes];
char fieldSeparator=',';
if (args.length == 3)
for (int i=0;i<numOfAttributes;i++)
ts[i]=Type.INT_TYPE;
else {
String typeString=args[3];
String[] typeStringAr = typeString.split(",");
if (typeStringAr.length!=numOfAttributes)
{
System.err.println("The number of types does not agree with the number of columns");
return;
}
int index=0;
for (String s: typeStringAr) {
if (s.toLowerCase().equals("int"))
ts[index++]=Type.INT_TYPE;
else if (s.toLowerCase().equals("string"))
ts[index++]=Type.STRING_TYPE;
else {
System.err.println("Unknown type " + s);
return;
}
}
if (args.length==5)
fieldSeparator=args[4].charAt(0);
}
HeapFileEncoder.convert(sourceTxtFile,targetDatFile,
BufferPool.getPageSize(),numOfAttributes,ts,fieldSeparator);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (args[0].equals("print")) {
File tableFile = new File(args[1]);
int columns = Integer.parseInt(args[2]);
DbFile table = Utility.openHeapFile(columns, tableFile);
TransactionId tid = new TransactionId();
DbFileIterator it = table.iterator(tid);
if(null == it){
System.out.println("Error: method HeapFile.iterator(TransactionId tid) not yet implemented!");
} else {
it.open();
while (it.hasNext()) {
Tuple t = it.next();
System.out.println(t);
}
it.close();
}
}
else if (args[0].equals("parser")) {
// Strip the first argument and call the parser
String[] newargs = new String[args.length-1];
for (int i = 1; i < args.length; ++i) {
newargs[i-1] = args[i];
}
try {
//dynamically load Parser -- if it doesn't exist, print error message
Class<?> c = Class.forName("simpledb.Parser");
Class<?> s = String[].class;
java.lang.reflect.Method m = c.getMethod("main", s);
m.invoke(null, (java.lang.Object)newargs);
} catch (ClassNotFoundException cne) {
System.out.println("Class Parser not found -- perhaps you are trying to run the parser as a part of lab1?");
}
catch (Exception e) {
System.out.println("Error in parser.");
e.printStackTrace();
}
}
else {
System.err.println("Unknown command: " + args[0]);
System.exit(1);
}
}
}