[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/uniqueid/ A better
'random' unique id generation. Now we try to allocate ids
Albertas Agejevas
alga at pov.lt
Fri Jun 11 08:18:20 EDT 2004
Log message for revision 25347:
A better 'random' unique id generation. Now we try to allocate ids
sequentially, so they fall into the same BTree bucket.
-=-
Modified: Zope3/trunk/src/zope/app/uniqueid/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/__init__.py 2004-06-11 11:17:39 UTC (rev 25346)
+++ Zope3/trunk/src/zope/app/uniqueid/__init__.py 2004-06-11 12:18:20 UTC (rev 25347)
@@ -41,6 +41,8 @@
__parent__ = None
__name__ = None
+ _v_nextid = None
+
def __init__(self):
self.ids = OIBTree.OIBTree()
self.refs = IOBTree.IOBTree()
@@ -59,10 +61,20 @@
return self.ids[ref]
def _generateId(self):
+ """Generate an id which is not yet taken.
+
+ This tries to allocate sequential ids so they fall into the
+ same BTree bucket, and randomizes if it stumbles upon a
+ used one.
+ """
while True:
- uid = random.randint(0, 2**31)
+ if self._v_nextid is None:
+ self._v_nextid = random.randint(0, 2**31)
+ uid = self._v_nextid
+ self._v_nextid += 1
if uid not in self.refs:
return uid
+ self._v_nextid = None
def register(self, ob):
ob = trustedRemoveSecurityProxy(ob)
Modified: Zope3/trunk/src/zope/app/uniqueid/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/uniqueid/tests.py 2004-06-11 11:17:39 UTC (rev 25346)
+++ Zope3/trunk/src/zope/app/uniqueid/tests.py 2004-06-11 12:18:20 UTC (rev 25347)
@@ -104,13 +104,28 @@
result.sort()
expected.sort()
self.assertEquals(result, expected)
-
+
u.unregister(obj)
u.unregister(obj2)
self.assertEquals(len(u), 0)
self.assertEquals(u.items(), [])
+ def test_getenrateId(self):
+ from zope.app.uniqueid import UniqueIdUtility
+ u = UniqueIdUtility()
+ self.assertEquals(u._v_nextid, None)
+ id1 = u._generateId()
+ self.assert_(u._v_nextid is not None)
+ id2 = u._generateId()
+ self.assert_(id1 + 1, id2)
+ u.refs[id2 + 1] = "Taken"
+ id3 = u._generateId()
+ self.assertNotEqual(id3, id2 + 1)
+ self.assertNotEqual(id3, id2)
+ self.assertNotEqual(id3, id1)
+
+
class TestReferenceToPersistent(ReferenceSetupMixin, unittest.TestCase):
def test(self):
More information about the Zope3-Checkins
mailing list