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 |
||
17 |
||
18 |
||
19 |
||
859
by Martin Pool
- add HashCache.write and a simple test for it |
20 |
CACHE_HEADER = "### bzr statcache v5" |
21 |
||
22 |
||
846
by Martin Pool
- start adding refactored/simplified hash cache |
23 |
def _fingerprint(abspath): |
24 |
import os, stat |
|
25 |
||
26 |
try: |
|
27 |
fs = os.lstat(abspath) |
|
28 |
except OSError: |
|
29 |
# might be missing, etc
|
|
30 |
return None |
|
31 |
||
32 |
if stat.S_ISDIR(fs.st_mode): |
|
33 |
return None |
|
34 |
||
35 |
return (fs.st_size, fs.st_mtime, |
|
36 |
fs.st_ctime, fs.st_ino, fs.st_dev) |
|
37 |
||
38 |
||
39 |
class HashCache(object): |
|
40 |
"""Cache for looking up file SHA-1.
|
|
41 |
||
42 |
Files are considered to match the cached value if the fingerprint
|
|
43 |
of the file has not changed. This includes its mtime, ctime,
|
|
44 |
device number, inode number, and size. This should catch
|
|
45 |
modifications or replacement of the file by a new one.
|
|
46 |
||
47 |
This may not catch modifications that do not change the file's
|
|
48 |
size and that occur within the resolution window of the
|
|
49 |
timestamps. To handle this we specifically do not cache files
|
|
50 |
which have changed since the start of the present second, since
|
|
51 |
they could undetectably change again.
|
|
52 |
||
53 |
This scheme may fail if the machine's clock steps backwards.
|
|
54 |
Don't do that.
|
|
55 |
||
56 |
This does not canonicalize the paths passed in; that should be
|
|
57 |
done by the caller.
|
|
58 |
||
860
by Martin Pool
- refactor hashcache to use just one dictionary |
59 |
_cache
|
60 |
Indexed by path, points to a two-tuple of the SHA-1 of the file.
|
|
61 |
and its fingerprint.
|
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
62 |
|
63 |
stat_count
|
|
64 |
number of times files have been statted
|
|
65 |
||
66 |
hit_count
|
|
67 |
number of times files have been retrieved from the cache, avoiding a
|
|
68 |
re-read
|
|
69 |
|
|
70 |
miss_count
|
|
71 |
number of misses (times files have been completely re-read)
|
|
72 |
"""
|
|
73 |
def __init__(self, basedir): |
|
74 |
self.basedir = basedir |
|
75 |
self.hit_count = 0 |
|
76 |
self.miss_count = 0 |
|
77 |
self.stat_count = 0 |
|
78 |
self.danger_count = 0 |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
79 |
|
80 |
self._cache = {} |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
81 |
|
82 |
||
83 |
def clear(self): |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
84 |
"""Discard all cached information.
|
85 |
||
86 |
This does not reset the counters."""
|
|
87 |
self._cache_sha1 = {} |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
88 |
|
89 |
||
90 |
def get_sha1(self, path): |
|
91 |
"""Return the hex SHA-1 of the contents of the file at path.
|
|
92 |
||
93 |
XXX: If the file does not exist or is not a plain file???
|
|
94 |
"""
|
|
95 |
||
96 |
import os, time |
|
97 |
from bzrlib.osutils import sha_file |
|
98 |
||
99 |
abspath = os.path.join(self.basedir, path) |
|
100 |
fp = _fingerprint(abspath) |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
101 |
c = self._cache.get(path) |
102 |
if c: |
|
103 |
cache_sha1, cache_fp = c |
|
104 |
else: |
|
105 |
cache_sha1, cache_fp = None, None |
|
846
by Martin Pool
- start adding refactored/simplified hash cache |
106 |
|
107 |
self.stat_count += 1 |
|
108 |
||
109 |
if not fp: |
|
110 |
# not a regular file
|
|
111 |
return None |
|
112 |
elif cache_fp and (cache_fp == fp): |
|
113 |
self.hit_count += 1 |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
114 |
return cache_sha1 |
846
by Martin Pool
- start adding refactored/simplified hash cache |
115 |
else: |
116 |
self.miss_count += 1 |
|
117 |
digest = sha_file(file(abspath, 'rb')) |
|
118 |
||
119 |
now = int(time.time()) |
|
120 |
if fp[1] >= now or fp[2] >= now: |
|
121 |
# changed too recently; can't be cached. we can
|
|
122 |
# return the result and it could possibly be cached
|
|
123 |
# next time.
|
|
124 |
self.danger_count += 1 |
|
125 |
if cache_fp: |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
126 |
del self._cache[path] |
846
by Martin Pool
- start adding refactored/simplified hash cache |
127 |
else: |
860
by Martin Pool
- refactor hashcache to use just one dictionary |
128 |
self._cache[path] = (digest, fp) |
846
by Martin Pool
- start adding refactored/simplified hash cache |
129 |
|
130 |
return digest |
|
131 |
||
859
by Martin Pool
- add HashCache.write and a simple test for it |
132 |
|
133 |
||
134 |
def write(self, cachefn): |
|
135 |
"""Write contents of cache to file."""
|
|
136 |
from atomicfile import AtomicFile |
|
137 |
||
138 |
outf = AtomicFile(cachefn, 'wb') |
|
139 |
try: |
|
140 |
outf.write(CACHE_HEADER + '\n') |
|
141 |
||
860
by Martin Pool
- refactor hashcache to use just one dictionary |
142 |
for path, c in self._cache.iteritems(): |
859
by Martin Pool
- add HashCache.write and a simple test for it |
143 |
assert '//' not in path, path |
144 |
outf.write(path.encode('utf-8')) |
|
145 |
outf.write('// ') |
|
860
by Martin Pool
- refactor hashcache to use just one dictionary |
146 |
print >>outf, c[0], # hex sha1 |
147 |
for fld in c[1]: |
|
859
by Martin Pool
- add HashCache.write and a simple test for it |
148 |
print >>outf, fld, |
149 |
print >>outf |
|
150 |
||
151 |
outf.commit() |
|
152 |
finally: |
|
153 |
if not outf.closed: |
|
154 |
outf.abort() |
|
155 |