/// <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; }
public void SP_TestReadWritePage() { string dataFile = "SP_TestData1.tpdb"; if (File.Exists(dataFile)) { File.Delete(dataFile); } TestData[] pageData = { new TestData { data = "Record_0", recordIdx = 0 }, new TestData { data = "Record_1", recordIdx = 1 }, new TestData { data = "Record_2", recordIdx = 2 } }; int pageIndex = int.MinValue; // write the page using (FileStreamWrapper dataFileStream = FileStreamWrapper.CreateObject(dataFile)) { // populate the page with some data StoragePage page = new StoragePage(); AddRecords(page, pageData); // write the file to disk pageIndex = page.WritePageData(dataFileStream, -1); } // read the page using (FileStreamWrapper dataFileStream = FileStreamWrapper.CreateObject(dataFile)) { // read page from file StoragePage page = new StoragePage(); page.ReadPageData(dataFileStream, pageIndex); // validate the page data ReadRecords(page, pageData); } }