846
by Martin Pool
- start adding refactored/simplified hash cache |
1 |
# (C) 2005 Canonical Ltd
|
2 |
||
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
953
by Martin Pool
- refactor imports and stats for hashcache |
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.
|
|
864
by Martin Pool
doc |
22 |
|
23 |
# TODO: Perhaps return more details on the file to avoid statting it
|
|
24 |
# again: nonexistent, file type, size, etc
|
|
25 |
||
1213
by Martin Pool
- move import in hashcache |
26 |
# TODO: Perhaps use a Python pickle instead of a text file; might be faster.
|
27 |
||
864
by Martin Pool
doc |
28 |
|
29 |
||
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
30 |
CACHE_HEADER = "### bzr hashcache v5\n" |
859
by Martin Pool
- add HashCache.write and a simple test for it |
31 |
|
953
by Martin Pool
- refactor imports and stats for hashcache |
32 |
import os, stat, time |
1092.2.6
by Robert Collins
symlink support updated to work |
33 |
import sha |
953
by Martin Pool
- refactor imports and stats for hashcache |
34 |
|
35 |
from bzrlib.osutils import sha_file |
|
36 |
from bzrlib.trace import mutter, warning |
|
1213
by Martin Pool
- move import in hashcache |
37 |
from bzrlib.atomicfile import AtomicFile |
38 |
||
953
by Martin Pool
- refactor imports and stats for hashcache |
39 |
|
1092.2.6
by Robert Collins
symlink support updated to work |
40 |
FP_MODE_COLUMN = 5 |
859
by Martin Pool
- add HashCache.write and a simple test for it |
41 |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
42 |
def _fingerprint(abspath): |
43 |
try: |
|
44 |
fs = os.lstat(abspath) |
|
45 |
except OSError: |
|
46 |
# might be missing, etc
|
|
47 |
return None |
|
48 |
||
49 |
if stat.S_ISDIR(fs.st_mode): |
|
50 |
return None |
|
51 |
||
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
52 |
# we discard any high precision because it's not reliable; perhaps we
|
53 |
# could do better on some systems?
|
|
54 |
return (fs.st_size, long(fs.st_mtime), |
|
1092.2.6
by Robert Collins
symlink support updated to work |
55 |
long(fs.st_ctime), fs.st_ino, fs.st_dev, fs.st_mode) |
846
by Martin Pool
- start adding refactored/simplified hash cache |
56 |
|
57 |
||
58 |
class HashCache(object): |
|
59 |
"""Cache for looking up file SHA-1.
|
|
60 |
||
61 |
Files are considered to match the cached value if the fingerprint
|
|
62 |
of the file has not changed. This includes its mtime, ctime,
|
|
63 |
device number, inode number, and size. This should catch
|
|
64 |
modifications or replacement of the file by a new one.
|
|
65 |
||
66 |
This may not catch modifications that do not change the file's
|
|
67 |
size and that occur within the resolution window of the
|
|
68 |
timestamps. To handle this we specifically do not cache files
|
|
69 |
which have changed since the start of the present second, since
|
|
70 |
they could undetectably change again.
|
|
71 |
||
72 |
This scheme may fail if the machine's clock steps backwards.
|
|
73 |
Don't do that.
|
|
74 |
||
75 |
This does not canonicalize the paths passed in; that should be
|
|
76 |
done by the caller.
|
|
77 |
||
860
by Martin Pool
- refactor hashcache to use just one dictionary |
78 |
_cache
|
79 |
Indexed by path, points to a two-tuple of the SHA-1 of the file.
|
|
80 |
and its fingerprint.
|
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
81 |
|
82 |
stat_count
|
|
83 |
number of times files have been statted
|
|
84 |
||
85 |
hit_count
|
|
86 |
number of times files have been retrieved from the cache, avoiding a
|
|
87 |
re-read
|
|
88 |
|
|
89 |
miss_count
|
|
90 |
number of misses (times files have been completely re-read)
|
|
91 |
"""
|
|
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
92 |
needs_write = False |
93 |
||
846
by Martin Pool
- start adding refactored/simplified hash cache |
94 |
def __init__(self, basedir): |
95 |
self.basedir = basedir |
|
96 |
self.hit_count = 0 |
|
97 |
self.miss_count = 0 |
|
98 |
self.stat_count = 0 |
|
99 |
self.danger_count = 0 |
|
953
by Martin Pool
- refactor imports and stats for hashcache |
100 |
self.removed_count = 0 |
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
101 |
self.update_count = 0 |
860
by Martin Pool
- refactor hashcache to use just one dictionary |
102 |
self._cache = {} |
846
by Martin Pool
- start adding refactored/simplified hash cache |
103 |
|
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
104 |
def cache_file_name(self): |
1185.17.1
by Martin Pool
[pick] clear hashcache in format upgrade to avoid worrisome warning |
105 |
# FIXME: duplicate path logic here, this should be
|
106 |
# something like 'branch.controlfile'.
|
|
953
by Martin Pool
- refactor imports and stats for hashcache |
107 |
return os.sep.join([self.basedir, '.bzr', 'stat-cache']) |
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
108 |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
109 |
def clear(self): |
860
by Martin Pool
- refactor hashcache to use just one dictionary |
110 |
"""Discard all cached information.
|
111 |
||
112 |
This does not reset the counters."""
|
|
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
113 |
if self._cache: |
114 |
self.needs_write = True |
|
115 |
self._cache = {} |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
116 |
|
117 |
||
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
118 |
def scan(self): |
119 |
"""Scan all files and remove entries where the cache entry is obsolete.
|
|
120 |
|
|
121 |
Obsolete entries are those where the file has been modified or deleted
|
|
122 |
since the entry was inserted.
|
|
123 |
"""
|
|
124 |
prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()] |
|
953
by Martin Pool
- refactor imports and stats for hashcache |
125 |
prep.sort() |
126 |
||
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
127 |
for inum, path, cache_entry in prep: |
128 |
abspath = os.sep.join([self.basedir, path]) |
|
129 |
fp = _fingerprint(abspath) |
|
130 |
self.stat_count += 1 |
|
131 |
||
132 |
cache_fp = cache_entry[1] |
|
133 |
||
134 |
if (not fp) or (cache_fp != fp): |
|
135 |
# not here or not a regular file anymore
|
|
136 |
self.removed_count += 1 |
|
137 |
self.needs_write = True |
|
138 |
del self._cache[path] |
|
139 |
||
953
by Martin Pool
- refactor imports and stats for hashcache |
140 |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
141 |
def get_sha1(self, path): |
953
by Martin Pool
- refactor imports and stats for hashcache |
142 |
"""Return the sha1 of a file.
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
143 |
"""
|
953
by Martin Pool
- refactor imports and stats for hashcache |
144 |
abspath = os.sep.join([self.basedir, path]) |
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
145 |
self.stat_count += 1 |
146 |
file_fp = _fingerprint(abspath) |
|
147 |
||
148 |
if not file_fp: |
|
149 |
# not a regular file or not existing
|
|
150 |
if path in self._cache: |
|
151 |
self.removed_count += 1 |
|
152 |
self.needs_write = True |
|
153 |
del self._cache[path] |
|
154 |
return None |
|
953
by Martin Pool
- refactor imports and stats for hashcache |
155 |
|
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
156 |
if path in self._cache: |
157 |
cache_sha1, cache_fp = self._cache[path] |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
158 |
else: |
159 |
cache_sha1, cache_fp = None, None |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
160 |
|
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
161 |
if cache_fp == file_fp: |
846
by Martin Pool
- start adding refactored/simplified hash cache |
162 |
self.hit_count += 1 |
860
by Martin Pool
- refactor hashcache to use just one dictionary |
163 |
return cache_sha1 |
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
164 |
|
165 |
self.miss_count += 1 |
|
1092.2.6
by Robert Collins
symlink support updated to work |
166 |
|
167 |
||
168 |
mode = file_fp[FP_MODE_COLUMN] |
|
169 |
if stat.S_ISREG(mode): |
|
170 |
digest = sha_file(file(abspath, 'rb', buffering=65000)) |
|
171 |
elif stat.S_ISLNK(mode): |
|
172 |
link_target = os.readlink(abspath) |
|
173 |
digest = sha.new(os.readlink(abspath)).hexdigest() |
|
174 |
else: |
|
175 |
raise BzrError("file %r: unknown file stat mode: %o"%(abspath,mode)) |
|
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
176 |
|
177 |
now = int(time.time()) |
|
178 |
if file_fp[1] >= now or file_fp[2] >= now: |
|
179 |
# changed too recently; can't be cached. we can
|
|
180 |
# return the result and it could possibly be cached
|
|
181 |
# next time.
|
|
182 |
self.danger_count += 1 |
|
183 |
if cache_fp: |
|
184 |
self.removed_count += 1 |
|
185 |
self.needs_write = True |
|
186 |
del self._cache[path] |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
187 |
else: |
954
by Martin Pool
- separate out code that just scans the hash cache to find files that are possibly |
188 |
self.update_count += 1 |
189 |
self.needs_write = True |
|
190 |
self._cache[path] = (digest, file_fp) |
|
191 |
return digest |
|
192 |
||
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
193 |
def write(self): |
859
by Martin Pool
- add HashCache.write and a simple test for it |
194 |
"""Write contents of cache to file."""
|
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
195 |
outf = AtomicFile(self.cache_file_name(), 'wb') |
859
by Martin Pool
- add HashCache.write and a simple test for it |
196 |
try: |
862
by Martin Pool
- code to re-read hashcache from file |
197 |
print >>outf, CACHE_HEADER, |
859
by Martin Pool
- add HashCache.write and a simple test for it |
198 |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
199 |
for path, c in self._cache.iteritems(): |
859
by Martin Pool
- add HashCache.write and a simple test for it |
200 |
assert '//' not in path, path |
201 |
outf.write(path.encode('utf-8')) |
|
202 |
outf.write('// ') |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
203 |
print >>outf, c[0], # hex sha1 |
204 |
for fld in c[1]: |
|
862
by Martin Pool
- code to re-read hashcache from file |
205 |
print >>outf, "%d" % fld, |
859
by Martin Pool
- add HashCache.write and a simple test for it |
206 |
print >>outf |
207 |
||
208 |
outf.commit() |
|
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
209 |
self.needs_write = False |
859
by Martin Pool
- add HashCache.write and a simple test for it |
210 |
finally: |
211 |
if not outf.closed: |
|
212 |
outf.abort() |
|
213 |
||
862
by Martin Pool
- code to re-read hashcache from file |
214 |
|
215 |
||
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
216 |
def read(self): |
862
by Martin Pool
- code to re-read hashcache from file |
217 |
"""Reinstate cache from file.
|
218 |
||
219 |
Overwrites existing cache.
|
|
220 |
||
221 |
If the cache file has the wrong version marker, this just clears
|
|
222 |
the cache."""
|
|
223 |
self._cache = {} |
|
224 |
||
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
225 |
fn = self.cache_file_name() |
226 |
try: |
|
948
by Martin Pool
- more buffering when reading/writing hashcache |
227 |
inf = file(fn, 'rb', buffering=65000) |
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
228 |
except IOError, e: |
229 |
mutter("failed to open %s: %s" % (fn, e)) |
|
1214
by Martin Pool
- hashcache should be written out if it can't be read |
230 |
# better write it now so it is valid
|
231 |
self.needs_write = True |
|
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
232 |
return
|
233 |
||
234 |
||
862
by Martin Pool
- code to re-read hashcache from file |
235 |
hdr = inf.readline() |
236 |
if hdr != CACHE_HEADER: |
|
237 |
mutter('cache header marker not found at top of %s; discarding cache' |
|
878
by Martin Pool
- fix typo |
238 |
% fn) |
1214
by Martin Pool
- hashcache should be written out if it can't be read |
239 |
self.needs_write = True |
862
by Martin Pool
- code to re-read hashcache from file |
240 |
return
|
241 |
||
242 |
for l in inf: |
|
243 |
pos = l.index('// ') |
|
244 |
path = l[:pos].decode('utf-8') |
|
245 |
if path in self._cache: |
|
246 |
warning('duplicated path %r in cache' % path) |
|
247 |
continue
|
|
248 |
||
249 |
pos += 3 |
|
250 |
fields = l[pos:].split(' ') |
|
1092.2.6
by Robert Collins
symlink support updated to work |
251 |
if len(fields) != 7: |
862
by Martin Pool
- code to re-read hashcache from file |
252 |
warning("bad line in hashcache: %r" % l) |
253 |
continue
|
|
254 |
||
255 |
sha1 = fields[0] |
|
256 |
if len(sha1) != 40: |
|
257 |
warning("bad sha1 in hashcache: %r" % sha1) |
|
258 |
continue
|
|
259 |
||
260 |
fp = tuple(map(long, fields[1:])) |
|
261 |
||
262 |
self._cache[path] = (sha1, fp) |
|
263 |
||
866
by Martin Pool
- use new path-based hashcache for WorkingTree- squash mtime/ctime to whole seconds- update and if necessary write out hashcache when WorkingTree object is created. |
264 |
self.needs_write = False |
265 |
||
266 |
||
862
by Martin Pool
- code to re-read hashcache from file |
267 |
|
268 |