~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revfile.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-09 07:04:27 UTC
  • Revision ID: mbp@sourcefrog.net-20050409070427-dfe3119cf1a0b290ea2f6ae2
Revfile: better __iter__ method that reads the whole index file in one go!

Show diffs side-by-side

added added

removed removed

Lines of Context:
381
381
        """Index by sequence id returns the index field"""
382
382
        ## TODO: Can avoid seek if we just moved there...
383
383
        self._seek_index(idx)
384
 
        return self._read_next_index()
 
384
        idxrec = self._read_next_index()
 
385
        if idxrec == None:
 
386
            raise IndexError()
 
387
        else:
 
388
            return idxrec
385
389
 
386
390
 
387
391
    def _seek_index(self, idx):
388
392
        if idx < 0:
389
393
            raise RevfileError("invalid index %r" % idx)
390
394
        self.idxfile.seek((idx + 1) * _RECORDSIZE)
 
395
 
 
396
 
 
397
 
 
398
    def __iter__(self):
 
399
        """Read back all index records.
 
400
 
 
401
        Do not seek the index file while this is underway!"""
 
402
        sys.stderr.write(" ** iter called ** \n")
 
403
        self._seek_index(0)
 
404
        while True:
 
405
            idxrec = self._read_next_index()
 
406
            if not idxrec:
 
407
                break
 
408
            yield idxrec
391
409
        
392
410
 
393
411
    def _read_next_index(self):
394
412
        rec = self.idxfile.read(_RECORDSIZE)
395
413
        if not rec:
396
 
            raise IndexError("end of index file")
 
414
            return None
397
415
        elif len(rec) != _RECORDSIZE:
398
416
            raise RevfileError("short read of %d bytes getting index %d from %r"
399
417
                               % (len(rec), idx, self.basename))