Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTupleDesc.java
More file actions
320 lines (248 loc) · 8.51 KB
/
Copy pathTupleDesc.java
File metadata and controls
320 lines (248 loc) · 8.51 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package simpledb;
import java.io.Serializable;
import java.util.*;
/**
* TupleDesc describes the schema of a tuple.
*/
public class TupleDesc implements Serializable {
private TDItem[] schema;
/**
* A help class to facilitate organizing the information of each field
* */
public static class TDItem implements Serializable {
private static final long serialVersionUID = 1L;
/**
* The type of the field
* */
public final Type fieldType;
/**
* The name of the field
* */
public final String fieldName;
public TDItem(Type t, String n) {
this.fieldName = n;
this.fieldType = t;
}
public String toString() {
return fieldName + "(" + fieldType + ")";
}
public boolean equals(Object o){
if(o instanceof TDItem)
{
TDItem item = (TDItem) o;
boolean field_types_eq = item.fieldType.equals(this.fieldType);
/*boolean field_names_eq = false;
if(item.fieldName != null && fieldName !=null)
field_names_eq = item.fieldName.equals(this.fieldName);
else if(item.fieldName == null && fieldName ==null)
field_names_eq = true;
if (field_names_eq && field_types_eq) return true;*/
return field_types_eq;
}
return false;
}
}
/**
* @return
* An iterator which iterates over all the field TDItems
* that are included in this TupleDesc
* */
public Iterator<TDItem> iterator() {
List<TDItem> td_list = Arrays.asList(schema);
return td_list.iterator();
}
private static final long serialVersionUID = 1L;
/**
* Create a new TupleDesc with typeAr.length fields with fields of the
* specified types, with associated named fields.
*
* @param typeAr
* array specifying the number of and types of fields in this
* TupleDesc. It must contain at least one entry.
* @param fieldAr
* array specifying the names of the fields. Note that names may
* be null.
*/
public TupleDesc(Type[] typeAr, String[] fieldAr) {
//System.out.println("TupleDesc()");
this.schema = new TDItem[typeAr.length];
for(int i=0; i< schema.length; i++)
{
//System.out.println("i is: "+i);
TDItem to_add = new TDItem(typeAr[i], fieldAr[i]);
schema[i] = to_add;
//System.out.println(schema[i]);
}
}
/**
* Constructor. Create a new tuple desc with typeAr.length fields with
* fields of the specified types, with anonymous (unnamed) fields.
*
* @param typeAr
* array specifying the number of and types of fields in this
* TupleDesc. It must contain at least one entry.
*/
public TupleDesc(Type[] typeAr) {
this.schema = new TDItem[typeAr.length];
for(int i = 0; i < schema.length; i++)
{
TDItem to_add = new TDItem(typeAr[i], null);
schema[i] = to_add;
}
}
/**
* @return the number of fields in this TupleDesc
*/
public int numFields() {
int output = schema.length;
return output;
}
/**
* Gets the (possibly null) field name of the ith field of this TupleDesc.
*
* @param i
* index of the field name to return. It must be a valid index.
* @return the name of the ith field
* @throws NoSuchElementException
* if i is not a valid field reference.
*/
public String getFieldName(int i) throws NoSuchElementException {
check_range(i); //throws error if out of range
TDItem my_TDItem = schema[i];
String field_name = my_TDItem.fieldName;
return field_name;
}
private int check_range(int i) throws NoSuchElementException {
if(i>=schema.length || i < 0)
{
System.out.println("Error: index: " + i + "-- out of range in schema.length: " + schema.length);
throw new NoSuchElementException();
}
return 0;
}
/**
* Gets the type of the ith field of this TupleDesc.
*
* @param i
* The index of the field to get the type of. It must be a valid
* index.
* @return the type of the ith field
* @throws NoSuchElementException
* if i is not a valid field reference.
*/
public Type getFieldType(int i) throws NoSuchElementException {
check_range(i);
TDItem my_TDItem = schema[i];
Type my_type = my_TDItem.fieldType;
//System.out.println(my_type.toString());
return my_type;
}
/**
* Find the index of the field with a given name.
*
* @param name
* name of the field.
* @return the index of the field that is first to have the given name.
* @throws NoSuchElementException
* if no field with a matching name is found.
*/
public int fieldNameToIndex(String name) throws NoSuchElementException {
for(int i = 0; i < schema.length; i++)
{
TDItem my_TDItem = schema[i];
String field_name = my_TDItem.fieldName;
if(field_name == null)
break;
if(field_name.equals(name))
return i;
}
throw new NoSuchElementException();
}
/**
* @return The size (in bytes) of tuples corresponding to this TupleDesc.
* Note that tuples from a given TupleDesc are of a fixed size.
*/
public int getSize() {
int size = 0;
for(int i = 0; i< schema.length; i++)
{
Type my_type = schema[i].fieldType;
int obj_size = my_type.getLen();
size += obj_size;
}
return size;
}
/**
* Merge two TupleDescs into one, with td1.numFields + td2.numFields fields,
* with the first td1.numFields coming from td1 and the remaining from td2.
*
* @param td1
* The TupleDesc with the first fields of the new TupleDesc
* @param td2
* The TupleDesc with the last fields of the TupleDesc
* @return the new TupleDesc
*/
public static TupleDesc merge(TupleDesc td1, TupleDesc td2) {
int new_len = td1.schema.length + td2.schema.length;
Type[] typeAr = new Type[new_len];
String[] fieldAr = new String[new_len];
for(int i=0; i< td1.schema.length; i++)
{
typeAr[i] = td1.schema[i].fieldType;
fieldAr[i] = td1.schema[i].fieldName;
}
for(int i = 0; i< td2.schema.length; i++)
{
typeAr[i+ td1.schema.length] = td2.schema[i].fieldType;
fieldAr[i+ td1.schema.length] = td2.schema[i].fieldName;
}
return new TupleDesc(typeAr, fieldAr);
}
/**
* Compares the specified object with this TupleDesc for equality. Two
* TupleDescs are considered equal if they are the same size and if the n-th
* type in this TupleDesc is equal to the n-th type in td.
*
* @param o
* the Object to be compared for equality with this TupleDesc.
* @return true if the object is equal to this TupleDesc.
*/
public boolean equals(Object o) {
if(o instanceof TupleDesc)
{
TupleDesc other = (TupleDesc) o;
if(schema.length != other.schema.length)
return false;
for(int i = 0; i < this.schema.length; i++)
{
if(! schema[i].equals(other.schema[i]))
return false;
}
return true;
}
else return false;
}
public int hashCode() {
// If you want to use TupleDesc as keys for HashMap, implement this so
// that equal objects have equals hashCode() results
throw new UnsupportedOperationException("unimplemented");
}
/**
* Returns a String describing this descriptor. It should be of the form
* "fieldType[0](fieldName[0]), ..., fieldType[M](fieldName[M])", although
* the exact format does not matter.
*
* @return String describing this descriptor.
*/
public String toString() {
String output = "";
for(int i= 0; i<schema.length; i++)
{
TDItem my_item = schema[i];
output += my_item.toString();
if(i+1 !=schema.length)
output += ", ";
}
return output;
}
}
You can’t perform that action at this time.
