Data Export and Import in Python

The following APIs save a database image snapshot or class data to the specified external file. APIs are also provided to perform incremental online backup. (See the Incremental Backup and Restore page for further details.)

Database Snapshots

To save an image of an in-memory database (transient objects) to a specified file, the Connection class provides the save_snapshot() method. A file is always created, or overwritten if it already exists. The method signature is:

    def save_snapshot(self, path: str, save_metadata: bool = True, save_crc: bool = True) -> bool:
            

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:

    def save_class(self, file_path: str, cls: type) -> bool:

    def load_class(self, file_path: str, cls: type = None, do_merge: bool = False) -> bool:
            

Note that if the do_merge 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.

XML Export and Import

The Python wrapper exposes XML export/import through the connection object:

    conn.exportXML(file_path)
    conn.importXML(file_path, transaction_size=0)

On success, these methods return `None`. File and database errors are reported as Python exceptions from the eXtremeDB Python wrapper.

Example usage:

    IMPORT_TRANSACTION_SIZE = 25
    XML_FILENAME = "db.xml"

    db = exdb.open_database(
        dbname="xml-export-db",
        dictionary=dictionary,
        is_disk=False,
        db_segment_size=DB_SEGMENT_SIZE)

    try:
        with db.connect() as conn:
            # Insert data here.
            conn.exportXML(XML_FILENAME)
    finally:
        db.close()

    target_db = exdb.open_database(
        dbname="xml-import-db",
        dictionary=dictionary,
        is_disk=False,
        db_segment_size=DB_SEGMENT_SIZE)

    try:
        with target_db.connect() as conn:
            conn.importXML(XML_FILENAME, IMPORT_TRANSACTION_SIZE)
    finally:
        target_db.close()  

The full example can be found at the samples/python/xml directory.