# wushidong125/sharding-db architecture

> Architecture diagram of the GitHub repository wushidong125/sharding-db, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wushidong125/sharding-db
- Repository: https://github.com/wushidong125/sharding-db

## Overview

##### Main Components
- Application Entry Point: The Spring Boot application (ShardingDbApplication.java) initializes the application.
- Configuration Layer: DataSourceConfig.java configures the data sources, establishes the Druid connection pool, and integrates the Sharding-JDBC properties.
- Sharding Strategy: The classes in the “base” package (ModuloDatabaseShardingAlgorithm.java and ModuloTableShardingAlgorithm.java) contain the logic for determining which database and table to use.
- API Layer: The “controller” package (OrderController.java) exposes endpoints (e.g., for order operations) to the external world.
- Persistence Layer: The “repository” (OrderRepository.java) and “entity” (Order.java) represent the data-access components using JPA/Hibernate and map the Order domain.
- Testing: Though not part of the runtime architecture, the “test” folder is for unit and integration tests.
- External Resources: The Sharding-JDBC library (from dangdangdotcom) and the Druid connection pool are key external technologies that should be represented.

##### Relationships and Data Flow
- Clients (could be a web interface or another external service) send HTTP requests to the OrderController.
- The OrderController handles these requests by interacting with the repository layer.
- The OrderRepository interacts with the database using JPA, while the DataSourceConfig ensures that requests are directed toward the appropriate shard.
- The sharding algorithm (comprising both database and table strategies) is invoked during data source selection by the underlying Sharding-JDBC mechanism.
- The connection to the database is managed via the Druid connection pool.

##### Key Architectural Patterns and Patterns to Emphasize
- Layered Architecture: Clearly separate the Controller, Configuration, and Persistence layers.
- Database Sharding: Visibly represent how the modulo-based algorithms are used to distribute data across multiple database and table shards.
- Integration with External Libraries: Indicate where the Sharding-JDBC and Druid components integrate with the core application.

##### Clarity and Detail
- All blocks must be clearly labeled with their names and roles.
- To annotate integration points with external libraries like the Sharding-JDBC and Druid connection pool if space allows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client / API Consumer"]:::external

    %% Spring Boot Application Container
    subgraph "Spring Boot Application"
        direction TB
        App["ShardingDbApplication (Entry Point)"]:::app
        Controller["OrderController (API Layer)"]:::controller
        Repository["OrderRepository (Persistence Layer)"]:::repository
        Entity["Order Entity"]:::entity
        Config["DataSourceConfig (Configuration & Sharding Integration)"]:::config
        ShardAlgDB["ModuloDatabaseShardingAlgorithm"]:::sharding
        ShardAlgTable["ModuloTableShardingAlgorithm"]:::sharding
    end

    %% External Technologies
    Druid["Druid Connection Pool"]:::externalTech
    ShardingJDBC["Sharding-JDBC (dangdangdotcom)"]:::externalTech

    %% External Database Shards
    subgraph "Database Shards"
        direction TB
        Shard1["Shard 1"]:::database
        Shard2["Shard 2"]:::database
    end

    %% Testing Block (Not part of runtime architecture)
    subgraph "Testing"
        direction TB
        Test["ShardingDbApplicationTests"]:::testing
    end

    %% Data Flow Connections
    Client -->|"HTTPRequest"| Controller
    Controller -->|"Calls"| Repository
    Repository -->|"JPA Query"| Config
    Config -->|"Routes to"| Shard1
    Config -->|"Routes to"| Shard2
    Config -->|"Executes"| ShardAlgDB
    Config -->|"Executes"| ShardAlgTable
    Config -->|"Configures"| Druid
    Config -->|"Integrates"| ShardingJDBC
    Repository -->|"Maps to"| Entity
    App -.->|"Bootstraps"| Controller
    App -.->|"Bootstraps"| Config

    %% Click Events
    click App "https://github.com/wushidong125/sharding-db/blob/master/src/main/java/com/example/demo/ShardingDbApplication.java"
    click Config "https://github.com/wushidong125/sharding-db/blob/master/src/main/java/com/example/demo/config/DataSourceConfig.java"
    click ShardAlgDB "https://github.com/wushidong125/sharding-db/blob/master/src/main/java/com/example/demo/base/ModuloDatabaseShardingAlgorithm.java"
    click ShardAlgTable "https://github.com/wushidong125/sharding-db/blob/master/src/main/java/com/example/demo/base/ModuloTableShardingAlgorithm.java"
    click Controller "https://github.com/wushidong125/sharding-db/blob/master/src/main/java/com/example/demo/controller/OrderController.java"
    click Repository "https://github.com/wushidong125/sharding-db/blob/master/src/main/java/com/example/demo/repository/OrderRepository.java"
    click Entity "https://github.com/wushidong125/sharding-db/blob/master/src/main/java/com/example/demo/entity/Order.java"
    click Test "https://github.com/wushidong125/sharding-db/blob/master/src/test/java/com/example/demo/ShardingDbApplicationTests.java"

    %% Styles
    classDef external fill:#FFE4B5,stroke:#333,stroke-width:2px;
    classDef app fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef controller fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef config fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef sharding fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef repository fill:#98FB98,stroke:#333,stroke-width:2px;
    classDef entity fill:#AFEEEE,stroke:#333,stroke-width:2px;
    classDef externalTech fill:#DDA0DD,stroke:#333,stroke-width:2px;
    classDef database fill:#F0E68C,stroke:#333,stroke-width:2px;
    classDef testing fill:#E6E6FA,stroke:#333,stroke-width:2px;
```
