Esempio n. 1
0
  /**
   * Defragment the page given. All Cells are moved to the end of the page and all free space is
   * collected into one big FreeBlk that occurs in between the header and cell pointer array and the
   * cell content area.
   *
   * @throws SqlJetException
   */
  private void defragmentPage() throws SqlJetException {

    final SqlJetMemPage pPage = this;

    int i; /* Loop counter */
    int pc; /* Address of a i-th cell */
    int hdr; /* Offset to the page header */
    int size; /* Size of a cell */
    int usableSize; /* Number of usable bytes on a page */
    int cellOffset; /* Offset to the cell pointer array */
    int cbrk; /* Offset to the cell content area */
    int nCell; /* Number of cells on the page */
    ISqlJetMemoryPointer data; /* The page data */
    ISqlJetMemoryPointer temp; /* Temp area for cell content */
    int iCellFirst; /* First allowable cell index */
    int iCellLast; /* Last possible cell index */

    assert (pPage.pDbPage.isWriteable());
    assert (pPage.pBt != null);
    assert (pPage.pBt.usableSize <= ISqlJetLimits.SQLJET_MAX_PAGE_SIZE);
    assert (pPage.nOverflow == 0);
    assert (pPage.pBt.mutex.held());
    temp = pPage.pBt.pPager.getTempSpace();
    data = pPage.aData;
    hdr = pPage.hdrOffset;
    cellOffset = pPage.cellOffset;
    nCell = pPage.nCell;
    assert (nCell == get2byte(data, hdr + 3));
    usableSize = pPage.pBt.usableSize;
    cbrk = get2byte(data, hdr + 5);
    memcpy(temp, cbrk, data, cbrk, usableSize - cbrk);
    cbrk = usableSize;
    iCellFirst = cellOffset + 2 * nCell;
    iCellLast = usableSize - 4;
    for (i = 0; i < nCell; i++) {
      final ISqlJetMemoryPointer pAddr =
          data.getBuffer().getPointer(cellOffset + i * 2); /* The i-th cell pointer */
      pc = get2byte(pAddr);
      if (pc < iCellFirst || pc > iCellLast) {
        throw new SqlJetException(SqlJetErrorCode.CORRUPT);
      }
      assert (pc >= iCellFirst && pc <= iCellLast);
      size = pPage.cellSizePtr(temp.getBuffer().getPointer(pc));
      cbrk -= size;
      if (cbrk < iCellFirst || pc + size > usableSize) {
        throw new SqlJetException(SqlJetErrorCode.CORRUPT);
      }
      assert (cbrk + size <= usableSize && cbrk >= iCellFirst);
      memcpy(data, cbrk, temp, pc, size);
      put2byte(pAddr, cbrk);
    }
    assert (cbrk >= iCellFirst);
    put2byte(data, hdr + 5, cbrk);
    SqlJetUtility.putUnsignedByte(data, hdr + 1, (byte) 0);
    SqlJetUtility.putUnsignedByte(data, hdr + 2, (byte) 0);
    SqlJetUtility.putUnsignedByte(data, hdr + 7, (byte) 0);
    memset(data, iCellFirst, (byte) 0, cbrk - iCellFirst);
    assert (pPage.pDbPage.isWriteable());
    if (cbrk - iCellFirst != pPage.nFree) {
      throw new SqlJetException(SqlJetErrorCode.CORRUPT);
    }
  }
