-
-
Notifications
You must be signed in to change notification settings - Fork 40
Make MetadataWriter more robust #782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jeremypw
wants to merge
13
commits into
master
Choose a base branch
from
jeremypw/metadatawriter-replace_assert
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7771bd2
Make blacklisting null safe
c7ebab7
Add PhotoMetadataError domain
dc28c32
Simplify MetadataWriter commit_editable - delegate checks to Photo
b14aab5
Simplify MetadataWriter commit master
a7c0120
Fix commit master
7a0a600
Reduce terminal noise from hidden files
ab937c3
Recognise possible null state
e2c9bc9
Init file_title to empty string
b723ee9
Replace asserts when setting dirty
a5661b0
Replace remaining asserts in MetadataWriter
6de6e64
Merge branch 'master' into jeremypw/metadatawriter-replace_assert
jeremypw d9504f3
Revert "Make blacklisting null safe"
4b9dd1d
Revert unnecessary explicit null check
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,8 +33,8 @@ public class MetadataWriter : Object { | |
| private class CommitJob : BackgroundJob { | ||
| public LibraryPhoto photo; | ||
| public Gee.Set<string>? current_keywords; | ||
| public Photo.ReimportMasterState reimport_master_state = null; | ||
| public Photo.ReimportEditableState reimport_editable_state = null; | ||
| public Photo.ReimportMasterState? reimport_master_state = null; | ||
| public Photo.ReimportEditableState? reimport_editable_state = null; | ||
| public Error? err = null; | ||
|
|
||
| public CommitJob (MetadataWriter owner, LibraryPhoto photo, Gee.Set<string>? keywords) { | ||
|
|
@@ -58,38 +58,22 @@ public class MetadataWriter : Object { | |
| // otherwise, we'll end up ruining the original, and as such, breaking the | ||
| // ability to revert to it. | ||
| bool skip_orientation = photo.has_editable (); | ||
|
|
||
| if (!photo.get_master_file_format ().can_write_metadata ()) | ||
| return; | ||
|
|
||
| PhotoMetadata metadata = photo.get_master_metadata (); | ||
| if (update_metadata (metadata, skip_orientation)) { | ||
| LibraryMonitor.blacklist_file (photo.get_master_file (), "MetadataWriter.commit_master"); | ||
| try { | ||
| photo.persist_master_metadata (metadata, out reimport_master_state); | ||
| } finally { | ||
| LibraryMonitor.unblacklist_file (photo.get_master_file ()); | ||
| } | ||
| // Photo does necessary checks and may throw error | ||
| photo.persist_master_metadata (metadata, out reimport_master_state); | ||
| } | ||
| } | ||
|
|
||
| private void commit_editable () throws Error { | ||
| if (!photo.has_editable () || !photo.get_editable_file_format ().can_write_metadata ()) | ||
| return; | ||
|
|
||
| PhotoMetadata? metadata = photo.get_editable_metadata (); | ||
| assert (metadata != null); | ||
|
|
||
| if (update_metadata (metadata)) { | ||
| LibraryMonitor.blacklist_file (photo.get_editable_file (), "MetadataWriter.commit_editable"); | ||
| try { | ||
| photo.persist_editable_metadata (metadata, out reimport_editable_state); | ||
| } finally { | ||
| LibraryMonitor.unblacklist_file (photo.get_editable_file ()); | ||
| } | ||
| // Photo does necessary checks and may throw error | ||
| photo.persist_editable_metadata (metadata, out reimport_editable_state); | ||
| } | ||
| } | ||
|
|
||
| // Vala compiler returns false if metadata null | ||
| private bool update_metadata (PhotoMetadata metadata, bool skip_orientation = false) { | ||
| bool changed = false; | ||
|
|
||
|
|
@@ -534,19 +518,21 @@ public class MetadataWriter : Object { | |
|
|
||
| // mark all the photos as dirty | ||
| if (!already_marked) { | ||
| try { | ||
| LibraryPhoto.global.transaction_controller.begin (); | ||
|
|
||
| foreach (LibraryPhoto photo in photos) | ||
| // Ensure transaction committed by moving outside try/catch | ||
| LibraryPhoto.global.transaction_controller.begin (); | ||
| foreach (LibraryPhoto photo in photos) { | ||
| try { | ||
| photo.set_master_metadata_dirty (true); | ||
|
|
||
| LibraryPhoto.global.transaction_controller.commit (); | ||
| } catch (Error err) { | ||
| if (err is DatabaseError) | ||
| AppWindow.database_error ((DatabaseError) err); | ||
| else | ||
| error ("Unable to mark metadata as dirty: %s", err.message); | ||
| } catch (Error err) { | ||
| if (err is DatabaseError) { | ||
| AppWindow.database_error ((DatabaseError) err); // Causes panic | ||
| } else { | ||
| critical ("Unable to mark metadata as dirty: %s", err.message); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| LibraryPhoto.global.transaction_controller.commit (); | ||
| } | ||
|
|
||
| // ok to drop this on the floor, now that they're marked dirty (will attempt to write them | ||
|
|
@@ -558,12 +544,19 @@ public class MetadataWriter : Object { | |
| debug ("[%s] adding %d photos to dirty list", reason, photos.size); | ||
| #endif | ||
|
|
||
| int queued = 0; | ||
| foreach (LibraryPhoto photo in photos) { | ||
| bool enqueued = dirty.enqueue (photo); | ||
| assert (enqueued); | ||
| if (photo.is_master_metadata_dirty ()) { | ||
| bool enqueued = dirty.enqueue (photo); | ||
| if (!enqueued) { | ||
| critical ("Failed to enqueue photo %s", photo.get_basename ()); | ||
| } else { | ||
| queued++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| count_enqueued_work (photos.size, true); | ||
| count_enqueued_work (queued, true); | ||
| } | ||
|
|
||
| private void cancel_all (bool wait) { | ||
|
|
@@ -588,9 +581,10 @@ public class MetadataWriter : Object { | |
|
|
||
| if (dirty.contains (photo)) { | ||
| bool removed = dirty.remove_first (photo); | ||
| assert (removed); | ||
|
|
||
| assert (!dirty.contains (photo)); | ||
| if (!removed || dirty.contains (photo)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking dirty seems redundant here since it is within an if-statement of the same check |
||
| // Either queue did not contain photo or there was more than one entry. Doesn't seem fatal? | ||
| critical ("Cancel job: failed to remove photo from dirty HashTimedQueue"); | ||
| } | ||
|
|
||
| count_cancelled_work (1, false); | ||
| cancelled = true; | ||
|
|
@@ -626,14 +620,21 @@ public class MetadataWriter : Object { | |
|
|
||
| private void on_update_completed (BackgroundJob j) { | ||
| CommitJob job = (CommitJob) j; | ||
| if (ignore_photo_alteration == job.photo) { // Guard against re-entering | ||
| return; | ||
| } | ||
|
|
||
| if (job.err != null) | ||
| warning ("Unable to update metadata for %s: %s", job.photo.to_string (), job.err.message); | ||
| else | ||
| message ("Completed writing metadata for %s", job.photo.to_string ()); | ||
|
|
||
| bool removed = pending.unset (job.photo); | ||
| assert (removed); | ||
| if (!removed) { | ||
| critical ("Photo was not in pending - ignore update completed"); | ||
| // Do we need to return or continue or terminate? Presumably the photo was not in the queue? | ||
| // Other errors continued so do same for now; | ||
| } | ||
|
|
||
| // since there's potentially multiple state-change operations here, use the transaction | ||
| // controller | ||
|
|
@@ -642,7 +643,12 @@ public class MetadataWriter : Object { | |
| if (job.reimport_master_state != null || job.reimport_editable_state != null) { | ||
| // finish_update_*_metadata are going to issue an "altered" signal, and we want to | ||
| // ignore it | ||
| assert (ignore_photo_alteration == null); | ||
| if (ignore_photo_alteration != null) { | ||
| critical ("ignore_photo_alteration is not null "); | ||
| ///TODO If this happens in practice then we need a way of pausing the job and calling this function later | ||
| // when ignore_photo_alteration becomes null | ||
| } | ||
|
|
||
| ignore_photo_alteration = job.photo; | ||
| try { | ||
| if (job.reimport_master_state != null) | ||
|
|
@@ -651,10 +657,8 @@ public class MetadataWriter : Object { | |
| if (job.reimport_editable_state != null) | ||
| job.photo.finish_update_editable_metadata (job.reimport_editable_state); | ||
| } catch (DatabaseError err) { | ||
| AppWindow.database_error (err); | ||
| AppWindow.database_error (err); // Causes panic | ||
| } finally { | ||
| // this assertion guards against reentrancy | ||
| assert (ignore_photo_alteration == job.photo); | ||
| ignore_photo_alteration = null; | ||
| } | ||
| } else { | ||
|
|
@@ -677,7 +681,9 @@ public class MetadataWriter : Object { | |
|
|
||
| private void on_update_cancelled (BackgroundJob j) { | ||
| bool removed = pending.unset (((CommitJob) j).photo); | ||
| assert (removed); | ||
| if (!removed) { | ||
| critical ("Failed to remove photo from pending on update cancelled"); | ||
| } | ||
|
|
||
| count_cancelled_work (1, true); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does Vala do this check because the argument isn't ending with
?or something else?