Package org.h2.tools

Class DirectRecover

  • All Implemented Interfaces:
    org.h2.store.DataHandler

    public class DirectRecover
    extends Recover
    Enhanced database recovery tool with streaming and compression support.

    DirectRecover extends the original Recover class to provide:

    • Streaming Processing: Uses pipes for memory-efficient processing of large databases
    • Parallel Execution: Dump generation and SQL writing happen concurrently
    • Compression Support: On-the-fly compression with GZIP, ZIP, BZIP2, or KANZI
    • No Intermediate Files: Direct streaming prevents disk space issues

    Usage Examples:

    Basic Recovery (No Compression):

     java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb
     

    With Built-in Compression:

     # GZIP compression (recommended for most cases)
     java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress gzip
    
     # ZIP compression (widely compatible)
     java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress zip
     

    With Optional External Libraries:

     # BZIP2 compression (requires Apache Commons Compress)
     java -cp "h2.jar:commons-compress.jar" org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress bzip2
    
     # KANZI compression (requires Kanzi library, best compression)
     java -cp "h2.jar:kanzi.jar" org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress kanzi
     

    Debug and Troubleshooting:

     # Enable verbose debug output
     java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb -trace -compress gzip
    
     # Process all databases in directory
     java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -compress gzip
     

    Command Line Options:

    Supported Options
    OptionDescriptionDefault
    -dir <directory>Database directory. (current directory)
    -db <database>Database name (without .mv.db extension) All databases in directory
    -compress <type>Compression: none, gzip, zip, bzip2, kanzinone
    -traceEnable debug output and verbose loggingfalse
    -help or -?Show help information-

    Output Files:

    • No compression: database.sql
    • GZIP: database.sql.gz
    • ZIP: database.sql.zip
    • BZIP2: database.sql.bz2
    • KANZI: database.sql.knz

    External Dependencies (Optional):

    Performance Notes:

    • Streaming: Memory usage is bounded (~256KB) regardless of database size
    • Parallel: Dump generation and SQL writing happen simultaneously
    • Compression: Applied on-the-fly, no temporary uncompressed files
    • Large Databases: Designed to handle multi-GB databases efficiently

    Error Handling:

    • Missing compression libraries fall back to uncompressed output
    • Corrupted databases are processed in recovery mode
    • Timeouts prevent indefinite hanging
    • Constructor Detail

      • DirectRecover

        public DirectRecover()
        Creates default instance
    • Method Detail

      • main

        public static void main​(java.lang.String... args)
                         throws java.sql.SQLException
        Options are case-sensitive.
        Supported options (in addition to base Recover options)
        [-compress <type>] Compress SQL output (none, gzip, zip, bzip2, kanzi, default: none)
        [-trace] Enable verbose debug output
        Parameters:
        args - the command line arguments
        Throws:
        java.sql.SQLException - on failure
      • runTool

        public void runTool​(java.lang.String... args)
                     throws java.sql.SQLException
        Enhanced runTool method with compression support.
        Overrides:
        runTool in class Recover
        Parameters:
        args - the command line arguments
        Throws:
        java.sql.SQLException - on failure
      • isCompressionAvailable

        public boolean isCompressionAvailable​(CompressionType type)
        Check if a compression library is available without loading it.
        Parameters:
        type - the compression type to check
        Returns:
        true if the library is available, false otherwise
      • getAvailableCompressionTypes

        public CompressionType[] getAvailableCompressionTypes()
        Get a list of available compression types based on classpath.
        Returns:
        array of available compression types
      • setCompressionType

        public void setCompressionType​(CompressionType type)
        Sets the compression type for SQL output files.
        Parameters:
        type - the compression type to use
      • getCompressionType

        public CompressionType getCompressionType()
        Gets the current compression type.
        Returns:
        the current compression type
      • execute

        public static void execute​(java.lang.String dir,
                                   java.lang.String db,
                                   java.io.PrintWriter writer)
                            throws java.sql.SQLException
        Public method to process database files and write to a provided writer (pipe-like).
        Parameters:
        dir - the directory
        db - the database name (null for all databases)
        writer - the output writer
        Throws:
        java.sql.SQLException - on failure