Esempio n. 2
0
  /** Add a page of the database file to the freelist. unref() is NOT called for pPage. */
  public void freePage() throws SqlJetException {
    SqlJetMemPage pPage1 = pBt.pPage1;
    int n, k;

    /* Prepare the page for freeing */
    assert (pBt.mutex.held());
    assert (this.pgno > 1);
    this.isInit = false;

    /* Increment the free page count on pPage1 */
    pPage1.pDbPage.write();
    n = get4byte(pPage1.aData, 36);
    put4byte(pPage1.aData, 36, n + 1);

    if (ISqlJetConfig.SECURE_DELETE) {
      /*
       * If the SQLITE_SECURE_DELETE compile-time option is enabled, then
       * always fully overwrite deleted information with zeros.
       */
      pDbPage.write();
      memset(aData, (byte) 0, pBt.pageSize);
    }

    /*
     * If the database supports auto-vacuum, write an entry in the
     * pointer-map to indicate that the page is free.
     */
    if (ISAUTOVACUUM()) {
      pBt.ptrmapPut(pgno, SqlJetBtreeShared.PTRMAP_FREEPAGE, 0);
    }

    if (n == 0) {
      /* This is the first free page */
      pDbPage.write();
      memset(aData, (byte) 0, 8);
      put4byte(pPage1.aData, 32, pgno);
      TRACE("FREE-PAGE: %d first\n", this.pgno);
    } else {
      /*
       * Other free pages already exist. Retrive the first trunk page* of
       * the freelist and find out how many leaves it has.
       */
      SqlJetMemPage pTrunk;
      pTrunk = pBt.getPage(get4byte(pPage1.aData, 32), false);
      k = get4byte(pTrunk.aData, 4);
      if (k >= pBt.usableSize / 4 - 8) {
        /*
         * The trunk is full. Turn the page being freed into a new*
         * trunk page with no leaves.** Note that the trunk page is not
         * really full until it contains* usableSize/4 - 2 entries, not
         * usableSize/4 - 8 entries as we have* coded. But due to a
         * coding error in versions of SQLite prior to* 3.6.0, databases
         * with freelist trunk pages holding more than* usableSize/4 - 8
         * entries will be reported as corrupt. In order* to maintain
         * backwards compatibility with older versions of SQLite,* we
         * will contain to restrict the number of entries to
         * usableSize/4 - 8* for now. At some point in the future (once
         * everyone has upgraded* to 3.6.0 or later) we should consider
         * fixing the conditional above* to read "usableSize/4-2"
         * instead of "usableSize/4-8".
         */
        pDbPage.write();
        put4byte(aData, pTrunk.pgno);
        put4byte(aData, 4, 0);
        put4byte(pPage1.aData, 32, pgno);
        TRACE("FREE-PAGE: %d new trunk page replacing %d\n", this.pgno, pTrunk.pgno);
      } else if (k < 0) {
        throw new SqlJetException(SqlJetErrorCode.CORRUPT);
      } else {
        /* Add the newly freed page as a leaf on the current trunk */
        pTrunk.pDbPage.write();
        put4byte(pTrunk.aData, 4, k + 1);
        put4byte(pTrunk.aData, 8 + k * 4, pgno);
        if (ISqlJetConfig.SECURE_DELETE) {
          pDbPage.dontWrite();
        }
        TRACE("FREE-PAGE: %d leaf on trunk page %d\n", this.pgno, pTrunk.pgno);
      }
      releasePage(pTrunk);
    }
  }
