Пример #1
0
        internal void assemblePage(int nCell, byte[] apCell, int[] aSize)
        {
            Debug.Assert(this.NOverflows == 0);
            Debug.Assert(MutexEx.Held(this.Shared.Mutex));
            Debug.Assert(nCell >= 0 && nCell <= (int)Btree.MX_CELL(this.Shared) && (int)Btree.MX_CELL(this.Shared) <= 10921);
            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            // Check that the page has just been zeroed by zeroPage()
            Debug.Assert(this.Cells == 0);
            //
            var data    = this.Data;                   // Pointer to data for pPage
            int hdr     = this.HeaderOffset;           // Offset of header on pPage
            var nUsable = (int)this.Shared.UsableSize; // Usable size of page

            Debug.Assert(ConvertEx.Get2nz(data, hdr + 5) == nUsable);
            var pCellptr = this.CellOffset + nCell * 2; // Address of next cell pointer
            var cellbody = nUsable;                     // Address of next cell body

            for (var i = nCell - 1; i >= 0; i--)
            {
                var sz = (ushort)aSize[i];
                pCellptr -= 2;
                cellbody -= sz;
                ConvertEx.Put2(data, pCellptr, cellbody);
                Buffer.BlockCopy(apCell, 0, data, cellbody, sz);
            }
            ConvertEx.Put2(data, hdr + 3, nCell);
            ConvertEx.Put2(data, hdr + 5, cellbody);
            this.FreeBytes -= (ushort)(nCell * 2 + nUsable - cellbody);
            this.Cells      = (ushort)nCell;
        }
Пример #2
0
        internal void zeroPage(int flags)
        {
            var data = this.Data;
            var pBt  = this.Shared;
            var hdr  = this.HeaderOffset;

            Debug.Assert(Pager.GetPageID(this.DbPage) == this.ID);
            Debug.Assert(Pager.sqlite3PagerGetExtra <MemPage>(this.DbPage) == this);
            Debug.Assert(Pager.sqlite3PagerGetData(this.DbPage) == data);
            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            Debug.Assert(MutexEx.Held(pBt.Mutex));
            if (pBt.SecureDelete)
            {
                Array.Clear(data, hdr, (int)(pBt.UsableSize - hdr));
            }
            data[hdr] = (byte)flags;
            var first = (ushort)(hdr + 8 + 4 * ((flags & Btree.PTF_LEAF) == 0 ? 1 : 0));

            Array.Clear(data, hdr + 1, 4);
            data[hdr + 7] = 0;
            ConvertEx.Put2(data, hdr + 5, pBt.UsableSize);
            this.FreeBytes = (ushort)(pBt.UsableSize - first);
            decodeFlags(flags);
            this.HeaderOffset = hdr;
            this.CellOffset   = first;
            this.NOverflows   = 0;
            Debug.Assert(pBt.PageSize >= 512 && pBt.PageSize <= 65536);
            this.MaskPage = (ushort)(pBt.PageSize - 1);
            this.Cells    = 0;
            this.HasInit  = true;
        }
Пример #3
0
        internal void dropCell(int idx, int sz, ref RC pRC)
        {
            if (pRC != RC.OK)
            {
                return;
            }
            Debug.Assert(idx >= 0 && idx < this.Cells);
#if DEBUG
            Debug.Assert(sz == cellSize(idx));
#endif
            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            Debug.Assert(MutexEx.Held(this.Shared.Mutex));
            var data = this.Data;
            var ptr  = this.CellOffset + 2 * idx;       // Used to move bytes around within data[]
            var pc   = (uint)ConvertEx.Get2(data, ptr); // Offset to cell content of cell being deleted
            var hdr  = this.HeaderOffset;               // Beginning of the header.  0 most pages.  100 page 1
            if (pc < (uint)ConvertEx.Get2(data, hdr + 5) || pc + sz > this.Shared.UsableSize)
            {
                pRC = SysEx.SQLITE_CORRUPT_BKPT();
                return;
            }
            var rc = freeSpace(pc, sz);
            if (rc != RC.OK)
            {
                pRC = rc;
                return;
            }
            Buffer.BlockCopy(data, ptr + 2, data, ptr, (this.Cells - 1 - idx) * 2);
            this.Cells--;
            data[this.HeaderOffset + 3] = (byte)(this.Cells >> 8);
            data[this.HeaderOffset + 4] = (byte)(this.Cells);
            this.FreeBytes += 2;
        }
Пример #4
0
 internal RC modifyPagePointer(Pgno iFrom, Pgno iTo, PTRMAP eType)
 {
     Debug.Assert(MutexEx.Held(this.Shared.Mutex));
     Debug.Assert(Pager.IsPageWriteable(this.DbPage));
     if (eType == PTRMAP.OVERFLOW2)
     {
         // The pointer is always the first 4 bytes of the page in this case.
         if (ConvertEx.Get4(this.Data) != iFrom)
         {
             return(SysEx.SQLITE_CORRUPT_BKPT());
         }
         ConvertEx.Put4L(this.Data, iTo);
     }
     else
     {
         var isInitOrig = this.HasInit;
         btreeInitPage();
         var nCell = this.Cells;
         int i;
         for (i = 0; i < nCell; i++)
         {
             var pCell = FindCell(i);
             if (eType == PTRMAP.OVERFLOW1)
             {
                 var info = new CellInfo();
                 btreeParseCellPtr(pCell, ref info);
                 if (info.iOverflow != 0)
                 {
                     if (iFrom == ConvertEx.Get4(this.Data, pCell + info.iOverflow))
                     {
                         ConvertEx.Put4(this.Data, pCell + info.iOverflow, (int)iTo);
                         break;
                     }
                 }
             }
             else
             {
                 if (ConvertEx.Get4(this.Data, pCell) == iFrom)
                 {
                     ConvertEx.Put4(this.Data, pCell, (int)iTo);
                     break;
                 }
             }
         }
         if (i == nCell)
         {
             if (eType != PTRMAP.BTREE || ConvertEx.Get4(this.Data, this.HeaderOffset + 8) != iFrom)
             {
                 return(SysEx.SQLITE_CORRUPT_BKPT());
             }
             ConvertEx.Put4L(this.Data, (uint)this.HeaderOffset + 8, iTo);
         }
         this.HasInit = isInitOrig;
     }
     return(RC.OK);
 }
Пример #5
0
        internal RC defragmentPage()
        {
            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            Debug.Assert(this.Shared != null);
            Debug.Assert(this.Shared.UsableSize <= Pager.SQLITE_MAX_PAGE_SIZE);
            Debug.Assert(this.NOverflows == 0);
            Debug.Assert(MutexEx.Held(this.Shared.Mutex));
            var temp       = this.Shared.Pager.sqlite3PagerTempSpace(); // Temp area for cell content
            var data       = this.Data;                                 // The page data
            var hdr        = this.HeaderOffset;                         // Offset to the page header
            var cellOffset = this.CellOffset;                           // Offset to the cell pointer array
            var nCell      = this.Cells;                                // Number of cells on the page

            Debug.Assert(nCell == ConvertEx.Get2(data, hdr + 3));
            var usableSize = (int)this.Shared.UsableSize;        // Number of usable bytes on a page
            var cbrk       = (int)ConvertEx.Get2(data, hdr + 5); // Offset to the cell content area

            Buffer.BlockCopy(data, cbrk, temp, cbrk, usableSize - cbrk);
            cbrk = usableSize;
            var iCellFirst = cellOffset + 2 * nCell; // First allowable cell index
            var iCellLast  = usableSize - 4;         // Last possible cell index

            for (var i = 0; i < nCell; i++)
            {
                var pAddr = cellOffset + i * 2;          // The i-th cell pointer
                var pc    = ConvertEx.Get2(data, pAddr); // Address of a i-th cell
#if !SQLITE_ENABLE_OVERSIZE_CELL_CHECK
                // These conditions have already been verified in btreeInitPage() if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
                if (pc < iCellFirst || pc > iCellLast)
                {
                    return(SysEx.SQLITE_CORRUPT_BKPT());
                }
#endif
                Debug.Assert(pc >= iCellFirst && pc <= iCellLast);
                var size = cellSizePtr(temp, pc); // Size of a cell
                cbrk -= size;
                if (cbrk < iCellFirst || pc + size > usableSize)
                {
                    return(SysEx.SQLITE_CORRUPT_BKPT());
                }
                Debug.Assert(cbrk + size <= usableSize && cbrk >= iCellFirst);
                Buffer.BlockCopy(temp, pc, data, cbrk, size);
                ConvertEx.Put2(data, pAddr, cbrk);
            }
            Debug.Assert(cbrk >= iCellFirst);
            ConvertEx.Put2(data, hdr + 5, cbrk);
            data[hdr + 1] = 0;
            data[hdr + 2] = 0;
            data[hdr + 7] = 0;
            var addr = cellOffset + 2 * nCell; // Offset of first byte after cell pointer array
            Array.Clear(data, addr, cbrk - addr);
            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            return(cbrk - iCellFirst != this.FreeBytes ? SysEx.SQLITE_CORRUPT_BKPT() : RC.OK);
        }
