Package org.h2.tools

# Class DirectRecover

java.lang.Object
org.h2.util.Tool
org.h2.tools.Recover
org.h2.tools.DirectRecover

All Implemented Interfaces:
  `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

## Field Summary

### Fields inherited from class org.h2.util.Tool

`out`

## Constructor Summary

Constructors
Constructor
Description
`DirectRecover()`

## Method Summary

Modifier and Type
Method
Description
`static void`
`execute(String dir, String db, 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.
`CompressionType`
`getCompressionType()`
Gets the current compression type.
`boolean`
`isCompressionAvailable(CompressionType type)`
Check if a compression library is available without loading it.
`static void`
`main(String... args)`
Options are case-sensitive.
`void`
`runTool(String... args)`
Enhanced runTool method with compression support.
`void`
`setCompressionType(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`

### Methods inherited from class org.h2.util.Tool

`getMainClassName, isOption, printNoDatabaseFilesFound, setOut, showUsage, showUsageAndThrowUnsupportedOption, throwUnsupportedOption`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DirectRecover

public DirectRecover()

## Method Details

### main

public static void main(String... args) throws 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:
  `SQLException` - on failure

### runTool

public void runTool(String... args) throws SQLException
Enhanced runTool method with compression support.

Overrides:
  `runTool` in class `Recover`
Parameters:
  `args` - the command line arguments
Throws:
  `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(String dir, String db, PrintWriter writer) throws 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:
  `SQLException` - on failure