Esempio n. 3
0
  /*
   * * Return a section of the pPage->aData to the freelist.* The first byte
   * of the new free block is pPage->aDisk[start]* and the size of the block
   * is "size" bytes.** Most of the effort here is involved in coalesing
   * adjacent* free blocks into a single big free block.
   */
  private void freeSpace(int start, int size) throws SqlJetException {

    SqlJetMemPage pPage = this;

    int addr, pbegin, hdr;
    ISqlJetMemoryPointer data = pPage.aData;

    assert (pPage.pBt != null);
    assert (start >= pPage.hdrOffset + 6 + (pPage.leaf ? 0 : 4));
    assert ((start + size) <= pPage.pBt.usableSize);
    assert (pPage.pBt.mutex.held());
    assert (size >= 0); /* Minimum cell size is 4 */

    if (ISqlJetConfig.SECURE_DELETE) {
      /*
       * Overwrite deleted information with zeros when the SECURE_DELETE*
       * option is enabled at compile-time
       */
      memset(data, start, (byte) 0, size);
    }

    /* Add the space back into the linked list of freeblocks */
    hdr = pPage.hdrOffset;
    addr = hdr + 1;
    while ((pbegin = get2byte(data, addr)) < start && pbegin > 0) {
      assert (pbegin <= pPage.pBt.usableSize - 4);
      if (pbegin <= addr) {
        throw new SqlJetException(SqlJetErrorCode.CORRUPT);
      }
      addr = pbegin;
    }
    if (pbegin > pPage.pBt.usableSize - 4) {
      throw new SqlJetException(SqlJetErrorCode.CORRUPT);
    }
    assert (pbegin > addr || pbegin == 0);
    put2byte(data, addr, start);
    put2byte(data, start, pbegin);
    put2byte(data, start + 2, size);
    pPage.nFree += size;

    /* Coalesce adjacent free blocks */
    addr = pPage.hdrOffset + 1;
    while ((pbegin = get2byte(data, addr)) > 0) {
      int pnext, psize, x;
      assert (pbegin > addr);
      assert (pbegin <= pPage.pBt.usableSize - 4);
      pnext = get2byte(data, pbegin);
      psize = get2byte(data, pbegin + 2);
      if (pbegin + psize + 3 >= pnext && pnext > 0) {
        int frag = pnext - (pbegin + psize);
        if ((frag < 0) || (frag > (int) SqlJetUtility.getUnsignedByte(data, pPage.hdrOffset + 7))) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }
        SqlJetUtility.putUnsignedByte(
            data,
            pPage.hdrOffset + 7,
            (byte) (SqlJetUtility.getUnsignedByte(data, pPage.hdrOffset + 7) - (byte) frag));
        x = get2byte(data, pnext);
        put2byte(data, pbegin, x);
        x = pnext + get2byte(data, pnext + 2) - pbegin;
        put2byte(data, pbegin + 2, x);
      } else {
        addr = pbegin;
      }
    }

    /* If the cell content area begins with a freeblock, remove it. */
    if (SqlJetUtility.getUnsignedByte(data, hdr + 1) == SqlJetUtility.getUnsignedByte(data, hdr + 5)
        && SqlJetUtility.getUnsignedByte(data, hdr + 2)
            == SqlJetUtility.getUnsignedByte(data, hdr + 6)) {
      int top;
      pbegin = get2byte(data, hdr + 1);
      memcpy(data, hdr + 1, data, pbegin, 2);
      top = get2byte(data, hdr + 5) + get2byte(data, pbegin + 2);
      put2byte(data, hdr + 5, top);
    }
    assert (pPage.pDbPage.isWriteable());
  }
