SQLite学习笔记(16)-B-tree(2)

2.5.2 B-tree结构形式

SQLite利用B-tree来组织数据库。一个数据库中含有许多B-tree,每个表和索引都有一个B-tree,其中表利用b+tree来实现,而索引是利用b-tree来实现的。

2.5.2.1 B-tree

B-tree是一种多路搜索树,一颗M阶的b-tree,或为空树,或者:

1.       定义任意非叶子结点最多只有M个儿子;且M>2;

2.       根结点的儿子数为[2, M];

3.       除根结点以外的非叶子结点的儿子数为[M/2, M];

4.       每个结点存放至少M/2-1(取上整)和至多M-1个关键字;(至少2个关键字)

5.       非叶子结点的关键字个数=指向儿子的指针个数-1;

6.       非叶子结点的关键字:K[1], K[2], …, K[M-1];且K[i] < K[i+1];

7.       非叶子结点的指针:P[1], P[2], …, P[M];其中P[1]指向关键字小于K[1]的子树,P[M]指向关键字大于K[M-1]的子树,其它P[i]指向关键字属于(K[i-1], K[i])的子树;

8.       所有叶子结点位于同一层。

图2.8 一个3阶b-tree

如图2.8表示一个3阶的b-tree。B-tree的搜索,从根结点开始,对结点内的关键字(有序)序列进行二分查找,如果命中则结束,否则进入查询关键字所属范围的儿子结点;重复,直到所对应的儿子指针为空,或已经是叶子结点。B-tree拥有以下特性:

1.       关键字集合分布在整颗树中;

2.       任何一个关键字出现且只出现在一个结点中;

3.       搜索有可能在非叶子结点结束;

4.       其搜索性能等价于在关键字全集内做一次二分查找;

5.       自动层次控制。

2.5.2.2 B+tree

B+tree是b-tree的变体,也是一种多路搜索树。其定义与b-tree基本相同,除了:

1.       非叶子结点的子树指针与关键字个数相同;

2.       非叶子结点的子树指针P[i],指向关键字值属于[K[i], K[i+1])的子树(B-树是开区间);

3.       为所有叶子结点增加一个链指针;

4.       所有关键字都在叶子结点出现。

图2.9 一个3阶b+tree

如图2.9表示一个3阶的b+tree。B+tree的搜索与b-tree也基本相同,区别是b+tree只有达到叶子结点才命中(b-tree可以在非叶子结点命中),其性能也等价于在关键字全集做一次二分查找。不过b+tree可以很方便的做顺序遍历。B+tree拥有以下特性:

1.       所有关键字都出现在叶子结点的链表中(稠密索引),且链表中的关键字恰好是有序的;

2.       不可能在非叶子结点命中;

3.       非叶子结点相当于是叶子结点的索引(稀疏索引),叶子结点相当于是存储(关键字)数据的数据层;

4.       更适合文件索引系统。

2.5.2.3 Btree结构

Btree的定义在btreeInt.h,其结构如下所示:

structBtree {
  sqlite3 *db;       /* The database connection holding thisbtree */
  BtShared *pBt;     /* Sharable content of this btree */
  u8inTrans;        /* TRANS_NONE, TRANS_READor TRANS_WRITE */
  u8 sharable;       /* True if we can share pBt with anotherdb */
  u8 locked;         /* True if db currently has pBt locked*/
  u8 hasIncrblobCur; /* True if there are oneor more Incrblob cursors */
  int wantToLock;    /* Number of nested calls tosqlite3BtreeEnter() */
  int nBackup;       /* Number of backup operations readingthis btree */
  u32 iDataVersion;  /* Combines withpBt->pPager->iDataVersion */
  Btree *pNext;      /* List of other sharable Btrees from thesame db */
  Btree *pPrev;      /* Back pointer of the same list */
#ifndefSQLITE_OMIT_SHARED_CACHE
  BtLock lock;       /* Object used to lock page 1 */
#endif
};

在系统运行中,有些数据库文件会被多个连接共享。这时,每个连接都会含有它所占有的这个数据库文件部分。而这个数据库文件的每个部分都会连接到相同的BtShared对象中。BtShared的定义在btreeInt.h中,其结构如下所示:

structBtShared {
  Pager *pPager;        /* The page cache */
  sqlite3 *db;          /* Database connection currentlyusing this Btree */
  BtCursor *pCursor;    /* A list of all open cursors */
  MemPage*pPage1;      /* First page of thedatabase */
  u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
#ifndefSQLITE_OMIT_AUTOVACUUM
  u8 autoVacuum;        /* True if auto-vacuum is enabled */
  u8 incrVacuum;        /* True if incr-vacuum is enabled */
  u8 bDoTruncate;       /* True to truncate db on commit */
#endif
  u8 inTransaction;     /* Transaction state */
  u8 max1bytePayload;   /* Maximum first byte of cell for a 1-bytepayload */
#ifdefSQLITE_HAS_CODEC
  u8 optimalReserve;    /* Desired amount of reserved space perpage */
#endif
  u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
  u16 maxLocal;         /* Maximum local payload innon-LEAFDATA tables */
  u16 minLocal;         /* Minimum local payload in non-LEAFDATAtables */
  u16 maxLeaf;          /* Maximum local payload in aLEAFDATA table */
  u16 minLeaf;          /* Minimum local payload in aLEAFDATA table */
  u32 pageSize;         /* Total number of bytes on a page */
  u32 usableSize;       /* Number of usable bytes on each page*/
  int nTransaction;     /* Number of open transactions (read +write) */
  u32 nPage;            /* Number of pages in the database*/
  void *pSchema;        /* Pointer to space allocated bysqlite3BtreeSchema() */
  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
  sqlite3_mutex *mutex; /* Non-recursive mutexrequired to access this object */
  Bitvec *pHasContent;  /* Set of pages moved to free-list thistransaction */
#ifndefSQLITE_OMIT_SHARED_CACHE
  int nRef;             /* Number of references to thisstructure */
  BtShared *pNext;      /* Next on a list of sharable BtSharedstructs */
  BtLock *pLock;        /* List of locks held on thisshared-btree struct */
  Btree *pWriter;       /* Btree with currently open writetransaction */
#endif
  u8 *pTmpSpace;        /* Temp space sufficient to hold asingle cell */
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值