| 778 | } |
| 779 | |
| 780 | DataStorageBridge::DeleteResult DataStorageBridge::DeleteNode(const std::string& uid, bool recursive) |
| 781 | { |
| 782 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 783 | return this->DispatchTask<DeleteResult>([this, uid, recursive]() |
| 784 | { |
| 785 | DeleteResult result; |
| 786 | result.success = false; |
| 787 | result.deletedUid = uid; |
| 788 | result.childrenCount = 0; |
| 789 | |
| 790 | auto dataStorage = m_DataStorage.Lock(); |
| 791 | if (dataStorage.IsNull()) |
| 792 | { |
| 793 | return result; |
| 794 | } |
| 795 | |
| 796 | auto node = m_UidMapper->FindNodeByUid(uid); |
| 797 | if (node == nullptr) |
| 798 | { |
| 799 | return result; |
| 800 | } |
| 801 | |
| 802 | // Check for children |
| 803 | auto derivatives = dataStorage->GetDerivations(node); |
| 804 | result.childrenCount = static_cast<int>(derivatives->Size()); |
| 805 | |
| 806 | if (result.childrenCount > 0 && !recursive) |
| 807 | { |
| 808 | // Has children but recursive not requested |
| 809 | return result; |
| 810 | } |
| 811 | |
| 812 | // If recursive, collect and delete all descendants first |
| 813 | if (recursive && result.childrenCount > 0) |
| 814 | { |
| 815 | // Collect all descendants (depth-first) |
| 816 | std::vector<DataNode*> toDelete; |
| 817 | std::function<void(DataNode*)> collectDescendants = [&](DataNode* n) { |
| 818 | auto children = dataStorage->GetDerivations(n); |
| 819 | for (auto it = children->Begin(); it != children->End(); ++it) |
| 820 | { |
| 821 | collectDescendants(it->Value().GetPointer()); |
| 822 | } |
| 823 | toDelete.push_back(n); |
| 824 | }; |
| 825 | |
| 826 | // Collect children (not the node itself yet) |
| 827 | for (auto it = derivatives->Begin(); it != derivatives->End(); ++it) |
| 828 | { |
| 829 | collectDescendants(it->Value().GetPointer()); |
| 830 | } |
| 831 | |
| 832 | // Delete all descendants (children first, then grandchildren, etc.) |
| 833 | for (auto descendant : toDelete) |
| 834 | { |
| 835 | // Use GetOrCreateUid to ensure all deleted children have UIDs for reporting |
| 836 | result.deletedChildren.push_back(m_UidMapper->GetOrCreateUid(descendant)); |
| 837 | dataStorage->Remove(descendant); |
no test coverage detected