public static void InsertFile(IndexNode node, Stream stream, Engine engine) { DataPage dataPage = null; var buffer = new byte[DataPage.DATA_PER_PAGE]; uint totalBytes = 0; int read = 0; int dataPerPage = (int)DataPage.DATA_PER_PAGE; while ((read = stream.Read(buffer, 0, dataPerPage)) > 0) { totalBytes += (uint)read; if (dataPage == null) // First read { dataPage = engine.GetPageData(node.DataPageID); } else { dataPage = GetNewDataPage(dataPage, engine); } if (!dataPage.IsEmpty) // This is never to happend!! { throw new FileDBException("Page {0} is not empty", dataPage.PageID); } Array.Copy(buffer, dataPage.DataBlock, read); dataPage.IsEmpty = false; dataPage.DataBlockLength = (short)read; } // If the last page point to another one, i need to fix that if (dataPage.NextPageID != uint.MaxValue) { engine.Header.FreeDataPageID = dataPage.NextPageID; dataPage.NextPageID = uint.MaxValue; } // Salve the last page on disk PageFactory.WriteToFile(dataPage, engine.Writer); // Save on node index that file length node.FileLength = totalBytes; }
public static void InsertFile(IndexNode node, Stream stream, Engine engine) { DataPage dataPage = null; var buffer = new byte[DataPage.DATA_PER_PAGE]; uint totalBytes = 0; int read = 0; int dataPerPage = (int)DataPage.DATA_PER_PAGE; while ((read = stream.Read(buffer, 0, dataPerPage)) > 0) { totalBytes += (uint)read; if (dataPage == null) // First read dataPage = engine.GetPageData(node.DataPageID); else dataPage = GetNewDataPage(dataPage, engine); if (!dataPage.IsEmpty) // This is never to happend!! throw new FileDBException("Page {0} is not empty", dataPage.PageID); Array.Copy(buffer, dataPage.DataBlock, read); dataPage.IsEmpty = false; dataPage.DataBlockLength = (short)read; } // If the last page point to another one, i need to fix that if (dataPage.NextPageID != uint.MaxValue) { engine.Header.FreeDataPageID = dataPage.NextPageID; dataPage.NextPageID = uint.MaxValue; } // Salve the last page on disk PageFactory.WriteToFile(dataPage, engine.Writer); // Save on node index that file length node.FileLength = totalBytes; }