Verified Owner
Top Description Inners Fields Constructors Methods
org.jabref.logic.shared

public Class DBMSProcessor

extends Object
Class Inheritance
Imports
java.sql.Connection, .DatabaseMetaData, .PreparedStatement, .ResultSet, .SQLException, .Statement, java.util.ArrayList, .HashMap, .HashSet, .List, .Locale, .Map, .Optional, .Set, .StringJoiner, java.util.stream.Collectors, org.jabref.logic.shared.exception.OfflineLockException, .SharedEntryNotPresentException, org.jabref.logic.shared.notifications.NotificationListener, .Notifier, org.jabref.model.entry.BibEntry, org.jabref.model.entry.event.EntriesEventSource, org.jabref.model.entry.field.Field, .FieldFactory, org.jabref.model.entry.types.EntryType, .EntryTypeFactory, org.jabref.model.metadata.MetaData, com.google.common.annotations.VisibleForTesting, com.google.common.collect.Lists, io.github.thibaultmeyer.cuid.CUID, org.jspecify.annotations.NonNull, .Nullable, org.slf4j.Logger, .LoggerFactory

Processes all incoming or outgoing bib data to external SQL Database and manages its structure.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static interface
private static interface

Field Summary

Modifier and TypeField and Description
protected final Connection
protected DatabaseConnectionProperties
private int
private final DatabaseConnection
private static final Set<String>
private @Nullable NotificationListener
protected static final Logger
private static final long
private final String
private int

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the structure matches the requirements, false if not.
checkBaseIntegrity
()

Scans the database for required tables.

protected boolean

Returns:

true if all given tables are present, else false.
checkTableAvailability
(String...
Table names to be checked
tableNames
)

Checks whether all given table names (case insensitive) exist in database.

public boolean

Returns:

true if the structure is old, else false.
databaseIsAtMostJabRef35
()

Determines whether the database is using an pre-3.6 structure.

private Optional<String>
pack-priv int
public DatabaseConnectionProperties
private List<BibEntry>
getNotYetExistingEntries(List<BibEntry> bibEntries)

Filters a list of BibEntry to those which do not yet exist in the database

public String
public List<BibEntry>
getSharedEntries(@NonNull List<Integer>
the list of Ids to filter. If list is empty, then no filter is applied
sharedIDs
)

Queries the database for shared entries.

public List<BibEntry>
public Optional<BibEntry>
getSharedEntry(int
Entry ID
sharedID
)

Returns: instance of BibEntry

public Map<Integer, Integer>
getSharedIDVersionMapping()

Retrieves a mapping between the columns SHARED_ID and VERSION.

public Map<String, String>
getSharedMetaData()

Fetches and returns all shared meta data.

public void
public void
insertEntry(BibEntry
BibEntry to be inserted.
bibEntry
)

For use in test only.

protected void
protected void
insertIntoFieldTable(List<BibEntry>
BibEntry to be inserted
bibEntries
)

Inserts the given list of BibEntry into FIELD table.

private void
private <T> T
inTransaction(DBMSProcessor.SqlWork<T> work)

Runs the work as one transaction on the (otherwise auto-committing) connection.

private boolean
private void
migrateFromOldStructure()

Copies the data of a shared database created by earlier JabRef versions into the current table structure: quoted upper-case tables in the schema jabref (structure version 1, JabRef 5.x/6.0-alpha) or in the default schema (structure version 0, JabRef < 5).

public List<BibEntry>
partitionAndGetSharedEntries(List<Integer> sharedIDs)

Queries the database for the given entries in 500 element batches.

public void
removeEntries(@NonNull List<BibEntry>
BibEntry to be deleted
bibEntries
)

Removes the shared bibEntry.

private void
removeObsoleteGroupTreeMetaData(Set<String> currentMetaDataKeys)

Removes group tree formats which are no longer present after a group update.

public void
setSharedMetaData(Map<String, String>
JabRef meta data as map
data
)

Clears and sets all shared meta data.

public void
setUp()

Creates and sets up the needed tables and columns according to the database type.

public void
setupSharedDatabase()

Creates and sets up the needed tables and columns according to the database type and performs a check whether the needed tables are present.

