mutable: add get_size_of_best_version to the interface, to simplify the web HEAD...
authorBrian Warner <warner@allmydata.com>
Wed, 13 Aug 2008 02:02:52 +0000 (19:02 -0700)
committerBrian Warner <warner@allmydata.com>
Wed, 13 Aug 2008 02:02:52 +0000 (19:02 -0700)
src/allmydata/interfaces.py
src/allmydata/mutable/node.py
src/allmydata/test/common.py
src/allmydata/test/test_mutable.py

index 19f4b126737625f8b6cb8060abc41a43cb482e07..5900537a03d94fefbfd10ec586d3a2ddd9294663 100644 (file)
@@ -539,6 +539,13 @@ class IMutableFileNode(IFileNode, IMutableFilesystemNode):
         UnrecoverableFileError.
         """
 
+    def get_size_of_best_version():
+        """Find the size of the version that would be downloaded with
+        download_best_version(), without actually downloading the whole file.
+
+        I return a Deferred that fires with an integer.
+        """
+
     def overwrite(new_contents):
         """Unconditionally replace the contents of the mutable file with new
         ones. This simply chains get_servermap(MODE_WRITE) and upload(). This
index e850c675ff62ba2f187ecb80aed7f4e398cd72ce..cf95ec55fcd468771b9e2c2fff90e4b64cacaa01 100644 (file)
@@ -310,6 +310,16 @@ class MutableFileNode:
             raise UnrecoverableFileError("no recoverable versions")
         return self._try_once_to_download_version(servermap, goal)
 
+    def get_size_of_best_version(self):
+        d = self.get_servermap(MODE_READ)
+        def _got_servermap(smap):
+            ver = smap.best_recoverable_version()
+            if not ver:
+                raise UnrecoverableFileError("no recoverable version")
+            return smap.size_of_version(ver)
+        d.addCallback(_got_servermap)
+        return d
+
     def overwrite(self, new_contents):
         return self._do_serialized(self._overwrite, new_contents)
     def _overwrite(self, new_contents):
index fc3a32a9aa3959c677d0f409cab5da9efc9a69e5..e3af2eee69a4bcf3e8baef55aa126da9e4cc5384 100644 (file)
@@ -130,6 +130,8 @@ class FakeMutableFileNode:
         return "\x00"*16
     def get_size(self):
         return "?" # TODO: see mutable.MutableFileNode.get_size
+    def get_size_of_best_version(self):
+        return defer.succeed(len(self.all_contents[self.storage_index]))
 
     def get_storage_index(self):
         return self.storage_index
index ca78bbb26a81672eae6a13c26687a1b5a47482af..3b05a8660675251529b656a0d235e0f7bf5b8abb 100644 (file)
@@ -306,6 +306,9 @@ class Filenode(unittest.TestCase, testutil.ShouldFailMixin):
             d.addCallback(lambda res: self.failUnlessIdentical(res, None))
             d.addCallback(lambda res: n.download_best_version())
             d.addCallback(lambda res: self.failUnlessEqual(res, "contents 1"))
+            d.addCallback(lambda res: n.get_size_of_best_version())
+            d.addCallback(lambda size:
+                          self.failUnlessEqual(size, len("contents 1")))
             d.addCallback(lambda res: n.overwrite("contents 2"))
             d.addCallback(lambda res: n.download_best_version())
             d.addCallback(lambda res: self.failUnlessEqual(res, "contents 2"))