Example #1
0
 public bool TryGetManagedFile(string path, out ManagedFile file)
 {
     try {
         file = GetManagedFile(path);
         return(true);
     } catch (ChunkDbException ex) {
         file = null;
         return(false);
     }
 }
Example #2
0
        /// <summary>
        /// Gets the chunk entry by the path and fileIndex.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="chunkIndex">The file index.</param>
        /// <returns></returns>
        public DataChunk GetChunkEntry(string filePath, int chunkIndex)
        {
            var txnProvider = new NHTransactionProvider(
                new NHSessionProvider(_sessionFactory));

            using (txnProvider) {
                using (var transaction = txnProvider.BeginTransaction()) {
                    var helper = new ChunkDbHelper(
                        txnProvider.SessionProvider.CurrentSession);
                    var         session = txnProvider.SessionProvider.CurrentSession;
                    ManagedFile file    = helper.GetManagedFile(filePath);
                    ICriteria   crit    = session.CreateCriteria <DataChunk>();
                    crit.Add(Expression.Eq(Projections.Property <DataChunk>(x => x.File), file))
                    .Add(Expression.Eq(Projections.Property <DataChunk>(x => x.ChunkIndex), chunkIndex));
                    DataChunk entry = crit.UniqueResult <DataChunk>();
                    return(entry);
                }
            }
        }
