Commit c14a6339 authored by Julien Muchembled's avatar Julien Muchembled

HBTreeFolder2: implement equivalent to BTreeFolder2._tree.iteritems

parent 84a277f6
...@@ -1616,10 +1616,10 @@ for source_klass, destination_klass in \ ...@@ -1616,10 +1616,10 @@ for source_klass, destination_klass in \
# special meanings in Python or Zope, and lead to strange errors # special meanings in Python or Zope, and lead to strange errors
# when set to an unexpected value. In fact, __implemented__ should not # when set to an unexpected value. In fact, __implemented__ should not
# be set this way, otherwise Zope crashes. # be set this way, otherwise Zope crashes.
method_id_list = [x for x in source_klass.__dict__.iterkeys() for method_id in source_klass.__dict__:
if not x.startswith('__') and callable(getattr(source_klass, x))] if (method_id[:2] != '__' and method_id[:7] != '_htree_' and
for method_id in method_id_list: callable(getattr(source_klass, method_id)) and
if not hasattr(destination_klass, method_id): not hasattr(destination_klass, method_id)):
setattr(destination_klass, method_id, NotImplementedClass(method_id)) setattr(destination_klass, method_id, NotImplementedClass(method_id))
# Zope 2.7 required to have methodId__roles__ defined # Zope 2.7 required to have methodId__roles__ defined
# to know the security ot the method # to know the security ot the method
......
...@@ -358,6 +358,40 @@ class HBTreeFolder2Base (Persistent): ...@@ -358,6 +358,40 @@ class HBTreeFolder2Base (Persistent):
return 0 return 0
return 1 return 1
def _htree_iteritems(self, min=None):
# BUG: Due to bad design of HBTreeFolder2, buckets other than the root
# one must not contain both buckets & leafs. Otherwise, this method
# fails.
h = self._htree
recurse_stack = []
try:
for sub_id in min and self.hashId(min) or ('',):
if recurse_stack:
i.next()
if type(h) is not OOBTree:
break
id += H_SEPARATOR + sub_id
if type(h.itervalues().next()) is not OOBTree:
sub_id = id
else:
id = sub_id
i = h.iteritems(sub_id)
recurse_stack.append(i)
h = h[sub_id]
except (KeyError, StopIteration):
pass
while recurse_stack:
i = recurse_stack.pop()
try:
while 1:
id, h = i.next()
if type(h) is OOBTree:
recurse_stack.append(i)
i = h.iteritems()
else:
yield id, h
except StopIteration:
pass
security.declareProtected(access_contents_information, security.declareProtected(access_contents_information,
'treeIds') 'treeIds')
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
# #
############################################################################## ##############################################################################
import random
import unittest import unittest
import ZODB import ZODB
import Testing import Testing
...@@ -194,6 +195,34 @@ class HBTreeFolder2Tests(unittest.TestCase): ...@@ -194,6 +195,34 @@ class HBTreeFolder2Tests(unittest.TestCase):
key.value = 'a' key.value = 'a'
self.f.manage_cleanup() self.f.manage_cleanup()
def testIterItems(self):
h = HBTreeFolder2()
id_list = ['a-b', 'a-cd',
'b',
'd-a-b', 'd-c-d', 'd-f-a',
'e-cd', 'e-f',
'f']
for i in id_list:
h._setOb(i, tuple(i))
while 1:
for min in (None, 'a', 'a-a', 'a-b',
'a-b-a', 'a-c', 'a-cd',
'a-cde', 'a-d', 'aa', 'b',
'b-c-d', 'c', 'd'):
if min:
m = min.split('-')
for i, id in enumerate(id_list):
if m <= id.split('-'):
break
else:
i = 0
expected = [(i, tuple(i)) for i in id_list[i:]]
self.assertEqual(expected, list(h._htree_iteritems(min)))
if not id_list:
break
i = random.choice(id_list)
id_list.remove(i)
h._delOb(i)
class TrojanKey: class TrojanKey:
"""Pretends to be a consistent, immutable, humble citizen... """Pretends to be a consistent, immutable, humble citizen...
......
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