fix caching
This commit is contained in:
parent
6b8a1e2087
commit
91e6fbe455
4 changed files with 83 additions and 54 deletions
|
|
@ -104,19 +104,37 @@ function M.set_contest_data(platform, contest_id, problems)
|
|||
problems = { problems, 'table' },
|
||||
})
|
||||
|
||||
if not cache_data[platform] then
|
||||
cache_data[platform] = {}
|
||||
cache_data[platform] = cache_data[platform] or {}
|
||||
local existing = cache_data[platform][contest_id] or {}
|
||||
|
||||
local existing_by_id = {}
|
||||
if existing.problems then
|
||||
for _, p in ipairs(existing.problems) do
|
||||
existing_by_id[p.id] = p
|
||||
end
|
||||
end
|
||||
|
||||
cache_data[platform][contest_id] = {
|
||||
problems = problems,
|
||||
}
|
||||
cache_data[platform][contest_id].index_map = {}
|
||||
|
||||
for i, problem in ipairs(problems) do
|
||||
cache_data[platform][contest_id].index_map[problem.id] = i
|
||||
local merged = {}
|
||||
for _, p in ipairs(problems) do
|
||||
local prev = existing_by_id[p.id] or {}
|
||||
local merged_p = {
|
||||
id = p.id,
|
||||
name = p.name or prev.name,
|
||||
test_cases = prev.test_cases,
|
||||
timeout_ms = prev.timeout_ms,
|
||||
memory_mb = prev.memory_mb,
|
||||
interactive = prev.interactive,
|
||||
}
|
||||
table.insert(merged, merged_p)
|
||||
end
|
||||
|
||||
existing.problems = merged
|
||||
existing.index_map = {}
|
||||
for i, p in ipairs(merged) do
|
||||
existing.index_map[p.id] = i
|
||||
end
|
||||
|
||||
cache_data[platform][contest_id] = existing
|
||||
M.save()
|
||||
end
|
||||
|
||||
|
|
@ -151,6 +169,7 @@ function M.get_test_cases(platform, contest_id, problem_id)
|
|||
or not cache_data[platform][contest_id].problems
|
||||
or not cache_data[platform][contest_id].index_map
|
||||
then
|
||||
print('bad, failing')
|
||||
return nil
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -9,11 +9,19 @@ local platforms = constants.PLATFORMS
|
|||
function M.handle_cache_command(cmd)
|
||||
if cmd.subcommand == 'read' then
|
||||
local data = cache.get_data_pretty()
|
||||
local name = 'cp.nvim://cache.lua'
|
||||
|
||||
local buf = vim.api.nvim_create_buf(true, true)
|
||||
vim.api.nvim_buf_set_name(buf, 'cp.nvim://cache.lua')
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(data, '\n'))
|
||||
vim.bo[buf].filetype = 'lua'
|
||||
local existing = vim.fn.bufnr(name)
|
||||
local buf
|
||||
if existing ~= -1 then
|
||||
buf = existing
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(data, '\n'))
|
||||
else
|
||||
buf = vim.api.nvim_create_buf(true, true)
|
||||
vim.api.nvim_buf_set_name(buf, name)
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(data, '\n'))
|
||||
vim.bo[buf].filetype = 'lua'
|
||||
end
|
||||
|
||||
vim.api.nvim_set_current_buf(buf)
|
||||
elseif cmd.subcommand == 'clear' then
|
||||
|
|
|
|||
|
|
@ -69,16 +69,23 @@ end
|
|||
function M.scrape_contest_metadata(platform, contest_id, callback)
|
||||
run_scraper(platform, 'metadata', { contest_id }, {
|
||||
on_exit = function(result)
|
||||
if not result.success or vim.tbl_isempty(result.data.problems) then
|
||||
if not result or not result.success then
|
||||
logger.log(
|
||||
('Failed to scrape metadata for %s contest %s - aborting.'):format(platform, contest_id),
|
||||
('Failed to scrape metadata for %s contest %s.'):format(platform, contest_id),
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
return
|
||||
end
|
||||
local data = result.data or {}
|
||||
if not data.problems or #data.problems == 0 then
|
||||
logger.log(
|
||||
('No problems returned for %s contest %s.'):format(platform, contest_id),
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
if type(callback) == 'function' then
|
||||
callback(result.data)
|
||||
callback(data)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -28,39 +28,31 @@ function M.set_platform(platform)
|
|||
return true
|
||||
end
|
||||
|
||||
local function scrape_contest_problems(problems)
|
||||
local function backfill_missing_tests(platform, contest_id, problems)
|
||||
cache.load()
|
||||
local missing_problems = {}
|
||||
|
||||
local platform, contest_id = state.get_platform() or '', state.get_contest_id() or ''
|
||||
|
||||
local missing = {}
|
||||
for _, prob in ipairs(problems) do
|
||||
local cached_tests = cache.get_test_cases(platform, contest_id, prob.id)
|
||||
if not cached_tests then
|
||||
table.insert(missing_problems, prob)
|
||||
if not cache.get_test_cases(platform, contest_id, prob.id) then
|
||||
table.insert(missing, prob.id)
|
||||
end
|
||||
end
|
||||
|
||||
if vim.tbl_isempty(missing_problems) then
|
||||
if #missing == 0 then
|
||||
logger.log(('All problems already cached for %s contest %s.'):format(platform, contest_id))
|
||||
return
|
||||
end
|
||||
|
||||
for _, prob in ipairs(missing_problems) do
|
||||
scraper.scrape_problem_tests(platform, contest_id, prob.id, function(result)
|
||||
for _, pid in ipairs(missing) do
|
||||
local captured = pid
|
||||
scraper.scrape_problem_tests(platform, contest_id, captured, function(result)
|
||||
local cached_tests = {}
|
||||
for i, test_case in ipairs(result.tests) do
|
||||
table.insert(cached_tests, {
|
||||
index = i,
|
||||
input = test_case.input,
|
||||
expected = test_case.expected,
|
||||
})
|
||||
if result.tests then
|
||||
for i, t in ipairs(result.tests) do
|
||||
cached_tests[i] = { index = i, input = t.input, expected = t.expected }
|
||||
end
|
||||
end
|
||||
|
||||
cache.set_test_cases(
|
||||
platform,
|
||||
contest_id,
|
||||
state.get_problem_id(),
|
||||
captured,
|
||||
cached_tests,
|
||||
result.timeout_ms,
|
||||
result.memory_mb
|
||||
|
|
@ -76,34 +68,34 @@ function M.setup_contest(platform, contest_id, language, problem_id)
|
|||
end
|
||||
|
||||
local config = config_module.get_config()
|
||||
|
||||
if not vim.tbl_contains(config.scrapers, platform) then
|
||||
logger.log(('Scraping disabled for %s.'):format(platform), vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
state.set_contest_id(contest_id)
|
||||
cache.load()
|
||||
local contest_data = cache.get_contest_data(platform, contest_id)
|
||||
|
||||
if
|
||||
not contest_data
|
||||
or not contest_data.problems
|
||||
or (problem_id and not cache.get_test_cases(platform, contest_id, problem_id))
|
||||
then
|
||||
if not contest_data or not contest_data.problems then
|
||||
logger.log('Fetching contests problems...', vim.log.levels.INFO, true)
|
||||
scraper.scrape_contest_metadata(platform, contest_id, function(result)
|
||||
local problems = result.problems
|
||||
|
||||
local problems = result.problems or {}
|
||||
cache.set_contest_data(platform, contest_id, problems)
|
||||
|
||||
logger.log(('Found %d problems for %s contest %s.'):format(#problems, platform, contest_id))
|
||||
|
||||
M.setup_problem(problem_id or problems[1].id, language)
|
||||
|
||||
scrape_contest_problems(problems)
|
||||
local pid = problem_id or (problems[1] and problems[1].id)
|
||||
if pid then
|
||||
M.setup_problem(pid, language)
|
||||
end
|
||||
backfill_missing_tests(platform, contest_id, problems)
|
||||
end)
|
||||
else
|
||||
M.setup_problem(problem_id, language)
|
||||
local problems = contest_data.problems
|
||||
local pid = problem_id or (problems[1] and problems[1].id)
|
||||
if pid then
|
||||
M.setup_problem(pid, language)
|
||||
end
|
||||
backfill_missing_tests(platform, contest_id, problems)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -175,7 +167,6 @@ function M.navigate_problem(direction, language)
|
|||
if direction == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
direction = direction > 0 and 1 or -1
|
||||
|
||||
local platform = state.get_platform()
|
||||
|
|
@ -204,13 +195,17 @@ function M.navigate_problem(direction, language)
|
|||
end
|
||||
|
||||
local problems = contest_data.problems
|
||||
local current_index = nil
|
||||
local current_index
|
||||
for i, prob in ipairs(problems) do
|
||||
if prob.id == current_problem_id then
|
||||
current_index = i
|
||||
break
|
||||
end
|
||||
end
|
||||
if not current_index then
|
||||
M.setup_contest(platform, contest_id, language, problems[1].id)
|
||||
return
|
||||
end
|
||||
|
||||
local new_index = current_index + direction
|
||||
if new_index < 1 or new_index > #problems then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue