Exemplo n.º 1
0
        private async Task <Page> ReadPageAsync(int pageNum, ushort pageSize, CancellationToken cancellationToken)
        {
            // Sanity checks
            if (stream == null)
            {
                // TODO - create custom exception
                throw new Exception("Block store is not open!");
            }

            // Seek to the proper spot
            SeekToPage(pageNum);

            // Read the bytes for the page
            var bytes = new byte[pageSize];

            await stream.ReadAsync(bytes, 0, pageSize);

            // Peek at the page type and build the proper page type.
            // TODO - a big switch statement is ugly...find a better way.
            var  pageType = BitConverter.ToUInt16(bytes, 1);
            Page page     = null;

            switch (pageType)
            {
            case ZeroPage.PageId:
                page = new ZeroPage();
                break;

            case HeaderPage.PageId:
                page = new HeaderPage();
                break;

            default:
                throw new Exception($"Unhandled page type: {pageType}.");
            }

            // Have the page deserialize itself
            page.Deserialize(bytes);

            // And return whatever we've got.
            return(page);
        }