fix Node/Network sanitization

This commit is contained in:
falsycat 2022-11-19 13:24:38 +09:00
parent 2cbf0035f4
commit 5bd00c97d2
2 changed files with 19 additions and 1 deletions

View File

@ -6,6 +6,7 @@
#include <span>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>
#include <yas/serialize.hpp>
@ -65,6 +66,8 @@ class NodeLinkStore {
inline std::unique_ptr<nf7::History::Command> CreateCommandToRemoveExpired(
uint64_t id, std::span<const std::string> in, std::span<const std::string> out) noexcept;
inline std::unique_ptr<nf7::History::Command> CreateCommandToRemoveExpired(
const std::unordered_set<uint64_t>& ids) noexcept;
std::span<const Link> items() const noexcept { return links_; }
@ -111,7 +114,19 @@ std::unique_ptr<nf7::History::Command> NodeLinkStore::CreateCommandToRemoveExpir
const bool rm =
(lk.src_id == id && std::find(out.begin(), out.end(), lk.src_name) == out.end()) ||
(lk.dst_id == id && std::find(in .begin(), in .end(), lk.dst_name) == in .end());
if (rm) cmds.push_back(SwapCommand::CreateToRemove(*this, Link(lk)));
if (rm) cmds.push_back(SwapCommand::CreateToRemove(*this, Link {lk}));
}
if (cmds.empty()) return nullptr;
return std::make_unique<nf7::AggregateCommand>(std::move(cmds));
}
std::unique_ptr<nf7::History::Command> NodeLinkStore::CreateCommandToRemoveExpired(
const std::unordered_set<uint64_t>& ids) noexcept {
std::vector<std::unique_ptr<nf7::History::Command>> cmds;
for (const auto& lk : links_) {
if (!ids.contains(lk.src_id) || !ids.contains(lk.dst_id)) {
cmds.push_back(SwapCommand::CreateToRemove(*this, Link {lk}));
}
}
if (cmds.empty()) return nullptr;
return std::make_unique<nf7::AggregateCommand>(std::move(cmds));

View File

@ -807,6 +807,9 @@ void Network::Sanitize() {
cmd->Apply();
}
}
if (auto cmd = links_.CreateCommandToRemoveExpired(ids)) {
cmd->Apply();
}
}
File* Network::PreFind(std::string_view name) const noexcept
try {