Пример #6
0
        internal static RC balance_deeper(MemPage pRoot, ref MemPage ppChild)
        {
            MemPage pChild    = null; // Pointer to a new child page
            Pgno    pgnoChild = 0;    // Page number of the new child page
            var     pBt       = pRoot.Shared;

            Debug.Assert(pRoot.NOverflows > 0);
            Debug.Assert(MutexEx.Held(pBt.Mutex));
            // Make pRoot, the root page of the b-tree, writable. Allocate a new page that will become the new right-child of pPage. Copy the contents
            // of the node stored on pRoot into the new child page.
            var rc = Pager.Write(pRoot.DbPage);

            if (rc == RC.OK)
            {
                rc = pBt.allocateBtreePage(ref pChild, ref pgnoChild, pRoot.ID, 0);
                copyNodeContent(pRoot, pChild, ref rc);
#if !SQLITE_OMIT_AUTOVACUUM
                if (pBt.AutoVacuum)
#else
                if (false)
#endif
                {
                    pBt.ptrmapPut(pgnoChild, PTRMAP.BTREE, pRoot.ID, ref rc);
                }
            }
            if (rc != RC.OK)
            {
                ppChild = null;
                pChild.releasePage();
                return(rc);
            }
            Debug.Assert(Pager.IsPageWriteable(pChild.DbPage));
            Debug.Assert(Pager.IsPageWriteable(pRoot.DbPage));
            Debug.Assert(pChild.Cells == pRoot.Cells);
            Btree.TRACE("BALANCE: copy root %d into %d\n", pRoot.ID, pChild.ID);
            // Copy the overflow cells from pRoot to pChild
            Array.Copy(pRoot.Overflows, pChild.Overflows, pRoot.NOverflows);
            pChild.NOverflows = pRoot.NOverflows;
            // Zero the contents of pRoot. Then install pChild as the right-child.
            pRoot.zeroPage(pChild.Data[0] & ~Btree.PTF_LEAF);
            ConvertEx.Put4L(pRoot.Data, pRoot.HeaderOffset + 8, pgnoChild);
            ppChild = pChild;
            return(RC.OK);
        }
