Comparison

util/datamanager.lua @ 13236:9c72f93b7a02

util.datamanager: Add way to close indexed list store
author Kim Alvefur <zash@zash.se>
date Fri, 21 Jul 2023 18:30:06 +0200
parent 13235:dbd7a6b09ada
child 13238:26327eac56dc
comparison
equal deleted inserted replaced
13235:dbd7a6b09ada 13236:9c72f93b7a02
459 return success, ret; 459 return success, ret;
460 end 460 end
461 return item; 461 return item;
462 end 462 end
463 463
464 local function list_close(list)
465 if list.index and list.index.file then
466 list.index.file:close();
467 end
468 return list.file:close();
469 end
470
464 local indexed_list_mt = { 471 local indexed_list_mt = {
465 __index = function(t, i) 472 __index = function(t, i)
466 if type(i) ~= "number" or i % 1 ~= 0 or i < 1 then 473 if type(i) ~= "number" or i % 1 ~= 0 or i < 1 then
467 return 474 return
468 end 475 end
474 return item; 481 return item;
475 end; 482 end;
476 __len = function(t) 483 __len = function(t)
477 return #t.index; 484 return #t.index;
478 end; 485 end;
486 __close = list_close;
479 } 487 }
480 488
481 local function list_load(username, host, datastore) 489 local function list_load(username, host, datastore)
482 local items = {}; 490 local items = {};
483 local data, err, errno = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); 491 local data, err, errno = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end});
514 local index, err = get_list_index(username, host, datastore); 522 local index, err = get_list_index(username, host, datastore);
515 if not index then 523 if not index then
516 file:close() 524 file:close()
517 return index, err; 525 return index, err;
518 end 526 end
519 return setmetatable({ file = file; index = index }, indexed_list_mt); 527 return setmetatable({ file = file; index = index; close = list_close }, indexed_list_mt);
520 end 528 end
521 529
522 local function shift_index(index_filename, index, trim_to, offset) 530 local function shift_index(index_filename, index, trim_to, offset)
523 local index_scratch = index_filename .. "~"; 531 local index_scratch = index_filename .. "~";
524 local new_index, err = io_open(index_scratch, "w"); 532 local new_index, err = io_open(index_scratch, "w");