immutable: refactor immutable filenodes and comparison thereof

* the two kinds of immutable filenode now have a common base class
* they store only an instance of their URI, not both an instance and a string
* they delegate comparison to that instance
This commit is contained in:
Zooko O'Whielacronx 2008-09-23 11:52:49 -07:00
parent 2ef977d85a
commit fa302544fa
2 changed files with 45 additions and 55 deletions

View File

@ -6,19 +6,17 @@ from allmydata import uri
from allmydata.immutable.checker import SimpleCHKFileChecker, \ from allmydata.immutable.checker import SimpleCHKFileChecker, \
SimpleCHKFileVerifier SimpleCHKFileVerifier
class FileNode: class ImmutableFileNode(object):
implements(IFileNode, ICheckable) implements(IFileNode, ICheckable)
checker_class = SimpleCHKFileChecker checker_class = SimpleCHKFileChecker
verifier_class = SimpleCHKFileVerifier verifier_class = SimpleCHKFileVerifier
def __init__(self, uri, client): def __init__(self, uri, client):
u = IFileURI(uri) self.u = IFileURI(uri)
self.u = u
self.uri = u.to_string()
self._client = client self._client = client
def get_uri(self): def get_readonly_uri(self):
return self.uri return self.get_uri()
def is_mutable(self): def is_mutable(self):
return False return False
@ -26,21 +24,31 @@ class FileNode:
def is_readonly(self): def is_readonly(self):
return True return True
def get_readonly_uri(self): def __hash__(self):
return self.uri return self.u.__hash__()
def __eq__(self, other):
if IFileNode.providedBy(other):
return self.u.__eq__(other.u)
else:
return False
def __ne__(self, other):
if IFileNode.providedBy(other):
return self.u.__eq__(other.u)
else:
return True
class FileNode(ImmutableFileNode):
checker_class = SimpleCHKFileChecker
def __init__(self, uri, client):
ImmutableFileNode.__init__(self, uri, client)
def get_uri(self):
return self.u.to_string()
def get_size(self): def get_size(self):
return self.u.get_size() return self.u.get_size()
def __hash__(self):
return hash((self.__class__, self.uri))
def __cmp__(self, them):
if cmp(type(self), type(them)):
return cmp(type(self), type(them))
if cmp(self.__class__, them.__class__):
return cmp(self.__class__, them.__class__)
return cmp(self.uri, them.uri)
def get_verifier(self): def get_verifier(self):
return self.u.get_verifier() return self.u.get_verifier()
@ -75,46 +83,24 @@ class FileNode:
def download(self, target): def download(self, target):
downloader = self._client.getServiceNamed("downloader") downloader = self._client.getServiceNamed("downloader")
return downloader.download(self.uri, target) return downloader.download(self.get_uri(), target)
def download_to_data(self): def download_to_data(self):
downloader = self._client.getServiceNamed("downloader") downloader = self._client.getServiceNamed("downloader")
return downloader.download_to_data(self.uri) return downloader.download_to_data(self.get_uri())
class LiteralFileNode: class LiteralFileNode(ImmutableFileNode):
implements(IFileNode, ICheckable)
def __init__(self, my_uri, client): def __init__(self, uri, client):
u = IFileURI(my_uri) ImmutableFileNode.__init__(self, uri, client)
assert isinstance(u, uri.LiteralFileURI)
self.uri = u.to_string()
self._client = client
def get_uri(self): def get_uri(self):
return self.uri return self.u.to_string()
def is_mutable(self):
return False
def is_readonly(self):
return True
def get_readonly_uri(self):
return self.uri
def get_size(self): def get_size(self):
return len(IURI(self.uri).data) return len(self.u.data)
def __hash__(self):
return hash((self.__class__, self.uri))
def __cmp__(self, them):
if cmp(type(self), type(them)):
return cmp(type(self), type(them))
if cmp(self.__class__, them.__class__):
return cmp(self.__class__, them.__class__)
return cmp(self.uri, them.uri)
def get_verifier(self): def get_verifier(self):
return None return None
@ -132,12 +118,12 @@ class LiteralFileNode:
def download(self, target): def download(self, target):
# note that this does not update the stats_provider # note that this does not update the stats_provider
data = IURI(self.uri).data data = self.u.data
target.open(len(data)) target.open(len(data))
target.write(data) target.write(data)
target.close() target.close()
return defer.maybeDeferred(target.finish) return defer.maybeDeferred(target.finish)
def download_to_data(self): def download_to_data(self):
data = IURI(self.uri).data data = self.u.data
return defer.succeed(data) return defer.succeed(data)

View File

@ -24,13 +24,17 @@ OPTIONALHTTPLEAD=r'(?:https?://(?:127.0.0.1|localhost):8123/uri/)?'
class _BaseURI: class _BaseURI:
def __hash__(self): def __hash__(self):
return hash((self.__class__, self.to_string())) return self.to_string().__hash__()
def __cmp__(self, them): def __eq__(self, them):
if cmp(type(self), type(them)): if isinstance(them, _BaseURI):
return cmp(type(self), type(them)) return self.to_string() == them.to_string()
if cmp(self.__class__, them.__class__): else:
return cmp(self.__class__, them.__class__) return False
return cmp(self.to_string(), them.to_string()) def __ne__(self, them):
if isinstance(them, _BaseURI):
return self.to_string() != them.to_string()
else:
return True
def to_human_encoding(self): def to_human_encoding(self):
return 'http://127.0.0.1:8123/uri/'+self.to_string() return 'http://127.0.0.1:8123/uri/'+self.to_string()