Пример #7
0
        internal static RC balance_nonroot(MemPage pParent, int iParentIdx, byte[] aOvflSpace, int isRoot)
        {
            var      apOld  = new MemPage[NB];     // pPage and up to two siblings
            var      apCopy = new MemPage[NB];     // Private copies of apOld[] pages
            var      apNew  = new MemPage[NB + 2]; // pPage and up to NB siblings after balancing
            var      apDiv  = new int[NB - 1];     // Divider cells in pParent
            var      cntNew = new int[NB + 2];     // Index in aCell[] of cell after i-th page
            var      szNew  = new int[NB + 2];     // Combined size of cells place on i-th page
            var      szCell = new ushort[1];       // Local size of all cells in apCell[]
            BtShared pBt;                          // The whole database
            int      nCell     = 0;                // Number of cells in apCell[]
            int      nMaxCells = 0;                // Allocated size of apCell, szCell, aFrom.
            int      nNew      = 0;                // Number of pages in apNew[]
            ushort   leafCorrection;               // 4 if pPage is a leaf.  0 if not
            int      leafData;                     // True if pPage is a leaf of a LEAFDATA tree
            int      usableSpace;                  // Bytes in pPage beyond the header
            int      pageFlags;                    // Value of pPage.aData[0]
            int      subtotal;                     // Subtotal of bytes in cells on one page
            int      iOvflSpace = 0;               // First unused byte of aOvflSpace[]

            //int szScratch;               // Size of scratch memory requested
            byte[][] apCell = null;                 // All cells begin balanced
            //
            pBt = pParent.Shared;
            Debug.Assert(MutexEx.Held(pBt.Mutex));
            Debug.Assert(Pager.IsPageWriteable(pParent.DbPage));
#if false
            Btree.TRACE("BALANCE: begin page %d child of %d\n", pPage.pgno, pParent.pgno);
#endif
            // At this point pParent may have at most one overflow cell. And if this overflow cell is present, it must be the cell with
            // index iParentIdx. This scenario comes about when this function is called (indirectly) from sqlite3BtreeDelete().
            Debug.Assert(pParent.NOverflows == 0 || pParent.NOverflows == 1);
            Debug.Assert(pParent.NOverflows == 0 || pParent.Overflows[0].Index == iParentIdx);
            // Find the sibling pages to balance. Also locate the cells in pParent that divide the siblings. An attempt is made to find NN siblings on
            // either side of pPage. More siblings are taken from one side, however, if there are fewer than NN siblings on the other side. If pParent
            // has NB or fewer children then all children of pParent are taken.
            // This loop also drops the divider cells from the parent page. This way, the remainder of the function does not have to deal with any
            // overflow cells in the parent page, since if any existed they will have already been removed.
            int nOld;  // Number of pages in apOld[]
            int nxDiv; // Next divider slot in pParent.aCell[]
            var i = pParent.NOverflows + pParent.Cells;
            if (i < 2)
            {
                nxDiv = 0;
                nOld  = i + 1;
            }
            else
            {
                nOld = 3;
                if (iParentIdx == 0)
                {
                    nxDiv = 0;
                }
                else if (iParentIdx == i)
                {
                    nxDiv = i - 2;
                }
                else
                {
                    nxDiv = iParentIdx - 1;
                }
                i = 2;
            }
            var pRight = ((i + nxDiv - pParent.NOverflows) == pParent.Cells ? pParent.HeaderOffset + 8 : pParent.FindCell(i + nxDiv - pParent.NOverflows)); // Location in parent of right-sibling pointer
            var pgno   = (Pgno)ConvertEx.Get4(pParent.Data, pRight);
            var rc     = RC.OK;
            while (true)
            {
                rc = pBt.getAndInitPage(pgno, ref apOld[i]);
                if (rc != RC.OK)
                {
                    goto balance_cleanup;
                }
                nMaxCells += 1 + apOld[i].Cells + apOld[i].NOverflows;
                if (i-- == 0)
                {
                    break;
                }
                if (i + nxDiv == pParent.Overflows[0].Index && pParent.NOverflows != 0)
                {
                    apDiv[i]           = 0;
                    pgno               = ConvertEx.Get4(pParent.Overflows[0].Cell, apDiv[i]);
                    szNew[i]           = pParent.cellSizePtr(apDiv[i]);
                    pParent.NOverflows = 0;
                }
                else
                {
                    apDiv[i] = pParent.FindCell(i + nxDiv - pParent.NOverflows);
                    pgno     = ConvertEx.Get4(pParent.Data, apDiv[i]);
                    szNew[i] = pParent.cellSizePtr(apDiv[i]);
                    // Drop the cell from the parent page. apDiv[i] still points to the cell within the parent, even though it has been dropped.
                    // This is safe because dropping a cell only overwrites the first four bytes of it, and this function does not need the first
                    // four bytes of the divider cell. So the pointer is safe to use later on.
                    //
                    // Unless SQLite is compiled in secure-delete mode. In this case, the dropCell() routine will overwrite the entire cell with zeroes.
                    // In this case, temporarily copy the cell into the aOvflSpace[] buffer. It will be copied out again as soon as the aSpace[] buffer
                    // is allocated.
                    //if (pBt.secureDelete)
                    //{
                    //  int iOff = (int)(apDiv[i]) - (int)(pParent.aData); //SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent.aData);
                    //         if( (iOff+szNew[i])>(int)pBt->usableSize )
                    //  {
                    //    rc = SQLITE_CORRUPT_BKPT();
                    //    Array.Clear(apOld[0].aData,0,apOld[0].aData.Length); //memset(apOld, 0, (i + 1) * sizeof(MemPage*));
                    //    goto balance_cleanup;
                    //  }
                    //  else
                    //  {
                    //    memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
                    //    apDiv[i] = &aOvflSpace[apDiv[i] - pParent.aData];
                    //  }
                    //}
                    pParent.dropCell(i + nxDiv - pParent.NOverflows, szNew[i], ref rc);
                }
            }
            // Make nMaxCells a multiple of 4 in order to preserve 8-byte alignment
            nMaxCells = (nMaxCells + 3) & ~3;
            // Allocate space for memory structures
            apCell = MallocEx.sqlite3ScratchMalloc(apCell, nMaxCells);
            if (szCell.Length < nMaxCells)
            {
                Array.Resize(ref szCell, nMaxCells);
            }
            // Load pointers to all cells on sibling pages and the divider cells into the local apCell[] array.  Make copies of the divider cells
            // into space obtained from aSpace1[] and remove the the divider Cells from pParent.
            // If the siblings are on leaf pages, then the child pointers of the divider cells are stripped from the cells before they are copied
            // into aSpace1[].  In this way, all cells in apCell[] are without child pointers.  If siblings are not leaves, then all cell in
            // apCell[] include child pointers.  Either way, all cells in apCell[] are alike.
            // leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
            //       leafData:  1 if pPage holds key+data and pParent holds only keys.
            leafCorrection = (ushort)(apOld[0].Leaf * 4);
            leafData       = apOld[0].HasData;
            int j;
            for (i = 0; i < nOld; i++)
            {
                // Before doing anything else, take a copy of the i'th original sibling The rest of this function will use data from the copies rather
                // that the original pages since the original pages will be in the process of being overwritten.
                var pOld  = apCopy[i] = apOld[i].Clone();
                var limit = pOld.Cells + pOld.NOverflows;
                if (pOld.NOverflows > 0 || true)
                {
                    for (j = 0; j < limit; j++)
                    {
                        Debug.Assert(nCell < nMaxCells);
                        var iFOFC = pOld.FindOverflowCell(j);
                        szCell[nCell] = pOld.cellSizePtr(iFOFC);
                        // Copy the Data Locally
                        if (apCell[nCell] == null)
                        {
                            apCell[nCell] = new byte[szCell[nCell]];
                        }
                        else if (apCell[nCell].Length < szCell[nCell])
                        {
                            Array.Resize(ref apCell[nCell], szCell[nCell]);
                        }
                        if (iFOFC < 0)  // Overflow Cell
                        {
                            Buffer.BlockCopy(pOld.Overflows[-(iFOFC + 1)].Cell, 0, apCell[nCell], 0, szCell[nCell]);
                        }
                        else
                        {
                            Buffer.BlockCopy(pOld.Data, iFOFC, apCell[nCell], 0, szCell[nCell]);
                        }
                        nCell++;
                    }
                }
                else
                {
                    var aData      = pOld.Data;
                    var maskPage   = pOld.MaskPage;
                    var cellOffset = pOld.CellOffset;
                    for (j = 0; j < limit; j++)
                    {
                        Debugger.Break();
                        Debug.Assert(nCell < nMaxCells);
                        apCell[nCell] = FindCellv2(aData, maskPage, cellOffset, j);
                        szCell[nCell] = pOld.cellSizePtr(apCell[nCell]);
                        nCell++;
                    }
                }
                if (i < nOld - 1 && 0 == leafData)
                {
                    var sz    = (ushort)szNew[i];
                    var pTemp = MallocEx.sqlite3Malloc(sz + leafCorrection);
                    Debug.Assert(nCell < nMaxCells);
                    szCell[nCell] = sz;
                    Debug.Assert(sz <= pBt.MaxLocal + 23);
                    Buffer.BlockCopy(pParent.Data, apDiv[i], pTemp, 0, sz);
                    if (apCell[nCell] == null || apCell[nCell].Length < sz)
                    {
                        Array.Resize(ref apCell[nCell], sz);
                    }
                    Buffer.BlockCopy(pTemp, leafCorrection, apCell[nCell], 0, sz);
                    Debug.Assert(leafCorrection == 0 || leafCorrection == 4);
                    szCell[nCell] = (ushort)(szCell[nCell] - leafCorrection);
                    if (0 == pOld.Leaf)
                    {
                        Debug.Assert(leafCorrection == 0);
                        Debug.Assert(pOld.HeaderOffset == 0);
                        // The right pointer of the child page pOld becomes the left pointer of the divider cell
                        Buffer.BlockCopy(pOld.Data, 8, apCell[nCell], 0, 4);//memcpy( apCell[nCell], ref pOld.aData[8], 4 );
                    }
                    else
                    {
                        Debug.Assert(leafCorrection == 4);
                        if (szCell[nCell] < 4)
                        {
                            // Do not allow any cells smaller than 4 bytes.
                            szCell[nCell] = 4;
                        }
                    }
                    nCell++;
                }
            }
            // Figure out the number of pages needed to hold all nCell cells. Store this number in "k".  Also compute szNew[] which is the total
            // size of all cells on the i-th page and cntNew[] which is the index in apCell[] of the cell that divides page i from page i+1.
            // cntNew[k] should equal nCell.
            // Values computed by this block:
            //           k: The total number of sibling pages
            //    szNew[i]: Spaced used on the i-th sibling page.
            //   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
            //              the right of the i-th sibling page.
            // usableSpace: Number of bytes of space available on each sibling.
            usableSpace = (int)pBt.UsableSize - 12 + leafCorrection;
            int k;
            for (subtotal = k = i = 0; i < nCell; i++)
            {
                Debug.Assert(i < nMaxCells);
                subtotal += szCell[i] + 2;
                if (subtotal > usableSpace)
                {
                    szNew[k]  = subtotal - szCell[i];
                    cntNew[k] = i;
                    if (leafData != 0)
                    {
                        i--;
                    }
                    subtotal = 0;
                    k++;
                    if (k > NB + 1)
                    {
                        rc = SysEx.SQLITE_CORRUPT_BKPT();
                        goto balance_cleanup;
                    }
                }
            }
            szNew[k]  = subtotal;
            cntNew[k] = nCell;
            k++;
            // The packing computed by the previous block is biased toward the siblings on the left side.  The left siblings are always nearly full, while the
            // right-most sibling might be nearly empty.  This block of code attempts to adjust the packing of siblings to get a better balance.
            //
            // This adjustment is more than an optimization.  The packing above might be so out of balance as to be illegal.  For example, the right-most
            // sibling might be completely empty.  This adjustment is not optional.
            for (i = k - 1; i > 0; i--)
            {
                var szRight = szNew[i];          // Size of sibling on the right
                var szLeft  = szNew[i - 1];      // Size of sibling on the left
                var r       = cntNew[i - 1] - 1; // Index of right-most cell in left sibling
                var d       = r + 1 - leafData;  // Index of first cell to the left of right sibling
                Debug.Assert(d < nMaxCells);
                Debug.Assert(r < nMaxCells);
                while (szRight == 0 || szRight + szCell[d] + 2 <= szLeft - (szCell[r] + 2))
                {
                    szRight += szCell[d] + 2;
                    szLeft  -= szCell[r] + 2;
                    cntNew[i - 1]--;
                    r = cntNew[i - 1] - 1;
                    d = r + 1 - leafData;
                }
                szNew[i]     = szRight;
                szNew[i - 1] = szLeft;
            }
            // Either we found one or more cells (cntnew[0])>0) or pPage is a virtual root page.  A virtual root page is when the real root
            // page is page 1 and we are the only child of that page.
            Debug.Assert(cntNew[0] > 0 || (pParent.ID == 1 && pParent.Cells == 0));
            Btree.TRACE("BALANCE: old: %d %d %d  ", apOld[0].ID, (nOld >= 2 ? apOld[1].ID : 0), (nOld >= 3 ? apOld[2].ID : 0));
            // Allocate k new pages.  Reuse old pages where possible.
            if (apOld[0].ID <= 1)
            {
                rc = SysEx.SQLITE_CORRUPT_BKPT();
                goto balance_cleanup;
            }
            pageFlags = apOld[0].Data[0];
            for (i = 0; i < k; i++)
            {
                var pNew = new MemPage();
                if (i < nOld)
                {
                    pNew     = apNew[i] = apOld[i];
                    apOld[i] = null;
                    rc       = Pager.Write(pNew.DbPage);
                    nNew++;
                    if (rc != RC.OK)
                    {
                        goto balance_cleanup;
                    }
                }
                else
                {
                    Debug.Assert(i > 0);
                    rc = pBt.allocateBtreePage(ref pNew, ref pgno, pgno, 0);
                    if (rc != 0)
                    {
                        goto balance_cleanup;
                    }
                    apNew[i] = pNew;
                    nNew++;

                    // Set the pointer-map entry for the new sibling page.
#if !SQLITE_OMIT_AUTOVACUUM
                    if (pBt.AutoVacuum)
#else
                    if (false)
#endif
                    {
                        pBt.ptrmapPut(pNew.ID, PTRMAP.BTREE, pParent.ID, ref rc);
                        if (rc != RC.OK)
                        {
                            goto balance_cleanup;
                        }
                    }
                }
            }
            // Free any old pages that were not reused as new pages.
            while (i < nOld)
            {
                apOld[i].freePage(ref rc);
                if (rc != RC.OK)
                {
                    goto balance_cleanup;
                }
                apOld[i].releasePage();
                apOld[i] = null;
                i++;
            }
            // Put the new pages in accending order.  This helps to keep entries in the disk file in order so that a scan
            // of the table is a linear scan through the file.  That in turn helps the operating system to deliver pages
            // from the disk more rapidly.
            // An O(n^2) insertion sort algorithm is used, but since n is never more than NB (a small constant), that should
            // not be a problem.
            // When NB==3, this one optimization makes the database about 25% faster for large insertions and deletions.
            for (i = 0; i < k - 1; i++)
            {
                var minV = (int)apNew[i].ID;
                var minI = i;
                for (j = i + 1; j < k; j++)
                {
                    if (apNew[j].ID < (uint)minV)
                    {
                        minI = j;
                        minV = (int)apNew[j].ID;
                    }
                }
                if (minI > i)
                {
                    var pT = apNew[i];
                    apNew[i]    = apNew[minI];
                    apNew[minI] = pT;
                }
            }
            Btree.TRACE("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", apNew[0].ID, szNew[0],
                        (nNew >= 2 ? apNew[1].ID : 0), (nNew >= 2 ? szNew[1] : 0),
                        (nNew >= 3 ? apNew[2].ID : 0), (nNew >= 3 ? szNew[2] : 0),
                        (nNew >= 4 ? apNew[3].ID : 0), (nNew >= 4 ? szNew[3] : 0),
                        (nNew >= 5 ? apNew[4].ID : 0), (nNew >= 5 ? szNew[4] : 0));
            Debug.Assert(Pager.IsPageWriteable(pParent.DbPage));
            ConvertEx.Put4L(pParent.Data, pRight, apNew[nNew - 1].ID);
            // Evenly distribute the data in apCell[] across the new pages. Insert divider cells into pParent as necessary.
            j = 0;
            for (i = 0; i < nNew; i++)
            {
                // Assemble the new sibling page.
                MemPage pNew = apNew[i];
                Debug.Assert(j < nMaxCells);
                pNew.zeroPage(pageFlags);
                pNew.assemblePage(cntNew[i] - j, apCell, szCell, j);
                Debug.Assert(pNew.Cells > 0 || (nNew == 1 && cntNew[0] == 0));
                Debug.Assert(pNew.NOverflows == 0);
                j = cntNew[i];
                // If the sibling page assembled above was not the right-most sibling, insert a divider cell into the parent page.
                Debug.Assert(i < nNew - 1 || j == nCell);
                if (j < nCell)
                {
                    Debug.Assert(j < nMaxCells);
                    var pCell = apCell[j];
                    var sz    = szCell[j] + leafCorrection;
                    var pTemp = MallocEx.sqlite3Malloc(sz);
                    if (pNew.Leaf == 0)
                    {
                        Buffer.BlockCopy(pCell, 0, pNew.Data, 8, 4);
                    }
                    else if (leafData != 0)
                    {
                        // If the tree is a leaf-data tree, and the siblings are leaves, then there is no divider cell in apCell[]. Instead, the divider
                        // cell consists of the integer key for the right-most cell of the sibling-page assembled above only.
                        var info = new CellInfo();
                        j--;
                        pNew.btreeParseCellPtr(apCell[j], ref info);
                        pCell = pTemp;
                        sz    = 4 + ConvertEx.PutVarint9L(pCell, 4, (ulong)info.nKey);
                        pTemp = null;
                    }
                    else
                    {
                        //------------ pCell -= 4;
                        var _pCell_4 = MallocEx.sqlite3Malloc(pCell.Length + 4);
                        Buffer.BlockCopy(pCell, 0, _pCell_4, 4, pCell.Length);
                        pCell = _pCell_4;
                        // Obscure case for non-leaf-data trees: If the cell at pCell was previously stored on a leaf node, and its reported size was 4
                        // bytes, then it may actually be smaller than this (see btreeParseCellPtr(), 4 bytes is the minimum size of
                        // any cell). But it is important to pass the correct size to insertCell(), so reparse the cell now.
                        // Note that this can never happen in an SQLite data file, as all cells are at least 4 bytes. It only happens in b-trees used
                        // to evaluate "IN (SELECT ...)" and similar clauses.
                        if (szCell[j] == 4)
                        {
                            Debug.Assert(leafCorrection == 4);
                            sz = pParent.cellSizePtr(pCell);
                        }
                    }
                    iOvflSpace += sz;
                    Debug.Assert(sz <= pBt.MaxLocal + 23);
                    Debug.Assert(iOvflSpace <= (int)pBt.PageSize);
                    pParent.insertCell(nxDiv, pCell, sz, pTemp, pNew.ID, ref rc);
                    if (rc != RC.OK)
                    {
                        goto balance_cleanup;
                    }
                    Debug.Assert(Pager.IsPageWriteable(pParent.DbPage));
                    j++;
                    nxDiv++;
                }
            }
            Debug.Assert(j == nCell);
            Debug.Assert(nOld > 0);
            Debug.Assert(nNew > 0);
            if ((pageFlags & Btree.PTF_LEAF) == 0)
            {
                Buffer.BlockCopy(apCopy[nOld - 1].Data, 8, apNew[nNew - 1].Data, 8, 4);
            }
            if (isRoot != 0 && pParent.Cells == 0 && pParent.HeaderOffset <= apNew[0].FreeBytes)
            {
                // The root page of the b-tree now contains no cells. The only sibling page is the right-child of the parent. Copy the contents of the
                // child page into the parent, decreasing the overall height of the b-tree structure by one. This is described as the "balance-shallower"
                // sub-algorithm in some documentation.
                // If this is an auto-vacuum database, the call to copyNodeContent() sets all pointer-map entries corresponding to database image pages
                // for which the pointer is stored within the content being copied.
                // The second Debug.Assert below verifies that the child page is defragmented (it must be, as it was just reconstructed using assemblePage()). This
                // is important if the parent page happens to be page 1 of the database image.  */
                Debug.Assert(nNew == 1);
                Debug.Assert(apNew[0].FreeBytes == (ConvertEx.Get2(apNew[0].Data, 5) - apNew[0].CellOffset - apNew[0].Cells * 2));
                copyNodeContent(apNew[0], pParent, ref rc);
                apNew[0].freePage(ref rc);
            }
            else
#if !SQLITE_OMIT_AUTOVACUUM
            if (pBt.AutoVacuum)
#else
            if (false)
#endif
            {
                // Fix the pointer-map entries for all the cells that were shifted around. There are several different types of pointer-map entries that need to
                // be dealt with by this routine. Some of these have been set already, but many have not. The following is a summary:
                //   1) The entries associated with new sibling pages that were not siblings when this function was called. These have already
                //      been set. We don't need to worry about old siblings that were moved to the free-list - the freePage() code has taken care
                //      of those.
                //   2) The pointer-map entries associated with the first overflow page in any overflow chains used by new divider cells. These
                //      have also already been taken care of by the insertCell() code.
                //   3) If the sibling pages are not leaves, then the child pages of cells stored on the sibling pages may need to be updated.
                //   4) If the sibling pages are not internal intkey nodes, then any overflow pages used by these cells may need to be updated
                //      (internal intkey nodes never contain pointers to overflow pages).
                //   5) If the sibling pages are not leaves, then the pointer-map entries for the right-child pages of each sibling may need
                //      to be updated.
                // Cases 1 and 2 are dealt with above by other code. The next block deals with cases 3 and 4 and the one after that, case 5. Since
                // setting a pointer map entry is a relatively expensive operation, this code only sets pointer map entries for child or overflow pages that have
                // actually moved between pages.
                var pNew      = apNew[0];
                var pOld      = apCopy[0];
                var nOverflow = pOld.NOverflows;
                var iNextOld  = pOld.Cells + nOverflow;
                var iOverflow = (nOverflow != 0 ? pOld.Overflows[0].Index : -1);
                j = 0;     // Current 'old' sibling page
                k = 0;     // Current 'new' sibling page
                for (i = 0; i < nCell; i++)
                {
                    var isDivider = 0;
                    while (i == iNextOld)
                    {
                        // Cell i is the cell immediately following the last cell on old sibling page j. If the siblings are not leaf pages of an
                        // intkey b-tree, then cell i was a divider cell.
                        pOld     = apCopy[++j];
                        iNextOld = i + (0 == leafData ? 1 : 0) + pOld.Cells + pOld.NOverflows;
                        if (pOld.NOverflows != 0)
                        {
                            nOverflow = pOld.NOverflows;
                            iOverflow = i + (0 == leafData ? 1 : 0) + pOld.Overflows[0].Index;
                        }
                        isDivider = 0 == leafData ? 1 : 0;
                    }
                    Debug.Assert(nOverflow > 0 || iOverflow < i);
                    Debug.Assert(nOverflow < 2 || pOld.Overflows[0].Index == pOld.Overflows[1].Index - 1);
                    Debug.Assert(nOverflow < 3 || pOld.Overflows[1].Index == pOld.Overflows[2].Index - 1);
                    if (i == iOverflow)
                    {
                        isDivider = 1;
                        if (--nOverflow > 0)
                        {
                            iOverflow++;
                        }
                    }
                    if (i == cntNew[k])
                    {
                        // Cell i is the cell immediately following the last cell on new sibling page k. If the siblings are not leaf pages of an
                        // intkey b-tree, then cell i is a divider cell.
                        pNew = apNew[++k];
                        if (leafData == 0)
                        {
                            continue;
                        }
                    }
                    Debug.Assert(j < nOld);
                    Debug.Assert(k < nNew);
                    // If the cell was originally divider cell (and is not now) or an overflow cell, or if the cell was located on a different sibling
                    // page before the balancing, then the pointer map entries associated with any child or overflow pages need to be updated.
                    if (isDivider != 0 || pOld.ID != pNew.ID)
                    {
                        if (leafCorrection == 0)
                        {
                            pBt.ptrmapPut(ConvertEx.Get4(apCell[i]), PTRMAP.BTREE, pNew.ID, ref rc);
                        }
                        if (szCell[i] > pNew.MinLocal)
                        {
                            pNew.ptrmapPutOvflPtr(apCell[i], ref rc);
                        }
                    }
                }
                if (leafCorrection == 0)
                {
                    for (i = 0; i < nNew; i++)
                    {
                        var key = ConvertEx.Get4(apNew[i].Data, 8);
                        pBt.ptrmapPut(key, PTRMAP.BTREE, apNew[i].ID, ref rc);
                    }
                }
#if false
// The ptrmapCheckPages() contains Debug.Assert() statements that verify that all pointer map pages are set correctly. This is helpful while
// debugging. This is usually disabled because a corrupt database may cause an Debug.Assert() statement to fail.
                ptrmapCheckPages(apNew, nNew);
                ptrmapCheckPages(pParent, 1);
#endif
            }
            Debug.Assert(pParent.HasInit);
            Btree.TRACE("BALANCE: finished: old=%d new=%d cells=%d\n", nOld, nNew, nCell);
            // Cleanup before returning.
balance_cleanup:
            MallocEx.sqlite3ScratchFree(apCell);
            for (i = 0; i < nOld; i++)
            {
                apOld[i].releasePage();
            }
            for (i = 0; i < nNew; i++)
            {
                apNew[i].releasePage();
            }
            return(rc);
        }
