Prechádzať zdrojové kódy

Some code refactoring.

Jonas Borgström 15 rokov pred
rodič
commit
60126b6330
2 zmenil súbory, kde vykonal 22 pridanie a 22 odobranie
  1. 20 20
      dedupestore/archiver.py
  2. 2 2
      dedupestore/chunkifier.py

+ 20 - 20
dedupestore/archiver.py

@@ -17,30 +17,32 @@ NS_CHUNKS  = 'CHUNKS'
 class Cache(object):
 class Cache(object):
     """Client Side cache
     """Client Side cache
     """
     """
-    def __init__(self, path, store):
+    def __init__(self, store):
         self.store = store
         self.store = store
-        self.path = path
+        self.path = os.path.join(os.path.expanduser('~'), '.dedupestore', 'cache', 
+                                 '%s.cache' % self.store.uuid)
         self.tid = -1
         self.tid = -1
         self.open()
         self.open()
         if self.tid != self.store.tid:
         if self.tid != self.store.tid:
-            print self.tid, self.store.tid
-            self.create()
+            self.init()
 
 
     def open(self):
     def open(self):
-        if self.store.tid == -1:
+        if not os.path.exists(self.path):
             return
             return
-        filename = os.path.join(self.path, '%s.cache' % self.store.uuid)
-        if not os.path.exists(filename):
+        print 'Loading cache: ', self.path, '...'
+        data = cPickle.loads(zlib.decompress(open(self.path, 'rb').read()))
+        if data['uuid'] != self.store.uuid:
+            print >> sys.stderr, 'Cache UUID mismatch'
             return
             return
-        print 'Loading cache: ', filename, '...'
-        data = cPickle.loads(zlib.decompress(open(filename, 'rb').read()))
         self.chunkmap = data['chunkmap']
         self.chunkmap = data['chunkmap']
         self.summap = data['summap']
         self.summap = data['summap']
         self.archives = data['archives']
         self.archives = data['archives']
         self.tid = data['tid']
         self.tid = data['tid']
         print 'done'
         print 'done'
 
 
-    def create(self):
+    def init(self):
+        """Initializes cache by fetching and reading all archive indicies
+        """
         self.summap = {}
         self.summap = {}
         self.chunkmap = {}
         self.chunkmap = {}
         self.archives = []
         self.archives = []
@@ -60,11 +62,14 @@ class Cache(object):
     def save(self):
     def save(self):
         assert self.store.state == Store.OPEN
         assert self.store.state == Store.OPEN
         print 'saving cache'
         print 'saving cache'
-        data = {'chunkmap': self.chunkmap, 'summap': self.summap,
+        data = {'uuid': self.store.uuid, 
+                'chunkmap': self.chunkmap, 'summap': self.summap,
                 'tid': self.store.tid, 'archives': self.archives}
                 'tid': self.store.tid, 'archives': self.archives}
-        filename = os.path.join(self.path, '%s.cache' % self.store.uuid)
-        print 'Saving cache as:', filename
-        with open(filename, 'wb') as fd:
+        print 'Saving cache as:', self.path
+        cachedir = os.path.dirname(self.path)
+        if not os.path.exists(cachedir):
+            os.makedirs(cachedir)
+        with open(self.path, 'wb') as fd:
             fd.write(zlib.compress(cPickle.dumps(data)))
             fd.write(zlib.compress(cPickle.dumps(data)))
         print 'done'
         print 'done'
 
 
@@ -221,8 +226,6 @@ class Archiver(object):
 
 
     def run(self):
     def run(self):
         parser = OptionParser()
         parser = OptionParser()
-        parser.add_option("-C", "--cache", dest="cache",
-                          help="cache directory to use", metavar="CACHE")
         parser.add_option("-s", "--store", dest="store",
         parser.add_option("-s", "--store", dest="store",
                           help="path to dedupe store", metavar="STORE")
                           help="path to dedupe store", metavar="STORE")
         parser.add_option("-c", "--create", dest="create_archive",
         parser.add_option("-c", "--create", dest="create_archive",
@@ -243,10 +246,7 @@ class Archiver(object):
             self.store = Store(options.store)
             self.store = Store(options.store)
         else:
         else:
             parser.error('No store path specified')
             parser.error('No store path specified')
-        if options.cache:
-            self.cache = Cache(options.cache, self.store)
-        else:
-            parser.error('No cache path specified')
+        self.cache = Cache(self.store)
         if options.list_archives:
         if options.list_archives:
             self.list_archives()
             self.list_archives()
         elif options.list_archive:
         elif options.list_archive:

+ 2 - 2
dedupestore/chunkifier.py

@@ -73,6 +73,8 @@ class ChunkifyIter(object):
                 else:
                 else:
                     self.done = True
                     self.done = True
                     return self.data[self.i:]
                     return self.data[self.i:]
+            elif o == self.chunk_size:
+                return self.data[self.i-self.chunk_size:self.i]
             elif self.sum in self.chunks:
             elif self.sum in self.chunks:
                 if o > 0:
                 if o > 0:
                     chunk = self.data[self.i - o:self.i]
                     chunk = self.data[self.i - o:self.i]
@@ -81,8 +83,6 @@ class ChunkifyIter(object):
                     self.i += self.chunk_size
                     self.i += self.chunk_size
                 self.full_sum = True
                 self.full_sum = True
                 return chunk
                 return chunk
-            elif o == self.chunk_size:
-                return self.data[self.i-self.chunk_size:self.i]
             else:
             else:
                 self.i += 1
                 self.i += 1
                 o += 1
                 o += 1