/// <summary> /// Reads a list of items from persistent storage. The list is written as a chain of pages, /// where the current page stores a pointer to the page storing the next range of items. /// The items can be any "serializable" C# type. /// </summary> /// <param name="stream">data file to read from</param> /// <param name="pageIdx">index of the page storing the head of the list</param> /// <param name="list">list of items read</param> /// <param name="pages">list of physical pages we read from</param> public void ReadList(FileStreamWrapper stream, int pageIdx, out List <T> list, out List <int> pages) { // read pages one by one while (ListHdr.EOLPageIndex != pageIdx) { // read the page data StoragePage page = new StoragePage(); int readPage = page.ReadPageData(stream, pageIdx); if (readPage != pageIdx) { throw new InvalidListException(); } // read the header ListHdr header = (ListHdr)page.ReadRecord(ListHdr.HeaderRecordIdx); if (null == header) { throw new InvalidListException(); } // process the page this.ReadCurrentPage(page, header.PageEntriesCount); // update the page index this.pageList.Add(pageIdx); pageIdx = header.NextPageIndex; } // set the output variables list = this.itemList; pages = this.pageList; }
private void CreateNewPage(int count, StoragePageManager freeSpaceMgr) { // create a new header object ListHdr header = new ListHdr() { NextPageIndex = ListHdr.EOLPageIndex, TotalEntriesCount = count, PageEntriesCount = 0, PageWriteIndex = freeSpaceMgr.GetFreePage() }; this.pageHeaderList.Push(header); // create the page - and write a placeholder record for the header StoragePage page = new StoragePage(); page.AddRecord(header); this.pageList.Push(page); }
private int WriteTopPage(FileStreamWrapper stream, int lastPageAddress) { // update the header ListHdr header = this.pageHeaderList.Pop(); header.NextPageIndex = lastPageAddress; // update the page StoragePage page = this.pageList.Pop(); page.WriteRecord(ListHdr.HeaderRecordIdx, header); // get the page to write to int pageIdx = header.PageWriteIndex; // write the page pageIdx = page.WritePageData(stream, pageIdx); this.pageIdxList.Insert(0, pageIdx); return(pageIdx); }