Пример #8
0
        static int NB = (NN * 2 + 1);   // Total pages involved in the balance

#if !SQLITE_OMIT_QUICKBALANCE
        internal static RC balance_quick(MemPage parentPage, MemPage page, byte[] space)
        {
            Debug.Assert(MutexEx.Held(page.Shared.Mutex));
            Debug.Assert(Pager.IsPageWriteable(parentPage.DbPage));
            Debug.Assert(page.NOverflows == 1);
            // This error condition is now caught prior to reaching this function
            if (page.Cells <= 0)
            {
                return(SysEx.SQLITE_CORRUPT_BKPT());
            }
            // Allocate a new page. This page will become the right-sibling of pPage. Make the parent page writable, so that the new divider cell
            // may be inserted. If both these operations are successful, proceed.
            var  shared  = page.Shared;   // B-Tree Database
            var  newPage = new MemPage(); // Newly allocated page
            Pgno pgnoNew = 0;             // Page number of pNew
            var  rc      = shared.allocateBtreePage(ref newPage, ref pgnoNew, 0, 0);

            if (rc != RC.OK)
            {
                return(rc);
            }
            var pOut   = 4;
            var pCell  = page.Overflows[0].Cell;
            var szCell = new int[1] {
                page.cellSizePtr(pCell)
            };

            Debug.Assert(Pager.IsPageWriteable(newPage.DbPage));
            Debug.Assert(page.Data[0] == (Btree.PTF_INTKEY | Btree.PTF_LEAFDATA | Btree.PTF_LEAF));
            newPage.zeroPage(Btree.PTF_INTKEY | Btree.PTF_LEAFDATA | Btree.PTF_LEAF);
            newPage.assemblePage(1, pCell, szCell);
            // If this is an auto-vacuum database, update the pointer map with entries for the new page, and any pointer from the
            // cell on the page to an overflow page. If either of these operations fails, the return code is set, but the contents
            // of the parent page are still manipulated by thh code below. That is Ok, at this point the parent page is guaranteed to
            // be marked as dirty. Returning an error code will cause a rollback, undoing any changes made to the parent page.
#if !SQLITE_OMIT_AUTOVACUUM
            if (shared.AutoVacuum)
#else
            if (false)
#endif
            {
                shared.ptrmapPut(pgnoNew, PTRMAP.BTREE, parentPage.ID, ref rc);
                if (szCell[0] > newPage.MinLocal)
                {
                    newPage.ptrmapPutOvflPtr(pCell, ref rc);
                }
            }
            // Create a divider cell to insert into pParent. The divider cell consists of a 4-byte page number (the page number of pPage) and
            // a variable length key value (which must be the same value as the largest key on pPage).
            // To find the largest key value on pPage, first find the right-most cell on pPage. The first two fields of this cell are the
            // record-length (a variable length integer at most 32-bits in size) and the key value (a variable length integer, may have any value).
            // The first of the while(...) loops below skips over the record-length field. The second while(...) loop copies the key value from the
            // cell on pPage into the pSpace buffer.
            var iCell = page.FindCell(page.Cells - 1);
            pCell = page.Data;
            var _pCell = iCell;
            var pStop  = _pCell + 9;
            while (((pCell[_pCell++]) & 0x80) != 0 && _pCell < pStop)
            {
                ;
            }
            pStop = _pCell + 9;
            while (((space[pOut++] = pCell[_pCell++]) & 0x80) != 0 && _pCell < pStop)
            {
                ;
            }
            // Insert the new divider cell into pParent.
            parentPage.insertCell(parentPage.Cells, space, pOut, null, page.ID, ref rc);
            // Set the right-child pointer of pParent to point to the new page.
            ConvertEx.Put4L(parentPage.Data, parentPage.HeaderOffset + 8, pgnoNew);
            // Release the reference to the new page.
            newPage.releasePage();
            return(rc);
        }
