# bhatti/db-locks architecture

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

- Interactive diagram: https://gitdiagram.com/bhatti/db-locks
- Repository: https://github.com/bhatti/db-locks

## Overview

##### Main Components

a) Client Layer:
- Client applications that would use this library
- LockManager interface as the main entry point

b) Core Components:
- LockManager Implementation
- Lock Store (DefaultLockStore and FairLockStore)
- Repository Layer with multiple implementations
- Domain Models

c) Storage Backends:
- Relational Databases (PostgreSQL, MySQL, SQLite)
- Redis
- AWS DynamoDB

**[Client Applications]**
- ↓
**[Lock Manager Layer]**
- ↓
**[Store Layer]**
- ↓
**[Repository Layer]**
- ↓
**[Storage Backends]**

##### Detailed Component Breakdown

Lock Manager Layer:
- LockManagerImpl
- Main operations: acquire_lock(), release_lock(), send_heartbeat()

Store Layer:
- DefaultLockStore
- FairLockStore
- LockCache

- Mutex Repositories
- ORM Mutex Repository
- Redis Mutex Repository
- DynamoDB Mutex Repository
- Semaphore Repositories
- ORM Semaphore Repository
- Redis Semaphore Repository
- DynamoDB Semaphore Repository
- Redis Fair Semaphore Repository

Storage Layer:

##### Key Relationships to Highlight

- Relationship between LockManager and Store implementations
- Connection between repositories and their respective storage backends
- Configuration flow from config.yaml to various components

##### Technical Details

- Database migrations flow
- Metrics collection path
- Error handling flow
- Configuration management

##### Additional Elements

- Notes about the lease-based locking mechanism
- Configuration options flow
- Metric collection points

##### Implementation Notes

- Version/lease information flow

Remember to include:
- Error handling paths
- Lease renewal flows
- Lock acquisition sequences
- Repository factory pattern implementation
- Configuration injection points
- Metric collection points

This will provide a comprehensive view of the distributed locking system while maintaining clarity about the relationships between components and the flow of operations.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Applications"]
        App1[Client App 1]
        App2[Client App 2]
    end

    subgraph Manager["Lock Manager Layer"]
        LockManager["Lock Manager Interface"]:::manager
        LockManagerImpl["Lock Manager Implementation"]:::manager
    end

    subgraph Store["Store Layer"]
        DefaultStore["Default Lock Store"]:::store
        FairStore["Fair Lock Store"]:::store
        LockCache["Lock Cache"]:::store
    end

    subgraph Repository["Repository Layer"]
        Factory["Repository Factory"]:::repository

        subgraph MutexRepos["Mutex Repositories"]
            ORMMutex["ORM Mutex Repository"]:::repository
            RedisMutex["Redis Mutex Repository"]:::repository
            DDBMutex["DynamoDB Mutex Repository"]:::repository
        end

        subgraph SemaphoreRepos["Semaphore Repositories"]
            ORMSemaphore["ORM Semaphore Repository"]:::repository
            RedisSemaphore["Redis Semaphore Repository"]:::repository
            DDBSemaphore["DynamoDB Semaphore Repository"]:::repository
            RedisFairSemaphore["Redis Fair Semaphore Repository"]:::repository
        end
    end

    subgraph Domain["Domain Layer"]
        Models["Domain Models"]:::domain
        Schema["Schema"]:::domain
        ErrorHandling["Error Handling"]:::domain
        Options["Configuration Options"]:::domain
    end

    subgraph Storage["Storage Layer"]
        Postgres[(PostgreSQL)]:::storage
        MySQL[(MySQL)]:::storage
        SQLite[(SQLite)]:::storage
        Redis[(Redis)]:::storage
        DynamoDB[(DynamoDB)]:::storage
    end

    subgraph Utils["Utilities"]
        Metrics["Lock Metrics"]:::utils
        DDBCommon["DynamoDB Common"]:::utils
        RedisCommon["Redis Common"]:::utils
        RDBCommon["Relational DB Common"]:::utils
    end

    %% Relationships
    App1 & App2 --> LockManager
    LockManager --> LockManagerImpl
    LockManagerImpl --> DefaultStore & FairStore
    DefaultStore & FairStore --> LockCache
    DefaultStore & FairStore --> Factory
    Factory --> MutexRepos & SemaphoreRepos
    
    ORMMutex & ORMSemaphore --> RDBCommon --> Postgres & MySQL & SQLite
    RedisMutex & RedisSemaphore & RedisFairSemaphore --> RedisCommon --> Redis
    DDBMutex & DDBSemaphore --> DDBCommon --> DynamoDB

    LockManagerImpl --> Metrics
    LockManagerImpl --> ErrorHandling
    Factory --> Options

    %% Click Events
    click LockManager "https://github.com/bhatti/db-locks/blob/main/src/manager/lock_manager.rs"
    click DefaultStore "https://github.com/bhatti/db-locks/blob/main/src/store/default_lock_store.rs"
    click FairStore "https://github.com/bhatti/db-locks/blob/main/src/store/fair_lock_store.rs"
    click LockCache "https://github.com/bhatti/db-locks/blob/main/src/store/lock_cache.rs"
    click Factory "https://github.com/bhatti/db-locks/blob/main/src/repository/factory.rs"
    click DDBMutex "https://github.com/bhatti/db-locks/blob/main/src/repository/ddb_mutex_repository.rs"
    click DDBSemaphore "https://github.com/bhatti/db-locks/blob/main/src/repository/ddb_semaphore_repository.rs"
    click RedisMutex "https://github.com/bhatti/db-locks/blob/main/src/repository/redis_mutex_repository.rs"
    click RedisSemaphore "https://github.com/bhatti/db-locks/blob/main/src/repository/redis_semaphore_repository.rs"
    click RedisFairSemaphore "https://github.com/bhatti/db-locks/blob/main/src/repository/redis_fair_semaphore_repository.rs"
    click ORMMutex "https://github.com/bhatti/db-locks/blob/main/src/repository/orm_mutex_repository.rs"
    click ORMSemaphore "https://github.com/bhatti/db-locks/blob/main/src/repository/orm_semaphore_repository.rs"
    click Models "https://github.com/bhatti/db-locks/blob/main/src/domain/models.rs"
    click Schema "https://github.com/bhatti/db-locks/blob/main/src/domain/schema.rs"
    click ErrorHandling "https://github.com/bhatti/db-locks/blob/main/src/domain/error.rs"
    click Options "https://github.com/bhatti/db-locks/blob/main/src/domain/options.rs"
    click Metrics "https://github.com/bhatti/db-locks/blob/main/src/utils/lock_metrics.rs"
    click DDBCommon "https://github.com/bhatti/db-locks/blob/main/src/repository/ddb_common.rs"
    click RedisCommon "https://github.com/bhatti/db-locks/blob/main/src/repository/redis_common.rs"
    click RDBCommon "https://github.com/bhatti/db-locks/blob/main/src/repository/rdb_common.rs"

    %% Styles
    classDef manager fill:#b3e0ff,stroke:#333,stroke-width:2px
    classDef store fill:#d1f2eb,stroke:#333,stroke-width:2px
    classDef repository fill:#d4efdf,stroke:#333,stroke-width:2px
    classDef domain fill:#f9e79f,stroke:#333,stroke-width:2px
    classDef storage fill:#f5b7b1,stroke:#333,stroke-width:2px
    classDef utils fill:#d2b4de,stroke:#333,stroke-width:2px
```
