Open jsandbrook opened 3 years ago
import radix
class CustomRadix(radix.Radix):
def delete(self, prefix):
node = self.search_exact(prefix)
if node is None:
raise KeyError('match not found')
# Check if the node to delete has children
left_child = node.left
right_child = node.right
super().delete(prefix)
if left_child:
self._rehash_node(left_child)
if right_child:
self._rehash_node(right_child)
def _rehash_node(self, node):
# Re-add the node to fix the tree structure
self.add(node.prefix)
# Example usage
if __name__ == "__main__":
rt = CustomRadix()
rt.add('1.2.2.0/23')
rt.add('1.2.2.0/24')
rt.add('1.2.3.0/24')
print("Nodes before deletion:")
for node in rt.nodes():
print(node)
# Delete the covering node
rt.delete('1.2.2.0/23')
print("\nNodes after deletion:")
for node in rt.nodes():
print(node)
# Check if the right node is accessible
right_node = rt.search_exact('1.2.3.0/24')
print("\nRight node after deletion:")
print(right_node)
Apologies if I'm stating that incorrectly. I don't have the best handle on the inner workings of this library.
When a covering node has 'right' and 'left' nodes, deleting it makes the right prefix inaccessible. It's fairly easy to reproduce. [1]
This specifically happens with version 0.10.0 installed explicitly without the C extension. With the C-extension, it works as expected [2].
[1] No c-extension
[2] C-Extension exists this time