public void AddFileWithBasicChunkMap(string filePath) { var fileIndices = new List <int>(); var hashes = new MemoryStream(); int eofIndex = 0; int eofChunkSize = 0; AddFileAllChunks(filePath, delegate(int chunkIndex, byte[] hash) { fileIndices.Add(chunkIndex); hashes.Write(hash, 0, hash.Length); }, (i, s) => { eofIndex = i; eofChunkSize = s; }); logger.DebugFormat("(EofIndex, EofChunkSize) = ({0}, {1})", eofIndex, eofChunkSize); var txnProvider = new NHTransactionProvider( new NHSessionProvider(_sessionFactory)); var session = txnProvider.SessionProvider.CurrentSession; using (txnProvider) { using (var transaction = txnProvider.BeginTransaction()) { var helper = new ChunkDbHelper(session); // File should have been inserted already. var file = helper.GetManagedFile(filePath); var dto = new ChunkMapDto { FileIndices = fileIndices.ToArray(), Hashes = hashes.ToArray(), EofChunkIndex = eofIndex, EofChunkSize = eofChunkSize, LastPieceInProfile = 0 }; hashes.Dispose(); file.ChunkMap = new ChunkMap(dto); if (logger.IsDebugEnabled) { var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); ChunkMapSerializer.SerializeToXml(tempFilePath + ".xml", file.ChunkMap); ChunkMapSerializer.Serialize(tempFilePath, dto); logger.DebugFormat("ChunkMap is logged to file {0}", tempFilePath); } // Have the file committed to DB. transaction.Commit(); } logger.DebugFormat("ChunkMap added to file."); FileUtil.PadFileWithZeros(filePath); } }
public ChunkMap(ChunkMapDto dto) { if (dto.FileIndices.Length != dto.Hashes.Length / DataChunk.HashSize) { throw new ArgumentException( "Hashes and FileIndices don't match.", "dto"); } FileIndices = dto.FileIndices; EofChunkIndex = dto.EofChunkIndex; EofChunkSize = dto.EofChunkSize; Hashes = dto.Hashes; LastPieceInProfile = dto.LastPieceInProfile; GenerateChunkIndices(); }
public static void Serialize(Stream stream, ChunkMapDto chunkMap) { Serializer.Serialize<ChunkMapDto>(stream, chunkMap); }
public static void Serialize(string filePath, ChunkMapDto chunkMap) { using (var stream = File.OpenWrite(filePath)) { Serialize(stream, chunkMap); } }
public static void Serialize(Stream stream, ChunkMapDto chunkMap) { Serializer.Serialize <ChunkMapDto>(stream, chunkMap); }