Пример #9
0
        internal void insertCell(int i, byte[] pCell, int sz, byte[] pTemp, Pgno iChild, ref RC pRC)
        {
            var nSkip = (iChild != 0 ? 4 : 0);

            if (pRC != RC.OK)
            {
                return;
            }
            Debug.Assert(i >= 0 && i <= this.Cells + this.NOverflows);
            Debug.Assert(this.Cells <= Btree.MX_CELL(this.Shared) && Btree.MX_CELL(this.Shared) <= 10921);
            Debug.Assert(this.NOverflows <= this.Overflows.Length);
            Debug.Assert(MutexEx.Held(this.Shared.Mutex));
            // The cell should normally be sized correctly.  However, when moving a malformed cell from a leaf page to an interior page, if the cell size
            // wanted to be less than 4 but got rounded up to 4 on the leaf, then size might be less than 8 (leaf-size + pointer) on the interior node.  Hence
            // the term after the || in the following assert().
            Debug.Assert(sz == cellSizePtr(pCell) || (sz == 8 && iChild > 0));
            if (this.NOverflows != 0 || sz + 2 > this.FreeBytes)
            {
                if (pTemp != null)
                {
                    Buffer.BlockCopy(pCell, nSkip, pTemp, nSkip, sz - nSkip);
                    pCell = pTemp;
                }
                if (iChild != 0)
                {
                    ConvertEx.Put4L(pCell, iChild);
                }
                var j = this.NOverflows++;
                Debug.Assert(j < this.Overflows.Length);
                this.Overflows[j].Cell  = pCell;
                this.Overflows[j].Index = (ushort)i;
            }
            else
            {
                var rc = Pager.Write(this.DbPage);
                if (rc != RC.OK)
                {
                    pRC = rc;
                    return;
                }
                Debug.Assert(Pager.IsPageWriteable(this.DbPage));
                var data       = this.Data;                   // The content of the whole page
                var cellOffset = (int)this.CellOffset;        // Address of first cell pointer in data[]
                var end        = cellOffset + 2 * this.Cells; // First byte past the last cell pointer in data[]
                var ins        = cellOffset + 2 * i;          // Index in data[] where new cell pointer is inserted
                int idx        = 0;                           // Where to write new cell content in data[]
                rc = allocateSpace(sz, ref idx);
                if (rc != RC.OK)
                {
                    pRC = rc;
                    return;
                }
                // The allocateSpace() routine guarantees the following two properties if it returns success
                Debug.Assert(idx >= end + 2);
                Debug.Assert(idx + sz <= (int)this.Shared.UsableSize);
                this.Cells++;
                this.FreeBytes -= (ushort)(2 + sz);
                Buffer.BlockCopy(pCell, nSkip, data, idx + nSkip, sz - nSkip);
                if (iChild != 0)
                {
                    ConvertEx.Put4L(data, (uint)idx, iChild);
                }
                for (var j = end; j > ins; j -= 2)
                {
                    data[j + 0] = data[j - 2];
                    data[j + 1] = data[j - 1];
                }
                ConvertEx.Put2(data, ins, idx);
                ConvertEx.Put2(data, this.HeaderOffset + 3, this.Cells);
#if !SQLITE_OMIT_AUTOVACUUM
                if (this.Shared.AutoVacuum)
                {
                    // The cell may contain a pointer to an overflow page. If so, write the entry for the overflow page into the pointer map.
                    ptrmapPutOvflPtr(pCell, ref pRC);
                }
#endif
            }
        }
