Data Export and Import in Java

Database Backup

The eXtremeDB database backup and restore functionality is implemented by the createBackup() and restoreBackup() methods of the Connection class. Please see the SDK sample incBackup for implementation details.

createBackup()

The createBackup() method has two overloads:

    public void createBackup(String fileName, String label, Database.BackupType type);

    public void createBackup(String fileName, String label, Database.BackupType type,
                             int compressionLevel, String cipher);
            

where the backup type and compressionLevel are defined as follows:

    public enum BackupType {
        Auto,
        Snapshot,
        Incremental
    };

    public static final int BACKUP_FLAG_COMPRESSED = 1;
    public static final int BACKUP_FLAG_ENCRYPTED  = 2;
            

restoreBackup()

Similarly, the restoreBackup() method can be called with or without an encryption key:

    public void restoreBackup(String fileName, String label);

    public void restoreBackup(String fileName, String label, String cipher);
            

listBackup()

In addition, the listBackup() method can be called to retrieve information from a backup file. It returns an array of BackupInfo objects:

    public Database.BackupInfo[] listBackup(String fileName);

    public static class BackupInfo 
    {
        public int        protocolVersion;
        public BackupType type;
        public int        flags;
        public int        backupNo;
        public long       timestamp;
        public long       size;
        public long       offset;
        public int        crc;
        public long       transNo;
        public int        memPageSize;
        public int        diskPageSize;
        public long       nPagesTotal;
        public String     dbName;
        public String     label;
    }
            

Additional Backup Parameters

Further backup parameters can be specified in the Database.Parameters object used for the open() method:

    /**
     * Size of backup counters array, bytes, power of two. Ignored if disk_max_database_size is set.
     * Default is 0 - disable backup feature.
     */
    public long backupMapSize;

    /**
     * Number of pages for last exclusive pass of backup procedure. Set to zero to disable threshold.
     * Default is 0.
     */
    public int backupMinPages;

    /**
     * Max number of passes before exclusive pass of backup procedure. Default is 10.
     */
    public int backupMaxPasses;

    /**
     * Name of a file used to store backup temporary data on mco_db_close() call.
     * Optional. Set to null for default "<persistent-storage>.bmap" file located at the same location
     * as the persistent storage file.
     */
    public String backupMapFile;
            

(These parameters correspond to backup_map_size, backup_min_pages, backup_max_passes, and backup_map_filename in the mco_db_params_t structure of the C API.)

Database Snapshots

To save an image of an in-memory database (transient objects) to a specified file, the Connection class provides the saveSnapshot() method. This snapshot can later be loaded by the Database method open() if the corresponding file path is specified in the Database.Parameters property databaseSnapshotFilePath. A file is always created, or overwritten if it already exists. The method returns true if the snapshot was successfully saved, and false if the specified file cannot be opened.

The method signature is:

    public boolean saveSnapshot(String databaseSnapshotFilePath)
            

Saving and Loading Individual Classes

It is sometimes desirable to export or import only the data for an individual database class. The Connection class provides the following methods for this purpose:

    public boolean saveClass(String filePath, Class<?> cls)

    public boolean loadClass(String filePath, Class<?> cls, boolean doMerge)
            

Note that if the doMerge argument is false, the existing data content of this class is cleared before loading from the image file. If true, the loaded objects are added to the existing class data. The methods return true if the objects were successfully saved or loaded, and false if the specified file cannot be opened.

Saving Database Metadata

It is possible to save the database metadata (data layout information) in xSQL config file format (JSON). This file is suitable for loading into xSQL to connect to the database as a client application. The Connection class provides the saveMetadata() method. The method signature is:

    public boolean saveMetadata(String databaseMetadataFilePath, boolean saveDefaults)
            

Saving Database Schema

It is possible to save the database schema to an external file. The Connection class provides the saveDictionary() method.

    public boolean saveDictionary(String databaseDictionaryFilePath)
            

XML Export/Import

The Java wrapper exposes XML export/import through com.mcobject.extremedb.Connection:

    public boolean exportXML(String filePath)
    public boolean importXML(String filePath, int transactionSize)
    public boolean importXML(String filePath)

importXML(filePath) is a convenience overload equivalent to importXML(filePath, 0).

importXML(filePath, transactionSize) throws IllegalArgumentException if transactionSize is negative. The methods return false when the specified XML file cannot be opened. Database-level errors are reported through the usual Java wrapper error mechanism.

Usage example:

    static final int IMPORT_TRANSACTION_SIZE = 25;
    static final String XML_FILENAME = "db.xml";

    Database db = new Database(options.config);
    db.open("xml-export-db", createParameters(options), DATABASE_SIZE);
    Connection con = new Connection(db);

    // Insert data here.

    if (!con.exportXML(XML_FILENAME)) {
        throw new RuntimeException("Failed to create XML file: " + XML_FILENAME);
    }

    con.disconnect();
    db.close();

    Database targetDb = new Database(options.config);
    targetDb.open("xml-import-db", createParameters(options), DATABASE_SIZE);
    Connection targetCon = new Connection(targetDb);

    if (!targetCon.importXML(XML_FILENAME, IMPORT_TRANSACTION_SIZE)) {
        throw new RuntimeException("Failed to open XML file: " + XML_FILENAME);
    }

The full sample can be found in the samples/java/xml directory.