2
0

Don't save changes made to model while it's being read from file.

This commit is contained in:
Maarten Billemont 2018-07-10 00:54:11 -04:00
parent 462dd4e89b
commit a16bc9a318
4 changed files with 25 additions and 9 deletions

View File

@ -12,20 +12,21 @@ public class Changeable {
private static final ExecutorService changeExecutor = Executors.newSingleThreadExecutor(); private static final ExecutorService changeExecutor = Executors.newSingleThreadExecutor();
private boolean changed; private boolean changed;
private boolean batchingChanges; private Grouping grouping = Grouping.APPLY;
void setChanged() { void setChanged() {
synchronized (changeExecutor) { synchronized (changeExecutor) {
if (changed) if (changed)
return; return;
changed = true;
if (batchingChanges) if (grouping != Grouping.IGNORE)
changed = true;
if (grouping != Grouping.APPLY)
return; return;
changeExecutor.submit( () -> { changeExecutor.submit( () -> {
synchronized (changeExecutor) { synchronized (changeExecutor) {
if (batchingChanges) if (grouping != Grouping.APPLY)
return; return;
changed = false; changed = false;
} }
@ -40,13 +41,19 @@ public class Changeable {
public void beginChanges() { public void beginChanges() {
synchronized (changeExecutor) { synchronized (changeExecutor) {
batchingChanges = true; grouping = Grouping.BATCH;
}
}
public void ignoreChanges() {
synchronized (changeExecutor) {
grouping = Grouping.IGNORE;
} }
} }
public boolean endChanges() { public boolean endChanges() {
synchronized (changeExecutor) { synchronized (changeExecutor) {
batchingChanges = false; grouping = Grouping.APPLY;
if (changed) { if (changed) {
this.changed = false; this.changed = false;
@ -56,4 +63,8 @@ public class Changeable {
return false; return false;
} }
} }
private enum Grouping {
APPLY, BATCH, IGNORE
}
} }

View File

@ -133,6 +133,8 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
public void setJSON(final MPJSONFile json) { public void setJSON(final MPJSONFile json) {
this.json = json; this.json = json;
setChanged();
} }
@Nonnull @Nonnull

View File

@ -74,11 +74,13 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
if (!headerStarted) if (!headerStarted)
// Starts the header. // Starts the header.
headerStarted = true; headerStarted = true;
else else {
// Ends the header. // Ends the header.
user = new MPFileUser( fullName, keyID, MPAlgorithm.Version.fromInt( mpVersion ).getAlgorithm(), user = new MPFileUser( fullName, keyID, MPAlgorithm.Version.fromInt( mpVersion ).getAlgorithm(),
avatar, defaultType, new Instant( 0 ), MPMarshalFormat.Flat, avatar, defaultType, new Instant( 0 ), MPMarshalFormat.Flat,
clearContent? MPMarshaller.ContentMode.VISIBLE: MPMarshaller.ContentMode.PROTECTED ); clearContent? MPMarshaller.ContentMode.VISIBLE: MPMarshaller.ContentMode.PROTECTED );
user.ignoreChanges();
}
// Comment. // Comment.
else if (line.startsWith( "#" )) { else if (line.startsWith( "#" )) {
@ -154,6 +156,7 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
if (user == null) if (user == null)
throw new MPMarshalException( "No full header found in import file." ); throw new MPMarshalException( "No full header found in import file." );
user.endChanges();
return user; return user;
} }
} }

View File

@ -145,7 +145,7 @@ public class MPJSONFile extends MPJSONAnyObject {
(user.default_type != null)? user.default_type: algorithm.mpw_default_result_type(), (user.default_type != null)? user.default_type: algorithm.mpw_default_result_type(),
(user.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( user.last_used ): new Instant(), (user.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( user.last_used ): new Instant(),
MPMarshalFormat.JSON, export.redacted? MPMarshaller.ContentMode.PROTECTED: MPMarshaller.ContentMode.VISIBLE ); MPMarshalFormat.JSON, export.redacted? MPMarshaller.ContentMode.PROTECTED: MPMarshaller.ContentMode.VISIBLE );
model.beginChanges(); model.ignoreChanges();
model.setJSON( this ); model.setJSON( this );
if (masterPassword != null) if (masterPassword != null)
model.authenticate( masterPassword ); model.authenticate( masterPassword );