Class DatabaseExtension

java.lang.Object
net.carcdr.yhocuspocus.extension.DatabaseExtension
All Implemented Interfaces:
Extension
Direct Known Subclasses:
InMemoryDatabaseExtension

public abstract class DatabaseExtension extends Object implements Extension
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 Details

    • DatabaseExtension

      public DatabaseExtension()
  • Method Details

    • priority

      public int priority()
      Extension priority for database operations. Higher priority ensures database extensions run before other extensions.
      Specified by:
      priority in interface Extension
      Returns:
      priority value (default 500)
    • onLoadDocument

      public CompletableFuture<Void> onLoadDocument(OnLoadDocumentPayload payload)
      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:
      onLoadDocument in interface Extension
      Parameters:
      payload - document load information
      Returns:
      future that completes when load is done
    • onStoreDocument

      public CompletableFuture<Void> onStoreDocument(OnStoreDocumentPayload payload)
      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:
      onStoreDocument in interface Extension
      Parameters:
      payload - document and state information
      Returns:
      future that completes when save is done
    • loadFromDatabase

      protected abstract byte[] loadFromDatabase(String documentName) throws Exception
      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

      protected abstract void saveToDatabase(String documentName, byte[] state) throws Exception
      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 save
      state - Y-CRDT state bytes
      Throws:
      Exception - if an error occurs during saving