Пример #10
0
        internal RC fillInCell(byte[] pCell, byte[] pKey, long nKey, byte[] pData, int nData, int nZero, ref int pnSize)
        {
            Debug.Assert(MutexEx.Held(this.Shared.Mutex));
            // pPage is not necessarily writeable since pCell might be auxiliary buffer space that is separate from the pPage buffer area
            // TODO -- Determine if the following Assert is needed under c#
            //Debug.Assert( pCell < pPage.aData || pCell >= &pPage.aData[pBt.pageSize] || sqlite3PagerIswriteable(pPage.pDbPage) );
            // Fill in the header.
            var nHeader = 0;

            if (this.Leaf == 0)
            {
                nHeader += 4;
            }
            if (this.HasData != 0)
            {
                nHeader += (int)ConvertEx.PutVariant9(pCell, (uint)nHeader, (int)(nData + nZero));
            }
            else
            {
                nData = nZero = 0;
            }
            nHeader += ConvertEx.PutVariant9L(pCell, (uint)nHeader, (ulong)nKey);
            var info = new CellInfo();

            btreeParseCellPtr(pCell, ref info);
            Debug.Assert(info.nHeader == nHeader);
            Debug.Assert(info.nKey == nKey);
            Debug.Assert(info.nData == (uint)(nData + nZero));
            // Fill in the payload
            var nPayload = nData + nZero;

            byte[] pSrc;
            int    nSrc;

            if (this.HasIntKey)
            {
                pSrc  = pData;
                nSrc  = nData;
                nData = 0;
            }
            else
            {
                if (Check.NEVER(nKey > 0x7fffffff || pKey == null))
                {
                    return(SysEx.SQLITE_CORRUPT_BKPT());
                }
                nPayload += (int)nKey;
                pSrc      = pKey;
                nSrc      = (int)nKey;
            }
            pnSize = info.nSize;
            var     spaceLeft     = (int)info.nLocal;
            var     pPayload      = pCell;
            var     pPayloadIndex = nHeader;
            var     pPrior        = pCell;
            var     pPriorIndex   = (int)info.iOverflow;
            var     pBt           = this.Shared;
            Pgno    pgnoOvfl      = 0;
            MemPage pToRelease    = null;

            while (nPayload > 0)
            {
                if (spaceLeft == 0)
                {
#if !SQLITE_OMIT_AUTOVACUUM
                    var pgnoPtrmap = pgnoOvfl; // Overflow page pointer-map entry page
                    if (pBt.AutoVacuum)
                    {
                        do
                        {
                            pgnoOvfl++;
                        }while (PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl == PENDING_BYTE_PAGE(pBt));
                    }
#endif
                    MemPage pOvfl = null;
                    var     rc    = pBt.allocateBtreePage(ref pOvfl, ref pgnoOvfl, pgnoOvfl, 0);
#if !SQLITE_OMIT_AUTOVACUUM
                    // 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 && rc == RC.OK)
                    {
                        var eType = (pgnoPtrmap != 0 ? PTRMAP.OVERFLOW2 : PTRMAP.OVERFLOW1);
                        pBt.ptrmapPut(pgnoOvfl, eType, pgnoPtrmap, ref rc);
                        if (rc != RC.OK)
                        {
                            pOvfl.releasePage();
                        }
                    }
#endif
                    if (rc != RC.OK)
                    {
                        pToRelease.releasePage();
                        return(rc);
                    }
                    // If pToRelease is not zero than pPrior points into the data area of pToRelease.  Make sure pToRelease is still writeable.
                    Debug.Assert(pToRelease == null || Pager.IsPageWriteable(pToRelease.DbPage));
                    // If pPrior is part of the data area of pPage, then make sure pPage is still writeable
                    // TODO -- Determine if the following Assert is needed under c#
                    //Debug.Assert( pPrior < pPage.aData || pPrior >= &pPage.aData[pBt.pageSize] || sqlite3PagerIswriteable(pPage.pDbPage) );
                    ConvertEx.Put4L(pPrior, (uint)pPriorIndex, pgnoOvfl);
                    pToRelease.releasePage();
                    pToRelease  = pOvfl;
                    pPrior      = pOvfl.Data;
                    pPriorIndex = 0;
                    ConvertEx.Put4(pPrior, 0);
                    pPayload      = pOvfl.Data;
                    pPayloadIndex = 4;
                    spaceLeft     = (int)pBt.UsableSize - 4;
                }
                var 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.
                Debug.Assert(pToRelease == null || Pager.IsPageWriteable(pToRelease.DbPage));
                // If pPayload is part of the data area of pPage, then make sure pPage is still writeable
                // TODO -- Determine if the following Assert is needed under c#
                //Debug.Assert( pPayload < pPage.aData || pPayload >= &pPage.aData[pBt.pageSize] || sqlite3PagerIswriteable(pPage.pDbPage) );
                var pSrcIndex = 0;
                if (nSrc > 0)
                {
                    if (n > nSrc)
                    {
                        n = nSrc;
                    }
                    Debug.Assert(pSrc != null);
                    Buffer.BlockCopy(pSrc, pSrcIndex, pPayload, pPayloadIndex, n);
                }
                else
                {
                    var pZeroBlob = MallocEx.sqlite3Malloc(n);
                    Buffer.BlockCopy(pZeroBlob, 0, pPayload, pPayloadIndex, n);
                }
                nPayload      -= n;
                pPayloadIndex += n;
                pSrcIndex     += n;
                nSrc          -= n;
                spaceLeft     -= n;
                if (nSrc == 0)
                {
                    nSrc = nData;
                    pSrc = pData;
                }
            }
            pToRelease.releasePage();
            return(RC.OK);
        }
