-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfingerprint_cache.py
More file actions
735 lines (688 loc) · 24 KB
/
fingerprint_cache.py
File metadata and controls
735 lines (688 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import json
import os
import queue
import sqlite3
import threading
import time
from typing import Callable, Dict, Optional
from utils.path_helpers import ensure_long_path
verbose: bool = True
def _dlog(label: str, msg: str, cb: Optional[Callable[[str], None]] = None) -> None:
if not verbose:
return
ts = time.strftime("%H:%M:%S")
line = f"{ts} [{label}] {msg}"
if cb:
cb(line)
else:
print(line)
_writer_lock = threading.Lock()
_writer: "FingerprintWriter | None" = None
_writer_db_path: str | None = None
def _initialize_db(conn: sqlite3.Connection) -> None:
conn.execute("PRAGMA journal_mode=WAL")
conn.execute(
"""
CREATE TABLE IF NOT EXISTS fingerprints (
path TEXT PRIMARY KEY,
mtime REAL,
size INTEGER,
duration INT,
fingerprint TEXT,
ext TEXT,
bitrate INT,
sample_rate INT,
bit_depth INT,
channels INT,
codec TEXT,
container TEXT,
tags_json TEXT,
artwork_json TEXT,
normalized_artist TEXT,
normalized_title TEXT,
normalized_album TEXT
);
"""
)
# --- Handle schema upgrades -------------------------------------------
cols = {row[1] for row in conn.execute("PRAGMA table_info(fingerprints)")}
if "mtime" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN mtime REAL")
if "size" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN size INTEGER")
if "duration" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN duration INT")
if "fingerprint" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN fingerprint TEXT")
if "ext" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN ext TEXT")
if "bitrate" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN bitrate INT")
if "sample_rate" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN sample_rate INT")
if "bit_depth" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN bit_depth INT")
if "channels" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN channels INT")
if "codec" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN codec TEXT")
if "container" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN container TEXT")
if "tags_json" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN tags_json TEXT")
if "artwork_json" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN artwork_json TEXT")
if "normalized_artist" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN normalized_artist TEXT")
if "normalized_title" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN normalized_title TEXT")
if "normalized_album" not in cols:
conn.execute("ALTER TABLE fingerprints ADD COLUMN normalized_album TEXT")
conn.commit()
class FingerprintWriter:
def __init__(
self,
db_path: str,
*,
batch_size: int = 50,
flush_interval: float = 1.0,
) -> None:
self._db_path = db_path
self._batch_size = batch_size
self._flush_interval = flush_interval
self._queue: "queue.Queue[tuple[str, object]]" = queue.Queue()
self._thread = threading.Thread(
target=self._run,
name="FingerprintCacheWriter",
daemon=True,
)
self._thread.start()
def enqueue(
self,
path: str,
mtime: float,
size: int,
duration: int | None,
fingerprint: str,
*,
ext: str | None = None,
bitrate: int | None = None,
sample_rate: int | None = None,
bit_depth: int | None = None,
channels: int | None = None,
codec: str | None = None,
container: str | None = None,
tags_json: str | None = None,
artwork_json: str | None = None,
normalized_artist: str | None = None,
normalized_title: str | None = None,
normalized_album: str | None = None,
) -> None:
self._queue.put(
(
"write",
(
path,
mtime,
size,
duration,
fingerprint,
ext,
bitrate,
sample_rate,
bit_depth,
channels,
codec,
container,
tags_json,
artwork_json,
normalized_artist,
normalized_title,
normalized_album,
),
)
)
def flush(self, timeout: float | None = None) -> None:
event = threading.Event()
self._queue.put(("flush", event))
event.wait(timeout=timeout)
def shutdown(self) -> None:
event = threading.Event()
self._queue.put(("shutdown", event))
self._thread.join()
event.wait(timeout=1.0)
def _run(self) -> None:
conn: sqlite3.Connection | None = None
try:
conn = sqlite3.connect(self._db_path, check_same_thread=False)
_initialize_db(conn)
pending: list[
tuple[
str,
float,
int,
int | None,
str,
str | None,
int | None,
int | None,
int | None,
int | None,
str | None,
str | None,
str | None,
str | None,
str | None,
str | None,
str | None,
]
] = []
last_flush = time.monotonic()
while True:
timeout = self._flush_interval - (time.monotonic() - last_flush)
if timeout < 0:
timeout = 0
try:
kind, payload = self._queue.get(timeout=timeout)
except queue.Empty:
kind = ""
payload = None
if kind == "write":
pending.append(payload) # type: ignore[arg-type]
if len(pending) >= self._batch_size:
self._flush_pending(conn, pending)
pending.clear()
last_flush = time.monotonic()
elif kind == "flush":
self._flush_pending(conn, pending)
pending.clear()
last_flush = time.monotonic()
if isinstance(payload, threading.Event):
payload.set()
elif kind == "shutdown":
self._flush_pending(conn, pending)
pending.clear()
self._drain_remaining(conn)
if isinstance(payload, threading.Event):
payload.set()
break
elif kind == "":
if pending:
self._flush_pending(conn, pending)
pending.clear()
last_flush = time.monotonic()
finally:
if conn is not None:
conn.close()
def _flush_pending(
self,
conn: sqlite3.Connection,
pending: list[
tuple[
str,
float,
int,
int | None,
str,
str | None,
int | None,
int | None,
int | None,
int | None,
str | None,
str | None,
str | None,
str | None,
str | None,
str | None,
str | None,
]
],
) -> None:
if not pending:
return
for attempt in range(3):
try:
conn.executemany(
"""
INSERT OR REPLACE INTO fingerprints (
path,
mtime,
size,
duration,
fingerprint,
ext,
bitrate,
sample_rate,
bit_depth,
channels,
codec,
container,
tags_json,
artwork_json,
normalized_artist,
normalized_title,
normalized_album
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
pending,
)
conn.commit()
return
except sqlite3.OperationalError as exc:
if "locked" not in str(exc).lower() or attempt >= 2:
_dlog("FP", f"cache write failed: {exc}")
return
time.sleep(0.05)
def _drain_remaining(self, conn: sqlite3.Connection) -> None:
pending: list[
tuple[
str,
float,
int,
int | None,
str,
str | None,
int | None,
int | None,
int | None,
int | None,
str | None,
str | None,
str | None,
str | None,
str | None,
str | None,
str | None,
]
] = []
while True:
try:
kind, payload = self._queue.get_nowait()
except queue.Empty:
break
if kind == "write":
pending.append(payload) # type: ignore[arg-type]
elif kind in {"flush", "shutdown"} and isinstance(payload, threading.Event):
payload.set()
if pending:
self._flush_pending(conn, pending)
def _get_writer(db_path: str) -> FingerprintWriter:
global _writer, _writer_db_path
with _writer_lock:
if _writer is None or _writer_db_path != db_path:
if _writer is not None:
_writer.shutdown()
os.makedirs(os.path.dirname(db_path), exist_ok=True)
_writer_db_path = db_path
_writer = FingerprintWriter(db_path)
return _writer
def _open_readonly_connection(db_path: str) -> sqlite3.Connection | None:
if not os.path.exists(db_path):
return None
conn = sqlite3.connect(db_path)
conn.execute("PRAGMA query_only = ON")
return conn
def _decode_fingerprint(fp: object) -> str | None:
if not isinstance(fp, (bytes, bytearray)):
return fp # type: ignore[return-value]
try:
return fp.decode("utf-8")
except Exception:
return fp.decode("latin1", errors="ignore")
def _ensure_db(db_path: str) -> sqlite3.Connection:
"""Legacy helper for callers that need a ready connection."""
os.makedirs(os.path.dirname(db_path), exist_ok=True)
conn = sqlite3.connect(db_path)
_initialize_db(conn)
return conn
def ensure_fingerprint_cache(db_path: str) -> None:
"""Ensure the fingerprint cache schema exists and is up to date."""
if not os.path.exists(db_path):
os.makedirs(os.path.dirname(db_path), exist_ok=True)
conn = sqlite3.connect(db_path)
try:
_initialize_db(conn)
finally:
conn.close()
def get_fingerprint(
path: str,
db_path: str,
compute_func: Callable[[str], tuple[int | None, str | None]],
log_callback: Optional[Callable[[str], None]] = None,
trace: Optional[Dict[str, object]] = None,
) -> Optional[str]:
"""Return fingerprint for path using cache; compute if missing."""
if log_callback is None:
log_callback = lambda msg: None
if trace is None:
trace = {}
path = ensure_long_path(path)
os.makedirs(os.path.dirname(db_path), exist_ok=True)
conn = _open_readonly_connection(db_path)
try:
mtime = os.path.getmtime(path)
size = os.path.getsize(path)
except OSError as e:
log_callback(f"! Could not stat {path}: {e}")
trace["source"] = "stat_error"
trace["error"] = str(e)
if conn is not None:
conn.close()
return None
row = None
if conn is not None:
row = conn.execute(
"SELECT mtime, size, fingerprint FROM fingerprints WHERE path=?",
(path,),
).fetchone()
cached_mtime = row[0] if row else None
cached_size = row[1] if row else None
if row and abs(cached_mtime - mtime) < 1e-6 and int(cached_size or 0) == int(size):
fp = row[2]
_dlog("FP", f"cache hit {path}", log_callback)
if conn is not None:
conn.close()
fp = _decode_fingerprint(fp)
if fp is None:
trace["source"] = "missing"
trace["error"] = "fingerprint unavailable"
return None
_dlog(
"FP",
f"fingerprint_prefix={fp[:16]} len={len(fp)}",
log_callback,
)
trace["source"] = "cache"
trace["error"] = ""
return fp
if row:
_dlog(
"FP",
f"cache invalidated {path} stored_mtime={cached_mtime} stored_size={cached_size} new_mtime={mtime} new_size={size}",
log_callback,
)
_dlog("FP", f"cache miss {path}", log_callback)
duration, fp_hash = compute_func(path)
if fp_hash is not None:
_dlog(
"FP",
f"computed fingerprint prefix={fp_hash[:16]} len={len(fp_hash)}",
log_callback,
)
_get_writer(db_path).enqueue(path, mtime, size, duration, fp_hash)
if conn is not None:
conn.close()
if fp_hash:
trace["source"] = "computed"
trace["error"] = ""
else:
trace["source"] = "missing"
trace["error"] = "fingerprint unavailable"
return fp_hash
def get_cached_fingerprint(
path: str,
db_path: str,
log_callback: Optional[Callable[[str], None]] = None,
trace: Optional[Dict[str, object]] = None,
*,
retries: int = 3,
retry_delay: float = 0.05,
) -> Optional[str]:
"""Return cached fingerprint for path without computing new fingerprints."""
if log_callback is None:
log_callback = lambda msg: None
if trace is None:
trace = {}
path = ensure_long_path(path)
os.makedirs(os.path.dirname(db_path), exist_ok=True)
for attempt in range(retries):
conn: sqlite3.Connection | None = None
try:
conn = _open_readonly_connection(db_path)
if conn is None:
trace["source"] = "missing"
trace["error"] = ""
return None
try:
mtime = os.path.getmtime(path)
size = os.path.getsize(path)
except OSError as e:
log_callback(f"! Could not stat {path}: {e}")
trace["source"] = "stat_error"
trace["error"] = str(e)
return None
row = conn.execute(
"SELECT mtime, size, fingerprint FROM fingerprints WHERE path=?",
(path,),
).fetchone()
cached_mtime = row[0] if row else None
cached_size = row[1] if row else None
if row and abs(cached_mtime - mtime) < 1e-6 and int(cached_size or 0) == int(size):
fp = row[2]
_dlog("FP", f"cache hit {path}", log_callback)
fp = _decode_fingerprint(fp)
if fp is None:
trace["source"] = "missing"
trace["error"] = "fingerprint unavailable"
return None
_dlog(
"FP",
f"fingerprint_prefix={fp[:16]} len={len(fp)}",
log_callback,
)
trace["source"] = "cache"
trace["error"] = ""
return fp
trace["source"] = "missing"
trace["error"] = ""
return None
except sqlite3.OperationalError as e:
if "locked" not in str(e).lower() or attempt >= retries - 1:
log_callback(f"! Fingerprint cache read failed: {e}")
trace["source"] = "cache_error"
trace["error"] = str(e)
return None
time.sleep(retry_delay)
finally:
if conn is not None:
conn.close()
return None
def get_cached_fingerprint_metadata(
path: str,
db_path: str,
log_callback: Optional[Callable[[str], None]] = None,
trace: Optional[Dict[str, object]] = None,
*,
retries: int = 3,
retry_delay: float = 0.05,
) -> tuple[str | None, dict[str, object] | None]:
"""Return cached fingerprint and metadata for path without computing new fingerprints."""
if log_callback is None:
log_callback = lambda msg: None
if trace is None:
trace = {}
def _load_json(payload: str | None) -> object | None:
if not payload:
return None
try:
return json.loads(payload)
except Exception:
return None
path = ensure_long_path(path)
os.makedirs(os.path.dirname(db_path), exist_ok=True)
for attempt in range(retries):
conn: sqlite3.Connection | None = None
try:
conn = _open_readonly_connection(db_path)
if conn is None:
trace["source"] = "missing"
trace["error"] = ""
return None, None
try:
mtime = os.path.getmtime(path)
size = os.path.getsize(path)
except OSError as e:
log_callback(f"! Could not stat {path}: {e}")
trace["source"] = "stat_error"
trace["error"] = str(e)
return None, None
row = conn.execute(
"""
SELECT
mtime,
size,
fingerprint,
ext,
bitrate,
sample_rate,
bit_depth,
channels,
codec,
container,
tags_json,
artwork_json,
normalized_artist,
normalized_title,
normalized_album
FROM fingerprints
WHERE path=?
""",
(path,),
).fetchone()
cached_mtime = row[0] if row else None
cached_size = row[1] if row else None
if row and abs(cached_mtime - mtime) < 1e-6 and int(cached_size or 0) == int(size):
fp = _decode_fingerprint(row[2])
_dlog("FP", f"cache hit {path}", log_callback)
if fp is None:
trace["source"] = "missing"
trace["error"] = "fingerprint unavailable"
return None, None
_dlog(
"FP",
f"fingerprint_prefix={fp[:16]} len={len(fp)}",
log_callback,
)
trace["source"] = "cache"
trace["error"] = ""
metadata = {
"ext": row[3],
"bitrate": row[4],
"sample_rate": row[5],
"bit_depth": row[6],
"channels": row[7],
"codec": row[8],
"container": row[9],
"tags": _load_json(row[10]),
"artwork": _load_json(row[11]),
"normalized_artist": row[12],
"normalized_title": row[13],
"normalized_album": row[14],
}
return fp, metadata
trace["source"] = "missing"
trace["error"] = ""
return None, None
except sqlite3.OperationalError as e:
if "locked" not in str(e).lower() or attempt >= retries - 1:
log_callback(f"! Fingerprint cache read failed: {e}")
trace["source"] = "cache_error"
trace["error"] = str(e)
return None, None
time.sleep(retry_delay)
finally:
if conn is not None:
conn.close()
return None, None
def store_fingerprint(
path: str,
db_path: str,
duration: int | None,
fingerprint: str | None,
log_callback: Optional[Callable[[str], None]] = None,
*,
ext: str | None = None,
bitrate: int | None = None,
sample_rate: int | None = None,
bit_depth: int | None = None,
channels: int | None = None,
codec: str | None = None,
container: str | None = None,
tags: dict[str, object] | None = None,
artwork: list[dict[str, object]] | None = None,
normalized_artist: str | None = None,
normalized_title: str | None = None,
normalized_album: str | None = None,
retries: int = 3,
retry_delay: float = 0.05,
flush: bool = False,
) -> bool:
"""Persist a fingerprint in the cache without computing it."""
if fingerprint is None:
return False
if log_callback is None:
log_callback = lambda msg: None
path = ensure_long_path(path)
os.makedirs(os.path.dirname(db_path), exist_ok=True)
for attempt in range(retries):
try:
try:
mtime = os.path.getmtime(path)
size = os.path.getsize(path)
except OSError as e:
log_callback(f"! Could not stat {path}: {e}")
return False
_get_writer(db_path).enqueue(
path,
mtime,
size,
duration,
fingerprint,
ext=ext,
bitrate=bitrate,
sample_rate=sample_rate,
bit_depth=bit_depth,
channels=channels,
codec=codec,
container=container,
tags_json=json.dumps(tags) if tags is not None else None,
artwork_json=json.dumps(artwork) if artwork is not None else None,
normalized_artist=normalized_artist,
normalized_title=normalized_title,
normalized_album=normalized_album,
)
if flush:
flush_fingerprint_writes(db_path)
return True
except sqlite3.OperationalError as e:
if "locked" not in str(e).lower() or attempt >= retries - 1:
log_callback(f"! Fingerprint cache write failed: {e}")
return False
time.sleep(retry_delay)
return False
def flush_fingerprint_writes(db_path: str, timeout: float | None = None) -> None:
"""Force a flush of queued fingerprint writes for immediate persistence."""
if not os.path.exists(db_path):
return
_get_writer(db_path).flush(timeout=timeout)
def shutdown_fingerprint_writer() -> None:
"""Drain queued fingerprint writes; call during application shutdown (e.g., exit handler)."""
global _writer, _writer_db_path
with _writer_lock:
if _writer is None:
return
_writer.shutdown()
_writer = None
_writer_db_path = None
def flush_cache(db_path: str) -> None:
if not os.path.exists(db_path):
return
shutdown_fingerprint_writer()
try:
os.remove(db_path)
except Exception:
conn = sqlite3.connect(db_path)
conn.execute("DROP TABLE IF EXISTS fingerprints")
conn.commit()
conn.close()