Verified Owner
Top Description Inners Fields Constructors Methods
org.jabref.gui.autosaveandbackup

public Class BackupManager

extends Object
Class Inheritance
Imports
java.io.IOException, java.nio.charset.Charset, .StandardCharsets, java.nio.file.Files, .Path, .StandardCopyOption, java.nio.file.attribute.FileTime, java.util.HashSet, .List, .Optional, .Queue, .Set, java.util.concurrent.LinkedBlockingQueue, .ScheduledThreadPoolExecutor, .TimeUnit, javafx.scene.control.TableColumn, org.jabref.gui.LibraryTab, org.jabref.gui.maintable.BibEntryTableViewModel, org.jabref.gui.maintable.columns.MainTableColumn, org.jabref.logic.exporter.AtomicFileWriter, .BibDatabaseWriter, .BibWriter, .SelfContainedSaveConfiguration, org.jabref.logic.preferences.CliPreferences, org.jabref.logic.util.BackupFileType, .CoarseChangeFilter, org.jabref.logic.util.io.BackupFileUtil, org.jabref.model.database.BibDatabase, .BibDatabaseContext, org.jabref.model.database.event.BibDatabaseContextChangedEvent, org.jabref.model.entry.BibEntry, .BibEntryTypesManager, .BibtexString, org.jabref.model.metadata.SaveOrder, .SelfContainedSaveOrder, com.google.common.eventbus.Subscribe, org.jspecify.annotations.NullMarked, org.slf4j.Logger, .LoggerFactory

Backups the given bib database file from BibDatabaseContext on every BibDatabaseContextChangedEvent. An intelligent java.util.concurrent.ExecutorService with a java.util.concurrent.BlockingQueue prevents a high load while making backups and rejects all redundant backup tasks. This class does not manage org.jabref.logic.util.BackupFileType#SAVE file.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Field Summary

Modifier and TypeField and Description
private final Queue<Path>
private final BibDatabaseContext
private final CoarseChangeFilter
private static final int
private final BibEntryTypesManager
private final ScheduledThreadPoolExecutor
private final LibraryTab
private static final Logger
private static final int
private boolean
private final CliPreferences
private static final Set<BackupManager>

Constructor Summary

AccessConstructor and Description
pack-priv
BackupManager(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, CoarseChangeFilter coarseChangeFilter, BibEntryTypesManager entryTypesManager, CliPreferences preferences)

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if backup file exists AND differs from originalPath. false is the "default" return value in the good case. In case a discarded file exists, false is returned, too. In the case of an exception true is returned to ensure that the user checks the output.
backupFileDiffers
(Path
Path to the file a backup should be checked for. Example: jabref.bib.
originalPath
,
Path backupDir)

Checks whether a backup file exists for the given database file.

pack-priv Optional<Path>
private static Path
determineDiscardedFile(Path file, Path backupDir)

public static void
discardBackup(BibDatabaseContext bibDatabaseContext, Path backupDir)

Marks the backup as discarded at the library which is associated with the given BibDatabaseContext.

public void
discardBackup(Path backupDir)

Marks the backups as discarded.

private void
fillQueue(Path backupDir)

pack-priv static Path
getBackupPathForNewBackup(Path originalPath, Path backupDir)

Determines the most recent backup file name

pack-priv static Optional<Path>
getLatestBackupPath(Path originalPath, Path backupDir)

Determines the most recent existing backup file name

public synchronized void
pack-priv void
performBackup(Path
the full path to the file where the library should be backed up to
backupPath
)

This method is called as soon as the scheduler says: "Do the backup"

SIDE EFFECT: Deletes oldest backup file

public static BackupManager.RestoreResult
restoreBackup(Path
Path to the file which should be equalized to the backup file.
originalPath
,
Path backupDir)

Restores the backup file by copying and overwriting the original one.

public static void
shutdown(BibDatabaseContext bibDatabaseContext, Path
The path to the backup directory
backupDir
,
boolean
True, if a backup should be created
createBackup
)

Shuts down the BackupManager which is associated with the given BibDatabaseContext.

private void
shutdown(Path
The backup directory
backupDir
,
boolean
If the backup manager should still perform a backup
createBackup
)

Unregisters the BackupManager from the eventBus of BibDatabaseContext.

