# ER図

本ドキュメントは、libuvの内部データ構造間の関係をER図（Entity-Relationship Diagram）としてmermaid形式で表現したものである。

libuvは外部データベースを使用しないため、これは「インメモリデータモデルの構造関係図」として位置づけられる。

## 全体構造図

```mermaid
erDiagram
    uv_loop_s {
        void_ptr data
        unsigned_int active_handles
        uv__queue handle_queue
        unsigned_int active_reqs_count
        void_ptr internal_fields
        unsigned_int stop_flag
        unsigned_long flags
        int backend_fd
        uv__queue pending_queue
        uv__queue watcher_queue
        unsigned_int nwatchers
        unsigned_int nfds
        uv__queue wq
        uint64_t timer_counter
        uint64_t time
    }

    uv_handle_s {
        void_ptr data
        uv_loop_ptr loop
        uv_handle_type type
        uv_close_cb close_cb
        uv__queue handle_queue
        int fd
        unsigned_int flags
    }

    uv_stream_s {
        size_t write_queue_size
        uv_alloc_cb alloc_cb
        uv_read_cb read_cb
        uv__io_t io_watcher
        uv__queue write_queue
        uv__queue write_completed_queue
        uv_connection_cb connection_cb
        int delayed_error
        int accepted_fd
    }

    uv_tcp_s {
        string inherits_uv_stream_s
    }

    uv_udp_s {
        size_t send_queue_size
        size_t send_queue_count
        uv_alloc_cb alloc_cb
        uv_udp_recv_cb recv_cb
        uv__io_t io_watcher
        uv__queue write_queue
    }

    uv_pipe_s {
        int ipc
        const_char_ptr pipe_fname
    }

    uv_tty_s {
        struct_termios orig_termios
        int mode
    }

    uv_poll_s {
        uv_poll_cb poll_cb
        uv__io_t io_watcher
    }

    uv_timer_s {
        uv_timer_cb timer_cb
        void_ptr_3 heap_node
        uint64_t timeout
        uint64_t repeat
        uint64_t start_id
    }

    uv_prepare_s {
        uv_prepare_cb prepare_cb
        uv__queue queue
    }

    uv_check_s {
        uv_check_cb check_cb
        uv__queue queue
    }

    uv_idle_s {
        uv_idle_cb idle_cb
        uv__queue queue
    }

    uv_async_s {
        uv_async_cb async_cb
        uv__queue queue
        int pending
    }

    uv_process_s {
        uv_exit_cb exit_cb
        int pid
        uv__queue queue
        int status
    }

    uv_fs_event_s {
        char_ptr path
        uv_fs_event_cb cb
    }

    uv_fs_poll_s {
        void_ptr poll_ctx
    }

    uv_signal_s {
        uv_signal_cb signal_cb
        int signum
        int rbe_color
        unsigned_int caught_signals
        unsigned_int dispatched_signals
    }

    uv_req_s {
        void_ptr data
        uv_req_type type
        void_ptr_6 reserved
    }

    uv_write_s {
        uv_write_cb cb
        uv_stream_ptr send_handle
        uv_stream_ptr handle
        uv__queue queue
        unsigned_int write_index
        unsigned_int nbufs
        int error
    }

    uv_connect_s {
        uv_connect_cb cb
        uv_stream_ptr handle
        uv__queue queue
    }

    uv_shutdown_s {
        uv_stream_ptr handle
        uv_shutdown_cb cb
    }

    uv_udp_send_s {
        uv_udp_ptr handle
        uv_udp_send_cb cb
        uv__queue queue
        unsigned_int nbufs
        ssize_t status
    }

    uv_fs_s {
        uv_fs_type fs_type
        uv_loop_ptr loop
        uv_fs_cb cb
        ssize_t result
        void_ptr ptr
        const_char_ptr path
        uv_stat_t statbuf
    }

    uv_work_s {
        uv_loop_ptr loop
        uv_work_cb work_cb
        uv_after_work_cb after_work_cb
    }

    uv_getaddrinfo_s {
        uv_loop_ptr loop
        uv_getaddrinfo_cb cb
        char_ptr hostname
        char_ptr service
        int retcode
    }

    uv_getnameinfo_s {
        uv_loop_ptr loop
        uv_getnameinfo_cb getnameinfo_cb
        char_1025 host
        char_32 service
        int retcode
    }

    uv_random_s {
        uv_loop_ptr loop
        int status
        void_ptr buf
        size_t buflen
        uv_random_cb cb
    }

    heap {
        heap_node_ptr min
        unsigned_int nelts
    }

    heap_node {
        heap_node_ptr left
        heap_node_ptr right
        heap_node_ptr parent
    }

    uv__queue {
        uv__queue_ptr next
        uv__queue_ptr prev
    }

    uv__io_s {
        uintptr_t bits
        uv__queue pending_queue
        uv__queue watcher_queue
        unsigned_int pevents
        unsigned_int events
        int fd
    }

    uv_metrics_s {
        uint64_t loop_count
        uint64_t events
        uint64_t events_waiting
    }

    uv__loop_internal_fields_s {
        unsigned_int flags
        uv__loop_metrics_t loop_metrics
        int current_timeout
    }

    %% イベントループとハンドルの関係
    uv_loop_s ||--o{ uv_handle_s : "manages"
    uv_loop_s ||--o{ uv_req_s : "tracks"
    uv_loop_s ||--|{ uv__queue : "uses queues"
    uv_loop_s ||--|| heap : "timer_heap"
    uv_loop_s ||--|| uv__loop_internal_fields_s : "internal_fields"
    uv_loop_s ||--|| uv_metrics_s : "metrics via internal_fields"

    %% ハンドル継承階層
    uv_handle_s ||--|| uv_stream_s : "extends"
    uv_handle_s ||--|| uv_udp_s : "extends"
    uv_handle_s ||--|| uv_poll_s : "extends"
    uv_handle_s ||--|| uv_timer_s : "extends"
    uv_handle_s ||--|| uv_prepare_s : "extends"
    uv_handle_s ||--|| uv_check_s : "extends"
    uv_handle_s ||--|| uv_idle_s : "extends"
    uv_handle_s ||--|| uv_async_s : "extends"
    uv_handle_s ||--|| uv_process_s : "extends"
    uv_handle_s ||--|| uv_fs_event_s : "extends"
    uv_handle_s ||--|| uv_fs_poll_s : "extends"
    uv_handle_s ||--|| uv_signal_s : "extends"

    uv_stream_s ||--|| uv_tcp_s : "extends"
    uv_stream_s ||--|| uv_pipe_s : "extends"
    uv_stream_s ||--|| uv_tty_s : "extends"

    %% リクエスト継承階層
    uv_req_s ||--|| uv_write_s : "extends"
    uv_req_s ||--|| uv_connect_s : "extends"
    uv_req_s ||--|| uv_shutdown_s : "extends"
    uv_req_s ||--|| uv_udp_send_s : "extends"
    uv_req_s ||--|| uv_fs_s : "extends"
    uv_req_s ||--|| uv_work_s : "extends"
    uv_req_s ||--|| uv_getaddrinfo_s : "extends"
    uv_req_s ||--|| uv_getnameinfo_s : "extends"
    uv_req_s ||--|| uv_random_s : "extends"

    %% リクエストとハンドルの関係
    uv_write_s }o--|| uv_stream_s : "writes to"
    uv_connect_s }o--|| uv_stream_s : "connects"
    uv_shutdown_s }o--|| uv_stream_s : "shuts down"
    uv_udp_send_s }o--|| uv_udp_s : "sends via"

    %% ヒープとタイマーの関係
    heap ||--o{ heap_node : "contains"
    uv_timer_s ||--|| heap_node : "managed by"

    %% I/Oウォッチャーの関係
    uv_stream_s ||--|| uv__io_s : "has io_watcher"
    uv_udp_s ||--|| uv__io_s : "has io_watcher"
    uv_poll_s ||--|| uv__io_s : "has io_watcher"
    uv_loop_s ||--o{ uv__io_s : "watchers array"
```

