-
Notifications
You must be signed in to change notification settings - Fork 15
/
Tuple.java
139 lines (123 loc) · 3.18 KB
/
Tuple.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
package simpledb;
import java.io.Serializable;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Tuple maintains information about the contents of a tuple. Tuples have a
* specified schema specified by a TupleDesc object and contain Field objects
* with the data for each field.
*/
public class Tuple implements Serializable {
private static final long serialVersionUID = 1L;
private TupleDesc td;
private Field[] fields;
private RecordId rid;
/**
* Create a new tuple with the specified schema (type).
*
* @param td
* the schema of this tuple. It must be a valid TupleDesc
* instance with at least one field.
*/
public Tuple(TupleDesc td) {
// Done
this.td = td;
fields = new Field[td.numFields()];
}
/**
* @return The TupleDesc representing the schema of this tuple.
*/
public TupleDesc getTupleDesc() {
// Done
return td;
}
/**
* @return The RecordId representing the location of this tuple on disk. May
* be null.
*/
public RecordId getRecordId() {
// Done
return rid;
}
/**
* Set the RecordId information for this tuple.
*
* @param rid
* the new RecordId for this tuple.
*/
public void setRecordId(RecordId rid) {
// Done
this.rid = rid;
}
/**
* Change the value of the ith field of this tuple.
*
* @param i
* index of the field to change. It must be a valid index.
* @param f
* new value for the field.
*/
public void setField(int i, Field f) {
// Done
fields[i] = f;
}
/**
* @return the value of the ith field, or null if it has not been set.
*
* @param i
* field index to return. Must be a valid index.
*/
public Field getField(int i) {
// Done
return fields[i];
}
/**
* Returns the contents of this Tuple as a string. Note that to pass the
* system tests, the format needs to be as follows:
*
* column1\tcolumn2\tcolumn3\t...\tcolumnN\n
*
* where \t is any whitespace, except newline, and \n is a newline
*/
public String toString() {
// Done
StringBuilder sb = new StringBuilder();
sb.append(fields[0].toString());
for (int i=1; i<fields.length; i++) {
sb.append("\t"+fields[i].toString());
}
return sb.toString();
}
/**
* @return
* An iterator which iterates over all the fields of this tuple
* */
public Iterator<Field> fields() {
// Done
return new Iterator<Field>() {
private int idx = -1;
@Override
public boolean hasNext() {
return idx+1<fields.length;
}
@Override
public Field next() {
if (++idx == fields.length) {
throw new NoSuchElementException();
} else {
return fields[idx];
}
}
@Override
public void remove() {
}
};
}
/**
* reset the TupleDesc of this tuple
* */
public void resetTupleDesc(TupleDesc td) {
// Done
this.td = td;
}
}