Example #1
0
        public static FileStreamWrapper CreateObject(string file)
        {
            FileStreamWrapper obj = new FileStreamWrapper();

            obj.Init(file);
            return(obj);
        }
Example #2
0
        /// <summary>
        /// Writes a list of items to persistent storage using a "chain" format, where each
        /// page stores a "pointer" to the next page containing the following range of elements.
        /// </summary>
        /// <param name="stream">data file to write to</param>
        /// <param name="freeSpaceMgr">object that keeps track of available pages in the file</param>
        /// <param name="list">list of items to write</param>
        /// <param name="pages">list of pages we wrote to</param>
        public void WriteList(FileStreamWrapper stream, StoragePageManager freeSpaceMgr, List <T> list, out List <int> pages)
        {
            // create the pages
            this.CreateNewPage(list.Count, freeSpaceMgr);
            for (int idx = 0; idx < list.Count; idx++)
            {
                var item = list[idx];

                try
                {
                    this.pageList.Peek().AddRecord(item);
                    this.pageHeaderList.Peek().PageEntriesCount++;
                }
                catch (StoragePage.InsuffcientSpaceException)
                {
                    this.CreateNewPage(list.Count, freeSpaceMgr);
                    idx--;
                }
            }

            // write the pages
            int lastPageIndex = ListHdr.EOLPageIndex;

            while (0 < this.pageList.Count)
            {
                lastPageIndex = this.WriteTopPage(stream, lastPageIndex);
            }

            // set the output parameter
            pages = this.pageIdxList;
        }
Example #3
0
        /// <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;
        }
Example #4
0
        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);
        }
Example #5
0
 public static FileStreamWrapper CreateObject(string file)
 {
     FileStreamWrapper obj = new FileStreamWrapper();
     obj.Init(file);
     return obj;
 }