Number Representations & States

"how numbers are stored and used in computers"

B-tree

The btree.c file is a core component of SQLite's storage engine. It implements the B-tree structures that manage how data is stored and retrieved from the database. a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time.

code.txt
1rc = allocateBtreePage(pBt, &pPage, &pgno, nearby, flags); 2zeroPage(pPage, flags | PTF_LEAF);

The page header is the first 64 bytes of the page. The page header contains information about the page, such as the page type, the page number, and the page size.

code.txt
1rc = balance(pCur);
code.txt
1rc = btreeCreateTable(pBt, pTable, zName, nCol, aCol, aType, aColl, pSchema);
code.txt
1rc = sqlite3BtreeMovetoUnpacked(pCur, pKey, biasRight, &pCur->ix, &loc);
code.txt
1rc = sqlite3BtreeInsert(pCur, pKey, pData, nData, pRowid); 2rc = sqlite3BtreeDelete(pCur, pKey, pData, nData, pRowid);
code.txt
1rc = sqlite3BtreeClearCursor(pCur); 2rc = sqlite3BtreeCloseCursor(pCur);
code.txt
1rc = sqlite3BtreeClose(pBt);
code.txt
1rc = sqlite3BtreeGetPage(pBt, pgno, &pPage, flags);

Page

The page is the smallest unit of data in SQLite. The page is 512 bytes.

code.txt
1sqlite3_prepare_v2(db, "SELECT * FROM test", -1, &stmt, NULL); 2sqlite3_step(stmt); 3sqlite3_finalize(stmt);

Page header

code.txt
1sqlite3_prepare_v2(db, "SELECT * FROM test", -1, &stmt, NULL); 2sqlite3_step(stmt); 3sqlite3_finalize(stmt);

B-tree header

The B-tree header is the next 64 bytes of the page. The B-tree header contains information about the B-tree, such as the root page number, the page type, and the page size.

code.txt
1sqlite3_prepare_v2(db, "SELECT * FROM test", -1, &stmt, NULL); 2sqlite3_step(stmt); 3sqlite3_finalize(stmt);

B-tree leaf page

The B-tree leaf page is the next 64 bytes of the page. The B-tree leaf page contains information about the B-tree, such as the root page number, the page type, and the page size.

code.txt
1sqlite3_prepare_v2(db, "SELECT * FROM test", -1, &stmt, NULL); 2sqlite3_step(stmt); 3sqlite3_finalize(stmt);

B-tree interior page

The B-tree interior page is the next 64 bytes of the page. The B-tree interior page contains information about the B-tree, such as the root page number, the page type, and the page size.

code.txt
1sqlite3_prepare_v2(db, "SELECT * FROM test", -1, &stmt, NULL); 2sqlite3_step(stmt); 3sqlite3_finalize(stmt);