Exemple #1
0
  public void copyNodeContent(final SqlJetMemPage pTo) throws SqlJetException {
    final SqlJetMemPage pFrom = this;
    final SqlJetBtreeShared pBt = pFrom.pBt;
    final ISqlJetMemoryPointer aFrom = pFrom.aData;
    final ISqlJetMemoryPointer aTo = pTo.aData;
    int iFromHdr = pFrom.hdrOffset;
    int iToHdr = ((pTo.pgno == 1) ? 100 : 0);
    int iData;

    assert (pFrom.isInit);
    assert (pFrom.nFree >= iToHdr);
    assert (get2byte(aFrom.getMoved(iFromHdr + 5)) <= (int) pBt.usableSize);

    /* Copy the b-tree node content from page pFrom to page pTo. */
    iData = get2byte(aFrom.getMoved(iFromHdr + 5));
    memcpy(aTo.getMoved(iData), aFrom.getMoved(iData), pBt.usableSize - iData);
    memcpy(aTo.getMoved(iToHdr), aFrom.getMoved(iFromHdr), pFrom.cellOffset + 2 * pFrom.nCell);

    /* Reinitialize page pTo so that the contents of the MemPage structure
     ** match the new data. The initialization of pTo can actually fail under
     ** fairly obscure circumstances, even though it is a copy of initialized
     ** page pFrom.
     */
    pTo.isInit = false;
    pTo.initPage();

    /* If this is an auto-vacuum database, update the pointer-map entries
     ** for any b-tree or overflow pages that pTo now contains the pointers to.
     */
    if (ISAUTOVACUUM()) {
      pTo.setChildPtrmaps();
    }
  }
Exemple #2
0
  /**
   * Add a list of cells to a page. The page should be initially empty. The cells are guaranteed to
   * fit on the page.
   *
   * @param nCell The number of cells to add to this page
   * @param apCell Pointers to cell bodies
   * @param aSize Sizes of the cells
   * @throws SqlJetException
   */
  public void assemblePage(
      int nCell, ISqlJetMemoryPointer[] apCell, int apCellPos, int[] aSize, int aSizePos)
      throws SqlJetException {
    final SqlJetMemPage pPage = this;

    int i; /* Loop counter */
    int hdr = pPage.hdrOffset; /* Index of page header */

    ISqlJetMemoryPointer data = pPage.aData; /* Data for the page */
    int nUsable = pPage.pBt.usableSize;

    assert (pPage.nOverflow == 0);
    assert (pPage.pBt.mutex.held());
    assert (nCell >= 0 && nCell <= pPage.pBt.MX_CELL() && pPage.pBt.MX_CELL() <= 10921);
    assert (pPage.pDbPage.isWriteable());
    assert (pPage.nCell == 0);
    assert (get2byte(data.getMoved(hdr + 5)) == nUsable);

    ISqlJetMemoryPointer pCellPtr = data.getMoved(pPage.cellOffset + nCell * 2);
    int cellbody = nUsable;
    for (i = nCell - 1; i >= 0; i--) {
      int sz = aSize[apCellPos + i];
      pCellPtr = pCellPtr.getMoved(-2);
      cellbody -= sz;
      put2byte(pCellPtr, cellbody);
      memcpy(data.getMoved(cellbody), apCell[apCellPos + i], sz);
    }
    put2byte(data.getMoved(hdr + 3), nCell);
    put2byte(data.getMoved(hdr + 5), cellbody);
    pPage.nFree -= (2 * nCell + pPage.pBt.usableSize - cellbody);
    pPage.nCell = nCell;
  }
Exemple #3
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);
    }
  }
Exemple #4
0
  /**
   * Allocate nByte bytes of space on a page.
   *
   * <p>Return the index into pPage->aData[] of the first byte of the new allocation. The caller
   * guarantees that there is enough space. This routine will never fail.
   *
   * <p>If the page contains nBytes of free space but does not contain nBytes of contiguous free
   * space, then this routine automatically calls defragementPage() to consolidate all free space
   * before allocating the new chunk.
   *
   * @param nByte
   * @return
   * @throws SqlJetException
   */
  private int allocateSpace(int nByte) throws SqlJetException {

    final SqlJetMemPage pPage = this;

    int addr, pc, hdr;
    int size;
    int nFrag;
    ISqlJetMemoryPointer data;

    data = pPage.aData;
    assert (pPage.pDbPage.isWriteable());
    assert (pPage.pBt != null);
    assert (pPage.pBt.mutex.held());

    assert (nByte >= 0); /* Minimum cell size is 4 */
    assert (pPage.nFree >= nByte);
    assert (pPage.nOverflow == 0);
    int usableSize = pPage.pBt.usableSize;
    assert (nByte < usableSize - 8);

    hdr = pPage.hdrOffset;

    nFrag = SqlJetUtility.getUnsignedByte(data, hdr + 7);
    assert (pPage.cellOffset == hdr + 12 - 4 * (pPage.leaf ? 1 : 0));
    int gap = pPage.cellOffset + 2 * pPage.nCell;
    int top = get2byte(data.getMoved(hdr + 5));
    if (gap > top) {
      throw new SqlJetException(SqlJetErrorCode.CORRUPT);
    }
    if (nFrag >= 60) {
      pPage.defragmentPage();
      top = get2byte(data.getMoved(hdr + 5));
    } else if (gap + 2 <= top) {
      addr = hdr + 1;
      while ((pc = get2byte(data, addr)) > 0) {
        if (pc > usableSize - 4 || pc < addr + 4) {
          throw new SqlJetException(SqlJetErrorCode.CORRUPT);
        }
        size = get2byte(data, pc + 2);
        if (size >= nByte) {
          int x = size - nByte;
          if (x < 4) {
            memcpy(data, addr, data, pc, 2);
            SqlJetUtility.putUnsignedByte(data, hdr + 7, nFrag + x);
          } else if (size + pc > usableSize) {
            throw new SqlJetException(SqlJetErrorCode.CORRUPT);
          } else {
            put2byte(data, pc + 2, x);
          }
          return pc + x;
        }
        addr = pc;
      }
    }
    if (gap + 2 + nByte > top) {
      pPage.defragmentPage();
      top = get2byte(data, hdr + 5);
      assert (gap + nByte <= top);
    }
    top -= nByte;
    put2byte(data, hdr + 5, top);
    assert (top + nByte <= pPage.pBt.usableSize);
    return top;
  }