public static BackupManager
start(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, CoarseChangeFilter coarseChangeFilter, BibEntryTypesManager entryTypesManager, CliPreferences preferences)

Starts the BackupManager which is associated with the given BibDatabaseContext.

private void
startBackupTask(Path backupDir)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

backupFilesQueueback to summary
private final Queue<Path> backupFilesQueue
bibDatabaseContextback to summary
private final BibDatabaseContext bibDatabaseContext
coarseChangeFilterback to summary
private final CoarseChangeFilter coarseChangeFilter
DELAY_BETWEEN_BACKUP_ATTEMPTS_IN_SECONDSback to summary
private static final int DELAY_BETWEEN_BACKUP_ATTEMPTS_IN_SECONDS
entryTypesManagerback to summary
private final BibEntryTypesManager entryTypesManager
executorback to summary
private final ScheduledThreadPoolExecutor executor
libraryTabback to summary
private final LibraryTab libraryTab
LOGGERback to summary
private static final Logger LOGGER
MAXIMUM_BACKUP_FILE_COUNTback to summary
private static final int MAXIMUM_BACKUP_FILE_COUNT
needsBackupback to summary
private boolean needsBackup
preferencesback to summary
private final CliPreferences preferences
RUNNING_INSTANCESback to summary
private static final Set<BackupManager> RUNNING_INSTANCES

Constructor Detail

BackupManagerback to summary
pack-priv BackupManager(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, CoarseChangeFilter coarseChangeFilter, BibEntryTypesManager entryTypesManager, CliPreferences preferences)

Method Detail

backupFileDiffersback to summary
public static boolean backupFileDiffers(Path originalPath, Path backupDir)

Checks whether a backup file exists for the given database file. If it exists, it is checked whether it is newer and different from the original.

In case a discarded file is present, the method also returns false, See also discardBackup(Path).

Parameters
originalPath:Path

Path to the file a backup should be checked for. Example: jabref.bib.

Returns:boolean

true if backup file exists AND differs from originalPath. false is the "default" return value in the good case. In case a discarded file exists, false is returned, too. In the case of an exception true is returned to ensure that the user checks the output.

determineBackupPathForNewBackupback to summary
pack-priv Optional<Path> determineBackupPathForNewBackup(Path backupDir)
determineDiscardedFileback to summary
private static Path determineDiscardedFile(Path file, Path backupDir)
discardBackupback to summary
public static void discardBackup(BibDatabaseContext bibDatabaseContext, Path backupDir)

Marks the backup as discarded at the library which is associated with the given BibDatabaseContext.

Parameters
bibDatabaseContext:BibDatabaseContext

Associated BibDatabaseContext

discardBackupback to summary
public void discardBackup(Path backupDir)

Marks the backups as discarded.

We do not delete any files, because the user might want to recover old backup files. Therefore, we mark discarded backups by a --discarded file.

fillQueueback to summary
private void fillQueue(Path backupDir)
getBackupPathForNewBackupback to summary
pack-priv static Path getBackupPathForNewBackup(Path originalPath, Path backupDir)

Determines the most recent backup file name

getLatestBackupPathback to summary
pack-priv static Optional<Path> getLatestBackupPath(Path originalPath, Path backupDir)

Determines the most recent existing backup file name

listenback to summary
public synchronized void listen(BibDatabaseContextChangedEvent event)
Annotations
@Subscribe
performBackupback to summary
pack-priv void performBackup(Path backupPath)

This method is called as soon as the scheduler says: "Do the backup"

SIDE EFFECT: Deletes oldest backup file

Parameters
backupPath:Path

the full path to the file where the library should be backed up to

restoreBackupback to summary
public static BackupManager.RestoreResult restoreBackup(Path originalPath, Path backupDir)

Restores the backup file by copying and overwriting the original one. impl->req~jabgui.autosaveandbackup.complete-backup~1

Parameters
originalPath:Path

Path to the file which should be equalized to the backup file.

shutdownback to summary
public static void shutdown(BibDatabaseContext bibDatabaseContext, Path backupDir, boolean createBackup)

Shuts down the BackupManager which is associated with the given BibDatabaseContext.

Parameters
bibDatabaseContext:BibDatabaseContext

Associated BibDatabaseContext

backupDir:Path

The path to the backup directory

