• sqlite Btree结构分析


    Btree结构中最要包含一个BtShared结构,该结构包含了一个打开的数据库的所有页面相关信息。

    View Code
    struct BtShared {
      Pager *pPager;        /* The page cache */
      BtCursor *pCursor;    /* A list of all open cursors */
      MemPage *pPage1;      /* First page of the database */
      u8 inStmt;            /* True if we are in a statement subtransaction */
      u8 readOnly;          /* True if the underlying file is readonly */
      u8 maxEmbedFrac;      /* Maximum payload as % of total page size */
      u8 minEmbedFrac;      /* Minimum payload as % of total page size */
      u8 minLeafFrac;       /* Minimum leaf payload as % of total page size */
      u8 pageSizeFixed;     /* True if the page size can no longer be changed */
    #ifndef SQLITE_OMIT_AUTOVACUUM
      u8 autoVacuum;        /* True if database supports auto-vacuum */
    #endif
      u16 pageSize;         /* Total number of bytes on a page */
      u16 usableSize;       /* Number of usable bytes on each page */
      int maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
      int minLocal;         /* Minimum local payload in non-LEAFDATA tables */
      int maxLeaf;          /* Maximum local payload in a LEAFDATA table */
      int minLeaf;          /* Minimum local payload in a LEAFDATA table */
      BusyHandler *pBusyHandler;   /* Callback for when there is lock contention */
      u8 inTransaction;     /* Transaction state */
      int nRef;             /* Number of references to this structure */
      int nTransaction;     /* Number of open transactions (read + write) */
      void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
      void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
    #ifndef SQLITE_OMIT_SHARED_CACHE
      BtLock *pLock;        /* List of locks held on this shared-btree struct */
      BtShared *pNext;      /* Next in ThreadData.pBtree linked list */
    #endif
    };

    1:pPager

    存储Btree页面缓存信息

    2:pCursor

    存储Btree中打开的一系列游标

    3:pPage1

    存放数据库文件的第一个页面

    4:maxEmbedFrac:

    Btree内部页中一个CELL最多能够使用的空间。255意味着100%,默认值为0x40(25%),这保证了一个页面至少包含4个CELL。

    5:minEmbedFrac:

    Btree内部页中一个CELL使用空间的最小值。默认值为0x20(12.5%)

    6:minLeafFrac:

    Btree叶子页中一个CELL使用空间的最小值。默认值为0x20(12.5%)

    7:pageSize:

    页面大小指示位(字节单位)

    8:pageSizeFixed

    页面大小能否改变指示位

    9:autoVacuum:

    数据库是否支持autoVacuum指示位。

    autoVacuum数据库:当一个事务从数据库中删除了数据并提交后,数据库文件的大小保持不变。即使整页的数据都被删除,该页也会变成“空闲页”等待再次被使用,而不会实际地被从数据库文件中删除。执行vacuum操作,可以通过重建数据库文件来清除数据库内所有的未用空间,使数据库文件变小。但是,如果一个数据库在创建时被指定为auto_vacuum数据库,当删除事务提交时,数据库文件会自动缩小。使用auto_vacuum数据库可以节省空间,但却会增加数据库操作的时间。

    10:usableSize

    指示每个页面可用的字节数。一个页面 尾部可能保留一部分空间作为扩展,实际可以使用的页面字节数就是“页面大小-保留空间”。

    11:maxLocal\ minLocal\maxLeaf\minLeaf

    页面的一个CELL可以使用的最大。最小空间实际大小,通过下面公式可计算出来。

      pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
      pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
      pBt->maxLeaf = pBt->usableSize - 35;
      pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;

    其中 12个字节为每个页面头的大小,23个字节为每个CELL内部的保存其它管理信息

      2-byte 保存CELL的地址
      4-byte 保存孩子节点的页号
      9-byte 最多可以使用9个字节来保存Btree中的关键字值
      4-byte 保存4个字节的数据值
      4-byte 保存overflow 页面号

    我们可以看到23个字节是最大可能使用的管理空间,根据配置不同实际不会使用到23个字节。

    12:pBusyHandler:

    保存客户端提供的数据库忙的回调函数句柄,用于忙时的回调。

    13:inTransaction

     存在TRANS_NONE\TRANS_READ\TRANS_WRITE三种状态,由于可能存在多个用户共享一个Btree结构,在TRANS_WRITE状态下只能有一个用户能够进行写事务。TRANS_READ 状态下任意多个用户能进行读事务。

    14:pLock

     一个表级锁的链表

    15:pNext

    一个线程可能打开不同的数据库,该结构表示该线程保存的一系列的Btree数据。


    转载请注明原始出处:http://www.cnblogs.com/chencheng/archive/2012/07/08/2581306.html

  • 相关阅读:
    Windows上安装配置SSH教程(3)——在Windows系统上安装与配置WinSCP
    Windows上安装配置SSH教程(2)——在Windows XP和Windows 10上安装并配置OpenSSH for Windows
    Windows上安装配置SSH教程(1)——知识点汇总
    Windows上安装配置SSH教程(5)——win10下使用Cygwin+Expect自动登陆ssh
    Win10安装cygwin并添加apt-cyg
    Windows上安装配置SSH教程(4)——WinSCP+OpenSSH 使用公钥自动登陆
    地线干扰与抑制(转)
    AMBA总线协议AHB、APB
    springcloud(六):配置中心git示例
    springcloud(四):熔断器Hystrix
  • 原文地址:https://www.cnblogs.com/chencheng/p/2581306.html
Copyright © 2020-2023  润新知