From: Kevan Carstensen <kevan@isnotajoke.com>
Date: Tue, 2 Aug 2011 02:09:05 +0000 (-0700)
Subject: immutable/filenode: implement unified filenode interface
X-Git-Tag: trac-5200~23
X-Git-Url: https://git.rkrishnan.org/%5B/quickstart.html?a=commitdiff_plain;h=edf9858fb844100ea7fd3c9156f55554ee9e947b;p=tahoe-lafs%2Ftahoe-lafs.git

immutable/filenode: implement unified filenode interface
---

diff --git a/src/allmydata/immutable/filenode.py b/src/allmydata/immutable/filenode.py
index 84d75035..0ac47a6c 100644
--- a/src/allmydata/immutable/filenode.py
+++ b/src/allmydata/immutable/filenode.py
@@ -5,10 +5,15 @@ import time
 now = time.time
 from zope.interface import implements
 from twisted.internet import defer
-from twisted.internet.interfaces import IConsumer
 
-from allmydata.interfaces import IImmutableFileNode, IUploadResults
 from allmydata import uri
+from twisted.internet.interfaces import IConsumer
+from twisted.protocols import basic
+from foolscap.api import eventually
+from allmydata.interfaces import IImmutableFileNode, ICheckable, \
+     IDownloadTarget, IUploadResults
+from allmydata.util import dictutil, log, base32, consumer
+from allmydata.immutable.checker import Checker
 from allmydata.check_results import CheckResults, CheckAndRepairResults
 from allmydata.util.dictutil import DictOfSets
 from pycryptopp.cipher.aes import AES
@@ -277,3 +282,33 @@ class ImmutableFileNode:
         return self._cnode.check_and_repair(monitor, verify, add_lease)
     def check(self, monitor, verify=False, add_lease=False):
         return self._cnode.check(monitor, verify, add_lease)
+
+    def get_best_readable_version(self):
+        """
+        Return an IReadable of the best version of this file. Since
+        immutable files can have only one version, we just return the
+        current filenode.
+        """
+        return defer.succeed(self)
+
+
+    def download_best_version(self):
+        """
+        Download the best version of this file, returning its contents
+        as a bytestring. Since there is only one version of an immutable
+        file, we download and return the contents of this file.
+        """
+        d = consumer.download_to_data(self)
+        return d
+
+    # for an immutable file, download_to_data (specified in IReadable)
+    # is the same as download_best_version (specified in IFileNode). For
+    # mutable files, the difference is more meaningful, since they can
+    # have multiple versions.
+    download_to_data = download_best_version
+
+
+    # get_size() (IReadable), get_current_size() (IFilesystemNode), and
+    # get_size_of_best_version(IFileNode) are all the same for immutable
+    # files.
+    get_size_of_best_version = get_current_size
diff --git a/src/allmydata/test/test_immutable.py b/src/allmydata/test/test_immutable.py
index b7aa6f65..ee299770 100644
--- a/src/allmydata/test/test_immutable.py
+++ b/src/allmydata/test/test_immutable.py
@@ -287,6 +287,32 @@ class Test(GridTestMixin, unittest.TestCase, common.ShouldFailMixin):
         d.addCallback(_try_download)
         return d
 
+    def test_download_to_data(self):
+        d = self.n.download_to_data()
+        d.addCallback(lambda data:
+            self.failUnlessEqual(data, common.TEST_DATA))
+        return d
+
+
+    def test_download_best_version(self):
+        d = self.n.download_best_version()
+        d.addCallback(lambda data:
+            self.failUnlessEqual(data, common.TEST_DATA))
+        return d
+
+
+    def test_get_best_readable_version(self):
+        d = self.n.get_best_readable_version()
+        d.addCallback(lambda n2:
+            self.failUnlessEqual(n2, self.n))
+        return d
+
+    def test_get_size_of_best_version(self):
+        d = self.n.get_size_of_best_version()
+        d.addCallback(lambda size:
+            self.failUnlessEqual(size, len(common.TEST_DATA)))
+        return d
+
 
 # XXX extend these tests to show bad behavior of various kinds from servers:
 # raising exception from each remove_foo() method, for example