--[[-- moonsource_weread.toc:目录缓存读取与按 idx/uid 定位。 --]] local Assert = require("support.assert") local Config = require("support.config") local db_path = Config.dir() .. "/wechat_toc_spec.sqlite3" for _idx, suffix in ipairs({ "", "-wal", "-shm" }) do os.remove(db_path .. suffix) end package.preload["utils.paths"] = function() return { dbPath = function() return db_path end } end local store = {} local upserted = {} local reads = 0 local now = 1000 local original_time = os.time os.time = function() return now end local writes = true package.preload["book.books_db"] = function() return { getToc = function(_source_id, stable_id, max_age) reads = reads + 1 local row = store[stable_id] if not row or (max_age and now - row.at >= max_age) then return nil end return row.payload, row.at end, setToc = function(_source_id, stable_id, payload) if not writes then return false end upserted[stable_id] = payload store[stable_id] = { payload = payload, at = now } return true end, } end package.preload["json"] = function() return { decode = require("support.json_stub").decode, encode = function(value) return "encoded:" .. tostring(#value) end, } end package.loaded["moonsource_weread.toc"] = nil local Toc = require("moonsource_weread.toc") local readToc = require("book.toc_db").read require("book.toc_db").read = function(...) reads = reads + 1 return readToc(...) end do Assert.is_nil(Toc.read("ext.weread", "missing")) Assert.is_nil(Toc.read("ext.weread", "broken")) end do -- payload 是目录数组(与 Mapper.chapters 输出一致),按数组索引定位。 store["b2"] = { payload = [[ [{"idx":1,"source_idx":2,"uid":"u1","toc_version":3},{"idx":2,"source_idx":4,"uid":"u2"}] ]], at = now } Assert.eq(Toc.uid("ext.weread", "b2", 1), "u1") Assert.eq(Toc.uid("ext.weread", "b2", 2), "u2") Assert.is_nil(Toc.uid("ext.weread", "b2", 99)) Assert.eq(Toc.index("ext.weread", "b2", "u2"), 2) Assert.is_nil(Toc.index("ext.weread", "b2", "u9")) Assert.eq(Toc.sourceIndex("ext.weread", "b2", 1), 2) Assert.eq(Toc.sourceIndex("ext.weread", "b2", 2), 4) Assert.is_nil(Toc.sourceIndex("ext.weread", "b2", 99)) end do store["old"] = { payload = [[{"idx":1,"source_idx":2,"uid":"old"}]], at = now } local old = Toc.read("ext.weread", "old") Assert.not_nil(old, "旧目录仍须支持离线读取") Assert.is_false(Toc.isCurrent(old)) end do -- 解码结果进内存:库里的行没了也照样命中,翻页/按章补报不再反复 decode 整份目录。 local before = reads store["b2"] = nil require("utils.db").removeIdentity("toc_entries", "ext.weread", "b2") require("utils.db").removeIdentity("toc_snapshots", "ext.weread", "b2") Assert.eq(Toc.uid("ext.weread", "b2", 1), "u1") Assert.eq(Toc.index("ext.weread", "b2", "u2"), 2) Assert.eq(reads, before, "命中内存缓存时不得再查库") -- clear 后回落到库(此时库里已无数据) Toc.clear() Assert.is_nil(Toc.read("ext.weread", "b2")) Assert.is_true(reads > before) end do -- put 落库并直接接管缓存,无需再 decode 一遍 local list = { { idx = 1, uid = "p1" }, { idx = 2, uid = "p2" }, { idx = 3, uid = "p3" } } Toc.put("ext.weread", "b3", list) Assert.is_nil(upserted["b3"], "不再写旧 JSON 列") local before = reads Assert.eq(Toc.uid("ext.weread", "b3", 3), "p3") Assert.eq(Toc.index("ext.weread", "b3", "p2"), 2) Assert.eq(reads, before) -- 章内进度 + 章节序号 → 全书 fraction Assert.eq(Toc.wholeFraction("ext.weread", "b3", 1, 0), 0) Assert.eq(Toc.wholeFraction("ext.weread", "b3", 2, 0.5), 0.5) Assert.eq(Toc.wholeFraction("ext.weread", "b3", 3, 1), 1) Assert.is_nil(Toc.wholeFraction("ext.weread", "unknown", 1, 0)) local cached_reads = reads Assert.eq(Toc.uid("ext.weread", "b3", 1), "p1") Assert.eq(reads, cached_reads) Toc.clear() Toc.read("ext.weread", "b3") Assert.is_true(reads > cached_reads, "clear 后必须回库") end do local list = { { idx = 1, uid = "failed" } } writes = false Assert.is_false(Toc.put("ext.weread", "failed", list)) Assert.is_nil(Toc.read("ext.weread", "failed")) writes = true -- 清内存后直接从逐章表恢复。 Toc.clear() Assert.eq(Toc.read("ext.weread", "b3")[1].uid, "p1") now = now + 6 * 60 * 60 Assert.eq(Toc.read("ext.weread", "b3")[1].uid, "p1", "目录不做时间保鲜,旧缓存仍可用") end os.time = original_time require("utils.db").close() for _idx, suffix in ipairs({ "", "-wal", "-shm" }) do os.remove(db_path .. suffix) end