[Zodb-checkins] CVS: StandaloneZODB/ZEO - TransactionBuffer.py:1.1.2.3

Jeremy Hylton jeremy@zope.com
Fri, 4 Jan 2002 17:21:36 -0500


Update of /cvs-repository/StandaloneZODB/ZEO
In directory cvs.zope.org:/tmp/cvs-serv14021

Modified Files:
      Tag: ZEO-ZRPC-Dev
	TransactionBuffer.py 
Log Message:
Use cPickle's fast Pickler() instead of marshal.

Remove try/except in next() and trust self.count instead.



=== StandaloneZODB/ZEO/TransactionBuffer.py 1.1.2.2 => 1.1.2.3 ===
 
 import tempfile
-import marshal
+import cPickle
 
 class TransactionBuffer:
+    
     def __init__(self):
         self.file = tempfile.TemporaryFile()
         self.count = 0
         self.size = 0
+        # It's safe to use a fast pickler because the only objects
+        # stored are builtin types -- strings or None.
+        self.pickler = cPickle.Pickler(self.file, 1)
+        self.pickler.fast = 1
 
     def store(self, oid, version, data):
         """Store oid, version, data for later retrieval"""
-        marshal.dump((oid, version, data), self.file)
-        self.count = self.count + 1
+        self.pickler.dump((oid, version, data))
+        self.count += 1
         # Estimate per-record cache size
         self.size = self.size + len(data) + (27 + 12)
         if version:
             self.size = self.size + len(version) + 4
 
     def invalidate(self, oid, version):
-        marshal.dump((oid, version, None), self.file)
-        self.count = self.count + 1
+        self.pickler.dump((oid, version, None))
+        self.count += 1
 
     def clear(self):
         """Mark the buffer as empty"""
@@ -46,16 +51,15 @@
         """Move the file pointer in advance of iteration"""
         self.file.flush()
         self.file.seek(0)
+        self.unpickler = cPickle.Unpickler(self.file)
 
     def next(self):
         """Return next tuple of data or None if EOF"""
-        if not self.count:
+        if self.count == 0:
+            del self.unpickler
             return None
-        try:
-            oid_ver_data = marshal.load(self.file)
-        except EOFError:
-            return None
-        self.count = self.count - 1
+        oid_ver_data = self.unpickler.load()
+        self.count -= 1
         return oid_ver_data
             
     def get_size(self):