Skip to content

Commit

Permalink
Less backups (#2995)
Browse files Browse the repository at this point in the history
* Add delta to FieldChangedEvents

* Do batched backups

* Turn the delta final

* Revert "Turn the delta final"

This reverts commit 0ca07d2.

* Fix checkstyle warnings

* One more try for fixing checkstyle
  • Loading branch information
lenhard authored and Siedlerchr committed Jul 11, 2017
1 parent b9bd27c commit e35b6ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;
Expand All @@ -40,14 +41,17 @@ public class BackupManager {

private static final String BACKUP_EXTENSION = ".sav";

private static final int MINOR_CHANGES_LIMIT = 5;

private static Set<BackupManager> runningInstances = new HashSet<>();

private int minorChangesCount = 0;

private final BibDatabaseContext bibDatabaseContext;
private final JabRefPreferences preferences;
private final ExecutorService executor;
private final Runnable backupTask = () -> determineBackupPath().ifPresent(this::performBackup);


private BackupManager(BibDatabaseContext bibDatabaseContext) {
this.bibDatabaseContext = bibDatabaseContext;
this.preferences = JabRefPreferences.getInstance();
Expand Down Expand Up @@ -129,7 +133,18 @@ private void performBackup(Path backupPath) {

@Subscribe
public synchronized void listen(@SuppressWarnings("unused") BibDatabaseContextChangedEvent event) {
startBackupTask();
if (!(event instanceof FieldChangedEvent)) {
startBackupTask();
} else {
// only do a backup if the field changes are more than one character or if there are enough of them
FieldChangedEvent fieldChange = (FieldChangedEvent) event;
if (fieldChange.getDelta() > 1 || minorChangesCount >= MINOR_CHANGES_LIMIT) {
startBackupTask();
minorChangesCount = 0;
} else {
minorChangesCount++;
}
}
}

private void startBackupTask() {
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/org/jabref/model/entry/event/FieldChangedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,68 @@ public class FieldChangedEvent extends EntryChangedEvent {
private final String fieldName;
private final String newValue;
private final String oldValue;
private int delta = 0;


/**
* @param bibEntry Affected BibEntry object
* @param bibEntry Affected BibEntry object
* @param fieldName Name of field which has been changed
* @param newValue new field value
* @param newValue old field value
* @param location location Location affected by this event
* @param newValue new field value
* @param newValue old field value
* @param location location Location affected by this event
*/
public FieldChangedEvent(BibEntry bibEntry, String fieldName, String newValue, String oldValue,
EntryEventSource location) {
EntryEventSource location) {
super(bibEntry, location);
this.fieldName = fieldName;
this.newValue = newValue;
this.oldValue = oldValue;
delta = computeDelta(oldValue, newValue);
}

/**
* @param bibEntry Affected BibEntry object
* @param bibEntry Affected BibEntry object
* @param fieldName Name of field which has been changed
* @param newValue new field value
* @param newValue new field value
*/
public FieldChangedEvent(BibEntry bibEntry, String fieldName, String newValue, String oldValue) {
super(bibEntry);
this.fieldName = fieldName;
this.newValue = newValue;
this.oldValue = oldValue;
delta = computeDelta(oldValue, newValue);
}

/**
* @param bibEntry Affected BibEntry object
* @param bibEntry Affected BibEntry object
* @param fieldName Name of field which has been changed
* @param newValue new field value
* @param location location Location affected by this event
* @param newValue new field value
* @param location location Location affected by this event
*/
public FieldChangedEvent(FieldChange fieldChange, EntryEventSource location) {
super(fieldChange.getEntry(), location);
this.fieldName = fieldChange.getField();
this.newValue = fieldChange.getNewValue();
this.oldValue = fieldChange.getOldValue();
delta = computeDelta(oldValue, newValue);
}

public FieldChangedEvent(FieldChange fieldChange) {
this(fieldChange, EntryEventSource.LOCAL);
}

private int computeDelta(String oldValue, String newValue) {
if (oldValue == newValue) {
return 0;
} else if (oldValue == null && newValue != null) {
return newValue.length();
} else if (newValue == null && oldValue != null) {
return oldValue.length();
} else {
return Math.abs(newValue.length() - oldValue.length());
}
}

public String getFieldName() {
return fieldName;
}
Expand All @@ -69,4 +85,8 @@ public String getOldValue() {
return oldValue;
}

public int getDelta() {
return delta;
}

}

0 comments on commit e35b6ac

Please sign in to comment.