CS186 Project 1: SimpleDB Exercise 1

本文档介绍了SimpleDB中基本的元组结构,由Field对象集合组成,每个Field代表元组中的一列。Field是一个接口,不同的数据类型如整数、字符串等都实现了它。元组的模式或类型由TupleDesc对象表示,包含一系列Type对象,描述每个字段的数据类型。任务是实现TupleDesc和Tuple类的骨架方法,并确保通过TupleTest和TupleDescTest单元测试。同时,需要重写RecordId的equals和hashCode方法。
摘要由CSDN通过智能技术生成

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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值