2.2. Fields and Tuples
Tuples in SimpleDB are quite basic. They consist of a collection of Field objects, one per field in the Tuple. Field is an interface that different data types (e.g., integer, string) implement. Tuple objects are created by the underlying access methods (e.g., heap files, or B-trees), as described in the next section. Tuples also have a type (or schema), called a tuple descriptor, represented by a TupleDesc object. This object consists of a collection of Type objects, one per field in the tuple, each of which describes the type of the corresponding field.
Exercise 1. Implement the skeleton methods in:
src/java/simpledb/TupleDesc.java
src/java/simpledb/Tuple.java
At this point, your code should pass the unit tests TupleTest and TupleDescTest. At this point, modifyRecordId() should fail because you havn’t implemented it yet.
这门课给的代码框架资料可以在我上传的资料库里找,需要数据库基础
这是TupleDesc.java的代码,用来描述元组模式,补全代码靠它的说明就能打出来,后期靠调试补全。
package simpledb;
import java.io.Serializable;
import java.util.*;
/**
* TupleDesc describes the schema of a tuple.元组的模式
*/
public class TupleDesc implements Serializable, Iterable<TupleDesc.TDItem> {
/**
* A help class to facilitate organizing the information of each field
*/
private List<TDItem> tdItems = new ArrayList<>();
public TupleDesc(){
}
public static class TDItem implements Serializable {
private static final long serialVersionUID = 1L;
/**
* The type of the field
*/
Type fieldType;
/**
* The name of the field
*/
String fieldName;
public TDItem(Type t, String n) {
this.fieldName = n;
this.fieldType = t;
}
public String toString() {
return fieldName + "(" + fieldType + ")";
}
}
/**
* @return An iterator which iterates over all the field TDItems
* that are included in this TupleDesc
*/
/*使用了继承Iterable方法*/
@Override
public Iterator<TDItem> iterator() {
// some code goes here
if (tdItems.size() > 0) {
return new Iterator<TDItem>() {
private Integer index = 0;
@Override
public boolean hasNext() {
return index < tdItems.size();
}
@Override
public TDItem next() {
int num = index;
index++;
return tdItems.get(num);
}
};
}else {
return null;
}
}
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) {
// some code goes here
int len =0;
if (typeAr.length!=fieldAr.length) {System.out.println("数据不符");return;}
len = typeAr.length;
for (int i=0;i<len;i++){
TDItem newtd = new TDItem(typeAr[i],fieldAr[i]);
this.tdItems.add(newtd);
}
}
/**
* Constructor. Create a new tuple desc