ReadHistory: refactoring with binary search (#9603)

Get rid of indexing and sorting, reduce flushing.
reviewable/pr9680/r1
hius07 2 years ago committed by GitHub
parent 54fae2987e
commit 4d4b04359c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,14 +22,12 @@ end
local function buildEntry(input_time, input_file) local function buildEntry(input_time, input_file)
local file_path = realpath(input_file) or input_file -- keep orig file path of deleted files local file_path = realpath(input_file) or input_file -- keep orig file path of deleted files
local file_exists = lfs.attributes(file_path, "mode") == "file"
return { return {
time = input_time, time = input_time,
text = input_file:gsub(".*/", ""),
file = file_path, file = file_path,
dim = not file_exists, -- "dim", as expected by Menu text = input_file:gsub(".*/", ""),
-- mandatory = file_exists and util.getFriendlySize(lfs.attributes(input_file, "size") or 0), dim = lfs.attributes(file_path, "mode") ~= "file", -- "dim", as expected by Menu
mandatory_func = function() -- Show the last read time (rather than file size) mandatory_func = function() -- Show the last read time
local readerui_instance = require("apps/reader/readerui"):_getRunningInstance() local readerui_instance = require("apps/reader/readerui"):_getRunningInstance()
local currently_opened_file = readerui_instance and readerui_instance.document and readerui_instance.document.file local currently_opened_file = readerui_instance and readerui_instance.document and readerui_instance.document.file
local last_read_ts local last_read_ts
@ -40,7 +38,7 @@ local function buildEntry(input_time, input_file)
else else
-- For past documents, the last save time of the settings is better -- For past documents, the last save time of the settings is better
-- as last read time than input_time (its last opening time, that -- as last read time than input_time (its last opening time, that
-- we fallback to it no sidecar file) -- we fallback to if no sidecar file)
last_read_ts = DocSettings:getLastSaveTime(file_path) or input_time last_read_ts = DocSettings:getLastSaveTime(file_path) or input_time
end end
return util.secondsToDate(last_read_ts, G_reader_settings:isTrue("twelve_hour_clock")) return util.secondsToDate(last_read_ts, G_reader_settings:isTrue("twelve_hour_clock"))
@ -50,59 +48,61 @@ local function buildEntry(input_time, input_file)
end, end,
callback = function() callback = function()
selectCallback(input_file) selectCallback(input_file)
end end,
} }
end end
local function fileFirstOrdering(l, r) function ReadHistory:getIndexByFile(item_file)
if l.file == r.file then for i, v in ipairs(self.hist) do
return l.time > r.time if item_file == v.file then
else return i
return l.file < r.file end
end end
end end
local function timeFirstOrdering(l, r) --- Returns leftmost index of the entry with item_time using binary search
if l.time == r.time then -- (items in history are sorted by time in reverse order).
return l.file < r.file -- If several entries have equal time, search within them by item_file in alphabetical order.
else -- If there are no entries with item_time, returns insertion index.
return l.time > r.time function ReadHistory:getIndexByTime(item_time, item_file)
local hist_nb = #self.hist
if hist_nb == 0 then
return 1
end end
end if item_time > self.hist[1].time then
return 1
function ReadHistory:_indexing(start) elseif item_time < self.hist[hist_nb].time then
--- @todo (Hzj_jie): Use binary search to find an item when deleting it. return hist_nb + 1
for i = start, #self.hist, 1 do
self.hist[i].index = i
end end
end local s, e, m, d = 1, hist_nb
while s <= e do
function ReadHistory:_sort() m = bit.rshift(s + e, 1)
local autoremove_deleted_items_from_history = if item_time < self.hist[m].time then
not G_reader_settings:nilOrFalse("autoremove_deleted_items_from_history") s, d = m + 1, 1
if autoremove_deleted_items_from_history then else
self:clearMissing() e, d = m - 1, 0
end
end end
table.sort(self.hist, fileFirstOrdering) local index = m + d
--- @todo (zijiehe): Use binary insert instead of a loop to deduplicate. if item_file then
for i = #self.hist, 2, -1 do while index <= #self.hist
if self.hist[i].file == self.hist[i - 1].file then and self.hist[index].time == item_time
table.remove(self.hist, i) and self.hist[index].file:gsub(".*/", "") < item_file do
index = index + 1
end end
end end
table.sort(self.hist, timeFirstOrdering) return index
self:_indexing(1)
end end
-- Reduces total count in hist list to a reasonable number by removing last --- Reduces number of history items to the required limit by removing old items.
-- several items.
function ReadHistory:_reduce() function ReadHistory:_reduce()
while #self.hist > 500 do local history_size = G_reader_settings:readSetting("history_size") or 500
table.remove(self.hist, #self.hist) while #self.hist > history_size do
table.remove(self.hist)
end end
end end
-- Flushes current history table into file. --- Saves history table to a file.
function ReadHistory:_flush() function ReadHistory:_flush()
local content = {} local content = {}
for _, v in ipairs(self.hist) do for _, v in ipairs(self.hist) do
@ -123,23 +123,25 @@ end
-- @treturn boolean true if the history_file has been updated and reloaded. -- @treturn boolean true if the history_file has been updated and reloaded.
function ReadHistory:_read(force_read) function ReadHistory:_read(force_read)
local history_file_modification_time = lfs.attributes(history_file, "modification") local history_file_modification_time = lfs.attributes(history_file, "modification")
if history_file_modification_time == nil if history_file_modification_time == nil then -- no history_file, proceed legacy only
or (not force_read and (history_file_modification_time <= self.last_read_time)) then return true
return false
end end
self.last_read_time = history_file_modification_time if force_read or (history_file_modification_time > self.last_read_time) then
local ok, data = pcall(dofile, history_file) self.last_read_time = history_file_modification_time
if ok and data then local ok, data = pcall(dofile, history_file)
self.hist = {} if ok and data then
for _, v in ipairs(data) do self.hist = {}
table.insert(self.hist, buildEntry(v.time, v.file)) for _, v in ipairs(data) do
table.insert(self.hist, buildEntry(v.time, v.file))
end
end end
return true
end end
return true
end end
-- Reads history from legacy history folder --- Reads history from legacy history folder.
function ReadHistory:_readLegacyHistory() function ReadHistory:_readLegacyHistory()
local history_updated
local history_dir = DataStorage:getHistoryDir() local history_dir = DataStorage:getHistoryDir()
for f in lfs.dir(history_dir) do for f in lfs.dir(history_dir) do
local path = joinPath(history_dir, f) local path = joinPath(history_dir, f)
@ -148,14 +150,20 @@ function ReadHistory:_readLegacyHistory()
if path ~= nil and path ~= "" then if path ~= nil and path ~= "" then
local file = DocSettings:getNameFromHistory(f) local file = DocSettings:getNameFromHistory(f)
if file ~= nil and file ~= "" then if file ~= nil and file ~= "" then
table.insert( local item_path = joinPath(path, file)
self.hist, local item_time = lfs.attributes(joinPath(history_dir, f), "modification")
buildEntry(lfs.attributes(joinPath(history_dir, f), "modification"), if self:addItem(item_path, item_time, true) then
joinPath(path, file))) history_updated = true
end
end end
end end
end end
end end
if history_updated then
self:_reduce()
self:_flush()
self:ensureLastFile()
end
end end
function ReadHistory:_init() function ReadHistory:_init()
@ -163,10 +171,10 @@ function ReadHistory:_init()
end end
function ReadHistory:ensureLastFile() function ReadHistory:ensureLastFile()
local last_existing_file = nil local last_existing_file
for i=1, #self.hist do for _, v in ipairs(self.hist) do
if lfs.attributes(self.hist[i].file, "mode") == "file" then if lfs.attributes(v.file, "mode") == "file" then
last_existing_file = self.hist[i].file last_existing_file = v.file
break break
end end
end end
@ -178,47 +186,63 @@ function ReadHistory:getLastFile()
return G_reader_settings:readSetting("lastfile") return G_reader_settings:readSetting("lastfile")
end end
--- Get last or previous file in history that is not current_file
-- (self.ui.document.file, provided as current_file, might have
-- been removed from history).
function ReadHistory:getPreviousFile(current_file) function ReadHistory:getPreviousFile(current_file)
-- Get last or previous file in history that is not current_file
-- (self.ui.document.file, probided as current_file, might have
-- been removed from history)
if not current_file then if not current_file then
current_file = G_reader_settings:readSetting("lastfile") current_file = G_reader_settings:readSetting("lastfile")
end end
for i=1, #self.hist do for _, v in ipairs(self.hist) do
-- skip current document and deleted items kept in history -- skip current document and deleted items kept in history
local file = self.hist[i].file if v.file ~= current_file and lfs.attributes(v.file, "mode") == "file" then
if file ~= current_file and lfs.attributes(file, "mode") == "file" then return v.file
return file
end end
end end
end end
--- Used in the BookShortcuts plugin.
function ReadHistory:getFileByDirectory(directory, recursive) function ReadHistory:getFileByDirectory(directory, recursive)
local real_path = realpath(directory) local real_path = realpath(directory)
for i=1, #self.hist do for _, v in ipairs(self.hist) do
local ipath = realpath(ffiutil.dirname(self.hist[i].file)) local ipath = realpath(ffiutil.dirname(v.file))
if ipath == real_path or (recursive and util.stringStartsWith(ipath, real_path)) then if ipath == real_path or (recursive and util.stringStartsWith(ipath, real_path)) then
return self.hist[i].file return v.file
end end
end end
end end
function ReadHistory:updateItemByPath(old_path, new_path)
local index = self:getIndexByFile(old_path)
if index then
self.hist[index].file = new_path
self.hist[index].text = new_path:gsub(".*/", "")
self.hist[index].callback = function()
selectCallback(new_path)
end
self:_flush()
self:reload(true)
end
if G_reader_settings:readSetting("lastfile") == old_path then
G_reader_settings:saveSetting("lastfile", new_path)
end
self:ensureLastFile()
end
--- Updates the history list after deleting a file.
function ReadHistory:fileDeleted(path) function ReadHistory:fileDeleted(path)
if G_reader_settings:isTrue("autoremove_deleted_items_from_history") then local index = self:getIndexByFile(path)
self:removeItemByPath(path) if index then
else if G_reader_settings:isTrue("autoremove_deleted_items_from_history") then
-- Make it dimed self:removeItem(self.hist[index], index)
for i=1, #self.hist do else
if self.hist[i].file == path then self.hist[index].dim = true
self.hist[i].dim = true self:ensureLastFile()
break
end
end end
self:ensureLastFile()
end end
end end
--- Removes the history item if the document settings has been reset.
function ReadHistory:fileSettingsPurged(path) function ReadHistory:fileSettingsPurged(path)
if G_reader_settings:isTrue("autoremove_deleted_items_from_history") then if G_reader_settings:isTrue("autoremove_deleted_items_from_history") then
-- Also remove it from history on purge when that setting is enabled -- Also remove it from history on purge when that setting is enabled
@ -226,81 +250,76 @@ function ReadHistory:fileSettingsPurged(path)
end end
end end
--- Checks the history list for deleted files and removes history items respectively.
function ReadHistory:clearMissing() function ReadHistory:clearMissing()
for i = #self.hist, 1, -1 do local history_updated
if self.hist[i].file == nil or lfs.attributes(self.hist[i].file, "mode") ~= "file" then for i, v in ipairs(self.hist) do
self:removeItem(self.hist[i], i) if v.file == nil or lfs.attributes(v.file, "mode") ~= "file" then
self:removeItem(v, i, true) -- no flush
history_updated = true
end end
end end
self:ensureLastFile() if history_updated then
end self:_flush()
self:ensureLastFile()
function ReadHistory:removeItemByPath(path)
for i = #self.hist, 1, -1 do
if self.hist[i].file == path then
self:removeItem(self.hist[i])
break
end
end end
self:ensureLastFile()
end end
function ReadHistory:updateItemByPath(old_path, new_path) function ReadHistory:removeItemByPath(path)
for i = #self.hist, 1, -1 do local index = self:getIndexByFile(path)
if self.hist[i].file == old_path then if index then
self.hist[i].file = new_path self:removeItem(self.hist[index], index)
self.hist[i].text = new_path:gsub(".*/", "")
self:_flush()
self:reload(true)
self.hist[i].callback = function()
selectCallback(new_path)
end
break
end
end
if G_reader_settings:readSetting("lastfile") == old_path then
G_reader_settings:saveSetting("lastfile", new_path)
end end
self:ensureLastFile()
end end
function ReadHistory:removeItem(item, idx) function ReadHistory:removeItem(item, idx, no_flush)
table.remove(self.hist, item.index or idx) local index = idx or self:getIndexByTime(item.time, item.file:gsub(".*/", ""))
table.remove(self.hist, index)
os.remove(DocSettings:getHistoryPath(item.file)) os.remove(DocSettings:getHistoryPath(item.file))
self:_indexing(item.index or idx) if not no_flush then
self:_flush() self:_flush()
self:ensureLastFile() self:ensureLastFile()
end
end end
function ReadHistory:addItem(file, ts) --- Adds new item (last opened document) to the top of the history list.
-- If item time (ts) is passed, add item to the history list at this time position.
function ReadHistory:addItem(file, ts, no_flash)
if file ~= nil and lfs.attributes(file, "mode") == "file" then if file ~= nil and lfs.attributes(file, "mode") == "file" then
local index = self:getIndexByFile(realpath(file))
if ts and index and self.hist[index].time == ts then
return -- this legacy item is in the history already
end
local now = ts or os.time() local now = ts or os.time()
table.insert(self.hist, 1, buildEntry(now, file))
--- @todo (zijiehe): We do not need to sort if we can use binary insert and
-- binary search.
-- util.execute("/bin/touch", "-a", file)
-- This emulates `touch -a` in LuaFileSystem's API, since it may be absent (Android)
-- or provided by busybox, which doesn't support the `-a` flag.
local mtime = lfs.attributes(file, "modification") local mtime = lfs.attributes(file, "modification")
lfs.touch(file, now, mtime) lfs.touch(file, now, mtime)
self:_sort() if index == 1 and not ts then -- last book
self:_reduce() self.hist[1].time = now
self:_flush() else -- old or new book
G_reader_settings:saveSetting("lastfile", file) if index then -- old book
table.remove(self.hist, index)
end
index = ts and self:getIndexByTime(ts, file:gsub(".*/", "")) or 1
table.insert(self.hist, index, buildEntry(now, file))
end
if not no_flash then
self:_reduce()
self:_flush()
self:ensureLastFile()
end
return true -- used while adding legacy items
end end
end end
--- Reloads history from history_file. --- Reloads history from history_file and legacy history folder.
-- @treturn boolean true if history_file has been updated and reload happened.
function ReadHistory:reload(force_read) function ReadHistory:reload(force_read)
if self:_read(force_read) then if self:_read(force_read) then
self:_readLegacyHistory() self:_readLegacyHistory()
self:_sort() if G_reader_settings:isTrue("autoremove_deleted_items_from_history") then
self:clearMissing()
end
self:_reduce() self:_reduce()
return true
end end
return false
end end
ReadHistory:_init() ReadHistory:_init()

@ -43,7 +43,6 @@ describe("ReadHistory module", function()
local function assert_item_is(h, i, name, fileRemoved) local function assert_item_is(h, i, name, fileRemoved)
assert.is.same(name, h.hist[i].text) assert.is.same(name, h.hist[i].text)
assert.is.same(i, h.hist[i].index)
assert.is.same(joinPath(realpath(test_data_dir()), name), h.hist[i].file) assert.is.same(joinPath(realpath(test_data_dir()), name), h.hist[i].file)
if fileRemoved then if fileRemoved then
assert.is_nil(realpath(test_file(name))) assert.is_nil(realpath(test_file(name)))
@ -260,28 +259,6 @@ describe("ReadHistory module", function()
rm(test_file("e")) rm(test_file("e"))
end) end)
it("should remove duplicate entry", function()
rm(file("history.lua"))
touch(test_file("a"))
touch(test_file("b"))
local h = reload()
now = now + 61
h:addItem(test_file("b"), now)
now = now + 61
h:addItem(test_file("b"), now)
touch(legacy_history_file("a"))
now = now + 61
h:addItem(test_file("a"), now) -- ensure a is before b
h = reload()
assert.is.same(2, #h.hist)
assert_item_is(h, 1, "a")
assert_item_is(h, 2, "b")
rm(legacy_history_file("a"))
rm(test_file("a"))
rm(test_file("b"))
end)
it("should reduce the total count", function() it("should reduce the total count", function()
local function to_file(i) local function to_file(i)
return test_file(string.format("%04d", i)) return test_file(string.format("%04d", i))

Loading…
Cancel
Save