public void
startNotificationListener(DBMSSynchronizer
DBMSSynchronizer which handles the notification.
dbmsSynchronizer
)

Listens for notifications from DBMS.

public void
stopNotificationListener()

Terminates the notification listener.

private boolean
tableExists(String qualifiedName)

public void
updateEntry(BibEntry
BibEntry affected by changes
localBibEntry
)

Replaces the whole BibEntry on the shared database - if the local entry is based on the shared entry's current version (optimistic offline lock).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionback to summary
protected final Connection connection
connectionPropertiesback to summary
protected DatabaseConnectionProperties connectionProperties
CURRENT_VERSION_DB_STRUCTback to summary
private int CURRENT_VERSION_DB_STRUCT
dbmsConnectionback to summary
private final DatabaseConnection dbmsConnection
GROUP_TREE_METADATA_KEYSback to summary
private static final Set<String> GROUP_TREE_METADATA_KEYS
listenerback to summary
private @Nullable NotificationListener listener
LOGGERback to summary
protected static final Logger LOGGER
MIGRATION_ADVISORY_LOCK_IDback to summary
private static final long MIGRATION_ADVISORY_LOCK_ID
processorIdback to summary
private final String processorId
VERSION_DB_STRUCT_DEFAULTback to summary
private int VERSION_DB_STRUCT_DEFAULT

Constructor Detail

DBMSProcessorback to summary
protected DBMSProcessor(DatabaseConnection dbmsConnection)

Method Detail

checkBaseIntegrityback to summary
public boolean checkBaseIntegrity() throws SQLException

Scans the database for required tables.

Returns:boolean

true if the structure matches the requirements, false if not.

Exceptions
SQLException:
in case of error
checkTableAvailabilityback to summary
protected boolean checkTableAvailability(String... tableNames) throws SQLException

Checks whether all given table names (case insensitive) exist in database.

Parameters
tableNames:String[]

Table names to be checked

Returns:boolean

true if all given tables are present, else false.

databaseIsAtMostJabRef35back to summary
public boolean databaseIsAtMostJabRef35() throws SQLException

Determines whether the database is using an pre-3.6 structure.

Returns:boolean

true if the structure is old, else false.

findOldSchemaback to summary
private Optional<String> findOldSchema() throws SQLException
getCURRENT_VERSION_DB_STRUCTback to summary
pack-priv int getCURRENT_VERSION_DB_STRUCT()
getDBMSConnectionPropertiesback to summary
public DatabaseConnectionProperties getDBMSConnectionProperties()
getNotYetExistingEntriesback to summary
private List<BibEntry> getNotYetExistingEntries(List<BibEntry> bibEntries) throws SQLException

Filters a list of BibEntry to those which do not yet exist in the database

getProcessorIdback to summary
public String getProcessorId()
getSharedEntriesback to summary
public List<BibEntry> getSharedEntries(@NonNull List<Integer> sharedIDs) throws SQLException

Queries the database for shared entries. Optionally, they are filtered by the given list of sharedIds

Parameters
sharedIDs:@NonNull List<Integer>

the list of Ids to filter. If list is empty, then no filter is applied

getSharedEntriesback to summary
public List<BibEntry> getSharedEntries() throws SQLException
getSharedEntryback to summary
public Optional<BibEntry> getSharedEntry(int sharedID) throws SQLException
Parameters
sharedID:int

Entry ID

Returns:Optional<BibEntry>

instance of BibEntry

getSharedIDVersionMappingback to summary
public Map<Integer, Integer> getSharedIDVersionMapping() throws SQLException

Retrieves a mapping between the columns SHARED_ID and VERSION.

getSharedMetaDataback to summary
public Map<String, String> getSharedMetaData() throws SQLException

Fetches and returns all shared meta data.

insertEntriesback to summary
public void insertEntries(List<BibEntry> bibEntries) throws SQLException
insertEntryback to summary
public void insertEntry(BibEntry bibEntry) throws SQLException

For use in test only. Inserts the BibEntry into the shared database.

Parameters
bibEntry:BibEntry

BibEntry to be inserted.

