How tencent/wcdb works
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