// allocate a new node from free list or from pager private BTreeNode GetNewNode(PageTypes nodeType) { // allocate MemoryPage newPage = _freeList.AllocatePage(); if (newPage == null) { newPage = _pager.GetNewPage(); } // initialize node BTreeNode node = new BTreeNode(newPage, nodeType); return(node); }
static void TestFreeList() { string dbPath = "./testdbfile.minidb"; File.Delete(dbPath); Pager pager = new Pager(dbPath, 4096, 100); List <MemoryPage> pageList = new List <MemoryPage>(); pageList.Add(pager.GetNewPage()); // page #2 pageList.Add(pager.GetNewPage()); // page #3 pageList.Add(pager.GetNewPage()); // page #4 pageList.Add(pager.GetNewPage()); // page #5 FreeList freeList = new FreeList(pager); // test initialization MemoryPage newPage = null; newPage = freeList.AllocatePage(); // freeList is now empty Assert.Null(newPage); // recycle pages // MemoryPage tempPage = pager.ReadPage(3); freeList.RecyclePage(pageList[2]); // freeList->4 freeList.RecyclePage(pageList[1]); // freeList->3->4 freeList.RecyclePage(pageList[3]); // freeList->5->3->4 // fetch page from free list newPage = freeList.AllocatePage(); // freeList->3->4 Assert.Equal(5, newPage.PageNumber); newPage = freeList.AllocatePage(); // freeList->4 Assert.Equal(3, newPage.PageNumber); // recycle a page freeList.RecyclePage(pageList[3]); // freeList->5->4 // fetch remaining pages newPage = freeList.AllocatePage(); // freeList->4 Assert.Equal(5, newPage.PageNumber); newPage = freeList.AllocatePage(); // freeList->null Assert.Equal(4, newPage.PageNumber); newPage = freeList.AllocatePage(); // freeList->null Assert.Null(newPage); pager.Close(); }