Package org.h2.tools
Class DirectRecover
- java.lang.Object
-
- org.h2.util.Tool
-
- org.h2.tools.Recover
-
- org.h2.tools.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 Option Description Default -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, kanzi none -trace Enable debug output and verbose logging false -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):
- Apache Commons Compress: Required for BZIP2 compression
Download: https://commons.apache.org/proper/commons-compress/ - Kanzi: Required for KANZI compression (best compression ratio)
Download: https://github.com/flanglet/kanzi-java
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 Summary
Constructors Constructor Description DirectRecover()Creates default instance
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidexecute(java.lang.String dir, java.lang.String db, java.io.PrintWriter writer)Public method to process database files and write to a provided writer (pipe-like).CompressionType[]getAvailableCompressionTypes()Get a list of available compression types based on classpath.CompressionTypegetCompressionType()Gets the current compression type.booleanisCompressionAvailable(CompressionType type)Check if a compression library is available without loading it.static voidmain(java.lang.String... args)Options are case-sensitive.voidrunTool(java.lang.String... args)Enhanced runTool method with compression support.voidsetCompressionType(CompressionType type)Sets the compression type for SQL output files.-
Methods inherited from class org.h2.tools.Recover
checkPowerOff, checkWritingAllowed, execute, getCompareMode, getDatabasePath, getLobFileListCache, getLobStorage, getLobSyncObject, getMaxLengthInplaceLob, getTempFileDeleter, openFile, readBlobMap, readClobMap, readLob
-
-
-
-
Method Detail
-
main
public static void main(java.lang.String... args) throws java.sql.SQLExceptionOptions 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.SQLExceptionEnhanced runTool method with compression support.
-
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.SQLExceptionPublic method to process database files and write to a provided writer (pipe-like).- Parameters:
dir- the directorydb- the database name (null for all databases)writer- the output writer- Throws:
java.sql.SQLException- on failure
-
-