[Pkg-bazaar-commits] ./bzr/unstable r862: - code to re-read hashcache from file

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:21:16 UTC 2009


------------------------------------------------------------
revno: 862
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Fri 2005-07-08 12:40:22 +1000
message:
  - code to re-read hashcache from file
modified:
  bzrlib/hashcache.py
  bzrlib/selftest/testhashcache.py
-------------- next part --------------
=== modified file 'bzrlib/hashcache.py'
--- a/bzrlib/hashcache.py	2005-07-08 02:21:13 +0000
+++ b/bzrlib/hashcache.py	2005-07-08 02:40:22 +0000
@@ -17,7 +17,7 @@
 
 
 
-CACHE_HEADER = "### bzr statcache v5"    
+CACHE_HEADER = "### bzr statcache v5\n"
 
 
 def _fingerprint(abspath):
@@ -137,7 +137,7 @@
 
         outf = AtomicFile(cachefn, 'wb')
         try:
-            outf.write(CACHE_HEADER + '\n')
+            print >>outf, CACHE_HEADER,
 
             for path, c  in self._cache.iteritems():
                 assert '//' not in path, path
@@ -145,7 +145,7 @@
                 outf.write('// ')
                 print >>outf, c[0],     # hex sha1
                 for fld in c[1]:
-                    print >>outf, fld,
+                    print >>outf, "%d" % fld,
                 print >>outf
 
             outf.commit()
@@ -153,3 +153,47 @@
             if not outf.closed:
                 outf.abort()
         
+
+
+    def read(self, cachefn):
+        """Reinstate cache from file.
+
+        Overwrites existing cache.
+
+        If the cache file has the wrong version marker, this just clears 
+        the cache."""
+        from bzrlib.trace import mutter, warning
+
+        inf = file(cachefn, 'rb')
+        self._cache = {}
+
+        hdr = inf.readline()
+        if hdr != CACHE_HEADER:
+            mutter('cache header marker not found at top of %s; discarding cache'
+                   % cachefn)
+            return
+
+        for l in inf:
+            pos = l.index('// ')
+            path = l[:pos].decode('utf-8')
+            if path in self._cache:
+                warning('duplicated path %r in cache' % path)
+                continue
+
+            pos += 3
+            fields = l[pos:].split(' ')
+            if len(fields) != 6:
+                warning("bad line in hashcache: %r" % l)
+                continue
+
+            sha1 = fields[0]
+            if len(sha1) != 40:
+                warning("bad sha1 in hashcache: %r" % sha1)
+                continue
+
+            fp = tuple(map(long, fields[1:]))
+
+            self._cache[path] = (sha1, fp)
+
+
+        

=== modified file 'bzrlib/selftest/testhashcache.py'
--- a/bzrlib/selftest/testhashcache.py	2005-07-08 02:28:58 +0000
+++ b/bzrlib/selftest/testhashcache.py	2005-07-08 02:40:22 +0000
@@ -96,7 +96,12 @@
         del hc
 
         hc = HashCache('.')
-        # hc.read('stat-cache')
+        hc.read('stat-cache')
+
+        self.assertEquals(len(hc._cache), 1)
+        self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
+        self.assertEquals(hc.hit_count, 1)
+        self.assertEquals(hc.miss_count, 0)
         
 
         



More information about the Pkg-bazaar-commits mailing list