Commit 30b9d5b1 authored by Julien Muchembled's avatar Julien Muchembled

HBTreeFolder2: automatic prune trees that become empty after that an object is deleted

parent 7c085c43
...@@ -322,7 +322,9 @@ class HBTreeFolder2Base (Persistent): ...@@ -322,7 +322,9 @@ class HBTreeFolder2Base (Persistent):
"""Remove the named object from the folder. """Remove the named object from the folder.
""" """
htree = self._htree htree = self._htree
h = []
for sub_id in self.hashId(id)[:-1]: for sub_id in self.hashId(id)[:-1]:
h.append((htree, sub_id))
htree = htree.get(sub_id) htree = htree.get(sub_id)
if type(htree) is not OOBTree: if type(htree) is not OOBTree:
raise KeyError(id) raise KeyError(id)
...@@ -330,6 +332,9 @@ class HBTreeFolder2Base (Persistent): ...@@ -330,6 +332,9 @@ class HBTreeFolder2Base (Persistent):
raise KeyError(id) raise KeyError(id)
del htree[id] del htree[id]
self._count.change(-1) self._count.change(-1)
while h and not htree:
htree, sub_id = h.pop()
del htree[sub_id]
security.declareProtected(view_management_screens, 'getBatchObjectListing') security.declareProtected(view_management_screens, 'getBatchObjectListing')
def getBatchObjectListing(self, REQUEST=None): def getBatchObjectListing(self, REQUEST=None):
......
...@@ -53,8 +53,10 @@ class HBTreeFolder2Tests(ERP5TypeTestCase): ...@@ -53,8 +53,10 @@ class HBTreeFolder2Tests(ERP5TypeTestCase):
self.assertRaises(KeyError, f._delOb, id) self.assertRaises(KeyError, f._delOb, id)
self.assertEqual(len(f), 1 + len(ok)) self.assertEqual(len(f), 1 + len(ok))
self.assertEqual(f.getTreeIdList(), [None, "b", "c-a"]) self.assertEqual(f.getTreeIdList(), [None, "b", "c-a"])
self.assertEqual(len(f._htree), 4)
for id in ok: for id in ok:
f._delOb(id) f._delOb(id)
self.assertEqual(len(f._htree), 1)
self.assertEqual(len(f), 1) self.assertEqual(len(f), 1)
self.assertEqual(f.getTreeIdList(), [None]) self.assertEqual(f.getTreeIdList(), [None])
self.assertEqual(ff.getTreeIdList(), []) self.assertEqual(ff.getTreeIdList(), [])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment