Summary of mit database experiment one


Code

Experiment 1: tuple and tupleDesc

Goal: implement tuple and tupleDesc
tupleDesc = tupleDescription

image.png

Experiment 2: catalog

image.png

  1. Contains tables and table schemas: table references, table names, table primary keys
  2. table addition and lookup
  3. Get the only catalog through Database.getCatalog()
  4. Important way to get tables. Get table information or table files by id.

Experiment 3: buffer pool

image.png

Experiment 4: Implement heapPageId, heapPage, recordID

1. Methods and properties that need to be implemented

  • heappage
1. getNumTuples() : 获取元组数目
2. HeapPage(HeapPageId id, byte[] data) 
3. getHeaderSize() :获取头(位示图)的长度
4. isSlotUsed(int i):该槽位是否被使用
5. iterator()
6. getNumEmptySlots() 


属性
1. pageid
2. tupleDesc								// 表的信息,元组信息和元组
3. []header and tuples

Just complete HeapPageIdTest, RecordIDTest, and HeapPageReadTest.

Experiment 5: Implement heapFile

image.png

  1. Read the file using randomFile
  2. iterator
    1. Start after opening, only read the first page
    2. If it is not empty, check whether it has reached the bottom of the page, if not, point to the beginning of the next page.

Experiment 6: Manipulation

area

  1. unique transaction id
  2. table alias
  3. table id
  4. table iterator

Concrete operation

  1. Alias ​​null.
  2. The iterator should be judged whether it is not empty to prevent no open.

Experiment 7:

  1. Create a test.txt file similar to the following, with a newline at the end
1,1,1
2,2,2
3,3,3

  1. Convert test.txt into a .dat file, using convert in HeapFileEncode
  2. Run the given test, pay attention to the corresponding file name
    1. some_data_file.dat
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);
        }
    }

}
  1. Or run the following file directly after creating the file
package simpledb;

import org.junit.Test;
import simpledb.common.Database;
import simpledb.common.Type;
import simpledb.execution.SeqScan;
import simpledb.storage.*;
import simpledb.transaction.TransactionId;
import java.io.*;
/**
 * @author: Zekun Fu
 * @date: 2023/2/22 10:04
 * @Description:
 */
public class SQLTest {
    
    
    
    // 看当前的类路径
    @Test
    public void testPath() {
    
    
        System.out.println(String.format("当前的类路径是%s", System.getProperty("user.dir")));
    }
    
    // 测试文件转化
    @Test
    public void convert()  {
    
    
        File infile = new File("test.txt");
        File outFile = new File("some_data_file.dat");
        try {
    
    
            HeapFileEncoder.convert(infile, outFile, BufferPool.getPageSize(), 3);      // 默认3个int类型
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    @Test
    public void test() throws IOException{
    
    
        // 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.

        // 创建表文件映射
        File file = new File("test.txt");
        if (!file.exists()) {
    
    
            throw new IOException(".dat文件不存在!");
        }
        File heapFile = new File("some_data_file.dat");
        HeapFileEncoder.convert(file, heapFile, BufferPool.getPageSize(), 3);
        HeapFile table1 = new HeapFile(heapFile, 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.print(tup);
            }
            f.close();
            Database.getBufferPool().transactionComplete(tid);
        } catch (Exception e) {
    
    
            System.out.println("Exception : " + e);
        }
        boolean end = heapFile.delete();
        System.out.println("临时数据库文件是否删除成功:" + end);
    }
}

question

  1. Is the file size in heapFile an integer multiple of pages? If not, what should I do if there is less than one page when reading data from the page? If yes, what should I do if there is less than one page when writing?
    1. When writing, it is written page by page, so when reading, there will not be less than one page?
    2. After verification, the size of each DBfIle must be an integer multiple of Page.
    3. If 4k, two int type data, can store 504 pieces of information
  2. Why should the primary key be placed in the table information in the catalog?
    1. The interface DBfile of the table, the id is determined by the id of the DBfile.
    2. table name and primary key
    3. Because the primary key and name are important information of the table.
  3. Why is NoSunElementException thrown?
    1. Because this is defined, the test can continue executing if this error is thrown.
  4. There is no output in experiment 7. I don't know if there is an error in the conversion of .dat or an error in reading the file.
    1. According to debug debugging, the problem was found.
    2. According to the trace, it is found that the fields in the output tuple exist, indicating that there is no error in the conversion, and there is no problem with the reading in the dbfile.
      1. read head first
      2. Then read tuples according to
      3. Finally, put the read content into the page.
    3. Then, since there is no problem with the reading and the iterator, there is no problem with the tuple returned in the end, so only the output toString has a problem. It is indeed the case. It was commented out at the time and forgot to restore it.

Guess you like

Origin blog.csdn.net/fuzekun/article/details/129231100