public unsafe Page ModifyPage(Tree tree, Page parent, long p, Cursor c) { long dirtyPageNum; Page page; if (_dirtyPages.TryGetValue(p, out dirtyPageNum)) { page = c.GetPage(dirtyPageNum) ?? _pager.Get(this, dirtyPageNum); page.Dirty = true; UpdateParentPageNumber(parent, page.PageNumber); return(page); } var newPage = AllocatePage(1); newPage.Dirty = true; var newPageNum = newPage.PageNumber; page = c.GetPage(p) ?? _pager.Get(this, p); NativeMethods.memcpy(newPage.Base, page.Base, _pager.PageSize); newPage.LastSearchPosition = page.LastSearchPosition; newPage.LastMatch = page.LastMatch; newPage.PageNumber = newPageNum; FreePage(p); _dirtyPages[p] = newPage.PageNumber; UpdateParentPageNumber(parent, newPage.PageNumber); return(newPage); }
private void Setup(IVirtualPager pager) { if (pager.NumberOfAllocatedPages == 0) { WriteEmptyHeaderPage(_pager.Get(null, 0)); WriteEmptyHeaderPage(_pager.Get(null, 1)); NextPageNumber = 2; using (var tx = new Transaction(_pager, this, _transactionsCounter + 1, TransactionFlags.ReadWrite, _freeSpaceRepository)) { var root = Tree.Create(tx, _sliceComparer); var freeSpace = Tree.Create(tx, _sliceComparer); // important to first create the two trees, then set them on the env FreeSpaceRoot = freeSpace; Root = root; tx.UpdateRoots(root, freeSpace); tx.Commit(); } return; } // existing db, let us load it // the first two pages are allocated for double buffering tx commits FileHeader *entry = FindLatestFileHeadeEntry(); NextPageNumber = entry->LastPageNumber + 1; _transactionsCounter = entry->TransactionId + 1; using (var tx = new Transaction(_pager, this, _transactionsCounter + 1, TransactionFlags.ReadWrite, _freeSpaceRepository)) { var root = Tree.Open(tx, _sliceComparer, &entry->Root); var freeSpace = Tree.Open(tx, _sliceComparer, &entry->FreeSpace); // important to first create the two trees, then set them on the env FreeSpaceRoot = freeSpace; Root = root; tx.Commit(); } }