Annotations
@VisibleForTesting
insertIntoEntryTableback to summary
protected void insertIntoEntryTable(List<BibEntry> bibEntries) throws SQLException
insertIntoFieldTableback to summary
protected void insertIntoFieldTable(List<BibEntry> bibEntries) throws SQLException

Inserts the given list of BibEntry into FIELD table. These entries do not yet exist in the remote database.

Parameters
bibEntries:List<BibEntry>

BibEntry to be inserted

inTransactionback to summary
private void inTransaction(DBMSProcessor.SqlAction work) throws SQLException
inTransactionback to summary
private <T> T inTransaction(DBMSProcessor.SqlWork<T> work) throws SQLException

Runs the work as one transaction on the (otherwise auto-committing) connection. Any exception rolls back.

isEntryTableEmptyback to summary
private boolean isEntryTableEmpty() throws SQLException
migrateFromOldStructureback to summary
private void migrateFromOldStructure() throws SQLException

Copies the data of a shared database created by earlier JabRef versions into the current table structure: quoted upper-case tables in the schema jabref (structure version 1, JabRef 5.x/6.0-alpha) or in the default schema (structure version 0, JabRef < 5). "TYPE" became entrytype, all other names only changed case.

The old tables are kept untouched, so older JabRef versions can still work with them.

partitionAndGetSharedEntriesback to summary
public List<BibEntry> partitionAndGetSharedEntries(List<Integer> sharedIDs) throws SQLException

Queries the database for the given entries in 500 element batches. Returns nothing for an empty id list.

removeEntriesback to summary
public void removeEntries(@NonNull List<BibEntry> bibEntries) throws SQLException

Removes the shared bibEntry.

Parameters
bibEntries:@NonNull List<BibEntry>

BibEntry to be deleted

removeObsoleteGroupTreeMetaDataback to summary
private void removeObsoleteGroupTreeMetaData(Set<String> currentMetaDataKeys) throws SQLException

Removes group tree formats which are no longer present after a group update.

setSharedMetaDataback to summary
public void setSharedMetaData(Map<String, String> data) throws SQLException

Clears and sets all shared meta data.

Parameters
data:Map<String, String>

JabRef meta data as map

setUpback to summary
public void setUp() throws SQLException

Creates and sets up the needed tables and columns according to the database type.

Exceptions
SQLException:
in case of error
setupSharedDatabaseback to summary
public void setupSharedDatabase() throws SQLException

Creates and sets up the needed tables and columns according to the database type and performs a check whether the needed tables are present.

Exceptions
SQLException:
in case of error
startNotificationListenerback to summary
public void startNotificationListener(DBMSSynchronizer dbmsSynchronizer)

Listens for notifications from DBMS. Needs to be implemented if LiveUpdate is supported by the DBMS

Parameters
dbmsSynchronizer:DBMSSynchronizer

DBMSSynchronizer which handles the notification.

stopNotificationListenerback to summary
public void stopNotificationListener()

Terminates the notification listener. Needs to be implemented if LiveUpdate is supported by the DBMS

tableExistsback to summary
private boolean tableExists(String qualifiedName) throws SQLException
updateEntryback to summary
public void updateEntry(BibEntry localBibEntry) throws OfflineLockException, SharedEntryNotPresentException, SQLException

Replaces the whole BibEntry on the shared database - if the local entry is based on the shared entry's current version (optimistic offline lock).

The version check and the increment are one statement, whose row lock serializes concurrent updates of the same entry: the second writer sees the incremented version and is refused instead of overwriting the first writer's fields with its stale copy.

Parameters
localBibEntry:BibEntry

BibEntry affected by changes

Exceptions
OfflineLockException:
if the shared entry has a newer version than the local one
SharedEntryNotPresentException:
if the entry does not exist on the shared side
org.jabref.logic.shared back to summary

private Interface DBMSProcessor.SqlAction

Annotations
@FunctionalInterface

Method Summary

Modifier and TypeMethod and Description
public void
run()

Method Detail

runback to summary
public void run() throws SQLException
org.jabref.logic.shared back to summary

private Interface DBMSProcessor.SqlWork<T>

Annotations
@FunctionalInterface

Method Summary

Modifier and TypeMethod and Description
public T
run()

Method Detail

runback to summary
public T run() throws SQLException