Skip to content

Commit ca7ac2d

Browse files
committed
ix: sort deletion queue by URI path depth (longest first)
1 parent ad4efa7 commit ca7ac2d

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

databusclient/api/delete.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import json
99
from typing import List
10+
from urllib.parse import urlparse
1011

1112
import requests
1213

@@ -55,13 +56,22 @@ def is_not_empty(self) -> bool:
5556
"""Return True if the queue contains any URIs."""
5657
return len(self.queue) > 0
5758

59+
def sorted_queue(self) -> List[str]:
60+
"""Return a list of queued URIs sorted by path depth (deepest first)."""
61+
return sorted(self.queue, key=self._path_depth, reverse=True)
62+
63+
@staticmethod
64+
def _path_depth(uri: str) -> int:
65+
"""Return number of non-empty path segments in the URI."""
66+
return len([part for part in urlparse(uri).path.split("/") if part])
67+
5868
def execute(self):
5969
"""Execute all queued deletions.
6070
6171
Each queued URI will be deleted using `_delete_resource`.
6272
"""
6373
_delete_list(
64-
list(self.queue),
74+
list(self.sorted_queue()),
6575
self.databus_key,
6676
force=True,
6777
)

0 commit comments

Comments
 (0)