Commit a5c0f41a authored by Jeremy Hylton's avatar Jeremy Hylton

Change TransactionBuffer to use a real iterator.

This eliminates a bunch of checks that weren't exercised by the test
suite.  Not sure if they were superstition or not, but I'm willing to
risk it.
parent 82d1310d
......@@ -985,22 +985,7 @@ class ClientStorage(object):
if self._cache is None:
return
try:
self._tbuf.begin_iterate()
except ValueError, msg:
raise ClientStorageError, (
"Unexpected error reading temporary file in "
"client storage: %s" % msg)
while 1:
try:
t = self._tbuf.next()
except ValueError, msg:
raise ClientStorageError, (
"Unexpected error reading temporary file in "
"client storage: %s" % msg)
if t is None:
break
oid, version, data = t
for oid, version, data in self._tbuf:
self._cache.invalidate(oid, version, tid)
# If data is None, we just invalidate.
if data is not None:
......
......@@ -116,41 +116,33 @@ class TransactionBuffer:
finally:
self.lock.release()
# unchecked constraints:
# 1. can't call store() after begin_iterate()
# 2. must call clear() after iteration finishes
def begin_iterate(self):
"""Move the file pointer in advance of iteration"""
def __iter__(self):
self.lock.acquire()
try:
if self.closed:
return
self.file.flush()
self.file.seek(0)
self.unpickler = cPickle.Unpickler(self.file)
return TBIterator(self.file, self.count)
finally:
self.lock.release()
def next(self):
self.lock.acquire()
try:
return self._next()
finally:
self.lock.release()
class TBIterator(object):
def __init__(self, f, count):
self.file = f
self.count = count
self.unpickler = cPickle.Unpickler(f)
def _next(self):
def __iter__(self):
return self
def next(self):
"""Return next tuple of data or None if EOF"""
if self.closed:
return None
if self.count == 0:
del self.unpickler
return None
self.file.seek(0)
self.size = 0
raise StopIteration
oid_ver_data = self.unpickler.load()
self.count -= 1
return oid_ver_data
def get_size(self):
"""Return size of data stored in buffer (just a hint)."""
return self.size
......@@ -35,12 +35,8 @@ class TransBufTests(unittest.TestCase):
tbuf = TransactionBuffer()
tbuf.store(*new_store_data())
tbuf.invalidate(*new_invalidate_data())
tbuf.begin_iterate()
while 1:
o = tbuf.next()
if o is None:
break
tbuf.clear()
for o in tbuf:
pass
def doUpdates(self, tbuf):
data = []
......@@ -52,9 +48,7 @@ class TransBufTests(unittest.TestCase):
tbuf.invalidate(*d)
data.append(d)
tbuf.begin_iterate()
for i in range(len(data)):
x = tbuf.next()
for i, x in enumerate(tbuf):
if x[2] is None:
# the tbuf add a dummy None to invalidates
x = x[:2]
......
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