changeset 945:bfbfc9be3faa

editor: add a pop() method to RevisionMeta to reduce resource usage The commit pass only has to read files once, removing the related data after the read helps not keeping large temporary files around after they have been stored
author Patrick Mezard <patrick@mezard.eu>
date Wed, 26 Sep 2012 21:01:17 +0200
parents d6db289f1548
children 289f2c7752a8
files hgsubversion/editor.py hgsubversion/replay.py tests/test_helpers.py
diffstat 3 files changed, 60 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/editor.py
+++ b/hgsubversion/editor.py
@@ -25,6 +25,9 @@ class NeverClosingStringIO(object):
         # object which prevent us from calling getvalue() afterwards.
         pass
 
+class EditingError(Exception):
+    pass
+
 class FileStore(object):
     def __init__(self, maxsize=None):
         self._tempdir = None
@@ -35,8 +38,12 @@ class FileStore(object):
             self._maxsize = 100*(2**20)
         self._size = 0
         self._data = {}
+        self._popped = set()
 
     def setfile(self, fname, data):
+        if fname in self._popped:
+            raise EditingError('trying to set a popped file %s' % fname)
+
         if self._maxsize < 0 or (len(data) + self._size) <= self._maxsize:
             self._data[fname] = data
             self._size += len(data)
@@ -54,6 +61,9 @@ class FileStore(object):
             self._files[fname] = fn
 
     def delfile(self, fname):
+        if fname in self._popped:
+            raise EditingError('trying to delete a popped file %s' % fname)
+
         if fname in self._data:
             del self._data[fname]
         elif fname in self._files:
@@ -61,6 +71,9 @@ class FileStore(object):
             os.unlink(path)
 
     def getfile(self, fname):
+        if fname in self._popped:
+            raise EditingError('trying to get a popped file %s' % fname)
+
         if fname in self._data:
             return self._data[fname]
         if self._tempdir is None or fname not in self._files:
@@ -72,6 +85,10 @@ class FileStore(object):
         finally:
             fp.close()
 
+    def popfile(self, fname):
+        self.delfile(fname)
+        self._popped.add(fname)
+
     def files(self):
         return list(self._files) + list(self._data)
 
@@ -129,6 +146,11 @@ class RevisionData(object):
         copied = self.copies.get(path)
         return data, isexec, islink, copied
 
+    def pop(self, path):
+        ret = self.get(path)
+        self.store.popfile(path)
+        return ret
+
     def delete(self, path):
         self.deleted[path] = True
         self.store.delfile(path)
@@ -181,9 +203,6 @@ class RevisionData(object):
     def close(self):
         self.store.close()
 
-class EditingError(Exception):
-    pass
-
 class CopiedFile(object):
     def __init__(self, node, path, copypath):
         self.node = node
--- a/hgsubversion/replay.py
+++ b/hgsubversion/replay.py
@@ -154,7 +154,7 @@ def _convert_rev(ui, meta, svn, r, tbdel
 
         def filectxfn(repo, memctx, path):
             current_file = files[path]
-            data, isexec, islink, copied = current.get(current_file)
+            data, isexec, islink, copied = current.pop(current_file)
             if isexec is None or islink is None:
                 flags = parentctx.flags(path)
                 if isexec is None:
new file mode 100644
--- /dev/null
+++ b/tests/test_helpers.py
@@ -0,0 +1,37 @@
+import os, sys, unittest
+
+_rootdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+sys.path.insert(0, _rootdir)
+
+from hgsubversion import editor
+
+class TestHelpers(unittest.TestCase):
+    def test_filestore(self):
+        fs = editor.FileStore(2)
+        fs.setfile('a', 'a')
+        fs.setfile('b', 'b')
+        self.assertEqual('a', fs._data.get('a'))
+        self.assertEqual('b', fs._data.get('b'))
+
+        fs.delfile('b')
+        self.assertRaises(IOError, lambda: fs.getfile('b'))
+        fs.setfile('bb', 'bb')
+        self.assertTrue('bb' in fs._files)
+        self.assertTrue('bb' not in fs._data)
+        self.assertEqual('bb', fs.getfile('bb'))
+
+        fs.delfile('bb')
+        self.assertTrue('bb' not in fs._files)
+        self.assertEqual([], os.listdir(fs._tempdir))
+        self.assertRaises(IOError, lambda: fs.getfile('bb'))
+
+        fs.setfile('bb', 'bb')
+        self.assertEqual(1, len(os.listdir(fs._tempdir)))
+        fs.popfile('bb')
+        self.assertEqual([], os.listdir(fs._tempdir))
+        self.assertRaises(editor.EditingError, lambda: fs.getfile('bb'))
+
+def suite():
+    return unittest.TestSuite([
+        unittest.TestLoader().loadTestsFromTestCase(TestHelpers),
+        ])