Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frecency)!: Use byte strings for DB IDs #473

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ jobs:
with:
repository: Olivine-Labs/luassert
path: vendor/luassert
- name: Checkout sqlite.lua
uses: actions/checkout@v3
with:
repository: kkharji/sqlite.lua
path: vendor/sqlite
- name: Run Unit Tests
run: make test
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ ensure-test-deps:
@mkdir -p vendor
@if test ! -d ./vendor/plenary.nvim; then git clone [email protected]:nvim-lua/plenary.nvim.git ./vendor/plenary.nvim/; fi
@if test ! -d ./vendor/luassert; then git clone [email protected]:Olivine-Labs/luassert.git ./vendor/luassert/; fi
@if test ! -d ./vendor/sqlite; then git clone [email protected]:kkharji/sqlite.lua.git ./vendor/sqlite/; fi

.PHONY: update-test-deps
update-test-deps: ensure-test-deps
@cd ./vendor/plenary.nvim/ && git pull && cd ..
@cd ./vendor/luassert/ && git pull && cd ..
@cd ./vendor/sqlite/ && git pull && cd ..

.PHONY: ensure-doc-deps
ensure-doc-deps:
Expand Down
4 changes: 2 additions & 2 deletions lua/legendary/api/db/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function M.update_item_score(item)
M.db_wrapper:update(item)
end

function M.sql_escape(str)
return M.db_wrapper.sql_escape(str)
function M.to_bytes(str)
return M.db_wrapper.to_bytes(str)
end

function M.get_client()
Expand Down
10 changes: 7 additions & 3 deletions lua/legendary/api/db/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,18 @@ local function row_id(row)
return (not vim.tbl_isempty(row)) and row[1].id or nil
end

function M.sql_escape(str)
return string.format("'%s'", string.gsub(str, "'", "\\'"))
function M.to_bytes(str)
local result = ''
for c in str:gmatch('.') do
result = result .. string.byte(c)
end
return result
end

---Update the stored data for an item
---@param item LegendaryItem
function M:update(item)
local item_id = M.sql_escape(item:frecency_id())
local item_id = M.to_bytes(item:frecency_id())
Log.trace('Updating item with ID "%s"', item_id)
local entry_id = row_id(self:transaction(self.queries.item_get_entries, { where = { item_id = item_id } }))
if not entry_id then
Expand Down
4 changes: 2 additions & 2 deletions lua/legendary/data/itemlist.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ function ItemList:sort_inplace(opts)
---@param item1 LegendaryItem
---@param item2 LegendaryItem
function(item1, item2)
local item1_id = DbClient.sql_escape(item1:frecency_id())
local item2_id = DbClient.sql_escape(item2:frecency_id())
local item1_id = DbClient.to_bytes(item1:frecency_id())
local item2_id = DbClient.to_bytes(item2:frecency_id())
local item1_score = frecency_scores[item1_id] or 0
local item2_score = frecency_scores[item2_id] or 0
return item1_score > item2_score
Expand Down
16 changes: 16 additions & 0 deletions tests/legendary/to_bytes_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local assert = require('luassert')
local wrapper = require('legendary.api.db.wrapper')

describe('to_bytes function', function()
local test_data = {
['test string'] = '11610111511632115116114105110103',
['with icons 󰊢'] = '119105116104321059911111011532243176138162238158168238153135',
['with quote chars \'"'] = '11910511610432113117111116101329910497114115323934',
['with emoji 🦀'] = '1191051161043210110911110610532240159166128',
}
for input, output in pairs(test_data) do
it(string.format('for input `%s`, output should be "%s"', input, output), function()
assert.are.same(wrapper.to_bytes(input), output)
end)
end
end)
1 change: 1 addition & 0 deletions tests/testrc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ vim.cmd([[
set rtp+=.
set rtp+=vendor/plenary.nvim
set rtp+=vendor/luassert
set rtp+=vendor/sqlite
runtime plugin/plenary.vim
]])
require('plenary.busted')
Loading