Browse Source

bandedstore: Handle aborted transactions better

Jonas Borgström 14 years ago
parent
commit
af8c943694
2 changed files with 14 additions and 3 deletions
  1. 4 0
      .gitignore
  2. 10 3
      dedupestore/bandstore.py

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+build
+*.pyc
+*.pyo
+

+ 10 - 3
dedupestore/bandstore.py

@@ -35,6 +35,10 @@ class BandStore(object):
         self.state = self.OPEN
         self.band = None
         self.to_delete = set()
+        band = self.next_band
+        while os.path.exists(self.band_filename(band)):
+            os.unlink(self.band_filename(band))
+            band += 1
 
     def create(self, path):
         os.mkdir(path)
@@ -84,18 +88,21 @@ class BandStore(object):
         else:
             raise self.DoesNotExist
 
+    def band_filename(self, band):
+        return os.path.join(self.path, 'bands', str(band))
+
     def retrieve_data(self, band, offset, size):
-        with open(os.path.join(self.path, 'bands', str(band)), 'rb') as fd:
+        with open(self.band_filename(band), 'rb') as fd:
             fd.seek(offset)
             return fd.read(size)
 
     def store_data(self, data):
         if self.band is None:
             self.band = self.next_band
-            assert not os.path.exists(os.path.join(self.path, 'bands', str(self.band)))
+            assert not os.path.exists(self.band_filename(self.band))
             self.next_band += 1
         band = self.band
-        with open(os.path.join(self.path, 'bands', str(band)), 'ab') as fd:
+        with open(self.band_filename(band), 'ab') as fd:
             offset = fd.tell()
             fd.write(data)
             if offset + len(data) > self.BAND_LIMIT: