SQLite学习(四) - SQLite源代码文件结构2

SQLite结构以及代码

 

 

 

1. Interface(main.c, legacy.c, and vdbeapi.c)
对于开发人员开放的结构都实现在main.c, legacy.c, and vdbeapi.c。 其他一些比如
sqlite3_get_table() - table.c
sqlite3_mprintf() - 

printf.c.
sqlite3_complete() - tokenize.c
Tcl interface - tclsqlite.c

2. Tokenizer(tokenize.c)
当执行SQL,Interface将SQL传给Tokenizer,Tokenizer把原始的SQL字符串转换成单个tokens,传给Parser. Tokenizer的实现代码在tokenize.c中。在SQLite中由Tokenizer调用Parser,熟悉YACC and BISON的开发者可能知道他们的调用顺序是相反的,由parser call the tokenizer。 SQLite两种调用都有。

3. Parser(lemon.c, parse.y)
Parser根据上下文环境给tokens赋予语义。SQLite Parser使用Lemon LALR(1) parser generator。 与YACC/BISON有相同的功能,但是它使用一种另一种不易出错的输入格式。 Lemon生成了一个可重入的,线程安全的Parser。完整的lemon代码只有一个C文件(lemon.c),在sqlite/tool子目录下,关于lemon的文档也可以在doc目录中找到。
Lemon defines the concept of a non-terminal destructor so that it does not leak memory when syntax errors are encountered. Parser的源代码是parse.y.

4. Code Generator(expr.c, where.c, auth.c等)
Parser将tokens转换成完成的SQL语句后,code generator将SQL转换成VIRTUAL MACHINE CODE
Code Generator代码
expr.c 负责生成表达式VM Code
where.c 负责生成SELECT, UPDATE and DELETE中WHERE语句的VM Code
auth.c sqlite3_set_authorizer()函数的实现代码
attach.c, build.c, delete.c,  insert.c, pragma.c, select.c, trigger.c, update.c, vacuum.c 这些负责与文件名相同的命令的功能的VM Code

5. Virtual Machine(vdbe.h, vdbe.c, vdbeInt.h, vdbeaux.c, vdbeapi.c)
virtual machine负责执行code generator 生成的VM Code. virtual machine实现了一个数据库的抽象引擎(控制数据库文件)。 VM有一个作为临时存储的栈。每一个VM Code指令包含一个opcode和最多三个operands。

VM头文件vdbe.h
VM源代码vdbe.c
VM相关结构体vdbeInt.h
VM帮助函数vdbeaux.c
VM开发函数vdbeapi.c, 比如sqlite3_bind_...函数
Individual values (strings, integer, floating point numbers, and BLOBs) are stored in an internal object named "Mem" which is implemented by vdbemem.c.
SQL内置函数  func.c  (ex: coalesce(), count(), substr(), and so forth)
日期和时间转换函数 date.c.

6. B-Tree(btree.h, btree.c)
btree相关代码btree.h, btree.c. SQLite每个表和Index都由一个单独的Btree存储。所有btree都存储在一个database文件中,database文件结构可以在btree.c注释中找到。

7. Page Cache(pager.h, pager.c)
Btree以chunk为单位读取和写数据。chunk默认值=1024bytes,但是可以改变,范围512~65536bytes。page cache负责reading, writing, and caching these chunks.
The page cache also provides the rollback and atomic commit abstraction
takes care of locking of the database file.
源代码pager.h, pager.c

8. OS Interface(os_unix.h/os_unix.c, os_win.h/os_win.c)
为了支持POSIX和win2系统,SQLite提供在OS上提供了一个抽象层。接口定义在头文件os.h. 每个系统有它自己的实现: os_unix.h/os_unix.c for Unix, os_win.h/os_win.c for Windows.

9.Utilities(util.c, hash.c, printf.c, random.c)
Memory allocation and caseless string comparison routines are located in .
Symbol tables used by the parser are maintained by hash tables found in hash.c.
The utf.c source file contains Unicode conversion subroutines. SQLite has its own private implementation of printf() (with some extensions) in printf.c
its own random number generator in random.c.

10. Test Code
If you count regression test scripts, more than half the total code base of SQLite is devoted to testing. There are many assert() statements in the main code files. In additional, the source files test1.c through test5.c together with md5.c implement extensions used for testing purposes only. The os_test.c backend interface is used to simulate power failures to verify the crash-recovery mechanism in the pager.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值