# tencent/wcdb architecture

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

- Interactive diagram: https://gitdiagram.com/tencent/wcdb
- Repository: https://github.com/tencent/wcdb

## Overview

##### Project Type & Purpose
- WCDB is an open-source, multi-language mobile database framework built on SQLite/SQLCipher.
- It exposes a **core C++ engine** offering ORM, WINQ, encryption, repair, compression, migration, and concurrency.
- It provides **language-specific wrappers** (C++, Java/Kotlin via JNI, Objective-C, Swift) that all sit on the same core logic.
- It’s not a single application but a **library framework** that you integrate into Android, iOS, macOS, Windows, Linux, or OpenHarmony apps.

##### File Structure & Architectural Layers
- Top‐level “src” directory contains:
- **common/**: C++ core engine modules (base, core pool, sqlite adaptations, FTS, repair, compression, migration, config)
- **bridge/**: C++ → ObjC/Swift/C API glue code
- **cpp/**: Public C++ user interface and ORM chain-call APIs
- **java/**: Java/Kotlin wrappers including annotation processors, JNI binder code, and Android modules
- **objc/**: Objective-C layer with “WCT” prefixed classes and category extensions
- **swift/**: Swift layer with bridge classes and pure-Swift chain-call and ORM
- **android/**: Gradle/NDK integration for Android and sample apps
- **tools/**: Build scripts, documentation generators, prebuilt binaries (zstd, openssl)
- External dependencies:
- **SQLite/SQLCipher** (native C code)
- **Zstd** (native compression library)
- Platform build systems: **CMake**, **Xcode project files**, **Gradle**

##### README Insights
- Describes a layered code structure (common C++ core, then 5 language interfaces).
- Emphasizes **share-one-core** architecture: all languages call into the same optimized engine.

##### Key Components
a. Core Engine Layer
- “common” C++ modules: base, core, sqlite adapters, FTS, repair, compression, migration, config.
- Native SQLite/SQLCipher and Zstd subsystems.
b. Bridge/Binders
- C API wrappers and “bridge” folders connecting C++ to ObjC, Swift, and JNI.
c. Language Interface Layer
- **C++ API**: Public headers under “cpp/”
- **Java/Kotlin**: Annotation processor → Java code in “java/main” → JNI C code → core engine
- **Objective-C**: WCT classes wrapping core engine via ObjC++ bridge
- **Swift**: Swift classes using bridging header to call core engine
d. Application Layer (examples & tests)
- Android sample apps under “android/samples”
- Unit/benchmark tests in each language’s “tests” folder
e. Build & Tooling
- CMakeLists, Xcode projects, Gradle scripts, and prebuild tool binaries

##### Relationships & Data Flows
- **Language code** calls → **bridge layer** → **C++ core engine** → **SQLite/SQLCipher**
- **Core engine** may call → **Zstd** for compression & decompression
- **Repair & migration modules** sit alongside core to handle corrupted DB or schema changes
- The **annotation processor** (Java/Kotlin) generates ORM bindings at compile time

##### Architectural Patterns & Principles
- **Layered Architecture**: clear separation between engine, bindings, and language interfaces.
- **Adapter/Bridge Pattern**: each language uses a bridge to adapt calls to the core engine.
- **Plugin-style Modules**: features (FTS, compression, repair, migration) are modular.
- **Connection Pooling**: Core manages multiple handles for concurrency.

##### Technologies & Frameworks
- C++17 (core engine)
- CMake, Xcode, Gradle
- JNI (Java/Android integration)
- Objective-C++ bridging for iOS/macOS
- Swift bridging headers
- SQLCipher for encryption
- Zstd for compression

##### Essential Elements to Capture
- Core C++ modules & data flow to SQLite/SQLCipher and Zstd
- Bridge layers for each language
- Public APIs per language
- Sample/test applications as consumers
- Build systems & code-generation components

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        A1["Android Samples"]:::test
        A2["Java Tests"]:::test
        A3["Kotlin Tests"]:::test
        A4["Objective-C Tests"]:::test
        A5["Swift Tests"]:::test
    end

    %% Java Interface Layer
    subgraph "Java Interface Layer"
        ANTP["Annotation Processor"]:::api
        JB["Java Builder (Gradle Plugin)"]:::api
        JCOMP["Java Compatibility Wrappers"]:::api
        JAP["Java API"]:::api
        JNI["JNI Bindings"]:::bridge
    end

    %% ObjC and Swift Interface Layers
    subgraph "Objective-C Interface Layer"
        OBJ["Objective-C API"]:::api
    end
    subgraph "Swift Interface Layer"
        SWI["Swift API"]:::api
    end

    %% C++ Interface Layer
    subgraph "C++ Interface Layer"
        CPPAPI["Public C++ API"]:::api
    end

    %% Bridge Layer
    subgraph "Bridge Layer"
        B1["C API Bridge"]:::bridge
        B2["JNI Bridge"]:::bridge
    end

    %% Core Engine Layer
    subgraph "Core Engine Layer"
        CMN["Common Modules"]:::core
        FTS["FTS/Repair/Migration/Compression"]:::core
        Pool["Connection Pool"]:::core
    end

    %% External Libraries
    subgraph "External Libraries"
        SQLITE["SQLite/SQLCipher"]:::ext
        ZSTD["Zstd"]:::ext
    end

    %% Build & Tools
    subgraph "Build & Tools"
        CMAKE["CMakeLists.txt"]:::tools
        XCODE["WCDB.xcodeproj"]:::tools
        GRADLE["Gradle Scripts"]:::tools
        TOOLS["Build Scripts & Binaries"]:::tools
    end

    %% Connections: Application to Interfaces
    A1 -->|uses| JNI
    A2 -->|runs on| JAP
    A3 -->|uses| OBJ
    A4 -->|uses| OBJ
    A5 -->|uses| SWI

    %% Java layer internals
    ANTP -->|generates code for| JAP
    JB -->|builds| JAP
    JCOMP -->|adapts| JAP
    JAP -->|calls| JNI

    %% ObjC & Swift through bridge
    OBJ -->|calls| B1
    SWI -->|calls| B1

    %% C++ API direct
    CPPAPI -->|calls| CMN

    %% Bridge to Core
    JNI -->|bridges| CMN
    B1 -->|bridges| CMN
    B2 -->|bridges| CMN

    %% Core internals
    CMN -->|manages| FTS
    CMN -->|manages| Pool
    CMN -->|adapts| SQLITE
    CMN -->|calls| ZSTD

    %% Build & Tools support
    CMAKE -->|configures build| CMN
    XCODE -->|configures build| CMN
    GRADLE -->|builds| JNI
    TOOLS -->|provides| ZSTD

    %% Click Events
    click CMN "https://github.com/tencent/wcdb/tree/master/src/common"
    click B1 "https://github.com/tencent/wcdb/tree/master/src/bridge"
    click CPPAPI "https://github.com/tencent/wcdb/tree/master/src/cpp"
    click ANTP "https://github.com/tencent/wcdb/tree/master/src/java/annotation"
    click JB "https://github.com/tencent/wcdb/tree/master/src/java/builder/gradle-plugin"
    click JCOMP "https://github.com/tencent/wcdb/tree/master/src/java/compat"
    click JAP "https://github.com/tencent/wcdb/tree/master/src/java/main/src/main/java"
    click JNI "https://github.com/tencent/wcdb/tree/master/src/java/main/src/main/cpp"
    click OBJ "https://github.com/tencent/wcdb/tree/master/src/objc"
    click SWI "https://github.com/tencent/wcdb/tree/master/src/swift"
    click CMAKE "https://github.com/tencent/wcdb/blob/master/src/CMakeLists.txt"
    click XCODE "https://github.com/tencent/wcdb/blob/master/src/WCDB.xcodeproj"
    click GRADLE "https://github.com/tencent/wcdb/blob/master/src/java/main/build.gradle"
    click TOOLS "https://github.com/tencent/wcdb/tree/master/tools"
    click ZSTD "https://github.com/tencent/wcdb/tree/master/tools/prebuild/zstd"

    %% Styles
    classDef core fill:#bbdefb,stroke:#0d47a1,color:#0d47a1;
    classDef bridge fill:#ffe0b2,stroke:#e65100,color:#e65100;
    classDef api fill:#c8e6c9,stroke:#2e7d32,color:#2e7d32;
    classDef ext fill:#eceff1,stroke:#607d8b,color:#607d8b;
    classDef tools fill:#fff9c4,stroke:#f57f17,color:#f57f17;
    classDef test fill:#e1bee7,stroke:#6a1b9a,color:#6a1b9a;
```