createBackup:boolean

True, if a backup should be created

shutdownback to summary
private void shutdown(Path backupDir, boolean createBackup)

Unregisters the BackupManager from the eventBus of BibDatabaseContext. This method should only be used when closing a database/JabRef in a normal way.

Parameters
backupDir:Path

The backup directory

createBackup:boolean

If the backup manager should still perform a backup

startback to summary
public static BackupManager start(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, CoarseChangeFilter coarseChangeFilter, BibEntryTypesManager entryTypesManager, CliPreferences preferences)

Starts the BackupManager which is associated with the given BibDatabaseContext. As long as no database file is present in BibDatabaseContext, the BackupManager will do nothing.

This method is not thread-safe. The caller has to ensure that this method is not called in parallel.

Parameters
bibDatabaseContext:BibDatabaseContext

Associated BibDatabaseContext

startBackupTaskback to summary
private void startBackupTask(Path backupDir)
org.jabref.gui.autosaveandbackup back to summary

public sealed Interface BackupManager.RestoreResult

permits Restored, Empty, Failed, NotFound
Known Direct Implementers
org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult.Restored, org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult.Empty, org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult.Failed, org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult.NotFound
Annotations
@NullMarked

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static record
public static record
public static record
public static record
org.jabref.gui.autosaveandbackup back to summary

public final Record BackupManager.RestoreResult.Empty

extends Record
implements RestoreResult
Class Inheritance
All Implemented Interfaces
org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult
Record Components
backupPath:Path 

Field Summary

Modifier and TypeField and Description
private final Path
backupPath

Record Component accessed by backupPath().

Constructor Summary

AccessConstructor and Description
public
Empty(Path backupPath)

Method Summary

Modifier and TypeMethod and Description
public Path
backupPath()

Record Component getter of backupPath.

public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

backupPathback to summary
private final Path backupPath

Record Component accessed by backupPath().

Constructor Detail

Emptyback to summary
public Empty(Path backupPath)

Method Detail

backupPathback to summary
public Path backupPath()

Record Component getter of backupPath.

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

org.jabref.gui.autosaveandbackup back to summary

public final Record BackupManager.RestoreResult.Failed

extends Record
implements RestoreResult
Class Inheritance
All Implemented Interfaces
org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult
Record Components
backupPath:Path 
exception:IOException 

Field Summary

Modifier and TypeField and Description
private final Path
backupPath

Record Component accessed by backupPath().

private final IOException
exception

Record Component accessed by exception().

Constructor Summary

AccessConstructor and Description
public
Failed(Path backupPath, IOException exception)

Method Summary

Modifier and TypeMethod and Description
public Path
backupPath()

Record Component getter of backupPath.

public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public IOException
exception()

Record Component getter of exception.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

backupPathback to summary
private final Path backupPath

Record Component accessed by backupPath().

exceptionback to summary
private final IOException exception

Record Component accessed by exception().

Constructor Detail

Failedback to summary
public Failed(Path backupPath, IOException exception)

Method Detail

backupPathback to summary
public Path backupPath()

Record Component getter of backupPath.

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
exceptionback to summary
public IOException exception()

Record Component getter of exception.

hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

org.jabref.gui.autosaveandbackup back to summary

public final Record BackupManager.RestoreResult.NotFound

extends Record
implements RestoreResult
Class Inheritance
All Implemented Interfaces
org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult
Record Components
originalPath:Path 

Field Summary

Modifier and TypeField and Description
private final Path
originalPath

Record Component accessed by originalPath().

Constructor Summary

AccessConstructor and Description
public
NotFound(Path originalPath)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public Path
originalPath()

Record Component getter of originalPath.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

originalPathback to summary
private final Path originalPath

Record Component accessed by originalPath().

Constructor Detail

NotFoundback to summary
public NotFound(Path originalPath)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

originalPathback to summary
public Path originalPath()

Record Component getter of originalPath.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

org.jabref.gui.autosaveandbackup back to summary

public final Record BackupManager.RestoreResult.Restored

extends Record
implements RestoreResult
Class Inheritance
All Implemented Interfaces
org.jabref.gui.autosaveandbackup.BackupManager.RestoreResult
Record Components
no record components

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Constructor Detail

Restoredback to summary
public Restored()

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.