Files
synq/server/app/store.py

39 lines
1.0 KiB
Python

import sqlite3
import threading
from pathlib import Path
_lock = threading.Lock()
def _connect(db_path: str) -> sqlite3.Connection:
conn = sqlite3.connect(db_path, check_same_thread=False)
conn.execute(
"CREATE TABLE IF NOT EXISTS seen_ids (id TEXT PRIMARY KEY, created_at TEXT NOT NULL)"
)
conn.commit()
return conn
class IdempotencyStore:
def __init__(self, db_path: str) -> None:
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
self._conn = _connect(db_path)
def already_seen(self, capture_id: str) -> bool:
row = self._conn.execute(
"SELECT 1 FROM seen_ids WHERE id = ?", (capture_id,)
).fetchone()
return row is not None
def mark_seen(self, capture_id: str, created_at: str) -> None:
self._conn.execute(
"INSERT OR IGNORE INTO seen_ids (id, created_at) VALUES (?, ?)",
(capture_id, created_at),
)
self._conn.commit()
def get_file_lock() -> threading.Lock:
return _lock