"""SQLite write serialization for async context"""

import asyncio
from typing import Callable, Any


class DBPool:
    """Serializes SQLite writes via asyncio.Lock. Reads are concurrent."""

    def __init__(self):
        self._write_lock = asyncio.Lock()

    async def execute_write(self, fn: Callable, *args, **kwargs) -> Any:
        async with self._write_lock:
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(None, lambda: fn(*args, **kwargs))

    async def execute_read(self, fn: Callable, *args, **kwargs) -> Any:
        loop = asyncio.get_event_loop()
        return await loop.run_in_executor(None, lambda: fn(*args, **kwargs))
