- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.java
More file actions
Latest commit
157 lines (140 loc) · 5.1 KB
/
Copy pathUtility.java
File metadata and controls
157 lines (140 loc) · 5.1 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
packagesimpledb;
importjava.io.*;
importjava.util.ArrayList;
importjava.util.UUID;
/** Helper methods used for testing and implementing random features. */
publicclassUtility {
/**
* @return a Type array of length len populated with Type.INT_TYPE
*/
publicstaticType[] getTypes(intlen) {
Type[] types = newType[len];
for (inti = 0; i < len; ++i)
types[i] = Type.INT_TYPE;
returntypes;
}
/**
* @return a String array of length len populated with the (possibly null) strings in val,
* and an appended increasing integer at the end (val1, val2, etc.).
*/
publicstaticString[] getStrings(intlen, Stringval) {
String[] strings = newString[len];
for (inti = 0; i < len; ++i)
strings[i] = val + i;
returnstrings;
}
/**
* @return a TupleDesc with n fields of type Type.INT_TYPE, each named
* name + n (name1, name2, etc.).
*/
publicstaticTupleDescgetTupleDesc(intn, Stringname) {
returnnewTupleDesc(getTypes(n), getStrings(n, name));
}
/**
* @return a TupleDesc with n fields of type Type.INT_TYPE
*/
publicstaticTupleDescgetTupleDesc(intn) {
returnnewTupleDesc(getTypes(n));
}
/**
* @return a Tuple with a single IntField with value n and with
* RecordId(HeapPageId(1,2), 3)
*/
publicstaticTuplegetHeapTuple(intn) {
Tupletup = newTuple(getTupleDesc(1));
tup.setRecordId(newRecordId(newHeapPageId(1, 2), 3));
tup.setField(0, newIntField(n));
returntup;
}
/**
* @return a Tuple with an IntField for every element of tupdata
* and RecordId(HeapPageId(1, 2), 3)
*/
publicstaticTuplegetHeapTuple(int[] tupdata) {
Tupletup = newTuple(getTupleDesc(tupdata.length));
tup.setRecordId(newRecordId(newHeapPageId(1, 2), 3));
for (inti = 0; i < tupdata.length; ++i)
tup.setField(i, newIntField(tupdata[i]));
returntup;
}
/**
* @return a Tuple with a 'width' IntFields each with value n and
* with RecordId(HeapPageId(1, 2), 3)
*/
publicstaticTuplegetHeapTuple(intn, intwidth) {
Tupletup = newTuple(getTupleDesc(width));
tup.setRecordId(newRecordId(newHeapPageId(1, 2), 3));
for (inti = 0; i < width; ++i)
tup.setField(i, newIntField(n));
returntup;
}
/**
* @return a Tuple with a 'width' IntFields with the value tupledata[i]
* in each field.
* do not set it's RecordId, hence do not distinguish which
* sort of file it belongs to.
*/
publicstaticTuplegetTuple(int[] tupledata, intwidth) {
if(tupledata.length != width) {
System.out.println("get Hash Tuple has the wrong length~");
System.exit(1);
}
Tupletup = newTuple(getTupleDesc(width));
for (inti = 0; i < width; ++i)
tup.setField(i, newIntField(tupledata[i]));
returntup;
}
/**
* A utility method to create a new HeapFile with a single empty page,
* assuming the path does not already exist. If the path exists, the file
* will be overwritten. The new table will be added to the Catalog with
* the specified number of columns as IntFields.
*/
publicstaticHeapFilecreateEmptyHeapFile(Stringpath, intcols)
throwsIOException {
Filef = newFile(path);
// touch the file
FileOutputStreamfos = newFileOutputStream(f);
fos.write(newbyte[0]);
fos.close();
HeapFilehf = openHeapFile(cols, f);
HeapPageIdpid = newHeapPageId(hf.getId(), 0);
HeapPagepage = null;
try {
page = newHeapPage(pid, HeapPage.createEmptyPageData());
} catch (IOExceptione) {
// this should never happen for an empty page; bail;
thrownewRuntimeException("failed to create empty page in HeapFile");
}
hf.writePage(page);
returnhf;
}
/** Opens a HeapFile and adds it to the catalog.
*
* @param cols number of columns in the table.
* @param f location of the file storing the table.
* @return the opened table.
*/
publicstaticHeapFileopenHeapFile(intcols, Filef) {
// create the HeapFile and add it to the catalog
TupleDesctd = getTupleDesc(cols);
HeapFilehf = newHeapFile(f, td);
Database.getCatalog().addTable(hf, UUID.randomUUID().toString());
returnhf;
}
publicstaticHeapFileopenHeapFile(intcols, StringcolPrefix, Filef) {
// create the HeapFile and add it to the catalog
TupleDesctd = getTupleDesc(cols, colPrefix);
HeapFilehf = newHeapFile(f, td);
Database.getCatalog().addTable(hf, UUID.randomUUID().toString());
returnhf;
}
publicstaticStringlistToString(ArrayList<Integer> list) {
Stringout = "";
for (Integeri : list) {
if (out.length() > 0) out += "\t";
out += i;
}
returnout;
}
}