-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuple.java
More file actions
executable file
·192 lines (172 loc) · 6.33 KB
/
Tuple.java
File metadata and controls
executable file
·192 lines (172 loc) · 6.33 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
package parser;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.ListIterator;
/* A tuple equals a record/row in a relation/table.
* A tuple contains at most MAX_NUM_OF_FIELDS_IN_RELATION=8 fields.
* Each field in a tuple has offset 0,1,2,... respectively,
* according to the defined schema.
* You can access a field by its offset or its field name.
* Usage: Most of cases you access the tuples in main memory,
* either through the MainMemory class,
* or through both the MainMemory and the Block class.
* You can access or change fields of a tuple through here.
* If you need to delete a tuple inside a memory block,
* "invalidate" the tuple
* by using Tuple::invalidate() or Block::invalidateTuple() .
* You are able to get schema of a particular tuple through here.
*/
public class Tuple implements Serializable {
protected SchemaManager schema_manager;
protected int schema_index; // points to the schema of the
// relation which the tuple belongs to
private ArrayList<Field> fields; // stores int and string fields
// DO NOT use the constructor here.
// Create an empty tuple only through Schema
// for internal use: returns an invalid tuple
protected Tuple() {
this.schema_manager=null;
this.schema_index=-1;
this.fields=new ArrayList<Field>();
}
protected Tuple(Tuple t) {
schema_manager=t.schema_manager;
schema_index=t.schema_index;
//fields=(ArrayList<Field>) DeepCopy.copy(t.fields);
this.fields=new ArrayList<Field>();
ListIterator<Field> lit=t.fields.listIterator();
while (lit.hasNext())
this.fields.add(new Field(lit.next()));
}
protected Tuple(SchemaManager schema_manager, int schema_index){
this.schema_manager=schema_manager;
this.schema_index=schema_index;
this.fields=new ArrayList<Field>();
if (this.schema_manager!=null) {
Schema schema=schema_manager.schemas[schema_index];
Field f;
int numberOfFields=schema.getNumOfFields();
for (int i=0;i<numberOfFields;i++) {
fields.add(f=new Field());
f.type=schema.getFieldType(i);
}
}
}
//returns true if the tuple is invalid
public boolean isNull() {
return fields.size()==0;
}
// returns the schema of the tuple
public Schema getSchema() {
return new Schema(schema_manager.schemas[schema_index]);
}
// returns the number of fields in the tuple
public int getNumOfFields() {
Schema schema=schema_manager.schemas[schema_index];
return schema.getNumOfFields();
}
// returns the number: tuples per block
public int getTuplesPerBlock() {
Schema schema=schema_manager.schemas[schema_index];
return schema.getTuplesPerBlock();
}
// invalidates the tuple
public void invalidate() {
fields.clear();
}
// returns false if the type is wrong or out of bound
public boolean setField(int offset,String s){
Schema schema=schema_manager.schemas[schema_index];
if (offset>=schema.getNumOfFields() || offset<0){
System.err.print("setField ERROR: offset "+offset+" is out of bound!"+"\n");
return false;
} else if (schema.getFieldType(offset)!=FieldType.STR20) {
System.err.print("setField ERROR: field type not FieldType.STR20!"+"\n");
return false;
} else {
fields.get(offset).str=s;
}
return true;
}
// returns false if the type is wrong or out of bound
public boolean setField(int offset,int i){
Schema schema=schema_manager.schemas[schema_index];
if (offset>=schema.getNumOfFields() || offset<0){
System.err.print("setField ERROR: offset "+offset+" is out of bound!"+"\n");
return false;
} else if (schema.getFieldType(offset)!=FieldType.INT) {
System.err.print("setField ERROR: field type not FieldType.INT!"+"\n");
return false;
} else {
fields.get(offset).integer=i;
}
return true;
}
// returns false if the type is wrong or the name is not found
public boolean setField(String field_name,String s){
Schema schema=schema_manager.schemas[schema_index];
if (!schema.fieldNameExists(field_name)) {
System.err.print("setField ERROR: field name " + field_name + " not found"+"\n");
return false;
}
int offset=schema.getFieldOffset(field_name);
if (schema.getFieldType(offset)!=FieldType.STR20) {
System.err.print("setField ERROR: field type not FieldType.STR20!"+"\n");
return false;
} else {
fields.get(offset).str=s;
}
return true;
}
// returns false if the type is wrong or the name is not found
public boolean setField(String field_name,int i){
Schema schema=schema_manager.schemas[schema_index];
if (!schema.fieldNameExists(field_name)) {
System.err.print("setField ERROR: field name " + field_name + " not found"+"\n");
return false;
}
int offset=schema.getFieldOffset(field_name);
if (schema.getFieldType(offset)!=FieldType.INT) {
System.err.print("setField ERROR: field type not FieldType.INT!"+"\n");
return false;
} else {
fields.get(offset).integer=i;
}
return true;
}
// returns default field if out of bound
public Field getField(int offset) {
if(offset<fields.size() && offset>=0){
return new Field(fields.get(offset));
} else {
System.err.print("getField ERROR: offset "+offset+" is out of bound!"+"\n");
return new Field();
}
}
// returns default field if out of bound
public Field getField(String field_name) {
Schema schema=schema_manager.schemas[schema_index];
int offset=schema.getFieldOffset(field_name);
if(offset<fields.size() && offset>=0){
return new Field(fields.get(offset));
} else {
System.err.print("getField ERROR: offset "+offset+" is out of bound!"+"\n");
return new Field();
}
}
public String toString(boolean print_field_names) {
String str="";
Schema schema=schema_manager.schemas[schema_index];
if (print_field_names) {
str+=schema.fieldNamesToString();
str+=("\n");
}
for (int i=0;i<fields.size();i++) {
str+=fields.get(i)+"\t";
}
return str;
}
public String toString() {
return toString(false);
}
}