Example #3
0
        public void AddFile(string path, byte[] chunkMapDto)
        {
            var txnProvider = new NHTransactionProvider(
                new NHSessionProvider(_sessionFactory));

            using (txnProvider) {
                using (var transaction = txnProvider.BeginTransaction()) {
                    var         cm          = ChunkMap.Create(chunkMapDto);
                    ManagedFile managedFile =
                        new ManagedFile {
                        Path     = path,
                        ChunkMap = cm,
                        Size     = cm.FileSize
                    };
                    var session = txnProvider.SessionProvider.CurrentSession;
                    session.Save(managedFile);
                    transaction.Commit();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Adds the specified entry.
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <exception cref="DuplicateNameException">Thrown when there is
        /// already an entry with the same hash exists.</exception>
        public void AddChunk(byte[] hash, string filePath, int chunkIndex)
        {
            var txnProvider = new NHTransactionProvider(
                new NHSessionProvider(_sessionFactory));

            using (txnProvider) {
                using (var transaction = txnProvider.BeginTransaction()) {
                    var helper = new ChunkDbHelper(
                        txnProvider.SessionProvider.CurrentSession);
                    ManagedFile file  = helper.GetManagedFile(filePath);
                    var         entry = new DataChunk {
                        File       = file,
                        ChunkIndex = chunkIndex,
                        Hash       = hash,
                        Count      = 0
                    };
                    helper.AddChunk(entry);
                    transaction.Commit();
                }
            }   // Dispose session.
        }
Example #5
0
        public void AddFileToDownload(string path, byte[] chunkMap,
                                      byte[] torrent, long fileSize)
        {
            var txnProvider = new NHTransactionProvider(
                new NHSessionProvider(_sessionFactory));

            using (txnProvider) {
                using (var transaction = txnProvider.BeginTransaction()) {
                    ManagedFile managedFile =
                        new ManagedFile {
                        Path        = path,
                        ChunkMap    = ChunkMap.Create(chunkMap),
                        TorrentFile = torrent,
                        Size        = fileSize
                    };
                    var session = txnProvider.SessionProvider.CurrentSession;
                    session.Save(managedFile);
                    transaction.Commit();
                }
            }
            logger.DebugFormat("ChunkMap is added for file {0}", path);
        }
Example #6
0
        /// <summary>
        /// Gets or creates the managed file for the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public ManagedFile CreateManagedFileFromLocalFile(string path)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            byte[] fileHash = FileUtil.GetFileHash(path);
            sw.Stop();
            logger.DebugFormat("Computing file hash took {0} milliseconds.",
                               sw.ElapsedMilliseconds);
            var file = new ManagedFile {
                FileHash = fileHash,
                Size     = new FileInfo(path).Length,
                Path     = path
            };

            try {
                _session.Save(file);
                return(file);
            } catch (HibernateException ex) {
                throw new ChunkDbException(
                          string.Format("File {0} already exists in DB.", path), ex);
            }
        }
Example #7
0
        /// <summary>
        /// Register the chunks in the given file to the DB.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="chunks">The chunks.</param>
        public void AddChunks(string filePath, int[] chunks)
        {
            logger.DebugFormat("Adding chunks {0} for file {1}.",
                               string.Join(",", System.Array.ConvertAll <int, string>(chunks,
                                                                                      x => x.ToString())), filePath);
            var txnProvider = new NHTransactionProvider(
                new NHSessionProvider(_sessionFactory));

            using (txnProvider) {
                using (var transaction = txnProvider.BeginTransaction()) {
                    var helper = new ChunkDbHelper(
                        txnProvider.SessionProvider.CurrentSession);
                    ManagedFile file            = helper.GetManagedFile(filePath);
                    var         chunkMap        = file.ChunkMap;
                    int         numAlreadyExist = 0;
                    foreach (var chunkIndex in chunks)
                    {
                        byte[] hash  = chunkMap.HashAt(chunkIndex);
                        var    entry = new DataChunk {
                            File       = file,
                            ChunkIndex = chunkIndex,
                            Hash       = hash,
                            Count      = 0
                        };
                        bool added = helper.AddChunkIfNotExists(entry);
                        if (!added)
                        {
                            numAlreadyExist++;
                        }
                    }
                    transaction.Commit();
                    logger.DebugFormat(
                        "Chunks added. {0} out of {1} chunks already exist.",
                        numAlreadyExist, chunks.Length);
                }
            }
        }
Example #8
0
        public ChunkMapDto BuildChunkMapDto()
        {
            ManagedFile file             = _chunkDbService.GetManagedFile(DataFilePath);
            int         totalChunkNumber = (int)(file.Size / DataChunk.ChunkSize);

            logger.DebugFormat("Total number of chunk for this file: {0}.",
                               totalChunkNumber);

            AccessProfile[] chunkStatsArr = GetChunksOrderedByEarliestRead();
            logger.DebugFormat("Brought in totally {0} chunks from profile.",
                               chunkStatsArr.Length);

            // The hashes have two parts: in the profile and out of the profile.
            int[]         fileIndices = new int[totalChunkNumber];
            byte[]        hashes      = new byte[totalChunkNumber * DataChunk.HashSize];
            HashSet <int> indicesSet  = new HashSet <int>();

            // Number of chunks read directly from file instead of pulled from DB.
            int numDirectRead  = 0;
            int fileIndicesCur = 0;

            for (; fileIndicesCur < chunkStatsArr.Length; fileIndicesCur++)
            {
                var chunk = chunkStatsArr[fileIndicesCur];
                fileIndices[fileIndicesCur] = chunk.ChunkIndex;
                indicesSet.Add(chunk.ChunkIndex);
                bool   readFile;
                byte[] hash = _fileHelper.GetHashFromChunkDbOrFile(DataFilePath,
                                                                   chunk.ChunkIndex, out readFile);
                if (readFile)
                {
                    numDirectRead++;
                }
                Buffer.BlockCopy(hash, 0, hashes, fileIndicesCur * DataChunk.HashSize, hash.Length);
            }

            int inProfileChunkNum = fileIndicesCur;

            logger.DebugFormat("Finished adding {0} in the profile chunks.", inProfileChunkNum);

            // Begin handling out of the profile chunks.
            for (int i = 0; i < totalChunkNumber; i++)
            {
                if (!indicesSet.Contains(i))
                {
                    // This chunk is not in the profile
                    fileIndices[fileIndicesCur] = i;
                    bool   readFile;
                    byte[] hash = _fileHelper.GetHashFromChunkDbOrFile(DataFilePath,
                                                                       i, out readFile);
                    if (readFile)
                    {
                        numDirectRead++;
                    }
                    Buffer.BlockCopy(hash, 0, hashes, fileIndicesCur * DataChunk.HashSize, hash.Length);
                    fileIndicesCur++;
                }
            }

            logger.DebugFormat("Finished adding {0} out of profile chunks.",
                               fileIndicesCur - inProfileChunkNum);
            logger.DebugFormat(
                "There are {0} chunks in total read directly from file.",
                numDirectRead);

            return(new ChunkMapDto {
                Hashes = hashes,
                FileIndices = fileIndices
            });
        }
Example #9
0
 public bool TryGetManagedFile(string path, out ManagedFile file)
 {
     try {
         file = GetManagedFile(path);
         return true;
     } catch (ChunkDbException ex) {
         file = null;
         return false;
     }
 }
Example #10
0
 /// <summary>
 /// Gets or creates the managed file for the specified path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <returns></returns>
 public ManagedFile CreateManagedFileFromLocalFile(string path)
 {
     Stopwatch sw = new Stopwatch();
     sw.Start();
     byte[] fileHash = FileUtil.GetFileHash(path);
     sw.Stop();
     logger.DebugFormat("Computing file hash took {0} milliseconds.",
         sw.ElapsedMilliseconds);
     var file = new ManagedFile {
         FileHash = fileHash,
         Size = new FileInfo(path).Length,
         Path = path
     };
     try {
         _session.Save(file);
         return file;
     } catch (HibernateException ex) {
         throw new ChunkDbException(
             string.Format("File {0} already exists in DB.", path), ex);
     }
 }