~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-30 22:01:00 UTC
  • mto: (1711.4.39 win32-accepted)
  • mto: This revision was merged to the branch mainline in revision 1836.
  • Revision ID: john@arbash-meinel.com-20060630220100-5e348dced9dfd531
try/finally to close files, _KnitData was keeping a handle to a file it never used again, and using transport.rename() when it wanted transport.move()

Show diffs side-by-side

added added

removed removed

Lines of Context:
390
390
        # writes
391
391
        transport.put(name + INDEX_SUFFIX + '.tmp', self.transport.get(self._index._filename),)
392
392
        # copy the data file
393
 
        transport.put(name + DATA_SUFFIX, self._data._open_file())
394
 
        # rename the copied index into place
395
 
        transport.rename(name + INDEX_SUFFIX + '.tmp', name + INDEX_SUFFIX)
 
393
        f = self._data._open_file()
 
394
        try:
 
395
            transport.put(name + DATA_SUFFIX, f)
 
396
        finally:
 
397
            f.close()
 
398
        # move the copied index into place
 
399
        transport.move(name + INDEX_SUFFIX + '.tmp', name + INDEX_SUFFIX)
396
400
 
397
401
    def create_empty(self, name, transport, mode=None):
398
402
        return KnitVersionedFile(name, transport, factory=self.factory, delta=self.delta, create=True)
1044
1048
            try:
1045
1049
                pb.update('read knit index', count, total)
1046
1050
                fp = self._transport.get(self._filename)
1047
 
                self.check_header(fp)
1048
 
                # readlines reads the whole file at once:
1049
 
                # bad for transports like http, good for local disk
1050
 
                # we save 60 ms doing this one change (
1051
 
                # from calling readline each time to calling
1052
 
                # readlines once.
1053
 
                # probably what we want for nice behaviour on
1054
 
                # http is a incremental readlines that yields, or
1055
 
                # a check for local vs non local indexes,
1056
 
                for l in fp.readlines():
1057
 
                    rec = l.split()
1058
 
                    if len(rec) < 5 or rec[-1] != ':':
1059
 
                        # corrupt line.
1060
 
                        # FIXME: in the future we should determine if its a
1061
 
                        # short write - and ignore it 
1062
 
                        # or a different failure, and raise. RBC 20060407
1063
 
                        continue
1064
 
                    count += 1
1065
 
                    total += 1
1066
 
                    #pb.update('read knit index', count, total)
1067
 
                    # See self._parse_parents
1068
 
                    parents = []
1069
 
                    for value in rec[4:-1]:
1070
 
                        if '.' == value[0]:
1071
 
                            # uncompressed reference
1072
 
                            parents.append(value[1:])
 
1051
                try:
 
1052
                    self.check_header(fp)
 
1053
                    # readlines reads the whole file at once:
 
1054
                    # bad for transports like http, good for local disk
 
1055
                    # we save 60 ms doing this one change (
 
1056
                    # from calling readline each time to calling
 
1057
                    # readlines once.
 
1058
                    # probably what we want for nice behaviour on
 
1059
                    # http is a incremental readlines that yields, or
 
1060
                    # a check for local vs non local indexes,
 
1061
                    for l in fp.readlines():
 
1062
                        rec = l.split()
 
1063
                        if len(rec) < 5 or rec[-1] != ':':
 
1064
                            # corrupt line.
 
1065
                            # FIXME: in the future we should determine if its a
 
1066
                            # short write - and ignore it 
 
1067
                            # or a different failure, and raise. RBC 20060407
 
1068
                            continue
 
1069
                        count += 1
 
1070
                        total += 1
 
1071
                        #pb.update('read knit index', count, total)
 
1072
                        # See self._parse_parents
 
1073
                        parents = []
 
1074
                        for value in rec[4:-1]:
 
1075
                            if '.' == value[0]:
 
1076
                                # uncompressed reference
 
1077
                                parents.append(value[1:])
 
1078
                            else:
 
1079
                                # this is 15/4000ms faster than isinstance,
 
1080
                                # (in lsprof)
 
1081
                                # this function is called thousands of times a 
 
1082
                                # second so small variations add up.
 
1083
                                assert value.__class__ is str
 
1084
                                parents.append(self._history[int(value)])
 
1085
                        # end self._parse_parents
 
1086
                        # self._cache_version(rec[0], 
 
1087
                        #                     rec[1].split(','),
 
1088
                        #                     int(rec[2]),
 
1089
                        #                     int(rec[3]),
 
1090
                        #                     parents)
 
1091
                        # --- self._cache_version
 
1092
                        # only want the _history index to reference the 1st 
 
1093
                        # index entry for version_id
 
1094
                        version_id = rec[0]
 
1095
                        if version_id not in self._cache:
 
1096
                            index = len(self._history)
 
1097
                            self._history.append(version_id)
1073
1098
                        else:
1074
 
                            # this is 15/4000ms faster than isinstance,
1075
 
                            # (in lsprof)
1076
 
                            # this function is called thousands of times a 
1077
 
                            # second so small variations add up.
1078
 
                            assert value.__class__ is str
1079
 
                            parents.append(self._history[int(value)])
1080
 
                    # end self._parse_parents
1081
 
                    # self._cache_version(rec[0], 
1082
 
                    #                     rec[1].split(','),
1083
 
                    #                     int(rec[2]),
1084
 
                    #                     int(rec[3]),
1085
 
                    #                     parents)
1086
 
                    # --- self._cache_version
1087
 
                    # only want the _history index to reference the 1st 
1088
 
                    # index entry for version_id
1089
 
                    version_id = rec[0]
1090
 
                    if version_id not in self._cache:
1091
 
                        index = len(self._history)
1092
 
                        self._history.append(version_id)
1093
 
                    else:
1094
 
                        index = self._cache[version_id][5]
1095
 
                    self._cache[version_id] = (version_id,
1096
 
                                               rec[1].split(','),
1097
 
                                               int(rec[2]),
1098
 
                                               int(rec[3]),
1099
 
                                               parents,
1100
 
                                               index)
1101
 
                    # --- self._cache_version 
 
1099
                            index = self._cache[version_id][5]
 
1100
                        self._cache[version_id] = (version_id,
 
1101
                                                   rec[1].split(','),
 
1102
                                                   int(rec[2]),
 
1103
                                                   int(rec[3]),
 
1104
                                                   parents,
 
1105
                                                   index)
 
1106
                        # --- self._cache_version 
 
1107
                finally:
 
1108
                    fp.close()
1102
1109
            except NoSuchFile, e:
1103
1110
                if mode != 'w' or not create:
1104
1111
                    raise
1276
1283
 
1277
1284
    def __init__(self, transport, filename, mode, create=False, file_mode=None):
1278
1285
        _KnitComponentFile.__init__(self, transport, filename, mode)
1279
 
        self._file = None
1280
1286
        self._checked = False
1281
1287
        if create:
1282
1288
            self._transport.put(self._filename, StringIO(''), mode=file_mode)
1286
1292
        pass
1287
1293
 
1288
1294
    def _open_file(self):
1289
 
        if self._file is None:
1290
 
            try:
1291
 
                self._file = self._transport.get(self._filename)
1292
 
            except NoSuchFile:
1293
 
                pass
1294
 
        return self._file
 
1295
        try:
 
1296
            return self._transport.get(self._filename)
 
1297
        except NoSuchFile:
 
1298
            pass
 
1299
        return None
1295
1300
 
1296
1301
    def _record_to_data(self, version_id, digest, lines):
1297
1302
        """Convert version_id, digest, lines into a raw data block.