[shotwell] metadata: Save one memcpy when generating the md5s



commit 30bc836cdaf9416bde793643d3d5c549aa638a35
Author: Jens Georg <mail jensge org>
Date:   Thu May 18 19:48:20 2017 +0200

    metadata: Save one memcpy when generating the md5s

 src/BatchImport.vala          |    9 +----
 src/camera/ImportPage.vala    |    4 +--
 src/photos/GdkSupport.vala    |    9 +----
 src/photos/PhotoMetadata.vala |   66 ++++++++++++++++++++++++++---------------
 src/photos/RawSupport.vala    |    9 +----
 5 files changed, 49 insertions(+), 48 deletions(-)
---
diff --git a/src/BatchImport.vala b/src/BatchImport.vala
index 27cf4da..f1a7cd8 100644
--- a/src/BatchImport.vala
+++ b/src/BatchImport.vala
@@ -1835,13 +1835,8 @@ private class PrepareFilesJob : BackgroundImportJob {
             }
             
             if (metadata != null) {
-                uint8[]? flattened_sans_thumbnail = metadata.flatten_exif(false);
-                if (flattened_sans_thumbnail != null && flattened_sans_thumbnail.length > 0)
-                    exif_only_md5 = md5_binary(flattened_sans_thumbnail, flattened_sans_thumbnail.length);
-                
-                uint8[]? flattened_thumbnail = metadata.flatten_exif_preview();
-                if (flattened_thumbnail != null && flattened_thumbnail.length > 0)
-                    thumbnail_md5 = md5_binary(flattened_thumbnail, flattened_thumbnail.length);
+                exif_only_md5 = metadata.exif_hash ();
+                thumbnail_md5 = metadata.thumbnail_hash();
             }
         }
 
