Comparison

util/datamanager.lua @ 13238:26327eac56dc

util.datamanager: Always reset index after list shift Shifting the index does not work reliably yet, better to rebuild it from scratch. Since there is minimal parsing involved in that, it should be more efficient anyway.
author Kim Alvefur <zash@zash.se>
date Sat, 22 Jul 2023 14:02:01 +0200
parent 13236:9c72f93b7a02
comparison
equal deleted inserted replaced
13237:59c3d775c7fa 13238:26327eac56dc
525 return index, err; 525 return index, err;
526 end 526 end
527 return setmetatable({ file = file; index = index; close = list_close }, indexed_list_mt); 527 return setmetatable({ file = file; index = index; close = list_close }, indexed_list_mt);
528 end 528 end
529 529
530 local function shift_index(index_filename, index, trim_to, offset) 530 local function shift_index(index_filename, index, trim_to, offset) -- luacheck: ignore 212
531 local index_scratch = index_filename .. "~"; 531 os_remove(index_filename);
532 local new_index, err = io_open(index_scratch, "w"); 532 return "deleted";
533 if not new_index then 533 -- TODO move and recalculate remaining items
534 os_remove(index_filename);
535 return "deleted", err;
536 end
537
538 local ok, err = new_index:write(index_magic);
539 if not ok then
540 new_index:close();
541 os_remove(index_filename);
542 os_remove(index_scratch);
543 return "deleted", err;
544 end
545
546 if not index.file or not index.file:seek("set", index_item_size * trim_to) then
547 new_index:close();
548 os_remove(index_filename);
549 os_remove(index_scratch);
550 return "deleted";
551 else
552 local pack, unpack = string.pack, string.unpack;
553 for item in index.file:lines(index_item_size) do
554 local ok, err = new_index:write(pack(index_fmt, unpack(index_fmt, item) - offset));
555 if not ok then
556 os_remove(index_filename);
557 os_remove(index_scratch);
558 return "deleted", err;
559 end
560 end
561 local ok, err = new_index:close();
562 if not ok then
563 os_remove(index_filename);
564 os_remove(index_scratch);
565 return "deleted", err;
566 end
567 return os_rename(index_scratch, index_filename);
568 end
569 end 534 end
570 535
571 local function list_shift(username, host, datastore, trim_to) 536 local function list_shift(username, host, datastore, trim_to)
572 if trim_to == 1 then 537 if trim_to == 1 then
573 return true 538 return true