h2database 数据库分析

1、 源码结构

git source: https://github.com/h2database/h2database.git

screenshot

h2 的源码不是标准的maven工程,需要重新进行目录组织生成maven工程,test目录下的代码需要依赖main目录的代码、tools目录下jaqu、mode、dev目录的代码。

2、启动测试
使用embeded memory模式启动,为了最大化数据更新的效率,停用undo/redo log,同时为了支持多线程访问使用MULTI_THREAD=1 && LOCK_MODE=1 模式。

2.1 执行

服务器启动代码如下(kotlin代码):

Class.forName("org.h2.Driver")
val conn = DriverManager.getConnection("jdbc:h2:mem:test;LOG=0;LOCK_MODE=1;UNDO_LOG=0;MV_STORE=false;MULTI_THREADED=1", "sa", "sa")

如果LOCK_MODE = 0 不允许使用MULTI_THREAD模式,更新数据的时候是对database进行synchronized操作,容易出现竞争。

2.2 流程

这一段启动代码的整个流程比较复杂:

startup_embedded
需要初始化INFOMATION_SCHEMA这个管理元数据的Schema。之后sql的执行流程就比较简单,把sql通过Recursive Decsent Parser解析为h2的各种command。

H2实现了ANSI-SQL89标准,并且实现了基于B-tree的存储引擎,而MVStore是新一代的存储引擎,用来替换基于Btree存储引擎。

H2 逻辑处理分层:

        JDBC Driver
        Connection/Session
        SQL Parser: Recursive-descent parser
        Command解析执行:
                org.h2.command.ddl Commands that modify schema data structures
                org.h2.command.dml Commands that modify data
        Table/Index/Constrains
                org.h2.table Implementations of different kinds of tables
                org.h2.index Implementations of different kinds of indices, indexes are simply stored as special kinds of tables.
        Undo Log, redo Log, Transaction Layer: org.h2.store
        B-tree engine, MVStore engine
        FileSystem abstraction: memory or file

2.3 涉及类

h2database 把数据库中的概念抽象为一一对应的数据模型:

h2db

初始化和执行过程中的的调用时序图

h2db1

3、TODO

  • RegularTable vs MVTable的效率
  • 内存模式不开启事务,如何支持 多线程?
  • 存储数据结构优化:Row、Table
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值