opendal_conn.py

Overview

The opendal_conn.py module provides an abstraction layer for interacting with OpenDAL (Open Data Abstraction Layer) storage systems, with a specialized focus on MySQL-based backend storage. It is designed to initialize, configure, and manage connections to OpenDAL-compatible storage, including setup of MySQL database tables and configuration parameters necessary for efficient storage handling.

This module encapsulates the complexities of configuring OpenDAL connections, especially when using MySQL as the underlying storage scheme, and exposes a singleton class OpenDALStorage that supports common storage operations like reading, writing, deleting, scanning, and checking existence of objects in the storage.


Detailed Documentation

Constants


Functions

get_opendal_config() -> dict

Fetches and prepares the configuration dictionary required to initialize an OpenDAL Operator. The configuration is read from a base configuration source (via get_base_config), supporting dynamic detection of the storage scheme.

Returns:
A dictionary containing all necessary parameters to instantiate an OpenDAL Operator.

Raises:
Any exceptions encountered during configuration loading are logged and re-raised.

Example Usage:

config = get_opendal_config()
operator = opendal.Operator(**config)

Classes

OpenDALStorage

A singleton class encapsulating the OpenDAL operator instance and providing a high-level API for common storage operations.

Singleton Decorator:
Ensures a single instance of OpenDALStorage exists throughout the application lifecycle.


Initialization
OpenDALStorage()

Methods

Method

Parameters

Return Type

Description

health()

None

Boolean

Performs a simple write operation to test if the storage backend is reachable and operational.

put(bucket, fnm, binary)

bucket (str), fnm (str), binary (bytes)

None

Writes binary data to the storage, under the path bucket/fnm.

get(bucket, fnm)

bucket (str), fnm (str)

bytes

Reads and returns the binary content stored at bucket/fnm. Raises if not found.

rm(bucket, fnm)

bucket (str), fnm (str)

None

Deletes the object at bucket/fnm and reinitializes the operator instance.

scan(bucket, fnm)

bucket (str), fnm (str)

Iterator

Returns an iterator that scans the storage under the prefix bucket/fnm, yielding object keys.

obj_exist(bucket, fnm)

bucket (str), fnm (str)

Boolean

Checks if the object at bucket/fnm exists in the storage.

init_db_config()

None

None

Initializes the MySQL database configuration by setting the global max_allowed_packet parameter.

init_opendal_mysql_table()

None

None

Creates the designated storage table in MySQL if it does not exist.


Method Details and Usage Examples








Implementation Details


Integration and Usage in the System


Visual Diagram

classDiagram
    class OpenDALStorage {
        -_kwargs: dict
        -_scheme: str
        -_operator: opendal.Operator
        +__init__()
        +health(): bool
        +put(bucket: str, fnm: str, binary: bytes)
        +get(bucket: str, fnm: str) : bytes
        +rm(bucket: str, fnm: str)
        +scan(bucket: str, fnm: str) : Iterator
        +obj_exist(bucket: str, fnm: str) : bool
        -init_db_config()
        -init_opendal_mysql_table()
    }

    OpenDALStorage ..> opendal.Operator : uses
    OpenDALStorage ..> pymysql.Connection : uses (for DB config)
    OpenDALStorage ..> get_opendal_config : calls

    class get_opendal_config {
        +() : dict
    }

Summary

The opendal_conn.py module provides a robust and configurable interface to OpenDAL storage systems, with special support for MySQL backend configuration and table initialization. It simplifies usage of OpenDAL through a singleton class, abstracts connection details, and ensures the environment is properly configured for handling large binary objects. This module is essential in systems requiring reliable, scalable object storage with a consistent API.