Exemple #5
0
  /**
   * Insert a new cell on pPage at cell index "i". pCell points to the content of the cell.
   *
   * <p>If the cell content will fit on the page, then put it there. If it will not fit, then make a
   * copy of the cell content into pTemp if pTemp is not null. Regardless of pTemp, allocate a new
   * entry in pPage->aOvfl[] and make it point to the cell content (either in pTemp or the original
   * pCell) and also record its index. Allocating a new entry in pPage->aCell[] implies that
   * pPage->nOverflow is incremented.
   *
   * <p>If nSkip is non-zero, then do not copy the first nSkip bytes of the cell. The caller will
   * overwrite them after this function returns. If nSkip is non-zero, then pCell may not point to
   * an invalid memory location (but pCell+nSkip is always valid).
   *
   * @param i New cell becomes the i-th cell of the page
   * @param pCell Content of the new cell
   * @param sz Bytes of content in pCell
   * @param pTemp Temp storage space for pCell, if needed
   * @param nSkip Do not write the first nSkip bytes of the cell
   * @throws SqlJetException
   */
  public void insertCell(
      int i, ISqlJetMemoryPointer pCell, int sz, ISqlJetMemoryPointer pTemp, int iChild)
      throws SqlJetException {

    int nSkip = (iChild > 0 ? 4 : 0);

    final SqlJetMemPage pPage = this;

    int idx; /* Where to write new cell content in data[] */
    int j; /* Loop counter */
    int top; /* First byte of content for any cell in data[] */
    int end; /* First byte past the last cell pointer in data[] */
    int ins; /* Index in data[] where new cell pointer is inserted */
    int hdr; /* Offset into data[] of the page header */
    int cellOffset; /* Address of first cell pointer in data[] */
    ISqlJetMemoryPointer data; /* The content of the whole page */

    assert (i >= 0 && i <= pPage.nCell + pPage.nOverflow);
    assert (pPage.nCell <= pPage.pBt.MX_CELL() && pPage.pBt.MX_CELL() <= 5460);
    assert (pPage.nOverflow <= pPage.aOvfl.length);
    assert (sz == pPage.cellSizePtr(pCell) || (sz == 8 && iChild > 0));
    assert (pPage.pBt.mutex.held());
    if (pPage.nOverflow != 0 || sz + 2 > pPage.nFree) {
      if (pTemp != null) {
        memcpy(pTemp, nSkip, pCell, nSkip, sz - nSkip);
        pCell = pTemp;
      }
      if (iChild > 0) {
        put4byte(pCell, iChild);
      }
      j = pPage.nOverflow++;
      // assert( j<(int)(sizeof(pPage.aOvfl)/sizeof(pPage.aOvfl[0])) );
      pPage.aOvfl[j].pCell = pCell;
      pPage.aOvfl[j].idx = i;
    } else {
      pPage.pDbPage.write();
      assert (pPage.pDbPage.isWriteable());
      data = pPage.aData;
      hdr = pPage.hdrOffset;
      top = get2byte(data, hdr + 5);
      cellOffset = pPage.cellOffset;
      end = cellOffset + 2 * pPage.nCell + 2;
      ins = cellOffset + 2 * i;
      if (end > top - sz) {
        pPage.defragmentPage();
        top = get2byte(data, hdr + 5);
        assert (end + sz <= top);
      }
      idx = pPage.allocateSpace(sz);
      assert (idx > 0);
      assert (end <= get2byte(data, hdr + 5));
      if (idx + sz > pPage.pBt.usableSize) {
        throw new SqlJetException(SqlJetErrorCode.CORRUPT);
      }
      pPage.nCell++;
      pPage.nFree -= (2 + sz);
      memcpy(data, idx + nSkip, pCell, nSkip, sz - nSkip);
      if (iChild > 0) {
        put4byte(data, idx, iChild);
      }
      for (j = end - 2; j > ins; j -= 2) {
        SqlJetUtility.putUnsignedByte(data, j, SqlJetUtility.getUnsignedByte(data, j - 2));
        SqlJetUtility.putUnsignedByte(data, j + 1, SqlJetUtility.getUnsignedByte(data, j - 1));
      }
      put2byte(data, ins, idx);
      put2byte(data, hdr + 3, pPage.nCell);
      if (pPage.pBt.autoVacuum) {
        /*
         * The cell may contain a pointer to an overflow page. If so,
         * write* the entry for the overflow page into the pointer map.
         */
        SqlJetBtreeCellInfo info = pPage.parseCellPtr(pCell);
        assert ((info.nData + (pPage.intKey ? 0 : info.nKey)) == info.nPayload);
        if ((info.nData + (pPage.intKey ? 0 : info.nKey)) > info.nLocal) {
          int pgnoOvfl = get4byte(pCell, info.iOverflow);
          pPage.pBt.ptrmapPut(pgnoOvfl, SqlJetBtreeShared.PTRMAP_OVERFLOW1, pPage.pgno);
        }
      }
    }
  }
Exemple #6
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());
  }
Exemple #7
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;
  }