From 4998aa96bb4153b0de834e0d8293ae19a185b03a Mon Sep 17 00:00:00 2001 From: amzxyz Date: Sun, 21 Dec 2025 09:11:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E8=A7=84=E9=81=BF=E5=9C=A8#579=E6=8F=90=E5=88=B0=E7=9A=84Lua?= =?UTF-8?q?=E6=9B=B4=E5=9C=A8=E5=86=85=E5=AE=B9=E4=B8=8D=E5=8F=98=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E6=9B=B4=E6=96=B0=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E5=AF=BC=E8=87=B4=E5=90=8C=E6=AD=A5=E8=BD=AF=E4=BB=B6?= =?UTF-8?q?=E8=AE=A4=E4=B8=BA=E5=8F=98=E6=9B=B4=EF=BC=8C=E8=BF=9B=E8=80=8C?= =?UTF-8?q?=E9=A2=91=E7=B9=81=E5=90=8C=E6=AD=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/super_sequence.lua | 43 +++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/lua/super_sequence.lua b/lua/super_sequence.lua index 8fe1d8c..6818f23 100644 --- a/lua/super_sequence.lua +++ b/lua/super_sequence.lua @@ -427,6 +427,7 @@ end ------------------------------------------------------------ -- 十一、把“合并结果”重写到我机导出(含 p=0) +-- [优化]:增加内容比对,仅在内容实质发生变化时才写盘,避免无效刷新 mtime ------------------------------------------------------------ local function rewrite_export_from_latest(latest) local ok = _sync_ready() @@ -438,33 +439,61 @@ local function rewrite_export_from_latest(latest) local export_path = _path_join(dir, export_name) local manifest = _manifest_path(dir) + -- 1. 确保清单文件存在并包含当前文件(这部分本身开销极小,保留原逻辑或同样做排重均可) if not _file_exists(manifest) then local mf = io.open(manifest, "w"); if mf then mf:close() end end do local names = _read_lines(manifest); local seen = {}; for _, n in ipairs(names) do seen[_trim(n)] = true end if not seen[export_name] then names[#names + 1] = export_name; _write_lines(manifest, names) end end - local f = io.open(export_path, "w"); if not f then return end + -- 2. 【核心修改】在内存中构建即将写入的内容 + local buffer = {} + local user_id = wanxiang.get_user_id() - if user_id then f:write("\001/user_id\t", user_id, "\n") end - f:write("\001/device_name\t", device_name, "\n") + if user_id then + table.insert(buffer, "\001/user_id\t" .. user_id) + end + table.insert(buffer, "\001/device_name\t" .. device_name) local inputs = {} for input, _ in pairs(latest) do inputs[#inputs + 1] = input end table.sort(inputs) + for _, input in ipairs(inputs) do local items, keys = latest[input], {} for item, _ in pairs(items) do keys[#keys + 1] = item end table.sort(keys) for _, item in ipairs(keys) do local a = items[item] - f:write(string.format("%s\ti=%s p=%s o=%s t=%s\n", - input, item, a.fixed_position or 0, a.offset or 0, a.updated_at or "")) + -- 构建行内容 + local line = string.format("%s\ti=%s p=%s o=%s t=%s", + input, item, a.fixed_position or 0, a.offset or 0, a.updated_at or "") + table.insert(buffer, line) end end - f:close() - --log.info(string.format("[sequence] export rewritten (merged LWW, incl tombstones): %s", export_path)) + -- 拼接成完整字符串 (末尾添加换行符以符合通常的文件习惯) + local new_content = table.concat(buffer, "\n") .. "\n" + + -- 3. 【核心修改】读取旧文件内容进行对比 + local old_content = nil + local f_read = io.open(export_path, "r") + if f_read then + old_content = f_read:read("*a") -- 读取全部内容 + f_read:close() + end + + -- 4. 【核心修改】只有内容不一致时才写入 + -- 如果文件不存在(old_content为nil) 或者 内容不同,则写入 + if old_content ~= new_content then + local f_write = io.open(export_path, "w") + if not f_write then return end + f_write:write(new_content) + f_write:close() + -- log.info(string.format("[sequence] export updated: %s", export_path)) + else + -- log.info(string.format("[sequence] export skipped (no changes): %s", export_path)) + end end ------------------------------------------------------------