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

📄 btree.c

📁 SQLite的VS2005封装
💻 C
📖 第 1 页 / 共 5 页
字号:
      data[pPage->hdrOffset+7] -= frag;      put2byte(&data[pbegin], get2byte(&data[pnext]));      put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);    }else{      addr = pbegin;    }  }  /* If the cell content area begins with a freeblock, remove it. */  if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){    int top;    pbegin = get2byte(&data[hdr+1]);    memcpy(&data[hdr+1], &data[pbegin], 2);    top = get2byte(&data[hdr+5]);    put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));  }}/*** Decode the flags byte (the first byte of the header) for a page** and initialize fields of the MemPage structure accordingly.**** Only the following combinations are supported.  Anything different** indicates a corrupt database files:****         PTF_ZERODATA**         PTF_ZERODATA | PTF_LEAF**         PTF_LEAFDATA | PTF_INTKEY**         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF*/static int decodeFlags(MemPage *pPage, int flagByte){  BtShared *pBt;     /* A copy of pPage->pBt */  assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );  assert( sqlite3_mutex_held(pPage->pBt->mutex) );  pPage->leaf = flagByte>>3;  assert( PTF_LEAF == 1<<3 );  flagByte &= ~PTF_LEAF;  pPage->childPtrSize = 4-4*pPage->leaf;  pBt = pPage->pBt;  if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){    pPage->intKey = 1;    pPage->hasData = pPage->leaf;    pPage->maxLocal = pBt->maxLeaf;    pPage->minLocal = pBt->minLeaf;  }else if( flagByte==PTF_ZERODATA ){    pPage->intKey = 0;    pPage->hasData = 0;    pPage->maxLocal = pBt->maxLocal;    pPage->minLocal = pBt->minLocal;  }else{    return SQLITE_CORRUPT_BKPT;  }  return SQLITE_OK;}/*** Initialize the auxiliary information for a disk block.**** The pParent parameter must be a pointer to the MemPage which** is the parent of the page being initialized.  The root of a** BTree has no parent and so for that page, pParent==NULL.**** Return SQLITE_OK on success.  If we see that the page does** not contain a well-formed database page, then return ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not** guarantee that the page is well-formed.  It only shows that** we failed to detect any corruption.*/int sqlite3BtreeInitPage(  MemPage *pPage,        /* The page to be initialized */  MemPage *pParent       /* The parent.  Might be NULL */){  int pc;            /* Address of a freeblock within pPage->aData[] */  int hdr;           /* Offset to beginning of page header */  u8 *data;          /* Equal to pPage->aData */  BtShared *pBt;        /* The main btree structure */  int usableSize;    /* Amount of usable space on each page */  int cellOffset;    /* Offset from start of page to first cell pointer */  int nFree;         /* Number of unused bytes on the page */  int top;           /* First byte of the cell content area */  pBt = pPage->pBt;  assert( pBt!=0 );  assert( pParent==0 || pParent->pBt==pBt );  assert( sqlite3_mutex_held(pBt->mutex) );  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );  if( pPage==pParent ){    return SQLITE_CORRUPT_BKPT;  }  if( (pPage->pParent!=pParent)   && (pPage->pParent!=0 || pPage->isInit==PAGE_ISINIT_FULL) ){    /* The parent page should never change unless the file is corrupt */    return SQLITE_CORRUPT_BKPT;  }  if( pPage->isInit==PAGE_ISINIT_FULL ) return SQLITE_OK;  if( pParent!=0 ){    pPage->pParent = pParent;    sqlite3PagerRef(pParent->pDbPage);  }  if( pPage->isInit==PAGE_ISINIT_NONE ){    hdr = pPage->hdrOffset;    data = pPage->aData;    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;    assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );    pPage->maskPage = pBt->pageSize - 1;    pPage->nOverflow = 0;    pPage->idxShift = 0;    usableSize = pBt->usableSize;    pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;    top = get2byte(&data[hdr+5]);    pPage->nCell = get2byte(&data[hdr+3]);    if( pPage->nCell>MX_CELL(pBt) ){      /* To many cells for a single page.  The page must be corrupt */      return SQLITE_CORRUPT_BKPT;    }    if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){      /* All pages must have at least one cell, except for root pages */      return SQLITE_CORRUPT_BKPT;    }      /* Compute the total free space on the page */    pc = get2byte(&data[hdr+1]);    nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);    while( pc>0 ){      int next, size;      if( pc>usableSize-4 ){        /* Free block is off the page */        return SQLITE_CORRUPT_BKPT;       }      next = get2byte(&data[pc]);      size = get2byte(&data[pc+2]);      if( next>0 && next<=pc+size+3 ){        /* Free blocks must be in accending order */        return SQLITE_CORRUPT_BKPT;       }      nFree += size;      pc = next;    }    pPage->nFree = nFree;    if( nFree>=usableSize ){      /* Free space cannot exceed total page size */      return SQLITE_CORRUPT_BKPT;     }  }#if 0  /* Check that all the offsets in the cell offset array are within range.   **   ** Omitting this consistency check and using the pPage->maskPage mask  ** to prevent overrunning the page buffer in findCell() results in a  ** 2.5% performance gain.  */  {    u8 *pOff;        /* Iterator used to check all cell offsets are in range */    u8 *pEnd;        /* Pointer to end of cell offset array */    u8 mask;         /* Mask of bits that must be zero in MSB of cell offsets */    mask = ~(((u8)(pBt->pageSize>>8))-1);    pEnd = &data[cellOffset + pPage->nCell*2];    for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2);    if( pOff!=pEnd ){      return SQLITE_CORRUPT_BKPT;    }  }#endif  pPage->isInit = PAGE_ISINIT_FULL;  return SQLITE_OK;}/*** Set up a raw page so that it looks like a database page holding** no entries.*/static void zeroPage(MemPage *pPage, int flags){  unsigned char *data = pPage->aData;  BtShared *pBt = pPage->pBt;  int hdr = pPage->hdrOffset;  int first;  assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );  assert( sqlite3PagerGetData(pPage->pDbPage) == data );  assert( sqlite3PagerIswriteable(pPage->pDbPage) );  assert( sqlite3_mutex_held(pBt->mutex) );  /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/  data[hdr] = flags;  first = hdr + 8 + 4*((flags&PTF_LEAF)==0);  memset(&data[hdr+1], 0, 4);  data[hdr+7] = 0;  put2byte(&data[hdr+5], pBt->usableSize);  pPage->nFree = pBt->usableSize - first;  decodeFlags(pPage, flags);  pPage->hdrOffset = hdr;  pPage->cellOffset = first;  pPage->nOverflow = 0;  assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );  pPage->maskPage = pBt->pageSize - 1;  pPage->idxShift = 0;  pPage->nCell = 0;  pPage->isInit = PAGE_ISINIT_FULL;}/*** Convert a DbPage obtained from the pager into a MemPage used by** the btree layer.*/static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);  pPage->aData = sqlite3PagerGetData(pDbPage);  pPage->pDbPage = pDbPage;  pPage->pBt = pBt;  pPage->pgno = pgno;  pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;  return pPage; }/*** Get a page from the pager.  Initialize the MemPage.pBt and** MemPage.aData elements if needed.**** If the noContent flag is set, it means that we do not care about** the content of the page at this time.  So do not go to the disk** to fetch the content.  Just fill in the content with zeros for now.** If in the future we call sqlite3PagerWrite() on this page, that** means we have started to be concerned about content and the disk** read should occur at that point.*/int sqlite3BtreeGetPage(  BtShared *pBt,       /* The btree */  Pgno pgno,           /* Number of the page to fetch */  MemPage **ppPage,    /* Return the page in this parameter */  int noContent        /* Do not load page content if true */){  int rc;  DbPage *pDbPage;  assert( sqlite3_mutex_held(pBt->mutex) );  rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);  if( rc ) return rc;  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);  return SQLITE_OK;}/*** Return the size of the database file in pages.  Or return -1 if** there is any kind of error.*/static int pagerPagecount(Pager *pPager){  int rc;  int nPage;  rc = sqlite3PagerPagecount(pPager, &nPage);  return (rc==SQLITE_OK?nPage:-1);}/*** Get a page from the pager and initialize it.  This routine** is just a convenience wrapper around separate calls to** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().*/static int getAndInitPage(  BtShared *pBt,          /* The database file */  Pgno pgno,           /* Number of the page to get */  MemPage **ppPage,    /* Write the page pointer here */  MemPage *pParent     /* Parent of the page */){  int rc;  DbPage *pDbPage;  MemPage *pPage;  assert( sqlite3_mutex_held(pBt->mutex) );  assert( !pParent || pParent->isInit==PAGE_ISINIT_FULL );  if( pgno==0 ){    return SQLITE_CORRUPT_BKPT;   }  /* It is often the case that the page we want is already in cache.  ** If so, get it directly.  This saves us from having to call  ** pagerPagecount() to make sure pgno is within limits, which results  ** in a measureable performance improvements.  */  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);  if( pDbPage ){    /* Page is already in cache */    *ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt);    rc = SQLITE_OK;  }else{    /* Page not in cache.  Acquire it. */    if( pgno>pagerPagecount(pBt->pPager) ){      return SQLITE_CORRUPT_BKPT;     }    rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);    if( rc ) return rc;    pPage = *ppPage;  }  if( pPage->isInit!=PAGE_ISINIT_FULL ){    rc = sqlite3BtreeInitPage(pPage, pParent);  }else if( pParent && (pPage==pParent || pPage->pParent!=pParent) ){    /* This condition indicates a loop in the b-tree structure (the scenario    ** where database corruption has caused a page to be a direct or    ** indirect descendant of itself).    */     rc = SQLITE_CORRUPT_BKPT;  }  if( rc!=SQLITE_OK ){    releasePage(pPage);    *ppPage = 0;  }  return rc;}/*** Release a MemPage.  This should be called once for each prior** call to sqlite3BtreeGetPage.*/static void releasePage(MemPage *pPage){  if( pPage ){    assert( pPage->aData );    assert( pPage->pBt );    assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );    assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );    assert( sqlite3_mutex_held(pPage->pBt->mutex) );    sqlite3PagerUnref(pPage->pDbPage);  }}/*** This routine is called when the reference count for a page** reaches zero.  We need to unref the pParent pointer when that** happens.*/static void pageDestructor(DbPage *pData){  MemPage *pPage;  pPage = (MemPage *)sqlite3PagerGetExtra(pData);  if( pPage ){    assert( pPage->isInit!=PAGE_ISINIT_FULL          || sqlite3_mutex_held(pPage->pBt->mutex)     );    if( pPage->pParent ){      MemPage *pParent = pPage->pParent;      assert( pParent->pBt==pPage->pBt );      pPage->pParent = 0;      releasePage(pParent);    }    if( pPage->isInit==PAGE_ISINIT_FULL ){      pPage->isInit = PAGE_ISINIT_DATA;    }  }}/*** During a rollback, when the pager reloads information into the cache** so that the cache is restored to its original state at the start of** the transaction, for each page restored this routine is called.**** This routine needs to reset the extra data section at the end of the** page to agree with the restored data.*/static void pageReinit(DbPage *pData){  MemPage *pPage;  pPage = (MemPage *)sqlite3PagerGetExtra(pData);  if( pPage->isInit==PAGE_ISINIT_FULL ){    assert( sqlite3_mutex_held(pPage->pBt->mutex) );    pPage->isInit = 0;    sqlite3BtreeInitPage(pPage, pPage->pParent);  }else if( pPage->isInit==PAGE_ISINIT_DATA ){    pPage->isInit = 0;  }}/*** Invoke the busy handler for a btree.*/static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){  BtShared *pBt = (BtShared*)pArg;  assert( pBt->db );  assert( sqlite3_mutex_held(pBt->db->mutex) );  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);}/*** Open a database file.** ** zFilename is the name of the database file.  If zFilename is NULL** a new database with a random name is created.  This randomly named** database file will be deleted when sqlite3BtreeClose() is called.** If zFilename is ":memory:" then an in-memory database is created** that is automatically destroyed when it is closed.*/int sqlite3BtreeOpen(  const char *zFilename,  /* Name of the file containing the BTree database */  sqlite3 *db,            /* Associated database handle */  Btree **ppBtree,        /* Pointer to new Btree object written here */  int flags,              /* Options */  int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */){  sqlite3_vfs *pVfs;      /* The VFS to use for this btree */  BtShared *pBt = 0;      /* Shared part of btree structure */  Btree *p;               /* Handle to return */  int rc = SQLITE_OK;  int nReserve;  unsigned char zDbHeader[100];  /* Set the variable isMemdb to true for an in-memory database, or   ** false for a file-based database. This symbol is only required if  ** either of the shared-data or autovacuum features are compiled   ** into the library.  */#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)  #ifdef SQLITE_OMIT_MEMORYDB    const int isMemdb = 0;  #else    const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");  #endif#endif  assert( db!=0 );  assert( sqlite3_mutex_held(db->mutex) );  pVfs = db->pVfs;  p = sqlite3MallocZero(sizeof(Btree));  if( !p ){    return SQLITE_NOMEM;  }

⌨️ 快捷键说明

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