Backend Of Sqlite

| 分类 Database  | 标签 sqlite 

1 Backends

Sqlite 默认有两个后端,一个作为主存,另外一个用作存储临时表。

 1: /*
 2: ** Each database connection is an instance of the following structure.
 3: */
 4: struct sqlite3 {
 5:     sqlite3_vfs *pVfs;            /* OS Interface */
 6:     struct Vdbe *pVdbe;           /* List of active virtual machines */
 7:     CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
 8:     sqlite3_mutex *mutex;         /* Connection mutex */
 9:     Db *aDb;                      /* All backends */
10:     int nDb;                      /* Number of backends currently in use */
11:     //...
12:     Db aDbStatic[2];              /* Static space for the 2 default backends */
13:     //...
14: };
15: 
16: /*
17: ** Each database file to be accessed by the system is an instance
18: ** of the following structure.  There are normally two of these structures
19: ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
20: ** aDb[1] is the database file used to hold temporary tables.  Additional
21: ** databases may be attached.
22: */
23: struct Db {
24:     char *zName;         /* Name of this database */
25:     Btree *pBt;          /* The B*Tree structure for this database file */
26:     u8 safety_level;     /* How aggressive at syncing data to disk */
27:     Schema *pSchema;     /* Pointer to database schema (possibly shared) */
28: };

默认后端使用 BTree 实现,并在 在打开数据库时( openDatabase() ) 打开和设置:

 1: static int openDatabase(
 2:     const char *zFilename, /* Database filename UTF-8 encoded */
 3:     sqlite3 **ppDb,        /* OUT: Returned database handle */
 4:     unsigned int flags,    /* Operational flags */
 5:     const char *zVfs       /* Name of the VFS to use */
 6:                         ){
 7: 
 8:     //...
 9:     /* Open the backend database driver */
10:     rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
11:                           flags | SQLITE_OPEN_MAIN_DB);
12:     sqlite3BtreeEnter(db->aDb[0].pBt);
13:     db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
14:     sqlite3BtreeLeave(db->aDb[0].pBt);
15:     db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
16: 
17:     /* The default safety_level for the main database is 'full'; for the temp
18:     ** database it is 'NONE'. This matches the pager layer defaults.
19:     */
20:     db->aDb[0].zName = "main";
21:     db->aDb[0].safety_level = 3;
22:     db->aDb[1].zName = "temp";
23:     db->aDb[1].safety_level = 1;
24: 
25:     db->magic = SQLITE_MAGIC_OPEN;
26: 
27:     //...
28: }

2 BTree

3 Schema

4 DataGram

sq_bk.png


上一篇     下一篇