[f-spot/fullfile-dupedetect: 3/6] Make dupe detection even faster by only hashing once.



commit 6f7e579c32cae5a57005dec22e432e0075f94b2e
Author: Ruben Vermeersch <ruben savanne be>
Date:   Thu Jun 10 09:42:20 2010 +0200

    Make dupe detection even faster by only hashing once.

 extensions/Tools/MergeDb/MergeDb.cs |    6 ++++--
 src/Import/ImportController.cs      |    6 ++++--
 src/PhotoStore.cs                   |   13 ++++++-------
 src/PhotoVersionCommands.cs         |    2 +-
 4 files changed, 15 insertions(+), 12 deletions(-)
---
diff --git a/extensions/Tools/MergeDb/MergeDb.cs b/extensions/Tools/MergeDb/MergeDb.cs
index e76beb7..85effb6 100644
--- a/extensions/Tools/MergeDb/MergeDb.cs
+++ b/extensions/Tools/MergeDb/MergeDb.cs
@@ -236,13 +236,15 @@ namespace MergeDbExtension
 			else
 				destination = photo_path;
 
+            var hash = Photo.GenerateMD5 (new SafeUri (photo_path));
+
 			// Don't copy if we are already home
 			if (photo_path == destination)
-				newp = to_store.Create (new SafeUri (destination), roll_map [photo.RollId]);
+				newp = to_store.Create (new SafeUri (destination), roll_map [photo.RollId], hash);
 			else {
 				System.IO.File.Copy (photo_path, destination);
 
-				newp = to_store.Create (new SafeUri (destination), new SafeUri (photo_path), roll_map [photo.RollId]);
+				newp = to_store.Create (new SafeUri (destination), new SafeUri (photo_path), roll_map [photo.RollId], hash);
 				try {
 					File.SetAttributes (destination, File.GetAttributes (destination) & ~FileAttributes.ReadOnly);
 					DateTime create = File.GetCreationTime (photo_path);
diff --git a/src/Import/ImportController.cs b/src/Import/ImportController.cs
index cd99970..a9df31e 100644
--- a/src/Import/ImportController.cs
+++ b/src/Import/ImportController.cs
@@ -292,9 +292,10 @@ namespace FSpot.Import
         void ImportPhoto (IBrowsableItem item, Roll roll)
         {
             var destination = FindImportDestination (item.DefaultVersion.Uri);
+            string hash = String.Empty;
 
             // Do duplicate detection
-            if (DuplicateDetect && store.HasDuplicate (item.DefaultVersion.Uri, destination)) {
+            if (DuplicateDetect && store.HasDuplicate (item.DefaultVersion.Uri, destination, out hash)) {
                 return;
             }
 
@@ -309,7 +310,8 @@ namespace FSpot.Import
             // Import photo
             var photo = store.Create (destination,
                                       item.DefaultVersion.Uri,
-                                      roll.Id);
+                                      roll.Id,
+                                      hash);
 
             bool needs_commit = false;
 
diff --git a/src/PhotoStore.cs b/src/PhotoStore.cs
index 8515122..4ab27ff 100644
--- a/src/PhotoStore.cs
+++ b/src/PhotoStore.cs
@@ -99,8 +99,8 @@ public class PhotoStore : DbStore<Photo> {
 		Database.ExecuteNonQuery ("CREATE INDEX idx_photos_roll_id ON photos(roll_id)");
 	}
 
-	public bool HasDuplicate (SafeUri uri, SafeUri destination) {
-		var hash = Photo.GenerateMD5 (uri);
+	public bool HasDuplicate (SafeUri uri, SafeUri destination, out string hash) {
+		hash = Photo.GenerateMD5 (uri);
 		var condition = new ConditionWrapper (String.Format ("import_md5 = \"{0}\"", hash));
 		var dupes_by_hash = Count ("photo_versions", condition);
 		if (dupes_by_hash > 0)
@@ -110,12 +110,12 @@ public class PhotoStore : DbStore<Photo> {
 		return false;
 	}
 
-	public Photo Create (SafeUri uri, uint roll_id)
+	public Photo Create (SafeUri uri, uint roll_id, string import_md5)
 	{
-		return Create (uri, uri, roll_id);
+		return Create (uri, uri, roll_id, import_md5);
 	}
 
-	public Photo Create (SafeUri new_uri, SafeUri orig_uri, uint roll_id)
+	public Photo Create (SafeUri new_uri, SafeUri orig_uri, uint roll_id, string import_md5)
 	{
 		Photo photo;
 
@@ -125,7 +125,6 @@ public class PhotoStore : DbStore<Photo> {
 		using (FSpot.ImageFile img = FSpot.ImageFile.Create (orig_uri)) {
 			long unix_time = DbUtils.UnixTimeFromDateTime (img.Date);
 			string description = img.Description != null  ? img.Description.Split ('\0') [0] : String.Empty;
-			string import_md5 = Photo.GenerateMD5 (new_uri);
 
 	 		uint id = (uint) Database.Execute (
 				new DbCommand (
@@ -494,7 +493,7 @@ public class PhotoStore : DbStore<Photo> {
 	public event EventHandler<DbItemEventArgs<Photo>> ItemsRemovedOverDBus;
 
 	public Photo CreateOverDBus (string new_path, string orig_path, uint roll_id)  {
-		Photo photo = Create (new SafeUri (new_path), new SafeUri (orig_path), roll_id);
+		Photo photo = Create (new SafeUri (new_path), new SafeUri (orig_path), roll_id, Photo.GenerateMD5 (new SafeUri (new_path)));
 		EmitAddedOverDBus (photo);
 
 		return photo;
diff --git a/src/PhotoVersionCommands.cs b/src/PhotoVersionCommands.cs
index f753849..bae1fae 100644
--- a/src/PhotoVersionCommands.cs
+++ b/src/PhotoVersionCommands.cs
@@ -173,7 +173,7 @@ public class PhotoVersionCommands
 			try {
 				if (ResponseType.Ok == HigMessageDialog.RunHigConfirmation(parent_window, DialogFlags.DestroyWithParent, 
 									   MessageType.Warning, msg, desc, ok_caption)) {
-					Photo new_photo = store.Create (photo.DefaultVersion.Uri, photo.RollId);
+					Photo new_photo = store.Create (photo.DefaultVersion.Uri, photo.RollId, photo.DefaultVersion.ImportMD5);
 					new_photo.CopyAttributesFrom (photo);
 					photo.DeleteVersion (photo.DefaultVersionId, false, true);
 					store.Commit (new Photo[] {new_photo, photo});



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]