Git Product home page Git Product logo

a-simple-database's Introduction

xiaofeiDB - A simple database demo for Sqlilite

Design of my Sqlite demo

The front-end consists of:

  • tokenizer
  • parser
  • code generator

The back-end consists of:

  • virtual machine
  • B-tree
  • pager
  • os interface

A simple REPL

Test

Single-table database

So far, xiaofeiDB supports two operations:

  • insert a row
  • print all rows out

Data Structure

It's very similar as Sqlite which uses B-tree for CRUD, However, for now, I use array instead of tree.

Each inserted row will be serialized and then compactly arranged in memory and stored like pages. a page has many rows and a table has many pages. This structure is just like virtual memory systems in computer architectures.

    void serialize_row(Row* source, void* destination) {
        memcpy(destination + ID_OFFSET, &(source->id), ID_SIZE);
        memcpy(destination + USERNAME_OFFSET, &(source->username), USERNAME_SIZE);
        memcpy(destination + EMAIL_OFFSET, &(source->email), EMAIL_SIZE);
    }
    void deserialize_row(void* source, Row* destination) {
        memcpy(&(destination->id), source + ID_OFFSET, ID_SIZE);
        memcpy(&(destination->username), source + USERNAME_OFFSET, USERNAME_SIZE);
        memcpy(&(destination->email), source + EMAIL_OFFSET, EMAIL_SIZE);
    }

Search to write/read

This function is how to figure out where to read/write in memory for a particular row:

    void* row_slot(Table* table, uint32_t rows_num) {
    uint32_t page_num = rows_num / ROWS_PER_PAGE;
    void* page = table->pages[page_num];
    // if this page does not exist yet, init one
    if (page == NULL) {
        page = table->pages[page_num] = malloc(sizeof(PAGE_SIZE));
    }

    uint32_t row_offset = rows_num % ROWS_PER_PAGE;
    uint32_t byte_offset = row_offset * ROW_SIZE;

    return page + byte_offset;
}

TEST

Test

Deal with some bugs

TODO

  • deal with the stackover
  • deal with some invalid input like negative id or something

FIX During test, when the input string is longer than the max length we set, scanf() will cause a buffer overflow and writing into a unexpected place.

To fix that, I use strtok() to split the input buffer. Before storing the input string into Row model, checking the length of each string first to avoid buffer overflow and id validation

a-simple-database's People

Contributors

454270186 avatar

Stargazers

Roman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.