~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-31 03:33:25 UTC
  • mto: This revision was merged to the branch mainline in revision 2118.
  • Revision ID: john@arbash-meinel.com-20061031033325-5775a400c6ce9da3
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1253
1253
        """
1254
1254
        lines = []
1255
1255
        encode_utf8 = cache_utf8.encode
1256
 
        for version_id, options, pos, size, parents in versions:
1257
 
            line = "\n%s %s %s %s %s :" % (encode_utf8(version_id),
1258
 
                                           ','.join(options),
1259
 
                                           pos,
1260
 
                                           size,
1261
 
                                           self._version_list_to_index(parents))
1262
 
            assert isinstance(line, str), \
1263
 
                'content must be utf-8 encoded: %r' % (line,)
1264
 
            lines.append(line)
1265
 
        if not self._need_to_create:
1266
 
            self._transport.append_bytes(self._filename, ''.join(lines))
1267
 
        else:
1268
 
            sio = StringIO()
1269
 
            sio.write(self.HEADER)
1270
 
            sio.writelines(lines)
1271
 
            sio.seek(0)
1272
 
            self._transport.put_file_non_atomic(self._filename, sio,
1273
 
                                create_parent_dir=self._create_parent_dir,
1274
 
                                mode=self._file_mode,
1275
 
                                dir_mode=self._dir_mode)
1276
 
            self._need_to_create = False
1277
 
 
1278
 
        # cache after writing, so that a failed write leads to missing cache
1279
 
        # entries not extra ones. XXX TODO: RBC 20060502 in the event of a 
1280
 
        # failure, reload the index or flush it or some such, to prevent
1281
 
        # writing records that did complete twice.
1282
 
        for version_id, options, pos, size, parents in versions:
1283
 
            self._cache_version(version_id, options, pos, size, parents)
1284
 
        
 
1256
        orig_history = self._history[:]
 
1257
        orig_cache = self._cache.copy()
 
1258
 
 
1259
        try:
 
1260
            for version_id, options, pos, size, parents in versions:
 
1261
                line = "\n%s %s %s %s %s :" % (encode_utf8(version_id),
 
1262
                                               ','.join(options),
 
1263
                                               pos,
 
1264
                                               size,
 
1265
                                               self._version_list_to_index(parents))
 
1266
                assert isinstance(line, str), \
 
1267
                    'content must be utf-8 encoded: %r' % (line,)
 
1268
                lines.append(line)
 
1269
                self._cache_version(version_id, options, pos, size, parents)
 
1270
            if not self._need_to_create:
 
1271
                self._transport.append_bytes(self._filename, ''.join(lines))
 
1272
            else:
 
1273
                sio = StringIO()
 
1274
                sio.write(self.HEADER)
 
1275
                sio.writelines(lines)
 
1276
                sio.seek(0)
 
1277
                self._transport.put_file_non_atomic(self._filename, sio,
 
1278
                                    create_parent_dir=self._create_parent_dir,
 
1279
                                    mode=self._file_mode,
 
1280
                                    dir_mode=self._dir_mode)
 
1281
                self._need_to_create = False
 
1282
        except:
 
1283
            # If any problems happen, restore the original values and re-raise
 
1284
            self._history = orig_history
 
1285
            self._cache = orig_cache
 
1286
            raise
 
1287
 
1285
1288
    def has_version(self, version_id):
1286
1289
        """True if the version is in the index."""
1287
1290
        return (version_id in self._cache)