mirror of
https://github.com/kristoferssolo/telescope-frecency.nvim.git
synced 2025-10-21 20:10:38 +00:00
refactor
This commit is contained in:
parent
afbb26d1e8
commit
d1fc0f5515
@ -58,9 +58,11 @@ local function get_file_scores()
|
||||
|
||||
local queries = sql_wrapper.queries
|
||||
local scores = {}
|
||||
local files = sql_wrapper:do_eval(queries.get_all_filepaths)
|
||||
local timestamp_ages = sql_wrapper:do_eval(queries.get_all_timestamp_ages)
|
||||
local files = sql_wrapper:do_transaction(queries.file_get_entries, {})
|
||||
local timestamp_ages = sql_wrapper:do_transaction(queries.timestamp_get_all_entry_ages, {})
|
||||
|
||||
-- print(vim.inspect(files))
|
||||
-- print(vim.inspect(timestamp_ages))
|
||||
if vim.tbl_isempty(files) then return scores end
|
||||
|
||||
for _, file_entry in ipairs(files) do
|
||||
@ -99,13 +101,13 @@ local function validate()
|
||||
if not sql_wrapper then return {} end
|
||||
|
||||
local queries = sql_wrapper.queries
|
||||
local files = sql_wrapper:do_transaction(queries.get_all_filepaths)
|
||||
local files = sql_wrapper:do_transaction(queries.file_get_entry, {})
|
||||
for _, entry in pairs(files) do
|
||||
if not util.fs_stat(entry.path).exists then
|
||||
-- remove entries from file and timestamp tables
|
||||
print("removing entry: " .. entry.path .. "[" .. entry.id .."]")
|
||||
sql_wrapper:do_transaction(queries.file_delete_entry, { id = entry.id })
|
||||
sql_wrapper:do_transaction(queries.timestamp_delete_with_file_id, { file_id = entry.id })
|
||||
sql_wrapper:do_eval(queries.file_delete_entry, { id = entry.id })
|
||||
sql_wrapper:do_eval(queries.timestamp_delete_with_file_id, { file_id = entry.id })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -10,26 +10,13 @@ end
|
||||
local MAX_TIMESTAMPS = 10
|
||||
|
||||
|
||||
-- TODO: replace at least SELECT evals with db:select()
|
||||
local queries = {
|
||||
file_add_entry = "INSERT INTO files (path, count) values(:path, 1);",
|
||||
file_delete_entry = "DELETE FROM files WHERE id == :id;",
|
||||
file_update_counter = "UPDATE files SET count = count + 1 WHERE path == :path;",
|
||||
timestamp_add_entry = "INSERT INTO timestamps (file_id, timestamp) values(:file_id, julianday('now'));",
|
||||
timestamp_delete_before_id = "DELETE FROM timestamps WHERE id < :id and file_id == :file_id;",
|
||||
timestamp_delete_with_file_id = "DELETE FROM timestamps WHERE file_id == :file_id;",
|
||||
get_all_filepaths = "SELECT * FROM files;",
|
||||
get_all_timestamp_ages = "SELECT id, file_id, CAST((julianday('now') - julianday(timestamp)) * 24 * 60 AS INTEGER) AS age FROM timestamps;",
|
||||
get_timestamp_ids_for_file = "SELECT id FROM timestamps WHERE file_id == :file_id;",
|
||||
}
|
||||
|
||||
-- local ignore_patterns = {
|
||||
-- }
|
||||
|
||||
|
||||
--
|
||||
|
||||
local M = {}
|
||||
M.queries = queries
|
||||
|
||||
function M:new()
|
||||
local o = {}
|
||||
@ -70,7 +57,6 @@ function M:bootstrap(opts)
|
||||
ensure = true,
|
||||
id = {"INTEGER", "PRIMARY", "KEY"},
|
||||
file_id = "INTEGER",
|
||||
count = "INTEGER",
|
||||
timestamp = "REAL"
|
||||
-- FOREIGN KEY(file_id) REFERENCES files(id)
|
||||
})
|
||||
@ -78,19 +64,67 @@ function M:bootstrap(opts)
|
||||
|
||||
end
|
||||
|
||||
-------------------------------------------
|
||||
|
||||
function M:do_eval(query, params)
|
||||
local res
|
||||
|
||||
self.db:with_open(function(db) res = db:eval(query, params) end)
|
||||
if res == true then res = {} end -- cater for eval returning true on empty set
|
||||
return res
|
||||
function M:do_transaction(t, params)
|
||||
local transaction = function(sql_cmd)
|
||||
return self.db:with_open(function(db)
|
||||
local case = {
|
||||
[1] = function() return db:select(t.cmd_data, params) end,
|
||||
[2] = function() return db:insert(t.cmd_data, params) end,
|
||||
[3] = function() return db:eval(t.cmd_data, params) end,
|
||||
}
|
||||
return case[sql_cmd]()
|
||||
end)
|
||||
end
|
||||
|
||||
function M:get_filepath_row_id(filepath)
|
||||
local res
|
||||
self.db:with_open(function(db) res = db:select("files", { where = { path = filepath}}) end)
|
||||
return not vim.tbl_isempty(res) and res[1].id or nil
|
||||
return transaction(t.cmd)
|
||||
end
|
||||
|
||||
local cmd = {
|
||||
select = 1,
|
||||
insert = 2,
|
||||
eval = 3,
|
||||
}
|
||||
|
||||
local queries = {
|
||||
file_add_entry = {
|
||||
cmd = cmd.insert,
|
||||
cmd_data = "files"
|
||||
},
|
||||
file_get_entries = {
|
||||
cmd = cmd.select,
|
||||
cmd_data = "files"
|
||||
},
|
||||
file_update_counter = {
|
||||
cmd = cmd.eval,
|
||||
cmd_data = "UPDATE files SET count = count + 1 WHERE path == :path;"
|
||||
},
|
||||
timestamp_add_entry = {
|
||||
cmd = cmd.eval,
|
||||
cmd_data = "INSERT INTO timestamps (file_id, timestamp) values(:file_id, julianday('now'));"
|
||||
},
|
||||
timestamp_get_all_entries = {
|
||||
cmd = cmd.select,
|
||||
cmd_data = "timestamps",
|
||||
},
|
||||
timestamp_get_all_entry_ages = {
|
||||
cmd = cmd.eval,
|
||||
cmd_data = "SELECT id, file_id, CAST((julianday('now') - julianday(timestamp)) * 24 * 60 AS INTEGER) AS age FROM timestamps;"
|
||||
},
|
||||
timestamp_delete_before_id = {
|
||||
cmd = cmd.eval,
|
||||
cmd_data = "DELETE FROM timestamps WHERE id < :id and file_id == :file_id;"
|
||||
},
|
||||
-- timestamp_delete_with_file_id = "DELETE FROM timestamps WHERE file_id == :file_id;",
|
||||
}
|
||||
|
||||
M.queries = queries
|
||||
|
||||
--
|
||||
|
||||
local function row_id(entry)
|
||||
return (not vim.tbl_isempty(entry)) and entry[1].id or nil
|
||||
end
|
||||
|
||||
function M:update(filepath)
|
||||
@ -102,23 +136,23 @@ function M:update(filepath)
|
||||
|
||||
-- create entry if it doesn't exist
|
||||
local file_id
|
||||
file_id = self:get_filepath_row_id(filepath)
|
||||
file_id = row_id(self:do_transaction(queries.file_get_entries, {where = {path = filepath}}))
|
||||
if not file_id then
|
||||
self:do_eval(queries.file_add_entry, { path = filepath })
|
||||
file_id = self:get_filepath_row_id(filepath)
|
||||
self:do_transaction(queries.file_add_entry, {path = filepath, count = 1})
|
||||
file_id = row_id(self:do_transaction(queries.file_get_entries, {where = {path = filepath}}))
|
||||
else
|
||||
-- ..or update existing entry
|
||||
self:do_eval(queries.file_update_counter, { path = filepath })
|
||||
self:do_transaction(queries.file_update_counter, {path = filepath})
|
||||
end
|
||||
|
||||
-- register timestamp for this update
|
||||
self:do_eval(queries.timestamp_add_entry, { file_id = file_id })
|
||||
self:do_transaction(queries.timestamp_add_entry, {file_id = file_id})
|
||||
|
||||
-- trim timestamps to MAX_TIMESTAMPS per file (there should be up to MAX_TS + 1 at this point)
|
||||
local timestamps = self:do_eval(queries.get_timestamp_ids_for_file, { file_id = file_id })
|
||||
local timestamps = self:do_transaction(queries.timestamp_get_all_entries, {where = {file_id = file_id}})
|
||||
local trim_at = timestamps[(#timestamps - MAX_TIMESTAMPS) + 1]
|
||||
if trim_at then
|
||||
self:do_eval(queries.timestamp_delete_before_id, { id = trim_at.id, file_id = file_id })
|
||||
self:do_transaction(queries.timestamp_delete_before_id, {id = trim_at.id, file_id = file_id})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user