/// <summary> /// Creates a file backed memory stream. /// </summary> /// <param name="stream">The <see cref="CustomFileStream"/> to buffer</param> /// <param name="pool">The <see cref="MemoryPool"/> to allocate memory from</param> /// <param name="header">The <see cref="FileHeaderBlock"/> to be managed when modifications occur</param> /// <param name="isNewFile">Tells if this is a newly created file. This will make sure that the /// first 10 pages have the header data copied to it.</param> public BufferedFile(CustomFileStream stream, MemoryPool pool, FileHeaderBlock header, bool isNewFile) { m_fileStructureBlockSize = header.BlockSize; m_diskBlockSize = pool.PageSize; m_lengthOfHeader = header.BlockSize * header.HeaderBlockCount; m_writeBuffer = new MemoryPoolStreamCore(pool); m_pool = pool; m_queue = stream; m_syncRoot = new object(); m_pageReplacementAlgorithm = new PageReplacementAlgorithm(pool); pool.RequestCollection += m_pool_RequestCollection; if (isNewFile) { try { m_queue.Open(); byte[] headerBytes = header.GetBytes(); for (int x = 0; x < header.HeaderBlockCount; x++) { m_queue.WriteRaw(0, headerBytes, headerBytes.Length); } } finally { m_queue.Close(); } } m_lengthOfCommittedData = (header.LastAllocatedBlock + 1) * header.BlockSize; m_writeBuffer.ConfigureAlignment(m_lengthOfCommittedData, pool.PageSize); }
public void Test() { MemoryPoolTest.TestMemoryLeak(); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); FileHeaderBlock header = FileHeaderBlock.CreateNew(4096); header = header.CloneEditable(); header.CreateNewFile(SubFileName.CreateRandom()); header.CreateNewFile(SubFileName.CreateRandom()); header.CreateNewFile(SubFileName.CreateRandom()); header.IsReadOnly = true; FileHeaderBlock header2 = FileHeaderBlock.Open(header.GetBytes()); CheckEqual(header2, header); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); //verify they are the same; MemoryPoolTest.TestMemoryLeak(); }
/// <summary> /// Executes a commit of data. This will flush the data to the disk use the provided header data to properly /// execute this function. /// </summary> /// <param name="header"></param> public void CommitChanges(FileHeaderBlock header) { using (var pageLock = new IoSession(this, m_pageReplacementAlgorithm)) { //Determine how much committed data to write long lengthOfAllData = (header.LastAllocatedBlock + 1) * (long)m_fileStructureBlockSize; long copyLength = lengthOfAllData - m_lengthOfCommittedData; //Write the uncommitted data. m_queue.Write(m_lengthOfCommittedData, m_writeBuffer, copyLength, waitForWriteToDisk: true); byte[] bytes = header.GetBytes(); if (header.HeaderBlockCount == 10) { //Update the new header to position 0, position 1, and one of position 2-9 m_queue.WriteRaw(0, bytes, m_fileStructureBlockSize); m_queue.WriteRaw(m_fileStructureBlockSize, bytes, m_fileStructureBlockSize); m_queue.WriteRaw(m_fileStructureBlockSize * ((header.SnapshotSequenceNumber & 7) + 2), bytes, m_fileStructureBlockSize); } else { for (int x = 0; x < header.HeaderBlockCount; x++) { m_queue.WriteRaw(x * m_fileStructureBlockSize, bytes, m_fileStructureBlockSize); } } m_queue.FlushFileBuffers(); long startPos; //Copy recently committed data to the buffer pool if ((m_lengthOfCommittedData & (m_diskBlockSize - 1)) != 0) //Only if there is a split page. { startPos = m_lengthOfCommittedData & (~(long)(m_diskBlockSize - 1)); //Finish filling up the split page in the buffer. IntPtr ptrDest; if (pageLock.TryGetSubPage(startPos, out ptrDest)) { int length; IntPtr ptrSrc; m_writeBuffer.ReadBlock(m_lengthOfCommittedData, out ptrSrc, out length); Footer.WriteChecksumResultsToFooter(ptrSrc, m_fileStructureBlockSize, length); ptrDest += (m_diskBlockSize - length); Memory.Copy(ptrSrc, ptrDest, length); } startPos += m_diskBlockSize; } else { startPos = m_lengthOfCommittedData; } while (startPos < lengthOfAllData) { //If the address doesn't exist in the current list. Read it from the disk. int poolPageIndex; IntPtr poolAddress; m_pool.AllocatePage(out poolPageIndex, out poolAddress); m_writeBuffer.CopyTo(startPos, poolAddress, m_diskBlockSize); Footer.WriteChecksumResultsToFooter(poolAddress, m_fileStructureBlockSize, m_diskBlockSize); if (!m_pageReplacementAlgorithm.TryAddPage(startPos, poolAddress, poolPageIndex)) { m_pool.ReleasePage(poolPageIndex); } startPos += m_diskBlockSize; } m_lengthOfCommittedData = lengthOfAllData; } ReleaseWriteBufferSpace(); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> /// <remarks>A debug function</remarks> internal static bool AreEqual(FileHeaderBlock other, FileHeaderBlock self) { if (other is null) { return(false); } if (self is null) { return(false); } if (self.CanWrite != other.CanWrite) { return(false); } if (self.CanRead != other.CanRead) { return(false); } if (self.ArchiveId != other.ArchiveId) { return(false); } if (self.ArchiveType != other.ArchiveType) { return(false); } if (self.SnapshotSequenceNumber != other.SnapshotSequenceNumber) { return(false); } if (self.LastAllocatedBlock != other.LastAllocatedBlock) { return(false); } if (self.FileCount != other.FileCount) { return(false); } //compare files. if (self.Files is null) { if (other.Files != null) { return(false); } } else { if (other.Files is null) { return(false); } if (self.Files.Count != other.Files.Count) { return(false); } for (int x = 0; x < self.Files.Count; x++) { SubFileHeader subFile = self.Files[x]; SubFileHeader subFileOther = other.Files[x]; if (subFile is null) { if (subFileOther != null) { return(false); } } else { if (subFileOther is null) { return(false); } if (!SubFileMetaDataTest.AreEqual(subFile, subFileOther)) { return(false); } } } } return(self.GetBytes().SequenceEqual(other.GetBytes())); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> /// <remarks>A debug function</remarks> internal static bool AreEqual(FileHeaderBlock other, FileHeaderBlock self) { if (other == null) return false; if (self == null) return false; if (self.CanWrite != other.CanWrite) return false; if (self.CanRead != other.CanRead) return false; if (self.ArchiveId != other.ArchiveId) return false; if (self.ArchiveType != other.ArchiveType) return false; if (self.SnapshotSequenceNumber != other.SnapshotSequenceNumber) return false; if (self.LastAllocatedBlock != other.LastAllocatedBlock) return false; if (self.FileCount != other.FileCount) return false; //compare files. if (self.Files == null) { if (other.Files != null) return false; } else { if (other.Files == null) return false; if (self.Files.Count != other.Files.Count) return false; for (int x = 0; x < self.Files.Count; x++) { SubFileHeader subFile = self.Files[x]; SubFileHeader subFileOther = other.Files[x]; if (subFile == null) { if (subFileOther != null) return false; } else { if (subFileOther == null) return false; if (!SubFileMetaDataTest.AreEqual(subFile, subFileOther)) return false; } } } return (self.GetBytes().SequenceEqual(other.GetBytes())); }
/// <summary> /// Executes a commit of data. This will flush the data to the disk use the provided header data to properly /// execute this function. /// </summary> /// <param name="header"></param> public void CommitChanges(FileHeaderBlock header) { using (var pageLock = new IoSession(this, m_pageReplacementAlgorithm)) { //Determine how much committed data to write long lengthOfAllData = (header.LastAllocatedBlock + 1) * (long)m_fileStructureBlockSize; long copyLength = lengthOfAllData - m_lengthOfCommittedData; //Write the uncommitted data. m_queue.Write(m_lengthOfCommittedData, m_writeBuffer, copyLength, waitForWriteToDisk: true); byte[] bytes = header.GetBytes(); if (header.HeaderBlockCount == 10) { //Update the new header to position 0, position 1, and one of position 2-9 m_queue.WriteRaw(0, bytes, m_fileStructureBlockSize); m_queue.WriteRaw(m_fileStructureBlockSize, bytes, m_fileStructureBlockSize); m_queue.WriteRaw(m_fileStructureBlockSize * ((header.SnapshotSequenceNumber & 7) + 2), bytes, m_fileStructureBlockSize); } else { for (int x = 0; x < header.HeaderBlockCount; x++) { m_queue.WriteRaw(x * m_fileStructureBlockSize, bytes, m_fileStructureBlockSize); } } m_queue.FlushFileBuffers(); long startPos; //Copy recently committed data to the buffer pool if ((m_lengthOfCommittedData & (m_diskBlockSize - 1)) != 0) //Only if there is a split page. { startPos = m_lengthOfCommittedData & (~(long)(m_diskBlockSize - 1)); //Finish filling up the split page in the buffer. IntPtr ptrDest; if (pageLock.TryGetSubPage(startPos, out ptrDest)) { int length; IntPtr ptrSrc; m_writeBuffer.ReadBlock(m_lengthOfCommittedData, out ptrSrc, out length); Footer.WriteChecksumResultsToFooter(ptrSrc, m_fileStructureBlockSize, length); ptrDest += (m_diskBlockSize - length); Memory.Copy(ptrSrc, ptrDest, length); } startPos += m_diskBlockSize; } else { startPos = m_lengthOfCommittedData; } while (startPos < lengthOfAllData) { //If the address doesn't exist in the current list. Read it from the disk. int poolPageIndex; IntPtr poolAddress; m_pool.AllocatePage(out poolPageIndex, out poolAddress); m_writeBuffer.CopyTo(startPos, poolAddress, m_diskBlockSize); Footer.WriteChecksumResultsToFooter(poolAddress, m_fileStructureBlockSize, m_diskBlockSize); if (!m_pageReplacementAlgorithm.TryAddPage(startPos, poolAddress, poolPageIndex)) m_pool.ReleasePage(poolPageIndex); startPos += m_diskBlockSize; } m_lengthOfCommittedData = lengthOfAllData; } ReleaseWriteBufferSpace(); }
/// <summary> /// Creates a file backed memory stream. /// </summary> /// <param name="stream">The <see cref="CustomFileStream"/> to buffer</param> /// <param name="pool">The <see cref="MemoryPool"/> to allocate memory from</param> /// <param name="header">The <see cref="FileHeaderBlock"/> to be managed when modifications occur</param> /// <param name="isNewFile">Tells if this is a newly created file. This will make sure that the /// first 10 pages have the header data copied to it.</param> public BufferedFile(CustomFileStream stream, MemoryPool pool, FileHeaderBlock header, bool isNewFile) { m_fileStructureBlockSize = header.BlockSize; m_diskBlockSize = pool.PageSize; m_lengthOfHeader = header.BlockSize * header.HeaderBlockCount; m_writeBuffer = new MemoryPoolStreamCore(pool); m_pool = pool; m_queue = stream; m_syncRoot = new object(); m_pageReplacementAlgorithm = new PageReplacementAlgorithm(pool); pool.RequestCollection += m_pool_RequestCollection; if (isNewFile) { byte[] headerBytes = header.GetBytes(); for (int x = 0; x < header.HeaderBlockCount; x++) { m_queue.WriteRaw(0, headerBytes, headerBytes.Length); } } m_lengthOfCommittedData = (header.LastAllocatedBlock + 1) * (long)header.BlockSize; m_writeBuffer.ConfigureAlignment(m_lengthOfCommittedData, pool.PageSize); }