iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 23
3
Software Development

Let's build a DBMS: StellarSQL -- a minimal SQL DBMS written in Rust系列 第 23

StellarSQL 22: Update Components of Database

22: Update Components of Database

2018/11/6

It's recommended to read on Gitbook

I modify some parts that I did yesterday.

Table in Database should be stored in HashMap. So checking the table is convenient.

!FILENAME component/database.rs

pub struct Database {
    pub name: String,
    pub tables: HashMap<String, Table>,
}

I decide to store all value in String. For the reasons that, (1) the Field has defined the DataType, so we can format the value if we want, (2) DBMS will not use the real value so frequently (only when processing where, but that's fine to deal in String), and (3) loading and saving data are all in String (actually, string to binary).

Therefore I only remain DataType:

!FILENAME component/datatype.rs

pub enum DataType {
    Char(u8),
    Double,
    Float,
    Int,
    Varchar(u8),
}

Also, I forget that attributes could have default values. So I add a default in Field. Also, I remove DataValue, because Field is only the definition of a table. Therefore I also update table.

!FILENAME component/field.rs

pub struct Field {
    pub name: String,
    pub datatype: DataType,
    pub not_null: bool,
    pub default: Option<String>,
    pub check: Checker,
}

I also update Table a lot. A Table should be able to store rows, and it might be just a part of data from a huge set of table files. So, it needs to know where is the data from, including which page and which range by the cursors.

!FILENAME component/table.rs

pub struct Table {
    /* definition */
    pub name: String,
    pub fields: HashMap<String, Field>, // aka attributes
    pub primary_key: Vec<String>,
    pub foreign_key: Vec<String>,
    pub reference_table: Option<String>,

    /* value */
    pub rows: Vec<Row>,

    /* storage */
    pub page: u64, // which page of this table
    pub cursors: (u64, u64), // cursors of range in a page
}

I cannot design very well at the moment, so I would find more that should be modified as time goes by. In real practice, I believe I am on the right way.


Quick Link
1.StellarSQL Repository
2.Gitbook of this series

Author
Liu, An-Chi (劉安齊). A software engineer, who loves writing code and promoting CS to people. Welcome to follow me at Facebook Page. More information on Personal Site and Github.


上一篇
StellarSQL 21: First Implementation for Database Components
下一篇
StellarSQL 23: Implement Table `insert`
系列文
Let's build a DBMS: StellarSQL -- a minimal SQL DBMS written in Rust30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言