[Zodb-checkins] CVS: Zope3/src/zodb/storage - interfaces.py:1.3.2.3 file.py:1.7.4.3 base.py:1.11.4.3

Jeremy Hylton jeremy@zope.com
Wed, 5 Feb 2003 15:58:31 -0500


Update of /cvs-repository/Zope3/src/zodb/storage
In directory cvs.zope.org:/tmp/cvs-serv29014/storage

Modified Files:
      Tag: storage-interface-branch
	interfaces.py file.py base.py 
Log Message:
Change iterator() implementation for FileStorage to use generators.


=== Zope3/src/zodb/storage/interfaces.py 1.3.2.2 => 1.3.2.3 ===
--- Zope3/src/zodb/storage/interfaces.py:1.3.2.2	Wed Feb  5 12:30:10 2003
+++ Zope3/src/zodb/storage/interfaces.py	Wed Feb  5 15:58:27 2003
@@ -163,14 +163,8 @@
     def pack(t):
         pass
 
-    def getSerial(oid):
-        pass
-
 class IUndoStorage(Interface):
     
-    def pack(t):
-        pass
-
     def loadSerial(oid, serial):
         pass
     


=== Zope3/src/zodb/storage/file.py 1.7.4.2 => 1.7.4.3 ===
--- Zope3/src/zodb/storage/file.py:1.7.4.2	Wed Feb  5 12:30:10 2003
+++ Zope3/src/zodb/storage/file.py	Wed Feb  5 15:58:27 2003
@@ -127,6 +127,8 @@
 $Id$
 """
 
+from __future__ import generators
+
 import base64
 from cPickle import Pickler, Unpickler, loads
 import errno
@@ -144,7 +146,7 @@
     fsync = None
 
 import zodb.db
-from zodb.storage.base import BaseStorage, TransactionRecord, DataRecord
+from zodb.storage.base import BaseStorage
 from zodb import conflict
 from zodb import interfaces
 from zodb.interfaces import UndoError, POSKeyError, MultipleUndoErrors
@@ -2178,23 +2180,7 @@
     seek(pos)
     file.truncate()
 
-class Iterator:
-    """A General simple iterator that uses the Python for-loop index protocol
-    """
-    __index=-1
-    __current=None
-
-    def __getitem__(self, i):
-        __index=self.__index
-        while i > __index:
-            __index=__index+1
-            self.__current=self.next(__index)
-
-        self.__index=__index
-        return self.__current
-
-
-class FileIterator(Iterator, FileStorageFormatter):
+class FileIterator(FileStorageFormatter):
     """Iterate over the transactions in a FileStorage file."""
     _ltid = z64
     _file = None
@@ -2247,16 +2233,16 @@
                           "(%s != %s)",
                           self._file.name, pos, u64(rtl), u64(stl))
 
-    def next(self, index=0):
+    def __iter__(self):
         if self._file is None:
             # A closed iterator.  XXX: Is IOError the best we can do?  For
             # now, mimic a read on a closed file.
-            raise IOError, 'iterator is closed'
+            raise IOError("iterator is closed")
         file=self._file
         seek=file.seek
         read=file.read
-        pos=self._pos
-
+        
+        pos = self._pos
         while 1:
             # Read the transaction record
             seek(pos)
@@ -2306,7 +2292,7 @@
                     break
 
             if self._stop is not None and tid > self._stop:
-                raise IndexError, index
+                return
 
             tpos=pos
             tend=tpos+tl
@@ -2340,13 +2326,10 @@
                 warn("%s redundant transaction length check failed at %s",
                      self._file.name, pos)
                 break
-            self._pos = pos + 8
-
-            return result
-
-        raise IndexError, index
+            pos += 8
+            yield result
 
-class RecordIterator(Iterator, FileStorageFormatter, TransactionRecord):
+class RecordIterator(FileStorageFormatter):
     """Iterate over the transactions in a FileStorage file."""
 
     __implements__ = ITransactionRecordIterator, ITransactionAttrs
@@ -2362,7 +2345,7 @@
         self._file = file
         self._tpos = tpos
 
-    def next(self, index=0):
+    def __iter__(self):
         pos = self._pos
         while pos < self._tend:
             # Read the data records for this transaction
@@ -2373,9 +2356,9 @@
             if pos + dlen > self._tend or h.tloc != self._tpos:
                 warn("%s data record exceeds transaction record at %s",
                      file.name, pos)
-                break
+                return
 
-            self._pos = pos + dlen
+            pos += dlen
             prev_txn = None
             if h.plen:
                 data = self._file.read(h.plen)
@@ -2391,13 +2374,9 @@
                     data, _s, tid = self._loadBackTxn(h.oid, h.back)
                     prev_txn = self.getTxnFromData(h.oid, h.back)
 
-            r = Record(h.oid, h.serial, h.version, data, prev_txn)
-
-            return r
-
-        raise IndexError, index
+            yield Record(h.oid, h.serial, h.version, data, prev_txn)
 
-class Record(DataRecord):
+class Record:
     """An abstract database record."""
 
     __implements__ = IDataRecord


=== Zope3/src/zodb/storage/base.py 1.11.4.2 => 1.11.4.3 ===
--- Zope3/src/zodb/storage/base.py:1.11.4.2	Wed Feb  5 12:30:10 2003
+++ Zope3/src/zodb/storage/base.py	Wed Feb  5 15:58:27 2003
@@ -343,15 +343,6 @@
             self.tpcVote(transaction)
             self.tpcFinish(transaction)
 
-class TransactionRecord:
-    """Abstract base class for iterator protocol."""
-
-    __implements__ = ITransactionAttrs
-
-class DataRecord:
-    """Abstract base class for iterator protocol."""
-
-
 
 class BerkeleyConfig:
     """Bag of attributes for configuring Berkeley based storages.