github 地址 : github.com/zexho994/lo…
在lab1中,总共提到了以下一个数据结构:
- Tuple , TupleDesc
- Catalog
- BufferPoll
- HeapFile
- HeapPage
看下这些数据结构的关系,首先是TupleDesc 和 Tuple,以数据表为例:
- TupleDesc描述这个Table的字段集合,集合的每个元素包含每个字段的名称和类型
- Tuple是Table的实际数据,一个Tuple对象对应一行数据。
比如下面这个表, 字段有3个,TupleDesc中包含3个字段 : name(string), age(int),email(string),数据有两行,所以就有两个Tuple对象,对应alice和bob的。
catalog 的介绍是这样:
The catalog (class Catalog in SimpleDB) consists of a list of the tables and schemas of the tables that are currently in the database. (一个列表,存储simpleDB下所有的table和schema信息)
You will need to support the ability to add a new table, as well as getting information about a particular table. (通过add方法添加table到catolog中,也可以指定获取数据)
Associated with each table is a TupleDesc object that allows operators to determine the types and number of fields in a table. 通过每个Table的tupleDesc可以知道Table中字段的数量和类型
The global catalog is a single instance of Catalog that is allocated for the entire SimpleDB process. The global catalog can be retrieved via the method Database.getCatalog(), and the same goes for the global buffer pool ( using Database.getBufferPool()). 是单例类型,通过Database.getCatalog()获取,同理,BufferPool也一样。
所以catalog中存储的是table和scheam信息,我们可以通过Catalog获取Table和scheam。
目前还不清楚scheam有哪些,以后lab应该会提到这个,table就是数据表。
BufferPool
The buffer pool (class BufferPool in SimpleDB) is responsible for caching pages in memory that have been recently read from disk. (它是存储在内存中的一个数据结构)
All operators read and write pages from various files on disk through the buffer pool. (可以通过buffer pool 读写页数据)
It consists of a fixed number of pages, defined by the numPages parameter to the BufferPool constructor. (创建的时候指定pool的最大存储容量)
In later labs, you will implement an eviction policy. (以后会需要实现置换策略,现在不用)
For this lab, you only need to implement the constructor and the BufferPool.getPage() method used by the SeqScan operator. (这个lab需要实现构造方法和getPage()两个地方,)
The BufferPool should store up to numPages pages. For this lab, if more than numPages requests are made for different pages, then instead of implementing an eviction policy, you may throw a DbException. In future labs you will be required to implement an eviction policy.(现在出现超过最大限制时,抛出异常即可)
BufferPool将磁盘中的数据缓存在内容中,目的是为了提高数据的读取速度。由于磁盘中的数据是按page块(4kb)方式存储 ,所以内存中存储也是按照page的方式存储。BufferPool中存储容量也不是无限的,例如lab中给的默认数量是50个,意味着50个之后要进行页面替换等操作,不过这是以后的lab要做的,现在不用实现,只管直接添加到bufferpool中。
BufferPool 没有提供直接的单元测试代码, 在后面的HeapFile中的测试用会对BufferPoll进行相应的测试。
对于HeapFile的理解要难一点,一个HeapFile对应一个File,在数据库中,一个File对应就是一张表,所以一个HeapFile要表示的就是一张表,这样我们通过HeapFile进行insertTuple,遍历tuple,获取Page()等操作就很合理了。
HeapPage对应的就是上面提到的Page,一个HeapPage对象存储一个byte[]数组,长度为4096KB,对应就是磁盘上一块4k的Page。
所以整体上,lab1中各个对象的关系是这样
lab1 测试
在根路径下创建一个文件 "some_data_file.txt"
1,1,1
2,2,2
3,4,4
这里有一个天坑,在最后一行要自己多加一行空行,不然会输出不了最下面一行。
执行命令
java -jar dist/simpledb.jar convert some_data_file.txt 3
创建一个类在 test.java 在 src/java/simpledb 下
package simpledb;
import java.io.*;
public class test {
public static void main(String[] argv) {
// construct a 3-column table schema
Type types[] = new Type[]{ Type.INT_TYPE, Type.INT_TYPE, Type.INT_TYPE };
String names[] = new String[]{ "field0", "field1", "field2" };
TupleDesc descriptor = new TupleDesc(types, names);
// create the table, associate it with some_data_file.dat
// and tell the catalog about the schema of this table.
HeapFile table1 = new HeapFile(new File("some_data_file.dat"), descriptor);
Database.getCatalog().addTable(table1, "test");
// construct the query: we use a simple SeqScan, which spoonfeeds
// tuples via its iterator.
TransactionId tid = new TransactionId();
SeqScan f = new SeqScan(tid, table1.getId());
try {
// and run it
f.open();
while (f.hasNext()) {
Tuple tup = f.next();
System.out.println(tup);
}
f.close();
Database.getBufferPool().transactionComplete(tid);
} catch (Exception e) {
System.out.println ("Exception : " + e);
}
}
}
依次执行下面两个命令:
ant
java -classpath dist/simpledb.jar simpledb.test
如果代码没有错误,控制台就会输出