Example #1
0
        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);
            }
        }
Example #2
0
 public static ChunkMap Create(byte[] serializedDto)
 {
     return(new ChunkMap(ChunkMapSerializer.Deserialize(
                             new MemoryStream(serializedDto))));
 }