⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileformat.html

📁 这是sqlite3.56的文档。拿来给大家阅读使用
💻 HTML
📖 第 1 页 / 共 3 页
字号:
</p><p>A frequent question is why the string says version 2.1 when (asof this writing) we are up to version 2.7.0 of SQLite and anychange to the second digit of the version is suppose to representa database format change.  The answer to this is that the B-treelayer has not changed any since version 2.1.  There have beendatabase format changes since version 2.1 but those changes haveall been in the schema layer.  Because the format of the b-treelayer is unchanged since version 2.1.0, the header string stillsays version 2.1.</p><p>After the format string is a 4-byte integer used to determine thebyte-order of the database.  The integer has a value of0xdae37528.  If this number is expressed as 0xda, 0xe3, 0x75, 0x28, thenthe database is in a big-endian format and all 16 and 32-bit integerselsewhere in the b-tree layer are also big-endian.  If the number isexpressed as 0x28, 0x75, 0xe3, and 0xda, then the database is in alittle-endian format and all other multi-byte numbers in the b-tree layer are also little-endian.  Prior to version 2.6.3, the SQLite engine was only able to read databasesthat used the same byte order as the processor they were running on.But beginning with 2.6.3, SQLite can read or write databases in anybyte order.</p><p>After the byte-order code are six 4-byte integers.  Each integer is in thebyte order determined by the byte-order code.  The first integer is thepage number for the first page of the freelist.  If there are no unusedpages in the database, then this integer is 0.  The second integer isthe number of unused pages in the database.  The last 4 integers arenot used by the b-tree layer.  These are the so-called "meta" values thatare passed up to the schema layerand used there for configuration and format version information.All bytes of page 1 past beyond the meta-value integers are unused and are initialized to zero.</p><p>Here is a summary of the information contained on page 1 in the b-tree layer:</p><ul><li>48 byte header string</li><li>4 byte integer used to determine the byte-order</li><li>4 byte integer which is the first page of the freelist</li><li>4 byte integer which is the number of pages on the freelist</li><li>36 bytes of meta-data arranged as nine 4-byte integers</li><li>928 bytes of unused space</li></ul><h4>3.2 &nbsp; Structure Of A Single B-Tree Page</h4><p>Conceptually, a b-tree page contains N database entries and N+1 pointersto other b-tree pages.</p><blockquote><table border=1 cellspacing=0 cellpadding=5><tr><td align="center">Ptr<br>0</td><td align="center">Entry<br>0</td><td align="center">Ptr<br>1</td><td align="center">Entry<br>1</td><td align="center"><b>...</b></td><td align="center">Ptr<br>N-1</td><td align="center">Entry<br>N-1</td><td align="center">Ptr<br>N</td></tr></table></blockquote><p>The entries are arranged in increasing order.  That is, the key toEntry 0 is less than the key to Entry 1, and the key to Entry 1 isless than the key of Entry 2, and so forth.  The pointers point topages containing additional entries that have keys in between theentries on either side.  So Ptr 0 points to another b-tree page thatcontains entries that all have keys less than Key 0, and Ptr 1points to a b-tree pages where all entries have keys greater than Key 0but less than Key 1, and so forth.</p><p>Each b-tree page in SQLite consists of a header, zero or more "cells"each holding a single entry and pointer, and zero or more "free blocks"that represent unused space on the page.</p><p>The header on a b-tree page is the first 8 bytes of the page.The header contains the valueof the right-most pointer (Ptr N) and the byte offset into the pageof the first cell and the first free block.  The pointer is a 32-bitvalue and the offsets are each 16-bit values.  We have:</p><blockquote><table border=1 cellspacing=0 cellpadding=5><tr><td align="center" width=30>0</td><td align="center" width=30>1</td><td align="center" width=30>2</td><td align="center" width=30>3</td><td align="center" width=30>4</td><td align="center" width=30>5</td><td align="center" width=30>6</td><td align="center" width=30>7</td></tr><tr><td align="center" colspan=4>Ptr N</td><td align="center" colspan=2>Cell 0</td><td align="center" colspan=2>Freeblock 0</td></tr></table></blockquote><p>The 1016 bytes of a b-tree page that come after the header containcells and freeblocks.  All 1016 bytes are covered by either a cellor a freeblock.</p><p>The cells are connected in a linked list.  Cell 0 contains Ptr 0 andEntry 0.  Bytes 4 and 5 of the header point to Cell 0.  Cell 0 thenpoints to Cell 1 which contains Ptr 1 and Entry 1.  And so forth.Cells vary in size.  Every cell has a 12-byte header and at least 4bytes of payload space.  Space is allocated to payload in incrementsof 4 bytes.  Thus the minimum size of a cell is 16 bytes and up to63 cells can fit on a single page.  The size of a cell is always a multipleof 4 bytes.A cell can have up to 238 bytes of payload space.  Ifthe payload is more than 238 bytes, then an additional 4 byte pagenumber is appended to the cell which is the page number of the firstoverflow page containing the additional payload.  The maximum sizeof a cell is thus 254 bytes, meaning that a least 4 cells can fit intothe 1016 bytes of space available on a b-tree page.An average cell is usually around 52 to 100 bytes in size with about10 or 20 cells to a page.</p><p>The data layout of a cell looks like this:</p><blockquote><table border=1 cellspacing=0 cellpadding=5><tr><td align="center" width=20>0</td><td align="center" width=20>1</td><td align="center" width=20>2</td><td align="center" width=20>3</td><td align="center" width=20>4</td><td align="center" width=20>5</td><td align="center" width=20>6</td><td align="center" width=20>7</td><td align="center" width=20>8</td><td align="center" width=20>9</td><td align="center" width=20>10</td><td align="center" width=20>11</td><td align="center" width=100>12 ... 249</td><td align="center" width=20>250</td><td align="center" width=20>251</td><td align="center" width=20>252</td><td align="center" width=20>253</td></tr><tr><td align="center" colspan=4>Ptr</td><td align="center" colspan=2>Keysize<br>(low)</td><td align="center" colspan=2>Next</td><td align="center" colspan=1>Ksz<br>(hi)</td><td align="center" colspan=1>Dsz<br>(hi)</td><td align="center" colspan=2>Datasize<br>(low)</td><td align="center" colspan=1>Payload</td><td align="center" colspan=4>Overflow<br>Pointer</td></tr></table></blockquote><p>The first four bytes are the pointer.  The size of the key is a 24-bitwhere the upper 8 bits are taken from byte 8 and the lower 16 bits aretaken from bytes 4 and 5 (or bytes 5 and 4 on little-endian machines.)The size of the data is another 24-bit value where the upper 8 bitsare taken from byte 9 and the lower 16 bits are taken from bytes 10 and11 or 11 and 10, depending on the byte order.  Bytes 6 and 7 are theoffset to the next cell in the linked list of all cells on the currentpage.  This offset is 0 for the last cell on the page.</p><p>The payload itself can be any number of bytes between 1 and 1048576.But space to hold the payload is allocated in 4-byte chunks up to238 bytes.  If the entry contains more than 238 bytes of payload, thenadditional payload data is stored on a linked list of overflow pages.A 4 byte page number is appended to the cell that contains the firstpage of this linked list.</p><p>Each overflow page begins with a 4-byte value which is thepage number of the next overflow page in the list.   This value is0 for the last page in the list.  The remaining1020 bytes of the overflow page are available for storing payload.Note that a full page is allocated regardless of the number of overflowbytes stored.  Thus, if the total payload for an entry is 239 bytes,the first 238 are stored in the cell and the overflow page stores justone byte.</p><p>The structure of an overflow page looks like this:</p><blockquote><table border=1 cellspacing=0 cellpadding=5><tr><td align="center" width=20>0</td><td align="center" width=20>1</td><td align="center" width=20>2</td><td align="center" width=20>3</td><td align="center" width=200>4 ... 1023</td></tr><tr><td align="center" colspan=4>Next Page</td><td align="center" colspan=1>Overflow Data</td></tr></table></blockquote><p>All space on a b-tree page which is not used by the header or by cellsis filled by freeblocks.  Freeblocks, like cells, are variable in size.The size of a freeblock is at least 4 bytes and is always a multiple of4 bytes.The first 4 bytes contain a header and the remaining bytesare unused.  The structure of the freeblock is as follows:</p><blockquote><table border=1 cellspacing=0 cellpadding=5><tr><td align="center" width=20>0</td><td align="center" width=20>1</td><td align="center" width=20>2</td><td align="center" width=20>3</td><td align="center" width=200>4 ... 1015</td></tr><tr><td align="center" colspan=2>Size</td><td align="center" colspan=2>Next</td><td align="center" colspan=1>Unused</td></tr></table></blockquote><p>Freeblocks are stored in a linked list in increasing order.  That isto say, the first freeblock occurs at a lower index into the page thanthe second free block, and so forth.  The first 2 bytes of the headerare an integer which is the total number of bytes in the freeblock.The second 2 bytes are the index into the page of the next freeblockin the list.  The last freeblock has a Next value of 0.</p><p>When a new b-tree is created in a database, the root page of the b-treeconsist of a header and a single 1016 byte freeblock.  As entries areadded, space is carved off of that freeblock and used to make cells.When b-tree entries are deleted, the space used by their cells is convertedinto freeblocks.  Adjacent freeblocks are merged, but the page can stillbecome fragmented.  The b-tree code will occasionally try to defragmentthe page by moving all cells to the beginning and constructing a singlefreeblock at the end to take up all remaining space.</p><h4>3.3 &nbsp; The B-Tree Free Page List</h4><p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -