feat(rust): 批次 A 只读批量 IO(stat_many / read_text_many / list_dir_entries)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m31s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m31s
This commit is contained in:
1 parent
317abf16c0
commit
fff0a643f2
5 files changed
+224
-29
No files matched your search
+87
-26
@@ -33,6 +33,22 @@
|
||||
const core = () => { const T = tk(); return (T && T.core) || null }
|
||||
const enabled = () => { const T = tk(); return !!(T && T.core && T.fs && T.path) }
|
||||
|
||||
// 2026-12-05(Rust 化批次 A):只读批量 IO。命令不可用/失败时返回 null ⇒ 调用方回退旧的逐文件实现。
|
||||
let ipcCalls = 0
|
||||
const rustInvoke = async (cmd, args) => { const c = core(); if (!c || typeof c.invoke !== 'function') return null; ipcCalls++; return await c.invoke(cmd, args) }
|
||||
const statManyOf = async (rels) => {
|
||||
if (!enabled() || !dir || !Array.isArray(rels) || !rels.length) return null
|
||||
try { const r = await rustInvoke('stat_many', { base: dir, rels: rels }); return Array.isArray(r) ? r : null } catch (e) { return null }
|
||||
}
|
||||
const readTextManyOf = async (rels) => {
|
||||
if (!enabled() || !dir || !Array.isArray(rels) || !rels.length) return null
|
||||
try { const r = await rustInvoke('read_text_many', { base: dir, rels: rels }); return Array.isArray(r) ? r : null } catch (e) { return null }
|
||||
}
|
||||
const listDirEntriesOf = async (rel) => {
|
||||
if (!enabled() || !dir) return null
|
||||
try { const r = await rustInvoke('list_dir_entries', { base: dir, rel: String(rel || '') }); return Array.isArray(r) ? r : null } catch (e) { return null }
|
||||
}
|
||||
|
||||
const join = async (...p) => {
|
||||
const P = path()
|
||||
if (!P || !P.join) return p.join('/')
|
||||
@@ -724,14 +740,23 @@
|
||||
if (!e || !e.isDirectory) continue
|
||||
const id = String(e.name || '')
|
||||
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{1,63}$/.test(id)) continue
|
||||
let fe = []
|
||||
try { fe = await fs().readDir(await join(await pluginDataDir(), id, PFILE_DIR)) } catch (err) { continue }
|
||||
// 2026-12-05:一次 list_dir_entries 拿 name+size(原来每个文件 readFile 只为取长度);失败回退
|
||||
const batched = await listDirEntriesOf(PDATA_DIR + '/' + id + '/' + PFILE_DIR)
|
||||
const files = []
|
||||
for (const f of fe) {
|
||||
if (!f || f.isDirectory || !pluginFileNameOk(f.name)) continue
|
||||
let size = 0
|
||||
try { const u8 = await fs().readFile(await join(await pluginDataDir(), id, PFILE_DIR, f.name)); size = u8 ? u8.length : 0 } catch (err2) { size = 0 }
|
||||
files.push({ name: f.name, size: size })
|
||||
if (batched) {
|
||||
for (const f of batched) {
|
||||
if (!f || f.is_dir || !pluginFileNameOk(f.name)) continue
|
||||
files.push({ name: String(f.name), size: Number(f.size) || 0 })
|
||||
}
|
||||
} else {
|
||||
let fe = []
|
||||
try { fe = await fs().readDir(await join(await pluginDataDir(), id, PFILE_DIR)) } catch (err) { continue }
|
||||
for (const f of fe) {
|
||||
if (!f || f.isDirectory || !pluginFileNameOk(f.name)) continue
|
||||
let size = 0
|
||||
try { const u8 = await fs().readFile(await join(await pluginDataDir(), id, PFILE_DIR, f.name)); size = u8 ? u8.length : 0 } catch (err2) { size = 0 }
|
||||
files.push({ name: f.name, size: size })
|
||||
}
|
||||
}
|
||||
if (files.length) { files.sort((a, b) => a.name.localeCompare(b.name)); out.push({ id: id, files: files }) }
|
||||
}
|
||||
@@ -772,16 +797,30 @@
|
||||
const entries = await fs().readDir(cdir)
|
||||
const wsFiles = entries.filter((e) => e && !e.isDirectory && /\.json$/.test(e.name))
|
||||
bpReport('canvases', 0, wsFiles.length)
|
||||
let wsRead = 0
|
||||
for (const e of wsFiles) {
|
||||
try {
|
||||
const text = await fs().readTextFile(await join(cdir, e.name))
|
||||
const wid = e.name.replace(/\.json$/, '')
|
||||
mem['iwb_ws_' + wid] = text
|
||||
fileTexts[wid] = text
|
||||
} catch (e) { /* 单个画布读失败不影响其它 */ }
|
||||
wsRead++
|
||||
bpReport('canvases', wsRead, wsFiles.length)
|
||||
// 2026-12-05:一次 IPC 读完所有画布(原来逐文件 readTextFile);失败回退旧循环
|
||||
const batched = await readTextManyOf(wsFiles.map((e) => CANVAS_DIR + '/' + e.name))
|
||||
if (batched) {
|
||||
let wsRead = 0
|
||||
for (const t of batched) {
|
||||
if (t && t.text != null) {
|
||||
const wid = String(t.rel || '').replace(/^canvases\//, '').replace(/\.json$/, '')
|
||||
if (wid) { mem['iwb_ws_' + wid] = String(t.text); fileTexts[wid] = String(t.text) }
|
||||
}
|
||||
wsRead++
|
||||
bpReport('canvases', wsRead, batched.length)
|
||||
}
|
||||
} else {
|
||||
let wsRead = 0
|
||||
for (const e of wsFiles) {
|
||||
try {
|
||||
const text = await fs().readTextFile(await join(cdir, e.name))
|
||||
const wid = e.name.replace(/\.json$/, '')
|
||||
mem['iwb_ws_' + wid] = text
|
||||
fileTexts[wid] = text
|
||||
} catch (e) { /* 单个画布读失败不影响其它 */ }
|
||||
wsRead++
|
||||
bpReport('canvases', wsRead, wsFiles.length)
|
||||
}
|
||||
}
|
||||
} catch (e) { /* 目录还没有 */ }
|
||||
// 资源库分类文件(library/*.json):文件比 data.json 新,直接覆盖同名键
|
||||
@@ -1617,14 +1656,24 @@
|
||||
try {
|
||||
const cd = await join(dir, 'canvases')
|
||||
const list = await fs().readDir(cd)
|
||||
for (const e of list) {
|
||||
if (!e || e.isDirectory || !/\.json$/i.test(e.name) || e.name === 'active.json') continue
|
||||
let txt = ''
|
||||
try { txt = await fs().readTextFile(await join(cd, e.name)) } catch (err) { continue }
|
||||
const m = txt.match(/"__blob"\s*:\s*"([^"]+)"/g) || []
|
||||
for (const hit of m) {
|
||||
const km = /"([^"]+)"$/.exec(hit)
|
||||
if (km) canvasKeys.add(km[1])
|
||||
const wsFiles = list.filter((e) => e && !e.isDirectory && /\.json$/i.test(e.name) && e.name !== 'active.json')
|
||||
// 2026-12-05:一次 IPC 读完所有画布(原来逐文件);失败回退
|
||||
const batched = wsFiles.length ? await readTextManyOf(wsFiles.map((e) => CANVAS_DIR + '/' + e.name)) : null
|
||||
if (batched) {
|
||||
for (const t of batched) {
|
||||
if (!t || t.text == null) continue
|
||||
const m = String(t.text).match(/"__blob"\s*:\s*"([^"]+)"/g) || []
|
||||
for (const hit of m) { const km = /"([^"]+)"$/.exec(hit); if (km) canvasKeys.add(km[1]) }
|
||||
}
|
||||
} else {
|
||||
for (const e of wsFiles) {
|
||||
let txt = ''
|
||||
try { txt = await fs().readTextFile(await join(cd, e.name)) } catch (err) { continue }
|
||||
const m = txt.match(/"__blob"\s*:\s*"([^"]+)"/g) || []
|
||||
for (const hit of m) {
|
||||
const km = /"([^"]+)"$/.exec(hit)
|
||||
if (km) canvasKeys.add(km[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) { /* 画布读不到就退回旧判据 */ }
|
||||
@@ -1732,9 +1781,21 @@
|
||||
if (!enabled() || !dir) return { ok: false, error: '资料目录未就绪(浏览器环境)' }
|
||||
const list = Array.isArray(index) ? index : []
|
||||
const rows = new Array(list.length)
|
||||
// 2026-12-05:先把所有 orig/thumb 路径一次 stat 回来(原来每条 2 次 exists IPC);失败回退
|
||||
const allRels = []
|
||||
for (const it of list) {
|
||||
const name = String((it && it.name) || '')
|
||||
const oa = assets[String((it && it.origIdb) || ('out_orig_' + name))]
|
||||
const ta = assets[String((it && it.idb) || ('out_' + name))]
|
||||
if (oa && oa.f) allRels.push(oa.f)
|
||||
if (ta && ta.f && !(oa && oa.f === ta.f)) allRels.push(ta.f)
|
||||
}
|
||||
const statList = allRels.length ? await statManyOf(Array.from(new Set(allRels))) : null
|
||||
const statMap = statList ? new Map(statList.map((r) => [String(r.rel), !!r.exists])) : null
|
||||
let cursor = 0
|
||||
const existsAt = async (rel) => {
|
||||
if (!rel) return false
|
||||
if (statMap) return !!statMap.get(String(rel))
|
||||
try { return !!(await fs().exists(await join(dir, rel))) } catch (e) { return false }
|
||||
}
|
||||
const worker = async () => {
|
||||
@@ -1792,7 +1853,7 @@
|
||||
kv: () => kv,
|
||||
dir: () => dir,
|
||||
setDir: async (next) => { dir = String(next || ''); await flush();},
|
||||
stats: () => ({ dir, keys: Object.keys(mem).length, images: Object.keys(assets).length }),
|
||||
stats: () => ({ dir, keys: Object.keys(mem).length, images: Object.keys(assets).length, ipc: ipcCalls }),
|
||||
__internals: { snapshot, loadInto, sha16, extOf, dataUrlToBytes, bytesToDataUrl, pluginFileNameOk },
|
||||
}
|
||||
// 关窗/收托盘前强制刷盘(beforeunload 在桌面端不保证,但能救一次是一次)
|
||||
|
||||
Reference in new issue
Block a user