/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
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.
64
65
    stat_count
66
        number of times files have been statted
67
68
    hit_count
69
        number of times files have been retrieved from the cache, avoiding a
70
        re-read
71
        
72
    miss_count
73
        number of misses (times files have been completely re-read)
74
    """
75
    def __init__(self, basedir):
76
        self.basedir = basedir
77
        self.hit_count = 0
78
        self.miss_count = 0
79
        self.stat_count = 0
80
        self.danger_count = 0
81
        self.cache_sha1 = {}
82
        self.validator = {}
83
84
85
    def clear(self):
86
        """Discard all cached information."""
87
        self.validator = {}
88
        self.cache_sha1 = {}
89
90
91
    def get_sha1(self, path):
92
        """Return the hex SHA-1 of the contents of the file at path.
93
94
        XXX: If the file does not exist or is not a plain file???
95
        """
96
97
        import os, time
98
        from bzrlib.osutils import sha_file
99
        
100
        abspath = os.path.join(self.basedir, path)
101
        fp = _fingerprint(abspath)
102
        cache_fp = self.validator.get(path)
103
104
        self.stat_count += 1
105
106
        if not fp:
107
            # not a regular file
108
            return None
109
        elif cache_fp and (cache_fp == fp):
110
            self.hit_count += 1
111
            return self.cache_sha1[path]
112
        else:
113
            self.miss_count += 1
114
            digest = sha_file(file(abspath, 'rb'))
115
116
            now = int(time.time())
117
            if fp[1] >= now or fp[2] >= now:
118
                # changed too recently; can't be cached.  we can
119
                # return the result and it could possibly be cached
120
                # next time.
121
                self.danger_count += 1 
122
                if cache_fp:
123
                    del self.validator[path]
124
                    del self.cache_sha1[path]
125
            else:
126
                self.validator[path] = fp
127
                self.cache_sha1[path] = digest
128
129
            return digest
130
859 by Martin Pool
- add HashCache.write and a simple test for it
131
132
133
    def write(self, cachefn):
134
        """Write contents of cache to file."""
135
        from atomicfile import AtomicFile
136
137
        outf = AtomicFile(cachefn, 'wb')
138
        try:
139
            outf.write(CACHE_HEADER + '\n')
140
141
            for path in self.cache_sha1:
142
                assert '//' not in path, path
143
                outf.write(path.encode('utf-8'))
144
                outf.write('// ')
145
                print >>outf, self.cache_sha1[path],
146
                for fld in self.validator[path]:
147
                    print >>outf, fld,
148
                print >>outf
149
150
            outf.commit()
151
        finally:
152
            if not outf.closed:
153
                outf.abort()
154