## ハンドル継承階層図

```mermaid
graph TD
    A[uv_handle_s<br/>全ハンドルの基底] --> B[uv_stream_s<br/>ストリーム基底]
    A --> C[uv_udp_s<br/>UDP]
    A --> D[uv_poll_s<br/>Poll]
    A --> E[uv_timer_s<br/>タイマー]
    A --> F[uv_prepare_s<br/>Prepare]
    A --> G[uv_check_s<br/>Check]
    A --> H[uv_idle_s<br/>Idle]
    A --> I[uv_async_s<br/>Async]
    A --> J[uv_process_s<br/>プロセス]
    A --> K[uv_fs_event_s<br/>FSイベント]
    A --> L[uv_fs_poll_s<br/>FSポーリング]
    A --> M[uv_signal_s<br/>シグナル]

    B --> N[uv_tcp_s<br/>TCP]
    B --> O[uv_pipe_s<br/>パイプ]
    B --> P[uv_tty_s<br/>TTY]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:2px
```

## リクエスト継承階層図

```mermaid
graph TD
    A[uv_req_s<br/>全リクエストの基底] --> B[uv_write_s<br/>書き込み]
    A --> C[uv_connect_s<br/>接続]
    A --> D[uv_shutdown_s<br/>シャットダウン]
    A --> E[uv_udp_send_s<br/>UDP送信]
    A --> F[uv_fs_s<br/>ファイルシステム]
    A --> G[uv_work_s<br/>スレッドプールワーク]
    A --> H[uv_getaddrinfo_s<br/>DNS解決]
    A --> I[uv_getnameinfo_s<br/>逆引きDNS]
    A --> J[uv_random_s<br/>乱数生成]

    style A fill:#f9f,stroke:#333,stroke-width:2px
```

## イベントループのデータ構造関係図

```mermaid
graph LR
    LOOP[uv_loop_s<br/>イベントループ] --> HQ[handle_queue<br/>uv__queue]
    LOOP --> PQ[pending_queue<br/>uv__queue]
    LOOP --> WQ[watcher_queue<br/>uv__queue]
    LOOP --> TH[timer_heap<br/>heap]
    LOOP --> IF[internal_fields<br/>uv__loop_internal_fields_s]
    LOOP --> CH[closing_handles<br/>リンクリスト]
    LOOP --> PH[process_handles<br/>uv__queue]
    LOOP --> PRH[prepare_handles<br/>uv__queue]
    LOOP --> CKH[check_handles<br/>uv__queue]
    LOOP --> IDH[idle_handles<br/>uv__queue]
    LOOP --> AH[async_handles<br/>uv__queue]

    HQ --> H1[uv_handle_s]
    HQ --> H2[uv_handle_s]
    HQ --> H3[uv_handle_s ...]

    TH --> TN1[heap_node<br/>uv_timer_s]
    TH --> TN2[heap_node<br/>uv_timer_s]

    IF --> LM[uv__loop_metrics_s]
    LM --> M[uv_metrics_s]

    style LOOP fill:#ffd,stroke:#333,stroke-width:2px
    style TH fill:#dff,stroke:#333,stroke-width:2px
    style HQ fill:#fdf,stroke:#333,stroke-width:2px
```