Пример #11
0
        internal RC allocateBtreePage(ref MemPage ppPage, ref Pgno pPgno, Pgno nearby, byte exact)
        {
            MemPage pTrunk     = null;
            MemPage pPrevTrunk = null;

            Debug.Assert(MutexEx.Held(this.Mutex));
            var pPage1 = this.Page1;
            var mxPage = btreePagecount();                // Total size of the database file
            var n      = ConvertEx.Get4(pPage1.Data, 36); // Number of pages on the freelist

            if (n >= mxPage)
            {
                return(SysEx.SQLITE_CORRUPT_BKPT());
            }
            RC rc;

            if (n > 0)
            {
                // There are pages on the freelist.  Reuse one of those pages.
                Pgno iTrunk;
                byte searchList = 0; // If the free-list must be searched for 'nearby'
                // If the 'exact' parameter was true and a query of the pointer-map shows that the page 'nearby' is somewhere on the free-list, then the entire-list will be searched for that page.
#if !SQLITE_OMIT_AUTOVACUUM
                if (exact != 0 && nearby <= mxPage)
                {
                    Debug.Assert(nearby > 0);
                    Debug.Assert(this.AutoVacuum);
                    PTRMAP eType  = 0;
                    uint   dummy0 = 0;
                    rc = ptrmapGet(nearby, ref eType, ref dummy0);
                    if (rc != RC.OK)
                    {
                        return(rc);
                    }
                    if (eType == PTRMAP.FREEPAGE)
                    {
                        searchList = 1;
                    }
                    pPgno = nearby;
                }
#endif
                // Decrement the free-list count by 1. Set iTrunk to the index of the first free-list trunk page. iPrevTrunk is initially 1.
                rc = Pager.Write(pPage1.DbPage);
                if (rc != RC.OK)
                {
                    return(rc);
                }
                ConvertEx.Put4(pPage1.Data, 36, n - 1);
                // The code within this loop is run only once if the 'searchList' variable is not true. Otherwise, it runs once for each trunk-page on the
                // free-list until the page 'nearby' is located.
                do
                {
                    pPrevTrunk = pTrunk;
                    iTrunk     = (pPrevTrunk != null ? ConvertEx.Get4(pPrevTrunk.Data, 0) : ConvertEx.Get4(pPage1.Data, 32));
                    rc         = (iTrunk > mxPage ? SysEx.SQLITE_CORRUPT_BKPT() : btreeGetPage(iTrunk, ref pTrunk, 0));
                    if (rc != RC.OK)
                    {
                        pTrunk = null;
                        goto end_allocate_page;
                    }
                    var k = ConvertEx.Get4(pTrunk.Data, 4); // # of leaves on this trunk page
                    if (k == 0 && searchList == 0)
                    {
                        // The trunk has no leaves and the list is not being searched. So extract the trunk page itself and use it as the newly allocated page
                        Debug.Assert(pPrevTrunk == null);
                        rc = Pager.Write(pTrunk.DbPage);
                        if (rc != RC.OK)
                        {
                            goto end_allocate_page;
                        }
                        pPgno = iTrunk;
                        Buffer.BlockCopy(pTrunk.Data, 0, pPage1.Data, 32, 4);
                        ppPage = pTrunk;
                        pTrunk = null;
                        Btree.TRACE("ALLOCATE: %d trunk - %d free pages left\n", pPgno, n - 1);
                    }
                    else if (k > (uint)(this.UsableSize / 4 - 2))
                    {
                        // Value of k is out of range. Database corruption
                        rc = SysEx.SQLITE_CORRUPT_BKPT();
                        goto end_allocate_page;
#if !SQLITE_OMIT_AUTOVACUUM
                    }
                    else if (searchList != 0 && nearby == iTrunk)
                    {
                        // The list is being searched and this trunk page is the page to allocate, regardless of whether it has leaves.
                        Debug.Assert(pPgno == iTrunk);
                        ppPage     = pTrunk;
                        searchList = 0;
                        rc         = Pager.Write(pTrunk.DbPage);
                        if (rc != RC.OK)
                        {
                            goto end_allocate_page;
                        }
                        if (k == 0)
                        {
                            if (pPrevTrunk == null)
                            {
                                pPage1.Data[32 + 0] = pTrunk.Data[0 + 0];
                                pPage1.Data[32 + 1] = pTrunk.Data[0 + 1];
                                pPage1.Data[32 + 2] = pTrunk.Data[0 + 2];
                                pPage1.Data[32 + 3] = pTrunk.Data[0 + 3];
                            }
                            else
                            {
                                rc = Pager.Write(pPrevTrunk.DbPage);
                                if (rc != RC.OK)
                                {
                                    goto end_allocate_page;
                                }
                                pPrevTrunk.Data[0 + 0] = pTrunk.Data[0 + 0];
                                pPrevTrunk.Data[0 + 1] = pTrunk.Data[0 + 1];
                                pPrevTrunk.Data[0 + 2] = pTrunk.Data[0 + 2];
                                pPrevTrunk.Data[0 + 3] = pTrunk.Data[0 + 3];
                            }
                        }
                        else
                        {
                            // The trunk page is required by the caller but it contains pointers to free-list leaves. The first leaf becomes a trunk page in this case.
                            var pNewTrunk = new MemPage();
                            var iNewTrunk = (Pgno)ConvertEx.Get4(pTrunk.Data, 8);
                            if (iNewTrunk > mxPage)
                            {
                                rc = SysEx.SQLITE_CORRUPT_BKPT();
                                goto end_allocate_page;
                            }
                            rc = btreeGetPage(iNewTrunk, ref pNewTrunk, 0);
                            if (rc != RC.OK)
                            {
                                goto end_allocate_page;
                            }
                            rc = Pager.Write(pNewTrunk.DbPage);
                            if (rc != RC.OK)
                            {
                                pNewTrunk.releasePage();
                                goto end_allocate_page;
                            }
                            pNewTrunk.Data[0 + 0] = pTrunk.Data[0 + 0];
                            pNewTrunk.Data[0 + 1] = pTrunk.Data[0 + 1];
                            pNewTrunk.Data[0 + 2] = pTrunk.Data[0 + 2];
                            pNewTrunk.Data[0 + 3] = pTrunk.Data[0 + 3];
                            ConvertEx.Put4(pNewTrunk.Data, 4, (uint)(k - 1));
                            Buffer.BlockCopy(pTrunk.Data, 12, pNewTrunk.Data, 8, (int)(k - 1) * 4);
                            pNewTrunk.releasePage();
                            if (pPrevTrunk == null)
                            {
                                Debug.Assert(Pager.IsPageWriteable(pPage1.DbPage));
                                ConvertEx.Put4(pPage1.Data, 32, iNewTrunk);
                            }
                            else
                            {
                                rc = Pager.Write(pPrevTrunk.DbPage);
                                if (rc != RC.OK)
                                {
                                    goto end_allocate_page;
                                }
                                ConvertEx.Put4(pPrevTrunk.Data, 0, iNewTrunk);
                            }
                        }
                        pTrunk = null;
                        Btree.TRACE("ALLOCATE: %d trunk - %d free pages left\n", pPgno, n - 1);
#endif
                    }
                    else if (k > 0)
                    {
                        // Extract a leaf from the trunk
                        uint closest;
                        var  aData = pTrunk.Data;
                        if (nearby > 0)
                        {
                            closest = 0;
                            var dist = Math.Abs((int)(ConvertEx.Get4(aData, 8) - nearby));
                            for (uint i = 1; i < k; i++)
                            {
                                int dist2 = Math.Abs((int)(ConvertEx.Get4(aData, 8 + i * 4) - nearby));
                                if (dist2 < dist)
                                {
                                    closest = i;
                                    dist    = dist2;
                                }
                            }
                        }
                        else
                        {
                            closest = 0;
                        }
                        //
                        var iPage = (Pgno)ConvertEx.Get4(aData, 8 + closest * 4);
                        if (iPage > mxPage)
                        {
                            rc = SysEx.SQLITE_CORRUPT_BKPT();
                            goto end_allocate_page;
                        }
                        if (searchList == 0 || iPage == nearby)
                        {
                            pPgno = iPage;
                            Btree.TRACE("ALLOCATE: %d was leaf %d of %d on trunk %d" + ": %d more free pages\n", pPgno, closest + 1, k, pTrunk.ID, n - 1);
                            rc = Pager.Write(pTrunk.DbPage);
                            if (rc != RC.OK)
                            {
                                goto end_allocate_page;
                            }
                            if (closest < k - 1)
                            {
                                Buffer.BlockCopy(aData, (int)(4 + k * 4), aData, 8 + (int)closest * 4, 4);
                            }
                            ConvertEx.Put4(aData, 4, (k - 1));
                            var noContent = (!btreeGetHasContent(pPgno) ? 1 : 0);
                            rc = btreeGetPage(pPgno, ref ppPage, noContent);
                            if (rc == RC.OK)
                            {
                                rc = Pager.Write((ppPage).DbPage);
                                if (rc != RC.OK)
                                {
                                    ppPage.releasePage();
                                }
                            }
                            searchList = 0;
                        }
                    }
                    pPrevTrunk.releasePage();
                    pPrevTrunk = null;
                } while (searchList != 0);
            }
            else
            {
                // There are no pages on the freelist, so create a new page at the end of the file
                rc = Pager.Write(this.Page1.DbPage);
                if (rc != RC.OK)
                {
                    return(rc);
                }
                this.Pages++;
                if (this.Pages == MemPage.PENDING_BYTE_PAGE(this))
                {
                    this.Pages++;
                }
#if !SQLITE_OMIT_AUTOVACUUM
                if (this.AutoVacuum && MemPage.PTRMAP_ISPAGE(this, this.Pages))
                {
                    // If pPgno refers to a pointer-map page, allocate two new pages at the end of the file instead of one. The first allocated page
                    // becomes a new pointer-map page, the second is used by the caller.
                    MemPage pPg = null;
                    Btree.TRACE("ALLOCATE: %d from end of file (pointer-map page)\n", pPgno);
                    Debug.Assert(this.Pages != MemPage.PENDING_BYTE_PAGE(this));
                    rc = btreeGetPage(this.Pages, ref pPg, 1);
                    if (rc == RC.OK)
                    {
                        rc = Pager.Write(pPg.DbPage);
                        pPg.releasePage();
                    }
                    if (rc != RC.OK)
                    {
                        return(rc);
                    }
                    this.Pages++;
                    if (this.Pages == MemPage.PENDING_BYTE_PAGE(this))
                    {
                        this.Pages++;
                    }
                }
#endif
                ConvertEx.Put4(this.Page1.Data, 28, this.Pages);
                pPgno = this.Pages;
                Debug.Assert(pPgno != MemPage.PENDING_BYTE_PAGE(this));
                rc = btreeGetPage(pPgno, ref ppPage, 1);
                if (rc != RC.OK)
                {
                    return(rc);
                }
                rc = Pager.Write((ppPage).DbPage);
                if (rc != RC.OK)
                {
                    ppPage.releasePage();
                }
                Btree.TRACE("ALLOCATE: %d from end of file\n", pPgno);
            }
            Debug.Assert(pPgno != MemPage.PENDING_BYTE_PAGE(this));

end_allocate_page:
            pTrunk.releasePage();
            pPrevTrunk.releasePage();
            if (rc == RC.OK)
            {
                if (Pager.GetPageRefCount((ppPage).DbPage) > 1)
                {
                    ppPage.releasePage();
                    return(SysEx.SQLITE_CORRUPT_BKPT());
                }
                (ppPage).HasInit = false;
            }
            else
            {
                ppPage = null;
            }
            Debug.Assert(rc != RC.OK || Pager.IsPageWriteable((ppPage).DbPage));
            return(rc);
        }
Пример #12
0
        internal RC btreeCreateTable(ref int piTable, CREATETABLE createTabFlags)
        {
            var  pBt      = this.Shared;
            var  pRoot    = new MemPage();
            Pgno pgnoRoot = 0;
            int  ptfFlags;         // Page-type flage for the root page of new table
            RC   rc;

            Debug.Assert(sqlite3BtreeHoldsMutex());
            Debug.Assert(pBt.InTransaction == TRANS.WRITE);
            Debug.Assert(!pBt.ReadOnly);
#if SQLITE_OMIT_AUTOVACUUM
            rc = allocateBtreePage(pBt, ref pRoot, ref pgnoRoot, 1, 0);
            if (rc != SQLITE.OK)
            {
                return(rc);
            }
#else
            if (pBt.AutoVacuum)
            {
                Pgno pgnoMove  = 0;             // Move a page here to make room for the root-page
                var  pPageMove = new MemPage(); // The page to move to.
                // Creating a new table may probably require moving an existing database to make room for the new tables root page. In case this page turns
                // out to be an overflow page, delete all overflow page-map caches held by open cursors.
                invalidateAllOverflowCache(pBt);
                // Read the value of meta[3] from the database to determine where the root page of the new table should go. meta[3] is the largest root-page
                // created so far, so the new root-page is (meta[3]+1).
                GetMeta((int)META.LARGEST_ROOT_PAGE, ref pgnoRoot);
                pgnoRoot++;
                // The new root-page may not be allocated on a pointer-map page, or the PENDING_BYTE page.
                while (pgnoRoot == MemPage.PTRMAP_PAGENO(pBt, pgnoRoot) || pgnoRoot == MemPage.PENDING_BYTE_PAGE(pBt))
                {
                    pgnoRoot++;
                }
                Debug.Assert(pgnoRoot >= 3);
                // Allocate a page. The page that currently resides at pgnoRoot will be moved to the allocated page (unless the allocated page happens to reside at pgnoRoot).
                rc = pBt.allocateBtreePage(ref pPageMove, ref pgnoMove, pgnoRoot, 1);
                if (rc != RC.OK)
                {
                    return(rc);
                }
                if (pgnoMove != pgnoRoot)
                {
                    // pgnoRoot is the page that will be used for the root-page of the new table (assuming an error did not occur). But we were
                    // allocated pgnoMove. If required (i.e. if it was not allocated by extending the file), the current page at position pgnoMove
                    // is already journaled.
                    PTRMAP eType    = 0;
                    Pgno   iPtrPage = 0;
                    pPageMove.releasePage();
                    // Move the page currently at pgnoRoot to pgnoMove.
                    rc = pBt.btreeGetPage(pgnoRoot, ref pRoot, 0);
                    if (rc != RC.OK)
                    {
                        return(rc);
                    }
                    rc = pBt.ptrmapGet(pgnoRoot, ref eType, ref iPtrPage);
                    if (eType == PTRMAP.ROOTPAGE || eType == PTRMAP.FREEPAGE)
                    {
                        rc = SysEx.SQLITE_CORRUPT_BKPT();
                    }
                    if (rc != RC.OK)
                    {
                        pRoot.releasePage();
                        return(rc);
                    }
                    Debug.Assert(eType != PTRMAP.ROOTPAGE);
                    Debug.Assert(eType != PTRMAP.FREEPAGE);
                    rc = MemPage.relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
                    pRoot.releasePage();
                    // Obtain the page at pgnoRoot
                    if (rc != RC.OK)
                    {
                        return(rc);
                    }
                    rc = pBt.btreeGetPage(pgnoRoot, ref pRoot, 0);
                    if (rc != RC.OK)
                    {
                        return(rc);
                    }
                    rc = Pager.Write(pRoot.DbPage);
                    if (rc != RC.OK)
                    {
                        pRoot.releasePage();
                        return(rc);
                    }
                }
                else
                {
                    pRoot = pPageMove;
                }
                // Update the pointer-map and meta-data with the new root-page number.
                pBt.ptrmapPut(pgnoRoot, PTRMAP.ROOTPAGE, 0, ref rc);
                if (rc != RC.OK)
                {
                    pRoot.releasePage();
                    return(rc);
                }
                // When the new root page was allocated, page 1 was made writable in order either to increase the database filesize, or to decrement the
                // freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
                Debug.Assert(Pager.IsPageWriteable(pBt.Page1.DbPage));
                rc = SetMeta(4, pgnoRoot);
                if (Check.NEVER(rc != RC.OK))
                {
                    pRoot.releasePage();
                    return(rc);
                }
            }
            else
            {
                rc = pBt.allocateBtreePage(ref pRoot, ref pgnoRoot, 1, 0);
                if (rc != RC.OK)
                {
                    return(rc);
                }
            }
#endif
            Debug.Assert(Pager.IsPageWriteable(pRoot.DbPage));
            ptfFlags = ((createTabFlags & CREATETABLE.INTKEY) != 0 ? PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF : PTF_ZERODATA | PTF_LEAF);
            pRoot.zeroPage(ptfFlags);
            Pager.Unref(pRoot.DbPage);
            Debug.Assert((pBt.OpenFlags & OPEN.SINGLE) == 0 || pgnoRoot == 2);
            piTable = (int)pgnoRoot;
            return(RC.OK);
        }
