Skip to content
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

Less backups #2995

Merged
merged 6 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 final static int MINOR_CHANGES_LIMIT = 5;

private int minorChangesCount = 0;

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

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;
}

}