[Zope-Checkins] CVS: ZODB3/ZODB - fsIndex.py:1.4.40.1

Jeremy Hylton jeremy@zope.com
Tue, 8 Jul 2003 09:53:16 -0400


Update of /cvs-repository/ZODB3/ZODB
In directory cvs.zope.org:/tmp/cvs-serv31249/ZODB

Modified Files:
      Tag: zodb33-devel-branch
	fsIndex.py 
Log Message:
Copy the fsIndex code from ZODB4.


=== ZODB3/ZODB/fsIndex.py 1.4 => 1.4.40.1 ===
--- ZODB3/ZODB/fsIndex.py:1.4	Tue Dec  3 13:45:16 2002
+++ ZODB3/ZODB/fsIndex.py	Tue Jul  8 09:53:11 2003
@@ -8,12 +8,11 @@
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
+# FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Implement an OID to File-position (long integer) mapping
-"""
-#
+"""Implement an OID to File-position (long integer) mapping."""
+
 # To save space, we do two things:
 #
 #     1. We split the keys (OIDS) into 6-byte prefixes and 2-byte suffixes.
@@ -29,29 +28,26 @@
 # suffix to 6-byte data. This should reduce the overall memory usage to
 # 8-16 bytes per OID.
 #
+# Since the mapping from suffix to data contains at most 256 entries,
+# we use a BTree bucket instead of a full BTree to store the results.
+#
 # We use p64 to convert integers to 8-byte strings and lop off the two
 # high-order bytes when saving. On loading data, we add the leading
-# bytes back before using U64 to convert the data back to (long)
+# bytes back before using u64 to convert the data back to (long)
 # integers.
 
-from BTrees._fsBTree import fsBTree as _fsBTree
-
+from __future__ import generators
 import struct
 
-# convert between numbers and six-byte strings
+from BTrees._fsBTree import fsBucket
 
-_t32 = 1L<< 32
+# convert between numbers and six-byte strings
 
 def num2str(n):
-    h, l = divmod(long(n), _t32)
-    return struct.pack(">HI", h, l)
+    return struct.pack(">Q", n)[2:]
 
 def str2num(s):
-    h, l = struct.unpack(">HI", s)
-    if h:
-        return (long(h) << 32) + l
-    else:
-        return l
+    return struct.unpack(">Q", "\000\000" + s)[0]
 
 class fsIndex:
 
@@ -75,7 +71,7 @@
         treekey = key[:6]
         tree = self._data.get(treekey)
         if tree is None:
-            tree = _fsBTree()
+            tree = fsBucket()
             self._data[treekey] = tree
         tree[key[6:]] = value
 
@@ -96,14 +92,19 @@
     def __contains__(self, key):
         tree = self._data.get(key[:6])
         if tree is None:
-            return 0
+            return False
         v = tree.get(key[6:], None)
         if v is None:
-            return 0
-        return 1
+            return False
+        return True
 
     def clear(self):
         self._data.clear()
+
+    def __iter__(self):
+        for prefix, tree in self._data.items():
+            for suffix in tree:
+                yield prefix + suffix
 
     def keys(self):
         r = []