~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Martin Pool
  • Date: 2005-06-30 08:40:59 UTC
  • mto: This revision was merged to the branch mainline in revision 852.
  • Revision ID: mbp@sourcefrog.net-20050630084059-d6eb6cb46972365b
Rename Weave.get_included to inclusions and getiter to get_iter

Refactor annotate() code 

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
20
 
def _fingerprint(abspath):
21
 
    import os, stat
22
 
 
23
 
    try:
24
 
        fs = os.lstat(abspath)
25
 
    except OSError:
26
 
        # might be missing, etc
27
 
        return None
28
 
 
29
 
    if stat.S_ISDIR(fs.st_mode):
30
 
        return None
31
 
 
32
 
    return (fs.st_size, fs.st_mtime,
33
 
            fs.st_ctime, fs.st_ino, fs.st_dev)
34
 
 
35
 
 
36
 
class HashCache(object):
37
 
    """Cache for looking up file SHA-1.
38
 
 
39
 
    Files are considered to match the cached value if the fingerprint
40
 
    of the file has not changed.  This includes its mtime, ctime,
41
 
    device number, inode number, and size.  This should catch
42
 
    modifications or replacement of the file by a new one.
43
 
 
44
 
    This may not catch modifications that do not change the file's
45
 
    size and that occur within the resolution window of the
46
 
    timestamps.  To handle this we specifically do not cache files
47
 
    which have changed since the start of the present second, since
48
 
    they could undetectably change again.
49
 
 
50
 
    This scheme may fail if the machine's clock steps backwards.
51
 
    Don't do that.
52
 
 
53
 
    This does not canonicalize the paths passed in; that should be
54
 
    done by the caller.
55
 
 
56
 
    cache_sha1
57
 
        Indexed by path, gives the SHA-1 of the file.
58
 
 
59
 
    validator
60
 
        Indexed by path, gives the fingerprint of the file last time it was read.
61
 
 
62
 
    stat_count
63
 
        number of times files have been statted
64
 
 
65
 
    hit_count
66
 
        number of times files have been retrieved from the cache, avoiding a
67
 
        re-read
68
 
        
69
 
    miss_count
70
 
        number of misses (times files have been completely re-read)
71
 
    """
72
 
    def __init__(self, basedir):
73
 
        self.basedir = basedir
74
 
        self.hit_count = 0
75
 
        self.miss_count = 0
76
 
        self.stat_count = 0
77
 
        self.danger_count = 0
78
 
        self.cache_sha1 = {}
79
 
        self.validator = {}
80
 
 
81
 
 
82
 
    def clear(self):
83
 
        """Discard all cached information."""
84
 
        self.validator = {}
85
 
        self.cache_sha1 = {}
86
 
 
87
 
 
88
 
    def get_sha1(self, path):
89
 
        """Return the hex SHA-1 of the contents of the file at path.
90
 
 
91
 
        XXX: If the file does not exist or is not a plain file???
92
 
        """
93
 
 
94
 
        import os, time
95
 
        from bzrlib.osutils import sha_file
96
 
        
97
 
        abspath = os.path.join(self.basedir, path)
98
 
        fp = _fingerprint(abspath)
99
 
        cache_fp = self.validator.get(path)
100
 
 
101
 
        self.stat_count += 1
102
 
 
103
 
        if not fp:
104
 
            # not a regular file
105
 
            return None
106
 
        elif cache_fp and (cache_fp == fp):
107
 
            self.hit_count += 1
108
 
            return self.cache_sha1[path]
109
 
        else:
110
 
            self.miss_count += 1
111
 
            digest = sha_file(file(abspath, 'rb'))
112
 
 
113
 
            now = int(time.time())
114
 
            if fp[1] >= now or fp[2] >= now:
115
 
                # changed too recently; can't be cached.  we can
116
 
                # return the result and it could possibly be cached
117
 
                # next time.
118
 
                self.danger_count += 1 
119
 
                if cache_fp:
120
 
                    del self.validator[path]
121
 
                    del self.cache_sha1[path]
122
 
            else:
123
 
                self.validator[path] = fp
124
 
                self.cache_sha1[path] = digest
125
 
 
126
 
            return digest
127