View Javadoc

1   /* $Id: DumpFileManager.java,v 1.9 2006/01/10 16:00:08 psionides Exp $ */
2   package net.sourceforge.jdbdump.dump;
3   
4   import java.io.File;
5   
6   /***
7    * A class that can save dumps into a file and then load them back again into memory.
8    * <br /><br />
9    * After a Dump object is created using DatabaseConnection.dump(), it can be saved into
10   * a file using a DumpFileManager. A Dump only contains database structure, not data,
11   * because the amount of data may be so big that it will not fit into the memory.
12   * So when a Dump is exported using exportDump(), first all information contained in it
13   * is saved, and then the Dump is used to download the rest of the data from the database.  
14   * <br /><br />
15   * The same is with loading a dump - first its structure is loaded into a Dump object using
16   * importDump(), and then in restore() all data is read from the same file after the structure
17   * is created in the database.
18   * <br /><br />
19   * Usage example:
20   * <pre>
21   * DatabaseConnectorFactory fac = DatabaseConnectorFactory.getInstance();
22   * DatabaseConnector conn = fac.createConnector("net.sourceforge.connect.connectors.MysqlConnector");
23   * DumpFileManager fman = new BinaryFileManager();
24   * try {
25   *     // dump base1 into dump.zip 
26   *     conn.connect("jdbc:mysql://database.myserver.com/base1", "admin", "AdMiNpAsS");
27   *     Dump dump = conn.dump();
28   *     fman.exportDump(dump, new File("dump.zip"), DumpFileManager.CompressionMethod.ZIP);
29   *     conn.disconnect();
30   *     
31   *     // restore dump.zip into base2
32   *     conn.connect("jdbc:mysql://database.myserver.com/base2", "admin", "AdMiNpAsS");
33   *     Dump dump2 = fman.importDump(new File("dump.zip"), DumpFileManager.CompressionMethod.ZIP);
34   *     conn.restore(dump2);
35   *     dump2.closeFileReader();
36   *     conn.disconnect();
37   * } catch (SQLException e) {
38   *     e.printStackTrace();
39   * }
40   * </pre>   
41   * 
42   * @author jsuder
43   */
44  
45  public interface DumpFileManager {
46  
47  	public enum CompressionMethod {
48  		/*** (De)compress the backup file using Zip method. */
49  		ZIP,
50  
51  		/*** (De)compress the backup file using Gzip method. */
52  		GZIP,
53  
54  		/*** Do not (de)compress the backup file at all. */
55  		NONE
56  	}
57  	
58  	/***
59  	 * Saves a backup of the database represented in the dump object into the specified file.
60  	 * First it saves the database structure which is already contained in the Dump, then
61  	 * uses the Dump to download all the data, table by table, record by record, and saves it
62  	 * into the file as soon as it's downloaded.
63  	 * 
64  	 * @param dump a database dump created using DatabaseConnector.dump() method
65  	 * @param file a file in which the backup will be stored
66  	 * @param compress compression method used to compress the file (zip, gzip or none)
67  	 */
68  
69  	public void exportDump(Dump dump, File file, CompressionMethod compress);
70  	
71  	/***
72  	 * Loads the database structure as a Dump object from the specified file.
73  	 * <br />
74  	 * This method doesn't load any data (i.e. table records) from the file yet. Data should be
75  	 * loaded by DatabaseConnector.restore() when the dump returned by this function is passed to
76  	 * it. The dump stores a reference to the input stream from this file to be able to read the data
77  	 * later; when it is no longer needed, it should be closed by calling closeFileReader() on the
78  	 * Dump (but that shouldn't be called until all data is read in DatabaseConnector.restore()).
79  	 * 
80  	 * @param file a file from which the backup should be read
81  	 * @param compress compression method used to decompress the file (zip, gzip or none)
82  	 * @return a database dump that can be uploaded using DatabaseConnector.restore() method
83  	 */
84  
85  	public Dump importDump(File file, CompressionMethod compress); 
86  }