MIT 6.830 LAB1 SimpleDB
2021/03/30-2021/03/31
前言
課程地址:http://db.lcs.mit.edu/6.830/sched.php
代碼:https://github.com/MIT-DB-Class/simple-db-hw
講義:https://github.com/MIT-DB-Class/course-info-2018/
lab1的實現比較簡單,具體代碼可以參考reference中的博客或是本人的Github,這里主要梳理一下項目結構
LAB1
exercise1 Fields and Tuples
Tuple是數據庫中的數據,TupleDesc就是數據庫中的schema
實現
- src/simpledb/TupleDesc.java
- src/simpledb/Tuple.java
TupleDesc就是數據庫中的schema,里面有一組TDItems定義了每一列的類型
Tuple是數據庫中的數據,里面的Field就是數據
比較簡單,分別在兩個類中添加List和一些成員變量即可
exercise2 Catalog
Catalog是整個數據庫的目錄,可以通過tableid獲取DbFile
實現
- src/simpledb/Catalog.java
Catelog是Table的集合,然后每個Table有DbFile, PrimaryKeyField, Name
全局catalog是分配給整個SimpleDB進程的Catalog類一個實例,可以通過方法Database.getCatalog()獲得,global buffer pool可以通過方法Database.getBufferPool()獲得。
添加以下數據結構后就很好實現了
public class Catalog {
class Table{
private DbFile file;
private String name;
private String pkeyField;
public Table(DbFile file, String name, String pkeyField){
this.file = file;
this.name = name;
this.pkeyField = pkeyField;
}
// getter and setter
}
/* <name,Table>*/
private ConcurrentHashMap<String,Table> stringTableMap;
/* <tableId,Table>*/
private ConcurrentHashMap<Integer,Table> integerTableMap;
...
}
exercise3 BufferPool
BufferPool通過Catalog獲取到對應的DbFile,從DbFile中readPage
實現
- src/simpledb/BufferPool.java
buffer pool(在SimpleDB中是BufferPool類)負責將內存最近讀過的物理頁緩存下來。所有的讀寫操作通過buffer pool讀寫硬盤上不同文件。
Database類提供了一個靜態方法Database.getBufferPool(),返回整個SimpleDB進程的BufferPool實例引用。
這里用到了Page和PageId類,注意到PageId中的hashCode
/**
* @return a hash code for this page, represented by the concatenation of
* the table number and the page number (needed if a PageId is used as a
* key in a hash table in the BufferPool, for example.)
* @see BufferPool
*/
public int hashCode();
暗示了我們使用hashCode作為key來構建一個hashmap
在Lab1中需要做的只需要添加以下數據結構並實現getPage即可
private int numPages;
/**
* pages in buffer pool
*/
private ConcurrentHashMap<Integer,Page> pages;
exercise4 HeapPage
HeapPage是內存中的一個頁,包含某個table中的若干個Tuple和TupleDesc;
HeapPageId通過tableid和pageNo來標識一個HeapPage;
RecordId是Page中某個Tuple的標識
實現
- src/simpledb/HeapPageId.java
- src/simpledb/RecordID.java
- src/simpledb/HeapPage.java
前兩個比較簡單,主要注意hashCode的算法,借助String類來實現,以RecordId為例
/**
* You should implement the hashCode() so that two equal RecordId instances
* (with respect to equals()) have the same hashCode().
*
* @return An int that is the same for equal RecordId objects.
*/
@Override
public int hashCode() {
String s = pageId.hashCode()+"#"+tupleNumber;
return s.hashCode();
}
exercise5 HeapFile
HeapFile是DbFile的一個實現,是BufferPool正是通過DbFile來讀取Page的;
HeapFile中需要實現一個DbIterator
實現
- src/simpledb/HeapFile.java
難點
- 需要計算File偏移量
- 不能直接調用BufferPool的readPage方法,要使用RandomAccessFile打開文件進行真正的讀,將磁盤文件讀進內存中
- 需要自己實現一個HeapFileIterator類,實現DbFileIterator接口
exercise6 Operators
實現了OpIterator接口,SimpleDP和程序交互的過程中,現在root operator上調用getNext,之后在子節點上繼續調用getNext,一直下去,直到leaf operators 被調用。他們從硬盤上讀取tuples,並在樹結構上傳遞。
實現
- src/simpledb/SeqScan.java
調用exercise5中實現的HeapFileIterator即可
reference
6.830 Lab 1: SimpleDB:https://blog.csdn.net/hjw199666/article/details/103486328
MIT6.830: Database System lab1 note:https://zhuanlan.zhihu.com/p/58595037
6.830 Lab 總覽:https://blog.csdn.net/hjw199666/article/details/103824797