Пример #13
0
        internal RC allocateSpace(int nByte, ref int pIdx)
        {
            var hdr  = this.HeaderOffset;
            var data = this.Data;

            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            Debug.Assert(this.Shared != null);
            Debug.Assert(MutexEx.Held(this.Shared.Mutex));
            Debug.Assert(nByte >= 0);  // Minimum cell size is 4
            Debug.Assert(this.FreeBytes >= nByte);
            Debug.Assert(this.NOverflows == 0);
            var usableSize = this.Shared.UsableSize; // Usable size of the page

            Debug.Assert(nByte < usableSize - 8);
            var nFrag = data[hdr + 7]; // Number of fragmented bytes on pPage

            Debug.Assert(this.CellOffset == hdr + 12 - 4 * this.Leaf);
            var gap = this.CellOffset + 2 * this.Cells; // First byte of gap between cell pointers and cell content
            var top = ConvertEx.Get2nz(data, hdr + 5);  // First byte of cell content area

            if (gap > top)
            {
                return(SysEx.SQLITE_CORRUPT_BKPT());
            }
            if (nFrag >= 60)
            {
                // Always defragment highly fragmented pages
                var rc = defragmentPage();
                if (rc != RC.OK)
                {
                    return(rc);
                }
                top = ConvertEx.Get2nz(data, hdr + 5);
            }
            else if (gap + 2 <= top)
            {
                // Search the freelist looking for a free slot big enough to satisfy the request. The allocation is made from the first free slot in
                // the list that is large enough to accomadate it.
                int pc;
                for (var addr = hdr + 1; (pc = ConvertEx.Get2(data, addr)) > 0; addr = pc)
                {
                    if (pc > usableSize - 4 || pc < addr + 4)
                    {
                        return(SysEx.SQLITE_CORRUPT_BKPT());
                    }
                    var size = ConvertEx.Get2(data, pc + 2); // Size of free slot
                    if (size >= nByte)
                    {
                        var x = size - nByte;
                        if (x < 4)
                        {
                            // Remove the slot from the free-list. Update the number of fragmented bytes within the page.
                            data[addr + 0] = data[pc + 0];
                            data[addr + 1] = data[pc + 1];
                            data[hdr + 7]  = (byte)(nFrag + x);
                        }
                        else if (size + pc > usableSize)
                        {
                            return(SysEx.SQLITE_CORRUPT_BKPT());
                        }
                        else
                        {
                            // The slot remains on the free-list. Reduce its size to account for the portion used by the new allocation.
                            ConvertEx.Put2(data, pc + 2, x);
                        }
                        pIdx = pc + x;
                        return(RC.OK);
                    }
                }
            }
            // Check to make sure there is enough space in the gap to satisfy the allocation.  If not, defragment.
            if (gap + 2 + nByte > top)
            {
                var rc = defragmentPage();
                if (rc != RC.OK)
                {
                    return(rc);
                }
                top = ConvertEx.Get2nz(data, hdr + 5);
                Debug.Assert(gap + nByte <= top);
            }
            // Allocate memory from the gap in between the cell pointer array and the cell content area.  The btreeInitPage() call has already
            // validated the freelist.  Given that the freelist is valid, there is no way that the allocation can extend off the end of the page.
            // The Debug.Assert() below verifies the previous sentence.
            top -= nByte;
            ConvertEx.Put2(data, hdr + 5, top);
            Debug.Assert(top + nByte <= (int)this.Shared.UsableSize);
            pIdx = top;
            return(RC.OK);
        }
Пример #14
0
        internal RC freeSpace(int start, int size)
        {
            var data = this.Data;

            Debug.Assert(this.Shared != null);
            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            Debug.Assert(start >= this.HeaderOffset + 6 + this.ChildPtrSize);
            Debug.Assert((start + size) <= (int)this.Shared.UsableSize);
            Debug.Assert(MutexEx.Held(this.Shared.Mutex));
            Debug.Assert(size >= 0);   // Minimum cell size is 4
            if (this.Shared.SecureDelete)
            {
                // Overwrite deleted information with zeros when the secure_delete option is enabled
                Array.Clear(data, start, size);
            }
            // Add the space back into the linked list of freeblocks.  Note that even though the freeblock list was checked by btreeInitPage(),
            // btreeInitPage() did not detect overlapping cells or freeblocks that overlapped cells.   Nor does it detect when the
            // cell content area exceeds the value in the page header.  If these situations arise, then subsequent insert operations might corrupt
            // the freelist.  So we do need to check for corruption while scanning the freelist.
            var hdr   = this.HeaderOffset;
            var addr  = hdr + 1;
            var iLast = (int)this.Shared.UsableSize - 4; // Largest possible freeblock offset

            Debug.Assert(start <= iLast);
            int pbegin;

            while ((pbegin = ConvertEx.Get2(data, addr)) < start && pbegin > 0)
            {
                if (pbegin < addr + 4)
                {
                    return(SysEx.SQLITE_CORRUPT_BKPT());
                }
                addr = pbegin;
            }
            if (pbegin > iLast)
            {
                return(SysEx.SQLITE_CORRUPT_BKPT());
            }
            Debug.Assert(pbegin > addr || pbegin == 0);
            ConvertEx.Put2(data, addr, start);
            ConvertEx.Put2(data, start, pbegin);
            ConvertEx.Put2(data, start + 2, size);
            this.FreeBytes = (ushort)(this.FreeBytes + size);
            // Coalesce adjacent free blocks
            addr = hdr + 1;
            while ((pbegin = ConvertEx.Get2(data, addr)) > 0)
            {
                Debug.Assert(pbegin > addr);
                Debug.Assert(pbegin <= (int)this.Shared.UsableSize - 4);
                var pnext = ConvertEx.Get2(data, pbegin);
                var psize = ConvertEx.Get2(data, pbegin + 2);
                if (pbegin + psize + 3 >= pnext && pnext > 0)
                {
                    var frag = pnext - (pbegin + psize);
                    if ((frag < 0) || (frag > (int)data[hdr + 7]))
                    {
                        return(SysEx.SQLITE_CORRUPT_BKPT());
                    }
                    data[hdr + 7] -= (byte)frag;
                    var x = ConvertEx.Get2(data, pnext);
                    ConvertEx.Put2(data, pbegin, x);
                    x = pnext + ConvertEx.Get2(data, pnext + 2) - pbegin;
                    ConvertEx.Put2(data, pbegin + 2, x);
                }
                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])
            {
                pbegin = ConvertEx.Get2(data, hdr + 1);
                ConvertEx.Put2(data, hdr + 1, ConvertEx.Get2(data, pbegin));
                var top = ConvertEx.Get2(data, hdr + 5) + ConvertEx.Get2(data, pbegin + 2);
                ConvertEx.Put2(data, hdr + 5, top);
            }
            Debug.Assert(Pager.IsPageWriteable(this.DbPage));
            return(RC.OK);
        }