Make pithcat work with pithosmap:// URLs
authorChristos Stavrakakis <cstavr@grnet.gr>
Tue, 4 Dec 2012 12:05:31 +0000 (14:05 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Wed, 5 Dec 2012 14:51:51 +0000 (16:51 +0200)
Modify pithcat to be able to parse and work with URLs of the form:
`pithosmap://hash/size' which are neeed for archipelagos.

snf-image-host/common.sh.in
snf-image-host/pithcat

index 075dc94..150fb4c 100644 (file)
@@ -233,6 +233,8 @@ get_backend_type() {
 
     if [[ "$id" =~ ^pithos: ]]; then
         echo "pithos"
+    elif [[ "$id" =~ ^pithosmap: ]]; then
+        echo "pithos"
     elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
         if [ "$network_backend_support" = "yes" ]; then
             echo "network";
index 7edacc1..2bc541e 100755 (executable)
@@ -28,6 +28,8 @@ the URL as the user when connecting to the backend.
 from optparse import OptionParser
 from sys import exit, stdout, stderr
 from os import environ
+from binascii import hexlify, unhexlify
+from collections import namedtuple
 
 try:
     from pithos.backends.modular import ModularBackend
@@ -44,30 +46,52 @@ parser.add_option('--data', dest='data', metavar='DIR',
 parser.add_option('-s', action='store_true', dest='size', default=False,
         help='print file size and exit')
 
+LocationURL = namedtuple('LocationURL', ['account', 'container', 'object'])
+HashmapURL = namedtuple('HashmapURL', ['hash', 'size'])
 
-def urlsplit(url):
-    """Returns (accout, container, object) from a location string"""
 
-    assert url.startswith('pithos://'), "Invalid URL"
-    t = url.split('/', 4)
-    assert len(t) == 5, "Invalid URL"
-    return t[2:5]
+def parse_url(url):
+    if url.startswith('pithos://'):
+        t = url.split('/', 4)
+        assert len(t) == 5, "Invalid URL"
+        return LocationURL(*t[2:5])
+    elif url.startswith('pithosmap://'):
+        t = url.split('/', 3)
+        assert len(t) == 4, "Invalid URL"
+        return HashmapURL(*t[2:4])
+    else:
+        raise Exception("Invalid URL")
 
 
 def print_size(backend, url):
     """Writes object's size to stdout."""
-
-    account, container, object = urlsplit(url)
-    meta = backend.get_object_meta(account, account, container, object, None)
-    print meta['bytes']
+    url = parse_url(url)
+    if type(url) is LocationURL:
+        account, container, object = url
+        meta = backend.get_object_meta(account, account, container, object,
+                                       None)
+        print meta['bytes']
+    elif type(url) is HashmapURL:
+        print url.size
+    else:
+        raise Exception("Invalid URL")
 
 
 def print_data(backend, url):
     """Writes object's size to stdout."""
 
-    account, container, object = urlsplit(url)
-    size, hashmap = backend.get_object_hashmap(account, account, container,
-            object)
+    url = parse_url(url)
+    if type(url) is LocationURL:
+        account, container, object = url
+        size, hashmap = backend.get_object_hashmap(account, account, container,
+                object)
+    elif type(url) is HashmapURL:
+        hashmap = [hexlify(x) \
+                   for x in backend.store.map_get(unhexlify(url.hash))]
+        size = int(url.size)
+    else:
+        raise Exception("Invalid URL")
+
     for hash in hashmap:
         block = backend.get_block(hash)
         if len(block) > size: