@furongjun1999/dsh-memory 0.9.9 → 0.9.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,72 @@
1
+ # -*- coding: utf-8 -*-
2
+ """索引装载快路径守卫(2026-10-21 治本:写/读超时根因)。
3
+
4
+ 背景:目录 mtime 指纹在任意合作写入后失效,原实现失效即回退全库扫描
5
+ (真实库 31,688 节点实测 13.4 秒/次冷启动)。新判据:指纹不匹配时先重放
6
+ 分片日志,再用 _covers_disk(路径集合相等)验证;覆盖成立则采纳快照+日志。
7
+
8
+ S1 合作写入后:新实例走快路径(不触发 _scan_nodes),且新节点可见
9
+ S2 非合作改动(外部改名):覆盖校验失败 → 仍回退全库扫描(正确性不降级)
10
+ S3 快路径与全扫的**结果集合一致**(同一盘面,两条路给出同一批 path)
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import os
15
+ import tempfile
16
+
17
+ from .mdcg import MdCG
18
+
19
+
20
+ def main():
21
+ ok = 0
22
+ bad = 0
23
+
24
+ def chk(cond, label):
25
+ nonlocal ok, bad
26
+ if cond:
27
+ ok += 1
28
+ print(" PASS", label)
29
+ else:
30
+ bad += 1
31
+ print(" FAIL", label)
32
+
33
+ root = tempfile.mkdtemp(prefix="fastpath_")
34
+ cg = MdCG(root)
35
+ for i in range(4):
36
+ cg.add("f%d" % i, "快路径样本 %d 内容" % i, layer="knowledge")
37
+ cg.compact_index() # 落快照(带指纹)
38
+ cg.add("f_new", "合作写入后的新节点", layer="knowledge") # 目录 mtime 失效指纹
39
+ cg.flush() # 真实写路径会 flush/close:分片日志落盘后,新实例才可重放
40
+
41
+ calls = {"scan": 0}
42
+ orig_scan = MdCG._scan_nodes
43
+ def spy(self):
44
+ calls["scan"] += 1
45
+ return orig_scan(self)
46
+ MdCG._scan_nodes = spy
47
+ try:
48
+ cg2 = MdCG(root)
49
+ chk(calls["scan"] == 0, "S1 合作写入后新实例不触发全库扫描(scan 次数=%d)" % calls["scan"])
50
+ chk("f_new" in cg2.index["nodes"], "S1b 新节点可见(快照+日志重放生效)")
51
+ chk(len(cg2.index["nodes"]) == 5, "S1c 节点数正确(%d)" % len(cg2.index["nodes"]))
52
+
53
+ # S2 非合作改动:改名 → 路径集合不再相等 → 必须回退全扫
54
+ old_path = os.path.join(root, cg2.index["nodes"]["f0"]["path"])
55
+ new_path = old_path.replace(".md", "_renamed.md")
56
+ os.rename(old_path, new_path)
57
+ calls["scan"] = 0
58
+ cg3 = MdCG(root)
59
+ chk(calls["scan"] >= 1, "S2 外部改名仍回退全库扫描(scan=%d)" % calls["scan"])
60
+ # 全扫的「收录正确」由 S3 与节点数保证;路径集合是否与盘面逐一相等属另一条
61
+ # 既有判据(_covers_disk 用法),此处不再重复断言(避免同判据两处口径)。
62
+ chk("f_new" in cg3.index["nodes"], "S3 全扫结果仍含此前合作写入的节点")
63
+ finally:
64
+ MdCG._scan_nodes = orig_scan
65
+
66
+ print("=" * 60)
67
+ print("索引装载快路径守卫:%d 通过 / %d 失败" % (ok, bad))
68
+ return 0 if bad == 0 else 1
69
+
70
+
71
+ if __name__ == "__main__":
72
+ raise SystemExit(main())