comicbox.formats.base.online.series_cache ¶
Thread-safe series cache for series-first batching (plan §3.10).
Maps (source_name, series_fingerprint) to the resolved upstream volume/series id, so the second and later issues of a series skip the cold-path candidate search and ask the source directly for “issue N in volume V”.
The population rule is FIRST WRITER WINS: a falsy-group collision (two unrelated comics that happened to share a fingerprint) must never overwrite an already-resolved entry. Expressing that as if key in cache: return followed by cache[key] = value is fine for the sequential caller (OnlineSession.tag_many) but races once the CLI’s -j N thread pool drives it: two workers can both pass the membership test and both claim to be the first writer, emitting two SeriesIdentified events for one series.
SeriesCache closes that by making the claim a single locked operation. It stays a plain MutableMapping so callers that just want a dict — and the tests that pass one — keep working unchanged.
SeriesCache ¶
Bases: MutableMapping[SeriesCacheKey, int]
A dict of resolved volume ids with an atomic first-writer claim.
Also arbitrates SINGLE-FLIGHT resolution of a series. Sorting a batch by series fingerprint makes the cache hit for the second and later issues of a run — but only once the first one has finished, and a -j N pool starts the first N files of a cluster at the same instant. All N then miss, and all N pay for the cold search: the batching saves nothing for exactly the files it was added for.
So one caller per key LEADS and the rest WAIT for it, then re-read the cache and take the cheap volume-scoped path. A leader that resolves nothing (no match, prompt declined, source failure) hands leadership to the next waiter rather than stranding the cluster.
Source code in comicbox/formats/base/online/series_cache.py
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 | |
__contains__(key) ¶
Membership test.
Source code in comicbox/formats/base/online/series_cache.py
187 188 189 190 191 | |
__delitem__(key) ¶
Drop key.
Source code in comicbox/formats/base/online/series_cache.py
170 171 172 173 174 | |
__getitem__(key) ¶
Return the resolved volume id for key.
Source code in comicbox/formats/base/online/series_cache.py
158 159 160 161 162 | |
__init__() ¶
Start empty.
Source code in comicbox/formats/base/online/series_cache.py
94 95 96 97 98 99 100 | |
__iter__() ¶
Iterate a snapshot, so mutation during iteration is safe.
Source code in comicbox/formats/base/online/series_cache.py
176 177 178 179 | |
__len__() ¶
Count the resolved series.
Source code in comicbox/formats/base/online/series_cache.py
181 182 183 184 185 | |
__setitem__(key, value) ¶
Set key, overwriting. Prefer claim for first-writer-wins.
Source code in comicbox/formats/base/online/series_cache.py
164 165 166 167 168 | |
claim(key, volume_id) ¶
Populate key and return True only if this caller was the first.
The membership test and the insert happen under one lock, so exactly one concurrent caller is told it won — which is what the SeriesIdentified event and the first-writer-wins contract both rely on.
Source code in comicbox/formats/base/online/series_cache.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
lead(key) ¶
Claim the right to resolve key; True when this caller must do it.
False means another caller is already resolving it and this one should wait. Resolved keys also return False — there is nothing left to lead. A leader MUST call release in a finally, or its waiters hold until their timeout.
Source code in comicbox/formats/base/online/series_cache.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
release(key) ¶
Finish leading key and wake everyone waiting on it.
Source code in comicbox/formats/base/online/series_cache.py
146 147 148 149 150 151 | |
snapshot() ¶
Return a consistent copy. Useful for persistence.
Source code in comicbox/formats/base/online/series_cache.py
153 154 155 156 | |
wait(key, timeout) ¶
Block until the current leader of key releases, or timeout.
Returns True when a leader released within the timeout. Either way the caller must re-read the cache: a leader can finish without resolving anything.
Source code in comicbox/formats/base/online/series_cache.py
132 133 134 135 136 137 138 139 140 141 142 143 144 | |
await_series_leader(cache, key, timeout=SERIES_LEAD_TIMEOUT_S) ¶
Block while another caller resolves key, if one is.
Returns as soon as there is no leader to wait for, which is also the plain-dict case. The caller re-reads the cache either way: a leader can finish without having resolved anything.
Source code in comicbox/formats/base/online/series_cache.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
claim_series(cache, key, volume_id) ¶
First-writer-wins population that works on any MutableMapping.
Uses SeriesCache.claim when the mapping provides it (the concurrent case) and falls back to check-then-set for a plain dict, which is all a single-threaded caller needs.
Source code in comicbox/formats/base/online/series_cache.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
filename_series_fingerprint(path) ¶
Lightweight series fingerprint derived from the filename alone.
Lets a batch dispatcher cluster same-series comics together before opening any archives, so the first issue of each cluster pays for the cold-path search and the rest read the resolved volume id back out of the cache. Both dispatchers use it: OnlineSession.tag_many and the CLI’s Runner.
Falls back to the bare filename when comicfn2dict can’t extract a series — those files won’t benefit from batching but won’t break it either (they sort to a deterministic “by filename” bucket).
Deliberately mirrors _series_fingerprint’s component choice (see comicbox/box/online_lookup.py): the per-issue year is excluded, because clustering by it would scatter a multi-year run into one cluster per year and hand the cache a fresh cold path for each — the opposite of what the clustering is for. The volume ordinal is kept, since that is what separates reboots.
Source code in comicbox/formats/base/online/series_cache.py
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 | |
lead_series(cache, key) ¶
Claim the right to resolve key, on any MutableMapping.
False for a mapping without the single-flight API — a plain dict from a sequential caller, which has nothing to race. A True caller MUST pair this with release_series in a finally.
Source code in comicbox/formats/base/online/series_cache.py
206 207 208 209 210 211 212 213 214 215 216 217 | |
release_series(cache, key) ¶
Hand leadership of key back, on any MutableMapping.
Source code in comicbox/formats/base/online/series_cache.py
220 221 222 223 224 225 226 | |