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