migrate calibre plugin to persist

reviewable/pr7138/r1
Martín Fdez 3 years ago committed by Frans de Jonge
parent b8f0dc3752
commit b27cf0d168

@ -127,7 +127,7 @@ function Calibre:getSearchMenuTable()
sub_item_table_func = function() sub_item_table_func = function()
local result = {} local result = {}
-- append previous scanned dirs to the list. -- append previous scanned dirs to the list.
local cache = LuaSettings:open(CalibreSearch.user_libraries) local cache = LuaSettings:open(CalibreSearch.cache_libs.path)
for path, _ in pairs(cache.data) do for path, _ in pairs(cache.data) do
table.insert(result, { table.insert(result, {
text = path, text = path,

@ -13,32 +13,20 @@ local InputDialog = require("ui/widget/inputdialog")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer") local InputContainer = require("ui/widget/container/inputcontainer")
local Menu = require("ui/widget/menu") local Menu = require("ui/widget/menu")
local Persist = require("persist")
local Screen = require("device").screen local Screen = require("device").screen
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local logger = require("logger") local logger = require("logger")
local socket = require("socket") local socket = require("socket")
local util = require("util")
local _ = require("gettext") local _ = require("gettext")
local T = require("ffi/util").template local T = require("ffi/util").template
-- cache files
local libraries_file = "calibre-libraries.lua"
local metadata_file = "calibre-books.lua"
-- loads a table from disk
local function loadTable(path)
local ok, data = pcall(dofile, path)
if ok then
return data
else
return nil, data
end
end
-- get root dir for disk scans -- get root dir for disk scans
local function getDefaultRootDir() local function getDefaultRootDir()
if Device:isCervantes() or Device:isKobo() then if Device:isCervantes() or Device:isKobo() then
return "/mnt" return "/mnt"
elseif Device:isEmulator() then
return lfs.currentdir()
else else
return Device.home_dir or lfs.currentdir() return Device.home_dir or lfs.currentdir()
end end
@ -183,8 +171,15 @@ local CalibreSearch = InputContainer:new{
"find_by_authors", "find_by_authors",
"find_by_path", "find_by_path",
}, },
user_libraries = DataStorage:getDataDir() .. "/cache/" .. libraries_file,
user_book_cache = DataStorage:getDataDir() .. "/cache/" .. metadata_file, cache_libs = Persist:new{
path = DataStorage:getDataDir() .. "/cache/calibre-libraries.lua",
},
cache_books = Persist:new{
path = DataStorage:getDataDir() .. "/cache/calibre-books.dat",
codec = "bitser",
},
} }
function CalibreSearch:ShowSearch() function CalibreSearch:ShowSearch()
@ -311,7 +306,7 @@ function CalibreSearch:find(option)
end end
if #self.libraries == 0 then if #self.libraries == 0 then
local libs, err = loadTable(self.user_libraries) local libs, err = self.cache_libs:load()
if not libs then if not libs then
logger.warn("no calibre libraries", err) logger.warn("no calibre libraries", err)
self:prompt(_("No calibre libraries")) self:prompt(_("No calibre libraries"))
@ -504,7 +499,7 @@ function CalibreSearch:prompt(message)
paths = paths .. "\n" .. _("SD card") .. ": " .. sd_paths paths = paths .. "\n" .. _("SD card") .. ": " .. sd_paths
end end
util.dumpTable(self.libraries, self.user_libraries) self.cache_libs:save(self.libraries)
self:invalidateCache() self:invalidateCache()
self.books = self:getMetadata() self.books = self:getMetadata()
local info_text local info_text
@ -559,7 +554,7 @@ end
-- invalidate current cache -- invalidate current cache
function CalibreSearch:invalidateCache() function CalibreSearch:invalidateCache()
util.removeFile(self.user_book_cache) self.cache_books:delete()
self.books = {} self.books = {}
end end
@ -571,12 +566,14 @@ function CalibreSearch:getMetadata()
-- try to load metadata from cache -- try to load metadata from cache
if self.cache_metadata then if self.cache_metadata then
local function cacheIsNewer(timestamp) local function cacheIsNewer(timestamp)
if not timestamp then return false end local file_timestamp = self.cache_books:timestamp()
if not timestamp or not file_timestamp then return false end
local Y, M, D, h, m, s = timestamp:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)") local Y, M, D, h, m, s = timestamp:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)")
local date = os.time({year = Y, month = M, day = D, hour = h, min = m, sec = s}) local date = os.time({year = Y, month = M, day = D, hour = h, min = m, sec = s})
return lfs.attributes(self.user_book_cache, "modification") > date return file_timestamp > date
end end
local cache, err = loadTable(self.user_book_cache)
local cache, err = self.cache_books:load()
if not cache then if not cache then
logger.warn("invalid cache:", err) logger.warn("invalid cache:", err)
else else
@ -612,7 +609,7 @@ function CalibreSearch:getMetadata()
for index, book in ipairs(books) do for index, book in ipairs(books) do
table.insert(dump, index, removeNull(book)) table.insert(dump, index, removeNull(book))
end end
util.dumpTable(dump, self.user_book_cache) self.cache_books:save(dump)
end end
local elapsed = socket.gettime() - start local elapsed = socket.gettime() - start
logger.info(string.format(template, #books, "calibre", elapsed * 1000)) logger.info(string.format(template, #books, "calibre", elapsed * 1000))

Loading…
Cancel
Save