Esempio n. 4
0
  /**
   * Create the byte sequence used to represent a cell on page pPage and write that byte sequence
   * into pCell[]. Overflow pages are allocated and filled in as necessary. The calling procedure is
   * responsible for making sure sufficient space has been allocated for pCell[].
   *
   * <p>Note that pCell does not necessary need to point to the pPage->aData area. pCell might point
   * to some temporary storage. The cell will be constructed in this temporary area then copied into
   * pPage->aData later.
   *
   * @param pCell Complete text of the cell
   * @param pKey The key
   * @param nKey The key
   * @param pData The data
   * @param nData The data
   * @param nZero Extra zero bytes to append to pData
   * @return cell size
   * @throws SqlJetException
   */
  public int fillInCell(
      ISqlJetMemoryPointer pCell,
      ISqlJetMemoryPointer pKey,
      long nKey,
      ISqlJetMemoryPointer pData,
      int nData,
      int nZero)
      throws SqlJetException {

    final SqlJetMemPage pPage = this;
    int pnSize = 0;

    int nPayload;
    ISqlJetMemoryPointer pSrc;
    int nSrc, n;
    int spaceLeft;
    SqlJetMemPage pOvfl = null;
    SqlJetMemPage pToRelease = null;
    ISqlJetMemoryPointer pPrior;
    ISqlJetMemoryPointer pPayload;
    SqlJetBtreeShared pBt = pPage.pBt;
    int[] pgnoOvfl = {0};
    int nHeader;
    SqlJetBtreeCellInfo info;

    assert (pPage.pBt.mutex.held());

    /*
     * pPage is not necessarily writeable since pCell might be auxiliary*
     * buffer space that is separate from the pPage buffer area
     */
    assert (pCell.getBuffer() != pPage.aData.getBuffer() || pPage.pDbPage.isWriteable());

    /* Fill in the header. */
    nHeader = 0;
    if (!pPage.leaf) {
      nHeader += 4;
    }
    if (pPage.hasData) {
      nHeader += putVarint(pointer(pCell, nHeader), nData + nZero);
    } else {
      nData = nZero = 0;
    }
    nHeader += putVarint(pointer(pCell, nHeader), nKey);
    info = pPage.parseCellPtr(pCell);
    assert (info.nHeader == nHeader);
    assert (info.nKey == nKey);
    assert (info.nData == nData + nZero);

    /* Fill in the payload */
    nPayload = nData + nZero;
    if (pPage.intKey) {
      pSrc = pData;
      nSrc = nData;
      nData = 0;
    } else {
      /* TBD: Perhaps raise SQLITE_CORRUPT if nKey is larger than 31 bits? */
      nPayload += (int) nKey;
      pSrc = pKey;
      nSrc = (int) nKey;
    }
    pnSize = info.nSize;
    spaceLeft = info.nLocal;
    pPayload = pointer(pCell, nHeader);
    pPrior = pointer(pCell, info.iOverflow);

    while (nPayload > 0) {
      if (spaceLeft == 0) {
        int pgnoPtrmap = pgnoOvfl[0]; /*
                                               * Overflow page pointer-map entry
                                               * page
                                               */
        if (pBt.autoVacuum) {
          do {
            pgnoOvfl[0]++;
          } while (pBt.PTRMAP_ISPAGE(pgnoOvfl[0]) || pgnoOvfl[0] == pBt.PENDING_BYTE_PAGE());
        }
        try {
          pOvfl = pBt.allocatePage(pgnoOvfl, pgnoOvfl[0], false);
          /*
           * If the database supports auto-vacuum, and the second or
           * subsequent* overflow page is being allocated, add an
           * entry to the pointer-map* for that page now.** If this is
           * the first overflow page, then write a partial entry* to
           * the pointer-map. If we write nothing to this pointer-map
           * slot,* then the optimistic overflow chain processing in
           * clearCell()* may misinterpret the uninitialised values
           * and delete the* wrong pages from the database.
           */
          if (pBt.autoVacuum) {
            byte eType =
                (pgnoPtrmap != 0
                    ? SqlJetBtreeShared.PTRMAP_OVERFLOW2
                    : SqlJetBtreeShared.PTRMAP_OVERFLOW1);
            try {
              pBt.ptrmapPut(pgnoOvfl[0], eType, pgnoPtrmap);
            } catch (SqlJetException e) {
              releasePage(pOvfl);
            }
          }
        } catch (SqlJetException e) {
          releasePage(pToRelease);
          throw e;
        }

        /*
         * If pToRelease is not zero than pPrior points into the data
         * area* of pToRelease. Make sure pToRelease is still writeable.
         */
        assert (pToRelease == null || pToRelease.pDbPage.isWriteable());

        /*
         * If pPrior is part of the data area of pPage, then make sure
         * pPage* is still writeable
         */
        assert (pPrior.getBuffer() != pPage.aData.getBuffer() || pPage.pDbPage.isWriteable());

        put4byte(pPrior, pgnoOvfl[0]);
        releasePage(pToRelease);
        pToRelease = pOvfl;
        pPrior = pOvfl.aData;
        put4byte(pPrior, 0);
        pPayload = pointer(pOvfl.aData, 4);
        spaceLeft = pBt.usableSize - 4;
      }
      n = nPayload;
      if (n > spaceLeft) n = spaceLeft;

      /*
       * If pToRelease is not zero than pPayload points into the data area
       * * of pToRelease. Make sure pToRelease is still writeable.
       */
      assert (pToRelease == null || pToRelease.pDbPage.isWriteable());

      /*
       * If pPayload is part of the data area of pPage, then make sure
       * pPage* is still writeable
       */
      assert (pPayload.getBuffer() != pPage.aData.getBuffer() || pPage.pDbPage.isWriteable());

      if (nSrc > 0) {
        if (n > nSrc) n = nSrc;
        assert (pSrc != null);
        memcpy(pPayload, pSrc, n);
      } else {
        memset(pPayload, (byte) 0, n);
      }
      nPayload -= n;
      movePtr(pPayload, n);
      pSrc = pointer(pSrc, n);
      nSrc -= n;
      spaceLeft -= n;
      if (nSrc == 0) {
        nSrc = nData;
        pSrc = pData;
      }
    }
    releasePage(pToRelease);

    return pnSize;
  }