diff --git a/src/camera/ImportPage.vala b/src/camera/ImportPage.vala
index f5e2b3a..c5d7934 100644
--- a/src/camera/ImportPage.vala
+++ b/src/camera/ImportPage.vala
@@ -1553,9 +1553,7 @@ public class ImportPage : CheckerboardPage {
             // calculate EXIF's fingerprint
             string? exif_only_md5 = null;
             if (metadata != null) {
-                uint8[]? flattened_sans_thumbnail = metadata.flatten_exif(false);
-                if (flattened_sans_thumbnail != null && flattened_sans_thumbnail.length > 0)
-                    exif_only_md5 = md5_binary(flattened_sans_thumbnail, flattened_sans_thumbnail.length);
+                exif_only_md5 = metadata.exif_hash();
             }
             
             // XXX: Cannot use the metadata for the thumbnail preview because libgphoto2
diff --git a/src/photos/GdkSupport.vala b/src/photos/GdkSupport.vala
index 51ae72c..2fd1926 100644
--- a/src/photos/GdkSupport.vala
+++ b/src/photos/GdkSupport.vala
@@ -55,13 +55,8 @@ public abstract class GdkSniffer : PhotoFileSniffer {
         }
         
         if (calc_md5 && detected.metadata != null) {
-            uint8[]? flattened_sans_thumbnail = detected.metadata.flatten_exif(false);
-            if (flattened_sans_thumbnail != null && flattened_sans_thumbnail.length > 0)
-                detected.exif_md5 = md5_binary(flattened_sans_thumbnail, flattened_sans_thumbnail.length);
-            
-            uint8[]? flattened_thumbnail = detected.metadata.flatten_exif_preview();
-            if (flattened_thumbnail != null && flattened_thumbnail.length > 0)
-                detected.thumbnail_md5 = md5_binary(flattened_thumbnail, flattened_thumbnail.length);
+            detected.exif_md5 = detected.metadata.exif_hash();
+            detected.thumbnail_md5 = detected.metadata.thumbnail_hash();
         }
         
         // if no MD5, don't read as much, as the needed info will probably be gleaned
diff --git a/src/photos/PhotoMetadata.vala b/src/photos/PhotoMetadata.vala
index e9a9042..e02eb5c 100644
--- a/src/photos/PhotoMetadata.vala
+++ b/src/photos/PhotoMetadata.vala
@@ -247,6 +247,8 @@ public class PhotoMetadata : MediaMetadata {
     private GExiv2.Metadata exiv2 = new GExiv2.Metadata();
     private Exif.Data? exif = null;
     string source_name = "<uninitialized>";
+    private string? metadata_hash = null;
+    private string? thumbnail_md5 = null;
     
     public PhotoMetadata() {
     }
@@ -691,43 +693,59 @@ public class PhotoMetadata : MediaMetadata {
     }
     
     // Returns raw bytes of EXIF metadata, including signature and optionally the preview (if present).
-    public uint8[]? flatten_exif(bool include_preview) {
+    public string? exif_hash() {
         if (exif == null)
             return null;
-        
-        // save thumbnail to strip if no attachments requested (so it can be added back and
-        // deallocated automatically)
-        uchar *thumbnail = exif.data;
-        uint thumbnail_size = exif.size;
-        if (!include_preview) {
-            exif.data = null;
-            exif.size = 0;
+
+        if (this.metadata_hash != null) {
+            return this.metadata_hash;
         }
-        
-        uint8[]? flattened = null;
-        
-        // save the struct to a buffer and copy into a Vala-friendly one
+
+        string? hash = null;
+
+        var thumb = exif.data;
+        var thumb_size = exif.size;
+
+        // Strip potential thumbnail
+        exif.data = null;
+        exif.size = 0;
+
         uchar *saved_data = null;
         uint saved_size = 0;
+
         exif.save_data(&saved_data, &saved_size);
+
+        exif.data = thumb;
+        exif.size = thumb_size;
+
         if (saved_size > 0 && saved_data != null) {
-            flattened = new uint8[saved_size];
-            Memory.copy(flattened, saved_data, saved_size);
-            
+            var md5 = new Checksum(ChecksumType.MD5);
+            md5.update((uchar []) saved_data, saved_size);
             Exif.Mem.new_default().free(saved_data);
+
+            this.metadata_hash = md5.get_string ();
         }
-        
-        // restore thumbnail (this works in either case)
-        exif.data = thumbnail;
-        exif.size = thumbnail_size;
-        
-        return flattened;
+
+        return hash;
     }
     
     // Returns raw bytes of EXIF preview, if present
-    public uint8[]? flatten_exif_preview() {
+    public string? thumbnail_hash() {
+        if (this.thumbnail_md5 != null) {
+            return this.thumbnail_md5;
+        }
+
         uchar[] buffer;
-        return exiv2.get_exif_thumbnail(out buffer) ? buffer : null;
+        if (exiv2.get_exif_thumbnail(out buffer)) {
+            var md5 = new Checksum(ChecksumType.MD5);
+            md5.update(buffer, buffer.length);
+
+            this.thumbnail_md5 = md5.get_string();
+
+            return this.thumbnail_md5;
+         }
+
+        return null;
     }
     
     public uint get_preview_count() {
diff --git a/src/photos/RawSupport.vala b/src/photos/RawSupport.vala
index 05f652c..2cd86e2 100644
--- a/src/photos/RawSupport.vala
+++ b/src/photos/RawSupport.vala
@@ -196,13 +196,8 @@ public class RawSniffer : PhotoFileSniffer {
         }
         
         if (detected.metadata != null) {
-            uint8[]? flattened_sans_thumbnail = detected.metadata.flatten_exif(false);
-            if (flattened_sans_thumbnail != null && flattened_sans_thumbnail.length > 0)
-                detected.exif_md5 = md5_binary(flattened_sans_thumbnail, flattened_sans_thumbnail.length);
-            
-            uint8[]? flattened_thumbnail = detected.metadata.flatten_exif_preview();
-            if (flattened_thumbnail != null && flattened_thumbnail.length > 0)
-                detected.thumbnail_md5 = md5_binary(flattened_thumbnail, flattened_thumbnail.length);
+            detected.exif_md5 = detected.metadata.exif_hash();
+            detected.thumbnail_md5 = detected.metadata.thumbnail_hash();
         }
         
         if (calc_md5)


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