在完全沒有數據庫支持,只具備編程能力的情況下,研究sqlite的源碼。學習的心路歷程。在Redis和Sqlite中選擇sqlite,原因感覺更有用一點對自己的工作。
sqlite分兩種源碼結構,一種是比較常見的sqlite3.c 一個文件十幾萬行代碼。另一種是,將各個模塊分離出的源碼結構。
選擇第二種研究源碼,幫助是可以提高讀碼的效率。另外一個問題是,只讀代碼擔心有理解的片面性,應該同時寫些測試程序。一,便於理解,二,驗證理解,並深化理解。
學習采用自上而下的學習方式。另外一點,學習sqlite3源碼所必須遵循的是:到底要在這里學習到什么?什么是最重要的?可以看懂每一行代碼,在整體上未必了解它為什么這么做,或者這樣做有什么好處?它如此實現舍棄了什么,又得到了什么?
做這個學習的目的是,了解數據庫的工作原理,比如ACID是如何支持。同時了解ACID能做到什么大部分數據庫是如何定義自己的支持級別的。SQL是如何解析的?如何能寫出優秀的SQL。
對數據庫基本原理的支持。本源碼中有哪些優秀的技術實現。
一般經驗:
分層結構,
1)在看一層實現時,不關注另外一層的細節處理。只關注本層實現,可以總結使用的底層的函數接口。這些接口是如何定義的,不必了解全部機理。類似使用操作系統函數CreateFile,只要了解次接口的定義,不需要了解文件系統是如何工作,操作系統是如何工作,磁盤又是如何工作,硬件是如何控制的。這樣會混淆一些概念。
2)了解本層實現時,應關注本層實現解決了那些問題,實現了哪些功能?能夠和數據庫原理結合來看,能了解具體實現功能的意義更好。
從架構圖上看,最上層的應該是Interface:
Much of the C-language Interface is found in source files main.c, legacy.c, and vdbeapi.c though some routines are scattered about in other files where they can have access to data structures with file scope. The sqlite3_get_table() routine is implemented in table.c. The sqlite3_mprintf() routine is found in printf.c. The sqlite3_complete() interface is in tokenize.c. The TCL Interface is implemented by tclsqlite.c.
To avoid name collisions, all external symbols in the SQLite library begin with the prefix sqlite3. Those symbols that are intended for external use (in other words, those symbols which form the API for SQLite) add an underscore, and thus begin with sqlite3_. Extension APIs sometimes add the extension name prior to the underscore; for example: sqlite3rbu_ or sqlite3session_.