Package net.carcdr.yhocuspocus.extension
Class DatabaseExtension
java.lang.Object
net.carcdr.yhocuspocus.extension.DatabaseExtension
- All Implemented Interfaces:
Extension
- Direct Known Subclasses:
InMemoryDatabaseExtension
Abstract base class for database persistence extensions.
This class provides a convenient pattern for implementing document persistence.
Subclasses only need to implement loadFromDatabase(String) and
saveToDatabase(String, byte[]).
Example usage:
public class PostgresDatabaseExtension extends DatabaseExtension {
private final DataSource dataSource;
{@literal @}Override
protected byte[] loadFromDatabase(String documentName) {
// Load from PostgreSQL
try (Connection conn = dataSource.getConnection()) {
// ... query logic
}
}
{@literal @}Override
protected void saveToDatabase(String documentName, byte[] state) {
// Save to PostgreSQL
try (Connection conn = dataSource.getConnection()) {
// ... insert/update logic
}
}
}
- Since:
- 1.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract byte[]loadFromDatabase(String documentName) Loads document state from the persistence layer.onLoadDocument(OnLoadDocumentPayload payload) Loads document state from the database.onStoreDocument(OnStoreDocumentPayload payload) Persists document state to the database.intpriority()Extension priority for database operations.protected abstract voidsaveToDatabase(String documentName, byte[] state) Saves document state to the persistence layer.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.carcdr.yhocuspocus.extension.Extension
afterLoadDocument, afterStoreDocument, afterUnloadDocument, beforeUnloadDocument, onAuthenticate, onChange, onConnect, onCreateDocument, onDestroy, onDisconnect
-
Constructor Details
-
DatabaseExtension
public DatabaseExtension()
-
-
Method Details
-
priority
public int priority()Extension priority for database operations. Higher priority ensures database extensions run before other extensions. -
onLoadDocument
Loads document state from the database.This hook runs when a document is first accessed. If the document exists in the database, this method should return its state. If not, return null.
- Specified by:
onLoadDocumentin interfaceExtension- Parameters:
payload- document load information- Returns:
- future that completes when load is done
-
onStoreDocument
Persists document state to the database.This hook runs after a quiet period (debounced) when the document has been modified. Extensions should persist the provided state.
- Specified by:
onStoreDocumentin interfaceExtension- Parameters:
payload- document and state information- Returns:
- future that completes when save is done
-
loadFromDatabase
Loads document state from the persistence layer.Implementations should query their storage backend and return the Y-CRDT state bytes, or null if the document doesn't exist.
- Parameters:
documentName- name of the document to load- Returns:
- Y-CRDT state bytes, or null if not found
- Throws:
Exception- if an error occurs during loading
-
saveToDatabase
Saves document state to the persistence layer.Implementations should store the Y-CRDT state bytes in their storage backend, creating a new entry or updating an existing one.
- Parameters:
documentName- name of the document to savestate- Y-CRDT state bytes- Throws:
Exception- if an error occurs during saving
-