From 802668818e930d91c37f8107e4c8822bd3ebdfac Mon Sep 17 00:00:00 2001 From: zyp Date: Wed, 25 Oct 2006 16:14:43 +0000 Subject: [project @ zyp-20061025161443-953b0f9ed85da86e] [project @ 64] Implemented caching. --- pyanidb/hash.py | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) (limited to 'pyanidb') diff --git a/pyanidb/hash.py b/pyanidb/hash.py index 70f49af..d655d53 100644 --- a/pyanidb/hash.py +++ b/pyanidb/hash.py @@ -1,25 +1,61 @@ -import multihash, threading, time +import multihash, threading, time, os +try: + import xattr +except ImportError: + xattr = None + +class File: + def __init__(self, name, algorithms, cache): + self.name = name + self.size = os.path.getsize(name) + self.mtime = os.path.getmtime(name) + self.cached = False + if cache: + self.read_cache() + if sum([not hasattr(self, a) for a in algorithms]): + self.cached = False + h = multihash.hash_file(name, algorithms) + for a in algorithms: + setattr(self, a, getattr(h, a)()) + self.write_cache() + + def read_cache(self): + cache = dict([(n[13:], xattr.getxattr(self.name, n)) for n in xattr.listxattr(self.name) if n.startswith('user.pyanidb.')]) + if 'mtime' not in cache or str(int(self.mtime)) != cache.pop('mtime'): + return + for n, v in cache.iteritems(): + setattr(self, n, v) + self.cached = True + + def write_cache(self): + try: + xattr.setxattr(self.name, 'user.pyanidb.mtime', str(int(self.mtime))) + for n in ('ed2k', 'md5', 'sha1', 'crc32'): + if hasattr(self, n): + xattr.setxattr(self.name, 'user.pyanidb.' + n, getattr(self, n)) + except IOError: + pass class Hashthread(threading.Thread): - def __init__(self, filelist, hashlist, algorithms, *args, **kwargs): + def __init__(self, filelist, hashlist, algorithms, cache, *args, **kwargs): self.filelist = filelist self.hashlist = hashlist self.algorithms = algorithms + self.cache = cache threading.Thread.__init__(self, *args, **kwargs) def run(self): try: while 1: f = self.filelist.pop(0) - h = multihash.hash_file(f, self.algorithms) - self.hashlist.append((f, h)) + self.hashlist.append(File(f, self.algorithms, self.cache)) except IndexError: return -def hash_files(files, num_threads = 1, algorithms = ('ed2k',)): +def hash_files(files, cache = False, num_threads = 1, algorithms = ('ed2k',)): hashlist = [] threads = [] for x in xrange(num_threads): - thread = Hashthread(files, hashlist, algorithms) + thread = Hashthread(files, hashlist, algorithms, cache) thread.start() threads.append(thread) while hashlist or sum([thread.isAlive() for thread in threads]): -- cgit v1.2.3