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 boolean changed;
private boolean batchingChanges;
private Grouping grouping = Grouping.APPLY;
void setChanged() {
synchronized (changeExecutor) {
if (changed)
return;
changed = true;
if (batchingChanges)
if (grouping != Grouping.IGNORE)
changed = true;
if (grouping != Grouping.APPLY)
return;
changeExecutor.submit( () -> {
synchronized (changeExecutor) {
if (batchingChanges)
if (grouping != Grouping.APPLY)
return;
changed = false;
}
@ -40,13 +41,19 @@ public class Changeable {
public void beginChanges() {
synchronized (changeExecutor) {
batchingChanges = true;
grouping = Grouping.BATCH;
}
}
public void ignoreChanges() {
synchronized (changeExecutor) {
grouping = Grouping.IGNORE;
}
}
public boolean endChanges() {
synchronized (changeExecutor) {
batchingChanges = false;
grouping = Grouping.APPLY;
if (changed) {
this.changed = false;
@ -56,4 +63,8 @@ public class Changeable {
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) {
this.json = json;
setChanged();
}
@Nonnull

View File

@ -74,11 +74,13 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
if (!headerStarted)
// Starts the header.
headerStarted = true;
else
else {
// Ends the header.
user = new MPFileUser( fullName, keyID, MPAlgorithm.Version.fromInt( mpVersion ).getAlgorithm(),
avatar, defaultType, new Instant( 0 ), MPMarshalFormat.Flat,
clearContent? MPMarshaller.ContentMode.VISIBLE: MPMarshaller.ContentMode.PROTECTED );
user.ignoreChanges();
}
// Comment.
else if (line.startsWith( "#" )) {
@ -154,6 +156,7 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
if (user == null)
throw new MPMarshalException( "No full header found in import file." );
user.endChanges();
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.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( user.last_used ): new Instant(),
MPMarshalFormat.JSON, export.redacted? MPMarshaller.ContentMode.PROTECTED: MPMarshaller.ContentMode.VISIBLE );
model.beginChanges();
model.ignoreChanges();
model.setJSON( this );
if (masterPassword != null)
model.authenticate( masterPassword );