~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Martin Pool
  • Date: 2005-07-17 18:38:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050717183843-6e57535ea3a77241
- weavebench allows number of iterations to be given as command line
  argument

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
# TODO: Up-front, stat all files in order and remove those which are deleted or 
18
 
# out-of-date.  Don't actually re-read them until they're needed.  That ought 
19
 
# to bring all the inodes into core so that future stats to them are fast, and 
20
 
# it preserves the nice property that any caller will always get up-to-date
21
 
# data except in unavoidable cases.
 
17
 
 
18
 
 
19
# TODO: Perhaps have a way to stat all the files in inode order, and
 
20
# then remember that they're all fresh for the lifetime of the object?
 
21
 
 
22
# TODO: Keep track of whether there are in-memory updates that need to
 
23
# be flushed.
22
24
 
23
25
# TODO: Perhaps return more details on the file to avoid statting it
24
26
# again: nonexistent, file type, size, etc
25
27
 
26
28
 
27
29
 
 
30
 
28
31
CACHE_HEADER = "### bzr hashcache v5\n"
29
32
 
30
 
import os, stat, time
31
 
 
32
 
from bzrlib.osutils import sha_file
33
 
from bzrlib.trace import mutter, warning
34
 
 
35
 
 
36
33
 
37
34
def _fingerprint(abspath):
 
35
    import os, stat
 
36
 
38
37
    try:
39
38
        fs = os.lstat(abspath)
40
39
    except OSError:
92
91
        self.miss_count = 0
93
92
        self.stat_count = 0
94
93
        self.danger_count = 0
95
 
        self.removed_count = 0
96
 
        self.update_count = 0
97
94
        self._cache = {}
98
95
 
99
96
 
 
97
 
100
98
    def cache_file_name(self):
101
 
        return os.sep.join([self.basedir, '.bzr', 'stat-cache'])
 
99
        import os.path
 
100
        return os.path.join(self.basedir, '.bzr', 'stat-cache')
102
101
 
103
102
 
104
103
 
112
111
            self._cache = {}
113
112
 
114
113
 
115
 
    def scan(self):
116
 
        """Scan all files and remove entries where the cache entry is obsolete.
117
 
        
118
 
        Obsolete entries are those where the file has been modified or deleted
119
 
        since the entry was inserted.        
120
 
        """
121
 
        prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()]
122
 
        prep.sort()
123
 
        
124
 
        for inum, path, cache_entry in prep:
125
 
            abspath = os.sep.join([self.basedir, path])
126
 
            fp = _fingerprint(abspath)
127
 
            self.stat_count += 1
128
 
            
129
 
            cache_fp = cache_entry[1]
130
 
    
131
 
            if (not fp) or (cache_fp != fp):
132
 
                # not here or not a regular file anymore
133
 
                self.removed_count += 1
134
 
                self.needs_write = True
135
 
                del self._cache[path]
136
 
 
137
 
 
138
 
 
139
114
    def get_sha1(self, path):
140
 
        """Return the sha1 of a file.
 
115
        """Return the hex SHA-1 of the contents of the file at path.
 
116
 
 
117
        XXX: If the file does not exist or is not a plain file???
141
118
        """
142
 
        abspath = os.sep.join([self.basedir, path])
 
119
 
 
120
        import os, time
 
121
        from bzrlib.osutils import sha_file
 
122
        from bzrlib.trace import mutter
 
123
        
 
124
        abspath = os.path.join(self.basedir, path)
 
125
        fp = _fingerprint(abspath)
 
126
        c = self._cache.get(path)
 
127
        if c:
 
128
            cache_sha1, cache_fp = c
 
129
        else:
 
130
            cache_sha1, cache_fp = None, None
 
131
 
143
132
        self.stat_count += 1
144
 
        file_fp = _fingerprint(abspath)
145
 
        
146
 
        if not file_fp:
147
 
            # not a regular file or not existing
148
 
            if path in self._cache:
149
 
                self.removed_count += 1
150
 
                self.needs_write = True
151
 
                del self._cache[path]
152
 
            return None        
153
 
 
154
 
        if path in self._cache:
155
 
            cache_sha1, cache_fp = self._cache[path]
156
 
        else:
157
 
            cache_sha1, cache_fp = None, None
158
 
 
159
 
        if cache_fp == file_fp:
 
133
 
 
134
        if not fp:
 
135
            # not a regular file
 
136
            return None
 
137
        elif cache_fp and (cache_fp == fp):
160
138
            self.hit_count += 1
161
139
            return cache_sha1
162
 
        
163
 
        self.miss_count += 1
164
 
        digest = sha_file(file(abspath, 'rb', buffering=65000))
 
140
        else:
 
141
            self.miss_count += 1
 
142
            digest = sha_file(file(abspath, 'rb'))
165
143
 
166
 
        now = int(time.time())
167
 
        if file_fp[1] >= now or file_fp[2] >= now:
168
 
            # changed too recently; can't be cached.  we can
169
 
            # return the result and it could possibly be cached
170
 
            # next time.
171
 
            self.danger_count += 1 
172
 
            if cache_fp:
173
 
                self.removed_count += 1
 
144
            now = int(time.time())
 
145
            if fp[1] >= now or fp[2] >= now:
 
146
                # changed too recently; can't be cached.  we can
 
147
                # return the result and it could possibly be cached
 
148
                # next time.
 
149
                self.danger_count += 1 
 
150
                if cache_fp:
 
151
                    mutter("remove outdated entry for %s" % path)
 
152
                    self.needs_write = True
 
153
                    del self._cache[path]
 
154
            elif (fp != cache_fp) or (digest != cache_sha1):
 
155
                mutter("update entry for %s" % path)
 
156
                mutter("  %r" % (fp,))
 
157
                mutter("  %r" % (cache_fp,))
174
158
                self.needs_write = True
175
 
                del self._cache[path]
176
 
        else:
177
 
            self.update_count += 1
178
 
            self.needs_write = True
179
 
            self._cache[path] = (digest, file_fp)
180
 
        
181
 
        return digest
182
 
        
 
159
                self._cache[path] = (digest, fp)
 
160
 
 
161
            return digest
183
162
 
184
163
 
185
164
 
215
194
 
216
195
        If the cache file has the wrong version marker, this just clears 
217
196
        the cache."""
 
197
        from bzrlib.trace import mutter, warning
 
198
 
218
199
        self._cache = {}
219
200
 
220
201
        fn = self.cache_file_name()
221
202
        try:
222
 
            inf = file(fn, 'rb', buffering=65000)
 
203
            inf = file(fn, 'rb')
223
204
        except IOError, e:
224
205
            mutter("failed to open %s: %s" % (fn, e))
225
206
            return