Comparison

util/pubsub.lua @ 11767:5610f7c5b261

util.pubsub: Add support for limiting number of items to retrieve Hopefully this will eventually be upgraded to RSM, which is why the argument is called 'resultspec' and is a table.
author Kim Alvefur <zash@zash.se>
date Sun, 05 Sep 2021 16:21:10 +0200
parent 11723:3ead0967e04d
child 11854:b605cbd5f13b
comparison
equal deleted inserted replaced
11766:6ad335cd43f9 11767:5610f7c5b261
640 self:broadcast("purge", node, node_obj.subscribers, nil, actor, node_obj); 640 self:broadcast("purge", node, node_obj.subscribers, nil, actor, node_obj);
641 end 641 end
642 return true 642 return true
643 end 643 end
644 644
645 function service:get_items(node, actor, ids) --> (true, { id, [id] = node }) or (false, err) 645 function service:get_items(node, actor, ids, resultspec) --> (true, { id, [id] = node }) or (false, err)
646 -- Access checking 646 -- Access checking
647 if not self:may(node, actor, "get_items") then 647 if not self:may(node, actor, "get_items") then
648 return false, "forbidden"; 648 return false, "forbidden";
649 end 649 end
650 -- 650 --
658 end 658 end
659 if type(ids) == "string" then -- COMPAT see #1305 659 if type(ids) == "string" then -- COMPAT see #1305
660 ids = { ids }; 660 ids = { ids };
661 end 661 end
662 local data = {}; 662 local data = {};
663 local limit = resultspec and resultspec.max;
663 if ids then 664 if ids then
664 for _, key in ipairs(ids) do 665 for _, key in ipairs(ids) do
665 local value = self.data[node]:get(key); 666 local value = self.data[node]:get(key);
666 if value then 667 if value then
667 data[#data+1] = key; 668 data[#data+1] = key;
668 data[key] = value; 669 data[key] = value;
670 -- Limits and ids seem like a problematic combination.
671 if limit and #data >= limit then break end
669 end 672 end
670 end 673 end
671 else 674 else
672 for key, value in self.data[node]:items() do 675 for key, value in self.data[node]:items() do
673 data[#data+1] = key; 676 data[#data+1] = key;
674 data[key] = value; 677 data[key] = value;
678 if limit and #data >= limit then break
679 end
675 end 680 end
676 end 681 end
677 return true, data; 682 return true, data;
678 end 683 end
679 684