From a12e98f7c701dab4711cf5fff43524a85abc1655 Mon Sep 17 00:00:00 2001 From: Senghan Bright Date: Sat, 16 Jan 2021 01:05:23 +0100 Subject: [PATCH] refactor: don't use string keys --- lua/telescope/_extensions/frecency.lua | 2 +- .../_extensions/frecency/db_client.lua | 12 ++-- .../_extensions/frecency/sql_wrapper.lua | 67 +++++++++---------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/lua/telescope/_extensions/frecency.lua b/lua/telescope/_extensions/frecency.lua index 13a5fb8..53dce68 100644 --- a/lua/telescope/_extensions/frecency.lua +++ b/lua/telescope/_extensions/frecency.lua @@ -9,7 +9,7 @@ end -- print("start") local db_client = require("telescope._extensions.frecency.db_client") -- vim.defer_fn(db_client.init, 100) -- TODO: this is a crappy attempt to lessen loadtime impact, use VimEnter? -db_client.init() -- TODO: this is a crappy attempt to lessen loadtime impact, use VimEnter? +db_client.init() -- finder code diff --git a/lua/telescope/_extensions/frecency/db_client.lua b/lua/telescope/_extensions/frecency/db_client.lua index 00fe0e9..de5fb9d 100644 --- a/lua/telescope/_extensions/frecency/db_client.lua +++ b/lua/telescope/_extensions/frecency/db_client.lua @@ -56,9 +56,10 @@ end local function get_file_scores() if not sql_wrapper then return {} end + local queries = sql_wrapper.queries local scores = {} - local files = sql_wrapper:do_transaction('get_all_filepaths') - local timestamp_ages = sql_wrapper:do_transaction('get_all_timestamp_ages') + local files = sql_wrapper:do_eval(queries.get_all_filepaths) + local timestamp_ages = sql_wrapper:do_eval(queries.get_all_timestamp_ages) if vim.tbl_isempty(files) then return scores end @@ -97,13 +98,14 @@ end local function validate() if not sql_wrapper then return {} end - local files = sql_wrapper:do_transaction('get_all_filepaths') + local queries = sql_wrapper.queries + local files = sql_wrapper:do_transaction(queries.get_all_filepaths) 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('file_delete_entry', { id = entry.id }) - sql_wrapper:do_transaction('timestamp_delete_with_file_id', { file_id = 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 }) end end end diff --git a/lua/telescope/_extensions/frecency/sql_wrapper.lua b/lua/telescope/_extensions/frecency/sql_wrapper.lua index f5ec0a5..763c4c6 100644 --- a/lua/telescope/_extensions/frecency/sql_wrapper.lua +++ b/lua/telescope/_extensions/frecency/sql_wrapper.lua @@ -10,17 +10,17 @@ 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_row"] = "SELECT * FROM files WHERE path == :path;", - ["get_timestamp_ids_for_file"] = "SELECT id FROM timestamps WHERE file_id == :file_id;", + 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 = { @@ -62,37 +62,36 @@ function M:bootstrap(opts) -- create tables if they don't exist self.db:create("files", { ensure = true, - id = {"integer", "primary", "key"}, - count = "integer", - path = "text" + id = {"INTEGER", "PRIMARY", "KEY"}, + count = "INTEGER", + path = "TEXT" }) self.db:create("timestamps", { ensure = true, - id = {"integer", "primary", "key"}, - file_id = "integer", - count = "integer", - timestamp = "real" + id = {"INTEGER", "PRIMARY", "KEY"}, + file_id = "INTEGER", + count = "INTEGER", + timestamp = "REAL" -- FOREIGN KEY(file_id) REFERENCES files(id) }) self.db:close() end -function M:do_transaction(query, params) - if not queries[query] then - print("invalid query_preset: " .. query ) - return - end +function M:do_eval(query, params) local res - self.db:with_open(function(db) res = db:eval(queries[query], params) end) + + 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 end -function M:get_row_id(filepath) - local result = self:do_transaction('get_row', { path = filepath }) - - return type(result) == "table" and result[1].id or nil +function M:get_filepath_row_id(filepath) + local res + local func = function(db) res = db:select("files", { where = { path = filepath}}) end + self.db:with_open(func) + return res and res[1].id or nil end function M:update(filepath) @@ -104,23 +103,23 @@ function M:update(filepath) -- create entry if it doesn't exist local file_id - file_id = self:get_row_id(filepath) + file_id = self:get_filepath_row_id(filepath) if not file_id then - self:do_transaction('file_add_entry', { path = filepath }) - file_id = self:get_row_id(filepath) + self:do_eval(queries.file_add_entry, { path = filepath }) + file_id = self:get_filepath_row_id(filepath) else -- ..or update existing entry - self:do_transaction('file_update_counter', { path = filepath }) + self:do_eval(queries.file_update_counter, { path = filepath }) end -- register timestamp for this update - self:do_transaction('timestamp_add_entry', { file_id = file_id }) + self:do_eval(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_transaction('get_timestamp_ids_for_file', { file_id = file_id }) + local timestamps = self:do_eval(queries.get_timestamp_ids_for_file, { file_id = file_id }) local trim_at = timestamps[(#timestamps - MAX_TIMESTAMPS) + 1] if trim_at then - self:do_transaction('timestamp_delete_before_id', { id = trim_at.id, file_id = file_id }) + self:do_eval(queries.timestamp_delete_before_id, { id = trim_at.id, file_id = file_id }) end end