Coverage for downtify/track_owners.py: 99%
123 statements
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
1"""Who put each library file on the server.
3A ``user`` only sees the tracks they asked to download; an ``admin``
4sees everything (see :mod:`downtify.users`). Files are never duplicated:
5when two users download the same track it lands on disk once (the
6existing dedup in :func:`downtify.track_index.resolve_existing_download`
7already guarantees that) and every downloader gets an ownership row
8here, keyed by the file's content key (basename + size, stable across
9folder moves - see :mod:`downtify.library_cache_keys`).
11The key changes when a file is rewritten in place (a re-download with
12different bytes, a tags/artwork upgrade, an audio replace). Rows heal
13themselves: every record carries the file's library path, so recording
14an owner re-points stale rows at the same path to the fresh key, and
15:func:`TrackOwners.rekey` does the same for flows that rewrite files
16without downloading.
18Files with no owners at all are pre-feature, monitor, reconcile or
19externally added files: admins see them, users don't. Removing the last
20owner is done only through the delete endpoints, which also take the
21file off disk then - nothing is ever garbage-collected behind anyone's
22back.
23"""
25from __future__ import annotations
27import contextlib
28import threading
29from datetime import datetime, timezone
30from pathlib import Path
31from typing import Collection
33from .db.config import Dialect
34from .db.core import Connection, Database, as_database
35from .db.dialect import insertion_order_col
38def _now_iso() -> str:
39 return datetime.now(timezone.utc).isoformat()
42def _repoint_rows(
43 conn: Connection,
44 rows: list[tuple[str, int]],
45 new_key: str,
46 path: str,
47) -> int:
48 """Move ``(old_key, user_id)`` rows onto *new_key*.
50 A row whose user already owns *new_key* (or an earlier row of the same
51 user in this batch) is an exact duplicate and is dropped instead, so
52 the primary key never collides. Returns how many rows moved.
53 """
55 owned = {
56 int(row['user_id'])
57 for row in conn.execute(
58 'SELECT user_id FROM track_owners WHERE content_key = ?',
59 (new_key,),
60 ).fetchall()
61 }
62 moved = 0
63 for old_key, user_id in rows:
64 if user_id in owned:
65 conn.execute(
66 'DELETE FROM track_owners WHERE content_key = ? '
67 'AND user_id = ?',
68 (old_key, user_id),
69 )
70 continue
71 owned.add(user_id)
72 if path:
73 conn.execute(
74 'UPDATE track_owners SET content_key = ?, stored_path = ? '
75 'WHERE content_key = ? AND user_id = ?',
76 (new_key, path, old_key, user_id),
77 )
78 else:
79 conn.execute(
80 'UPDATE track_owners SET content_key = ? '
81 'WHERE content_key = ? AND user_id = ?',
82 (new_key, old_key, user_id),
83 )
84 moved += 1
85 return moved
88class TrackOwners:
89 """``(content_key, user_id)`` rows in ``downtify_library.db``."""
91 def __init__(self, path_or_db: str | Path | Database) -> None:
92 self.db = as_database(path_or_db, 'library')
93 if isinstance(path_or_db, Database):
94 self._path = str(self.db.path) if self.db.path else None
95 else:
96 self._path = str(path_or_db)
97 self._lock = threading.Lock()
98 self._init_db()
100 def _connect(self) -> contextlib.AbstractContextManager[Connection]:
101 return self.db.connect()
103 def _init_db(self) -> None:
104 # PostgreSQL gets its schema from the migrations.
105 if self.db.dialect is not Dialect.SQLITE:
106 return
107 with self._connect() as conn:
108 conn.execute("""
109 CREATE TABLE IF NOT EXISTS track_owners (
110 content_key TEXT NOT NULL,
111 user_id INTEGER NOT NULL,
112 added_at TEXT NOT NULL,
113 stored_path TEXT NOT NULL DEFAULT '',
114 PRIMARY KEY (content_key, user_id)
115 )
116 """)
117 columns = {
118 row[1]
119 for row in conn.execute(
120 'PRAGMA table_info(track_owners)'
121 ).fetchall()
122 }
123 if 'stored_path' not in columns:
124 conn.execute(
125 'ALTER TABLE track_owners ADD COLUMN stored_path TEXT '
126 "NOT NULL DEFAULT ''"
127 )
128 conn.execute("""
129 CREATE INDEX IF NOT EXISTS idx_track_owners_user
130 ON track_owners (user_id)
131 """)
132 conn.execute("""
133 CREATE INDEX IF NOT EXISTS idx_track_owners_path
134 ON track_owners (stored_path)
135 """)
137 @staticmethod
138 def _norm_path(stored_path: str) -> str:
139 return str(stored_path or '').strip().replace('\\', '/')
141 def _order_col(self) -> str:
142 return insertion_order_col(self.db.dialect)
144 def add(
145 self, content_key: str, user_id: int, stored_path: str = ''
146 ) -> bool:
147 """Record *user_id* as an owner. ``True`` when it is new.
149 Stale rows at the same path (the file was rewritten since) are
150 re-pointed at the fresh key along the way, so an upgrade or a
151 re-download never orphans anyone's ownership.
152 """
154 key = str(content_key or '').strip()
155 if not key or not user_id:
156 return False
157 path = self._norm_path(stored_path)
158 with self._lock, self._connect() as conn:
159 cur = conn.execute(
160 """INSERT INTO track_owners
161 (content_key, user_id, added_at, stored_path)
162 VALUES (?, ?, ?, ?)
163 ON CONFLICT(content_key, user_id) DO NOTHING""",
164 (key, int(user_id), _now_iso(), path),
165 )
166 new = cur.rowcount > 0
167 if path:
168 conn.execute(
169 'UPDATE track_owners SET stored_path = ? '
170 'WHERE content_key = ? AND user_id = ?',
171 (path, key, int(user_id)),
172 )
173 self._heal_path(conn, path, key)
174 return new
176 def _heal_path(self, conn: Connection, path: str, key: str) -> None:
177 """Re-point rows at *path* whose key went stale, dropping the
178 exact duplicates left behind."""
180 rows = conn.execute(
181 'SELECT content_key, user_id FROM track_owners '
182 'WHERE stored_path = ? AND content_key != ? '
183 f'ORDER BY {self._order_col()}',
184 (path, key),
185 ).fetchall()
186 _repoint_rows(
187 conn,
188 [(str(r['content_key']), int(r['user_id'])) for r in rows],
189 key,
190 path,
191 )
193 def rekey(self, old_key: str, new_key: str, stored_path: str = '') -> int:
194 """Point rows at a rewritten file (upgrade/replace in place).
196 Returns how many rows moved. Exact duplicates left behind (the
197 same user already owned the fresh key) are dropped.
198 """
200 old = str(old_key or '').strip()
201 new = str(new_key or '').strip()
202 if not old or not new or old == new:
203 return 0
204 path = self._norm_path(stored_path)
205 with self._lock, self._connect() as conn:
206 rows = conn.execute(
207 'SELECT content_key, user_id FROM track_owners '
208 f'WHERE content_key = ? ORDER BY {self._order_col()}',
209 (old,),
210 ).fetchall()
211 return _repoint_rows(
212 conn,
213 [(str(r['content_key']), int(r['user_id'])) for r in rows],
214 new,
215 path,
216 )
218 def remove(self, content_key: str, user_id: int) -> int:
219 """Drop one owner's row; how many owners remain for the file."""
221 key = str(content_key or '').strip()
222 if not key or not user_id:
223 return self.count(key)
224 with self._lock, self._connect() as conn:
225 conn.execute(
226 'DELETE FROM track_owners WHERE content_key = ? '
227 'AND user_id = ?',
228 (key, int(user_id)),
229 )
230 row = conn.execute(
231 'SELECT COUNT(*) AS n FROM track_owners WHERE content_key = ?',
232 (key,),
233 ).fetchone()
234 return int(row['n'])
236 def drop_file(self, content_key: str) -> None:
237 """Forget every owner (the file itself is already gone)."""
239 key = str(content_key or '').strip()
240 if not key:
241 return
242 with self._lock, self._connect() as conn:
243 conn.execute(
244 'DELETE FROM track_owners WHERE content_key = ?', (key,)
245 )
247 def count(self, content_key: str) -> int:
248 with self._connect() as conn:
249 row = conn.execute(
250 'SELECT COUNT(*) AS n FROM track_owners WHERE content_key = ?',
251 (str(content_key or '').strip(),),
252 ).fetchone()
253 return int(row['n'])
255 def is_empty(self) -> bool:
256 """Whether no ownership was recorded yet (first start)."""
258 with self._connect() as conn:
259 row = conn.execute('SELECT 1 FROM track_owners LIMIT 1').fetchone()
260 return row is None
262 def owners(self, content_key: str) -> list[int]:
263 """Every owner's user id, oldest first."""
265 with self._connect() as conn:
266 rows = conn.execute(
267 'SELECT user_id FROM track_owners WHERE content_key = ? '
268 f'ORDER BY added_at, {self._order_col()}',
269 (str(content_key or '').strip(),),
270 ).fetchall()
271 return [int(row['user_id']) for row in rows]
273 def owned_keys(self, user_id: int) -> set[str]:
274 """Every content key *user_id* owns."""
276 with self._connect() as conn:
277 rows = conn.execute(
278 'SELECT content_key FROM track_owners WHERE user_id = ?',
279 (int(user_id),),
280 ).fetchall()
281 return {str(row['content_key']) for row in rows}
283 def count_by_user(self) -> dict[int, int]:
284 """How many ownership rows each account has (download history)."""
286 with self._connect() as conn:
287 rows = conn.execute(
288 'SELECT user_id, COUNT(*) AS n FROM track_owners '
289 'GROUP BY user_id'
290 ).fetchall()
291 return {int(row['user_id']): int(row['n']) for row in rows}
293 def remove_user(self, user_id: int) -> int:
294 """Drop every row of a deleted account; how many went."""
296 with self._lock, self._connect() as conn:
297 cur = conn.execute(
298 'DELETE FROM track_owners WHERE user_id = ?',
299 (int(user_id),),
300 )
301 return cur.rowcount
303 def owned_subset(
304 self, content_keys: Collection[str], user_id: int
305 ) -> set[str]:
306 """Which of *content_keys* *user_id* owns (one query, chunked)."""
308 wanted = [str(k or '').strip() for k in content_keys if k]
309 if not wanted or not user_id:
310 return set()
311 found: set[str] = set()
312 with self._connect() as conn:
313 for start in range(0, len(wanted), 500):
314 chunk = wanted[start : start + 500]
315 marks = ','.join('?' * len(chunk))
316 for row in conn.execute(
317 'SELECT content_key FROM track_owners '
318 f'WHERE user_id = ? AND content_key IN ({marks})',
319 (int(user_id), *chunk),
320 ).fetchall():
321 found.add(str(row['content_key']))
322 return found