Esempio n. 1
0
        /// <summary>
        /// Calls ComputeHahses() method of BlockHashFilter class to calculate phone's block hashes, calls GetHashRunId() to insert phone's
        /// record to the database, calls InsertHahses() of DatabaseAccess to insert the block hashes into the database.
        /// </summary>
        /// <param name="file">The BinaryFile reference corresponding to the phone's memory file.</param>
        /// <param name="blockSize">The size of a memory block on phone.</param>
        /// <param name="slideAmount">The distance between the starting offsets of two consecutive blocks in memory.</param>
        /// <returns>The PhoneId of the phone, i.e. the row ID of the phone's record in the database.</returns>
        public int ProcessFile(BinaryFile file, int blockSize, int slideAmount)
        {
            int phoneId = GetPhoneId(file);

            DateTime      start  = DateTime.Now;
            List <string> hashes = BlockHashFilter.ComputeHashes(file.Path, blockSize, slideAmount);
            DateTime      end    = DateTime.Now;

            TimeSpan duration          = end - start;
            long     numBlocks         = hashes.Count;
            long     timeToHashSeconds = Convert.ToInt32(Math.Round(duration.TotalSeconds));
            string   hashType          = "sha1";

            int hashRunId = GetHashRunId(blockSize, phoneId, file.MemoryId, slideAmount, numBlocks, timeToHashSeconds,
                                         hashType, "");

#if _USESQLSERVER_
            List <HashInfo> hashInfos = new List <HashInfo>();

            for (int k = 0; k < hashes.Count; k++)
            {
                hashInfos.Add(new HashInfo {
                    BlockIndex = k, Hash = hashes[k], HashRunId = hashRunId
                });
            }

            BulkInsertBase bulky = BulkInsertBase.Load(hashInfos);

            bulky.Flush();
#else
            DatabaseAccess.InsertHashes(hashes, hashRunId);
#endif

            return(phoneId);
        }
Esempio n. 2
0
        /// <summary>
        /// Calls ComputeHahses() method of BlockHashFilter class to calculate phone's block hashes, calls GetHashRunId() to insert phone's
        /// record to the database, calls InsertHahses() of DatabaseAccess to insert the block hashes into the database and HashRunUpdate() of
        /// DatabaseAccess to update the number of blocks and time to hash fields in the database.
        /// </summary>
        /// <param name="file">The BinaryFile reference corresponding to the phone's memory file.</param>
        /// <param name="blockSize">The size of a memory block on phone.</param>
        /// <param name="slideAmount">The distance between the starting offsets of two consecutive blocks in memory.</param>
        /// <param name="hashRunId">The ID of the row to which the block hashes of the phone are to be inserted.</param>
        /// <param name="pid">Phone id to be used.</param>
        /// <param name="memId">Memory id for the phone.</param>
        public void ProcessFile(BinaryFile file, int blockSize, int slideAmount, int hashRunId, int pid, string memId)
        {
            //The starting point in the phone file stream. Needed to maintain position state across multiple calls of the method.
            long nextStreamPosition = 0;
            long lastStreamPositon  = -1;
            int  blockIndex         = 0;

            DateTime start = DateTime.Now;

            while (lastStreamPositon < nextStreamPosition)
            {
                lastStreamPositon = nextStreamPosition;

                List <string> hashes = ComputeHashes(file.Path, blockSize, slideAmount, ref nextStreamPosition);

                if (hashes.Count == 0)
                {
                    continue;
                }
#if _USESQLSERVER_
                long numBlocks = hashes.Count;

                List <HashInfo> hashInfos = new List <HashInfo>();

                for (int k = 0; k < hashes.Count; k++)
                {
                    hashInfos.Add(new HashInfo {
                        BlockIndex = blockIndex, Hash = hashes[k], HashRunId = hashRunId
                    });

                    blockIndex++;
                }

                BulkInsertBase bulky = BulkInsertBase.Load(hashInfos);

                bulky.Flush();
#else
                List <BlockHashFilter.UnfilterdBlockResult> stored = new List <BlockHashFilter.UnfilterdBlockResult>();
                try {
                    blockIndex = DatabaseAccess.InsertHashes(hashes, hashRunId, stored);
                } catch (ThreadAbortException e) {
                    DatabaseAccess.ForgetPhone(memId, pid, hashRunId);
                    throw e;
                }
                if (blockIndex < 0)
                {
                    DatabaseAccess.ForgetPhone(memId, pid, hashRunId);
                    return;
                }
                storedBlocks = stored;
#endif
            }

            DateTime end               = DateTime.Now;
            TimeSpan duration          = end - start;
            long     timeToHashSeconds = Convert.ToInt32(Math.Round(duration.TotalSeconds));

#if _USESQLSERVER_
            using (PhoneDbDataContext dataContext = Dalbase.GetDataContext()) {
                dataContext.usp_HashRun_Update(hashRunId, blockIndex, timeToHashSeconds);
            }
#else
            DatabaseAccess.HashRunUpdate(hashRunId, blockIndex, (int)timeToHashSeconds);
#endif
        }