Exemple #1
0
 /// <summary>
 /// Called when the do not load hashes option is in effect. Generate block hashes
 /// but put nothing in the DB. Use a special phoneId of Int32.MaxValue-1.
 /// </summary>
 public void PseudoLoad()
 {
     this.PhoneId = -1;
     long nextStreamPosition = 0;
     List<string> hashes = ComputeHashes(files[0].Path, DefaultBlockSize, DefaultSlideAmount, ref nextStreamPosition);
     if (hashes.Count == 0) return;
     HashSet<string> loaded = new HashSet<string>();
     List<BlockHashFilter.UnfilterdBlockResult> stored = new List<BlockHashFilter.UnfilterdBlockResult>();
     int blockIndex = 0;
     foreach (string blkhash in hashes) {
         if (!loaded.Contains(blkhash)) {
                 BlockHashFilter.UnfilterdBlockResult ubr = new BlockHashFilter.UnfilterdBlockResult
                                                                 {
                                                                     blockIndexFirst = blockIndex,
                                                                     hash = blkhash
                                                                 };
                 stored.Add(ubr);
                 loaded.Add(blkhash);
         }
         blockIndex++;
     }
     loaded.Clear();
     this.PhoneId = Int32.MaxValue-1;
     this.storedBlocks = stored;
 }
 /// <summary>
 /// Inserts in the database, into the table: tbl_Hash, newly found block hashes of the phone.
 /// </summary>
 /// <param name="hashes">List of block hashes of the phone.</param>
 /// <param name="hashRunId">The ID of the row to which the block hashes of the phone are to be inserted.</param>
 /// <returns>The number of block hashes or -1 in case of an exception.</returns>
 public static int InsertHashes(List<string> hashes, int hashRunId, List<BlockHashFilter.UnfilterdBlockResult> stored=null)
 {
     // TODO: This is slow, but we are trying to maintain all-or-none integrity.
     // TODO: Can we do away with this? The concern is more user cancels than crashes.
     // TODO: We don't want a partial phone in the DB because it will either be detected as already there,
     // or hash block fliter itself out!
     SQLiteConnection sql = null;
     int blockIndex = 0;
     try {
         HashSet<string> loaded = new HashSet<string>();
         sql = OpenSql();
         SQLiteTransaction trans = sql.BeginTransaction();
         const string stmt = "INSERT INTO tbl_Hash(hashRunId, blockIndex, hash) " +
                             "VALUES(@RUNID, @BLKX, @HASH)";
         foreach (string blkhash in hashes) {
             // Only add if it's a unique hash.
             if (!loaded.Contains(blkhash)) {
                 SQLiteCommand cmd = new SQLiteCommand(stmt, sql);
                 cmd.Parameters.AddWithValue("@RUNID", hashRunId);
                 cmd.Parameters.AddWithValue("@BLKX", blockIndex);
                 cmd.Parameters.AddWithValue("@HASH", blkhash);
                 cmd.ExecuteNonQuery();
                 if (stored != null) {
                     BlockHashFilter.UnfilterdBlockResult ubr = new BlockHashFilter.UnfilterdBlockResult
                                                                    {
                                                                        blockIndexFirst = blockIndex,
                                                                        hash = blkhash
                                                                    };
                     stored.Add(ubr);
                 }
                 loaded.Add(blkhash);
             }
             blockIndex++;
         }
         loaded.Clear();
         trans.Commit();
         return blockIndex;
     } catch (ThreadAbortException e) {
         throw e;
     } catch (Exception) {
         return -1;
     } finally {
         if (sql != null) {
             try {
                 sql.Close();
                 sql.Dispose();
             } catch {
             }
         }
     }
 }