Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(catalog): 独立筛选顶部聚合的作品集合

longmeidao4e8a63378733
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c42dbaee..a58c14a0c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ Removed / Fixed / Security。
### 修复
+- **馆藏**:修复部分数据库统计下首页和 JAV 页顶部人物、厂牌聚合过慢的问题。
- **播放**:缺少显示时间戳表的 MP4 可在索引位于文件末尾时重建播放头;旧版失败缓存会重新检查这类布局,无法判断编辑起点的文件继续转码。
- **配置**:同源检查统一识别 HTTP、HTTPS 的默认端口,避免地址显式包含 `:80` 或 `:443` 时被误拒绝。
- **馆藏**:处理失败后的线程清理完成前保留停止中状态,避免立即重试撞上仍持有的处理锁;起步失败时显示本次错误,不再退回上一轮结果。
diff --git a/docs/REUSE.md b/docs/REUSE.md
index 0109dcca5..8f79f56a4 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -4,6 +4,8 @@
采用 bbingz 的 [PR #205](https://github.com/peach-mitao/peach/pull/205),固定来源提交 `bbf5aac8e9562211c324c2019bff35888266df84`,许可证沿用本项目 AGPL-3.0-or-later。作品筛选集合先在独立子查询中求出,创作者、标签和演员按既有关系聚合,保留去重计数与标签可见性。没有选中标签时移除无效 `tag_match`,复用既有修订缓存、同键锁与跨连接写入失效机制,不在线改写 SQLite 统计或增加索引迁移。
+顶部人物、厂牌的 `q_tops` 复用同一筛选方式:独立求出可用作品集合,再按实体聚合,保留代表作选择、种子抽样、续页和修订缓存。错误统计回归使用临时账本、冷缓存与新连接,按作品数量限制判定调用量,核对默认、JAV、已看及组合范围的输出等价。运行时不写 ledger,不改 SQLite 统计或索引。
+
## 混合媒体根与模型读取
馆藏采集复用 `platform.within_root` 的 SMB 根身份判断,本地路径保留字面归属比较,不逐影片访问挂载目录。STRM 是链接文件,其大小和推广名不作为远端影片的垃圾证据;垃圾队列读取时重新评分,既有人工决定与回收站记录保持原样。
diff --git a/src/peach/web_catalog.py b/src/peach/web_catalog.py
index a3c9af969..575c42cb5 100644
--- a/src/peach/web_catalog.py
+++ b/src/peach/web_catalog.py
@@ -1281,7 +1281,7 @@ def q_tops(contract: WebContract, n=28, jav=False, seed="", state="", page=0, fi
续页要是再回到那一段就会把同一个人给两次,所以那一段归第一页独占,续页从它的
末尾往后按数量数下去。库里六百多位女优,一排给到头才是「滚到底还能接着滚」。
- 两条分组 SQL 各要三百毫秒上下,结果只取决于账本与口径,与种子无关:候选池按
+ 两条分组 SQL 的结果只取决于账本与口径,与种子无关:候选池按
`TOPS_TABLES` 的版本缓存,种子抽样和读目录索引的那几个标志都在缓存之外现做。"""
state = state if state_predicate(state) else ""
scope = PRESENT_ASSET_CLAUSE + (JAV_ASSET_CLAUSE if jav else "") + state_clause(state)
@@ -1289,6 +1289,7 @@ def q_tops(contract: WebContract, n=28, jav=False, seed="", state="", page=0, fi
if filters and filters.get('folder'):
conditions, folder_params = catalog_filter(contract, filters)
scope += ' AND ' + ' AND '.join(conditions)
+ tops_assets = "SELECT a.id FROM asset a WHERE a.medium='video' " + scope
base = (
"SELECT e.id,e.canonical_name,count(DISTINCT ae.asset_id) n,"
"(SELECT a2.id FROM asset_entity ae2 JOIN asset a2 ON a2.id=ae2.asset_id "
@@ -1296,8 +1297,7 @@ def q_tops(contract: WebContract, n=28, jav=False, seed="", state="", page=0, fi
" AND (e.kind<>'performer' OR " + solo_performer_clause("a2.id", "e.id") + ") "
" ORDER BY (a2.play_count IS NULL),a2.size DESC LIMIT 1) rep "
"FROM asset_entity ae JOIN entity e ON e.id=ae.entity_id "
- "JOIN asset a ON a.id=ae.asset_id "
- "WHERE a.medium='video' AND e.kind=? " + scope +
+ "WHERE e.kind=? AND ae.asset_id IN (" + tops_assets + ") " +
"GROUP BY e.id,e.canonical_name ORDER BY n DESC LIMIT ? OFFSET ?"
)
first_page_span = n * TOPS_POOL_FACTOR if seed else n
diff --git a/tests/test_rm_web.py b/tests/test_rm_web.py
index 71e60c134..9f649a8e7 100644
--- a/tests/test_rm_web.py
+++ b/tests/test_rm_web.py
@@ -3907,6 +3907,47 @@ class TopsRotationTests(unittest.TestCase):
def names(self, **kwargs):
return [row["k"] for row in rm_web.q_tops(self.contract, 8, **kwargs)["performers"]]
+ def test_tops_bound_asset_predicate_work_with_inaccurate_statistics(self):
+ roots = dict(avatar_root=Path(self.tmp.name) / 'avatars', logo_root=Path(self.tmp.name) / 'logos')
+ with closing(sqlite3.connect(self.db_path)) as connection, connection:
+ connection.execute("UPDATE asset SET code=CASE WHEN id%3=0 THEN 'FC2-'||(1000000+id) END,"
+ 'play_count=CASE WHEN id%5=0 THEN 1 ELSE 0 END')
+ connection.execute('ANALYZE')
+ asset_count = connection.execute('SELECT count(*) FROM asset').fetchone()[0]
+ options = ({}, {'jav': True}, {'state': 'played'}, {'jav': True, 'state': 'played'})
+ expected = [rm_web.q_tops(rm_web.WebContract(Path(self.db_path), **roots), 8, seed='111', **scope)
+ for scope in options]
+ with closing(sqlite3.connect(self.db_path)) as connection, connection:
+ if connection.execute("SELECT 1 FROM sqlite_master WHERE name='sqlite_stat4'").fetchone():
+ connection.execute('DELETE FROM sqlite_stat4')
+ connection.executemany('UPDATE sqlite_stat1 SET stat=? WHERE idx=?', [
+ ('2957 1439', 'idx_asset_location'),
+ ('2957 1439 1', 'sqlite_autoindex_asset_1'),
+ ('0 0', 'idx_asset_region'), ('0 0 0', 'idx_asset_disposal_id'),
+ ('2957 1', 'idx_asset_name'), ('2957 1', 'idx_asset_size'),
+ ('2957 1439 1439', 'idx_asset_hash'),
+ ('12861 131 131', 'idx_asset_entity_entity_role'),
+ ('12861 10 1 1 1', 'sqlite_autoindex_asset_entity_1'),
+ ('1847 360 1', 'sqlite_autoindex_entity_1'),
+ ])
+ for scope, wanted in zip(options, expected):
+ with self.subTest(**scope):
+ calls = 0
+
+ def counted_extra(name, path, **kwargs):
+ nonlocal calls
+ calls += 1
+ return catalog_rules.is_local_extra(name, path, **kwargs)
+
+ # 新契约使用冷缓存和新连接,确保执行的是修改统计后的查询。
+ contract = rm_web.WebContract(Path(self.db_path), **roots)
+ with mock.patch('peach.repository.is_local_extra', counted_extra):
+ actual = rm_web.q_tops(contract, 8, seed='111', **scope)
+ self.assertTrue(wanted['performers'])
+ self.assertGreater(calls, 0)
+ self.assertLess(calls, asset_count * 4, f'asset predicate calls: {calls}')
+ self.assertEqual(actual, wanted)
+
def test_without_a_seed_it_stays_the_strict_top_list(self):
self.assertEqual(self.names(), self.names())
self.assertEqual(self.names()[0], "P00", "无种子时仍按数量取前 N")