arch.tcl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #
  2. # Run this Tcl script to generate the sqlite.html file.
  3. #
  4. set rcsid {$Id: arch.tcl,v 1.7 2001/11/24 13:23:05 drh Exp $}
  5. puts {<html>
  6. <head>
  7. <title>Architecture of SQLite</title>
  8. </head>
  9. <body bgcolor=white>
  10. <h1 align=center>
  11. The Architecture Of SQLite
  12. </h1>}
  13. puts "<p align=center>
  14. (This page was last modified on [lrange $rcsid 3 4] UTC)
  15. </p>"
  16. puts {
  17. <h2>Introduction</h2>
  18. <table align="right" border="1" cellpadding="15" cellspacing="1">
  19. <tr><th>Block Diagram Of SQLite</th></tr>
  20. <tr><td><img src="arch.png"></td></tr>
  21. </table>
  22. <p>This document describes the architecture of the SQLite library.
  23. The information here is useful to those who want to understand or
  24. modify the inner workings of SQLite.
  25. </p>
  26. <p>
  27. A block diagram showing the main components of SQLite
  28. and how they interrelate is shown at the right. The text that
  29. follows will provide a quick overview of each of these components.
  30. </p>
  31. <h2>Interface</h2>
  32. <p>Most of the public interface to the SQLite library is implemented by
  33. four functions found in the <b>main.c</b> source file. The
  34. <b>sqlite_get_table()</b> routine is implemented in <b>table.c</b>.
  35. The Tcl interface is implemented by <b>tclsqlite.c</b>. More
  36. information on the C interface to SQLite is
  37. <a href="c_interface.html">available separately</a>.<p>
  38. <p>To avoid name collisions with other software, all external
  39. symbols in the SQLite library begin with the prefix <b>sqlite</b>.
  40. Those symbols that are intended for external use (in other words,
  41. those symbols which form the API for SQLite) begin
  42. with <b>sqlite_</b>.</p>
  43. <h2>Tokenizer</h2>
  44. <p>When a string containing SQL statements is to be executed, the
  45. interface passes that string to the tokenizer. The job of the tokenizer
  46. is to break the original string up into tokens and pass those tokens
  47. one by one to the parser. The tokenizer is hand-coded in C.
  48. All of the code for the tokenizer
  49. is contained in the <b>tokenize.c</b> source file.</p>
  50. <p>Note that in this design, the tokenizer calls the parser. People
  51. who are familiar with YACC and BISON may be used to doing things the
  52. other way around -- having the parser call the tokenizer. The author
  53. of SQLite
  54. has done it both ways and finds things generally work out nicer for
  55. the tokenizer to call the parser. YACC has it backwards.</p>
  56. <h2>Parser</h2>
  57. <p>The parser is the piece that assigns meaning to tokens based on
  58. their context. The parser for SQLite is generated using the
  59. <a href="http://www.hwaci.com/sw/lemon/">Lemon</a> LALR(1) parser
  60. generator. Lemon does the same job as YACC/BISON, but is uses
  61. a different input syntax which is less error-prone.
  62. Lemon also generates a parser which is reentrant and thread-safe.
  63. And lemon defines the concept of a non-terminal destructor so
  64. that it does not leak memory when syntax errors are encountered.
  65. The source file that drives Lemon is found in <b>parse.y</b>.</p>
  66. <p>Because
  67. lemon is a program not normally found on development machines, the
  68. complete source code to lemon (just one C file) is included in the
  69. SQLite distribution in the "tool" subdirectory. Documentation on
  70. lemon is found in the "doc" subdirectory of the distribution.
  71. </p>
  72. <h2>Code Generator</h2>
  73. <p>After the parser assembles tokens into complete SQL statements,
  74. it calls the code generator to produce virtual machine code that
  75. will do the work that the SQL statements request. There are seven
  76. files in the code generator: <b>build.c</b>, <b>delete.c</b>,
  77. <b>expr.c</b>, <b>insert.c</b> <b>select.c</b>, <b>update.c</b>,
  78. and <b>where.c</b>.
  79. In these files is where most of the serious magic happens.
  80. <b>expr.c</b> handles code generation for expressions.
  81. <b>where.c</b> handles code generation for WHERE clauses on
  82. SELECT, UPDATE and DELETE statements. The files
  83. <b>delete.c</b>, <b>insert.c</b>, <b>select.c</b>, and
  84. <b>update.c</b> handle the code generation for SQL statements
  85. with the same names. (Each of these files calls routines
  86. in <b>expr.c</b> and <b>where.c</b> as necessary.) All other
  87. SQL statements are coded out of <b>build.c</b>.</p>
  88. <h2>Virtual Machine</h2>
  89. <p>The program generated by the code generator is executed by
  90. the virtual machine. Additional information about the virtual
  91. machine is <a href="opcode.html">available separately</a>.
  92. To summarize, the virtual machine implements an abstract computing
  93. engine specifically designed to manipulate database files. The
  94. machine has a stack which is used for intermediate storage.
  95. Each instruction contains an opcode and
  96. up to three additional operands.</p>
  97. <p>The virtual machine is entirely contained in a single
  98. source file <b>vdbe.c</b>. The virtual machine also has
  99. its own header file <b>vdbe.h</b> that defines an interface
  100. between the virtual machine and the rest of the SQLite library.</p>
  101. <h2>B-tree Driver</h2>
  102. <p>An SQLite database is maintained on disk using a B-tree implementation
  103. found in the <b>btree.c</b> source file. A separate B-tree is used for
  104. each table and index in the database. All B-trees are stored in the
  105. same disk file. Each page of a B-tree is 1024 bytes in size. The key
  106. and data for an entry are stored together in an area called "payload".
  107. Up to 236 bytes of payload can be stored on the same page as the B-tree
  108. entry. Any additional payload is stored in a chain of overflow pages.</p>
  109. <p>The interface to the B-tree subsystem is defined by the header file
  110. <b>btree.h</b>.
  111. </p>
  112. <h2>Page Cache</h2>
  113. <p>The B-tree module requests information from the disk in 1024 byte
  114. chunks. The page cache is reponsible for reading, writing, and
  115. caching these chunks at the behest of the B-tree module.
  116. The page cache also provides the rollback and atomic commit abstraction
  117. and takes care of reader/writer locking of the database file. The
  118. B-tree driver requests particular pages from the page cache and notifies
  119. the page cache when it wants to modify pages or commit or rollback
  120. changes and the page cache handles all the messy details of making sure
  121. the requests are handled quickly, safely, and efficiently.</p>
  122. <p>The code to implement the page cache is contained in the single C
  123. source file <b>pager.c</b>. The interface to the page cache subsystem
  124. is defined by the header file <b>pager.h</b>.
  125. </p>
  126. <h2>OS Interface</h2>
  127. <p>
  128. In order to provide portability between POSIX and Win32 operating systems,
  129. SQLite uses an abstraction layer to interace with the operating system.
  130. The <b>os.c</b> file contains about 20 routines used for opening and
  131. closing files, deleting files, creating and deleting locks on files,
  132. flushing the disk cache, and so forth. Each of these functions contains
  133. two implementations separated by #ifdefs: one for POSIX and the other
  134. for Win32. The interface to the OS abstraction layer is defined by
  135. the <b>os.h</b> header file.
  136. </p>
  137. }
  138. puts {
  139. <br clear="both" />
  140. <p><hr /></p>
  141. <p><a href="index.html"><img src="/goback.jpg" border=0 />
  142. Back to the SQLite Home Page</a>
  143. </p>
  144. </body></html>}