~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Martin Pool
  • Date: 2005-07-08 02:21:13 UTC
  • Revision ID: mbp@sourcefrog.net-20050708022113-940d11d7505b0ac8
- refactor hashcache to use just one dictionary

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
    This does not canonicalize the paths passed in; that should be
57
57
    done by the caller.
58
58
 
59
 
    cache_sha1
60
 
        Indexed by path, gives the SHA-1 of the file.
61
 
 
62
 
    validator
63
 
        Indexed by path, gives the fingerprint of the file last time it was read.
 
59
    _cache
 
60
        Indexed by path, points to a two-tuple of the SHA-1 of the file.
 
61
        and its fingerprint.
64
62
 
65
63
    stat_count
66
64
        number of times files have been statted
78
76
        self.miss_count = 0
79
77
        self.stat_count = 0
80
78
        self.danger_count = 0
81
 
        self.cache_sha1 = {}
82
 
        self.validator = {}
 
79
 
 
80
        self._cache = {}
83
81
 
84
82
 
85
83
    def clear(self):
86
 
        """Discard all cached information."""
87
 
        self.validator = {}
88
 
        self.cache_sha1 = {}
 
84
        """Discard all cached information.
 
85
 
 
86
        This does not reset the counters."""
 
87
        self._cache_sha1 = {}
89
88
 
90
89
 
91
90
    def get_sha1(self, path):
99
98
        
100
99
        abspath = os.path.join(self.basedir, path)
101
100
        fp = _fingerprint(abspath)
102
 
        cache_fp = self.validator.get(path)
 
101
        c = self._cache.get(path)
 
102
        if c:
 
103
            cache_sha1, cache_fp = c
 
104
        else:
 
105
            cache_sha1, cache_fp = None, None
103
106
 
104
107
        self.stat_count += 1
105
108
 
108
111
            return None
109
112
        elif cache_fp and (cache_fp == fp):
110
113
            self.hit_count += 1
111
 
            return self.cache_sha1[path]
 
114
            return cache_sha1
112
115
        else:
113
116
            self.miss_count += 1
114
117
            digest = sha_file(file(abspath, 'rb'))
120
123
                # next time.
121
124
                self.danger_count += 1 
122
125
                if cache_fp:
123
 
                    del self.validator[path]
124
 
                    del self.cache_sha1[path]
 
126
                    del self._cache[path]
125
127
            else:
126
 
                self.validator[path] = fp
127
 
                self.cache_sha1[path] = digest
 
128
                self._cache[path] = (digest, fp)
128
129
 
129
130
            return digest
130
131
 
138
139
        try:
139
140
            outf.write(CACHE_HEADER + '\n')
140
141
 
141
 
            for path in self.cache_sha1:
 
142
            for path, c  in self._cache.iteritems():
142
143
                assert '//' not in path, path
143
144
                outf.write(path.encode('utf-8'))
144
145
                outf.write('// ')
145
 
                print >>outf, self.cache_sha1[path],
146
 
                for fld in self.validator[path]:
 
146
                print >>outf, c[0],     # hex sha1
 
147
                for fld in c[1]:
147
148
                    print >>outf, fld,
148
149
                print >>outf
149
150