From 561caadc5c072b5ffc9de8a93943bacf90061b2c Mon Sep 17 00:00:00 2001 From: poire-z Date: Fri, 13 Oct 2017 19:18:36 +0200 Subject: [PATCH] Avoid recalculation of partial_md5_checksum at each opening (#3352) This is done by/for kosync plugin at each opening, because the docsettings was re-opened and saved for this, but later overwritten by the current koreader docsettings - so it was redone each time. This correctly adds this partial_md5_checksum to the current koreader docsettings, which will be saved on document closing - so it will not be redone next time. Note: this partial_md5_checksum is not (yet) used by anything. --- frontend/document/document.lua | 13 ++++++++++--- plugins/kosync.koplugin/main.lua | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/frontend/document/document.lua b/frontend/document/document.lua index ae2c65166..8876691bf 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -120,13 +120,18 @@ end -- Note that if PDF file size is around 1024, 4096, 16384, 65536, 262144 -- 1048576, 4194304, 16777216, 67108864, 268435456 or 1073741824, appending data -- by highlighting in KOReader may change the digest value. -function Document:fastDigest() +function Document:fastDigest(docsettings) if not self.file then return end local file = io.open(self.file, 'rb') if file then - local docsettings = require("docsettings"):open(self.file) + local tmp_docsettings = false + if not docsettings then -- if not provided, open/create it + docsettings = require("docsettings"):open(self.file) + tmp_docsettings = true + end local result = docsettings:readSetting("partial_md5_checksum") if not result then + logger.dbg("computing and storing partial_md5_checksum") local md5 = require("ffi/MD5") local lshift = bit.lshift local step, size = 1024, 1024 @@ -143,7 +148,9 @@ function Document:fastDigest() result = m:sum() docsettings:saveSetting("partial_md5_checksum", result) end - docsettings:close() + if tmp_docsettings then + docsettings:close() + end file:close() return result end diff --git a/plugins/kosync.koplugin/main.lua b/plugins/kosync.koplugin/main.lua index 856634b1c..4b90aae50 100644 --- a/plugins/kosync.koplugin/main.lua +++ b/plugins/kosync.koplugin/main.lua @@ -82,7 +82,7 @@ function KOSync:onReaderReady() -- Make sure checksum has been calculated at the very first time a document has been opened, to -- avoid document saving feature to impact the checksum, and eventually impact the document -- identity in the progress sync feature. - self.view.document:fastDigest() + self.view.document:fastDigest(self.ui.doc_settings) end function KOSync:addToMainMenu(menu_items)