Code Monkey home page Code Monkey logo

learn-db's Introduction

learn-db

使用rust实现的基于 Bitcask 引擎 的kv数据库。

索引篇

为了快速查找,所有的索引数据都是放在内存中,可以简单的理解为 HashMap.

不过为了能够并发的查询和写入,所以实现了分段锁(数组+链表);

另外,数据库运行的过程中,索引数量会不断的增减,所以也实现了动态扩缩容的能力。

具体实现:src/index/dynamic_index.rs

存储篇

写入实现

因为整个系统只有一个写入点,并且需要支持多线程写入, 很自然的想到了 多生产者+单消费者的队列模型。

为了提高性能,消费者可以获取多条数据,批量写入; 生产过程本身是异步的,如果有同步的需求,可以阻塞等待完成的回执。

具体实现:src/store/write_consumer.rs

读取实现

基于索引,可以定位到数据所在的文件以及偏移量, 所以读取就是简单的打开文件,设置偏移量,读取指定大小数据。

不过每次都打开文件比较浪费,所以可以把文件描述符池化。

具体实现:src/store/mod.rs:72

恢复与快照

系统运行过程中,会异步生成索引文件,启动时只需要把索引文件加载到内存就可以了;

同时,根据配置,一些比较老的数据文件,也会被回收掉,这样可以避免数据文件越来越多;

learn-db's People

Contributors

auyang-0626 avatar

Stargazers

 avatar  avatar

Watchers

James Cloos avatar  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.