Revision 53129af9
b/kamaki/clients/pithos.py | ||
---|---|---|
41 | 41 |
class PithosClient(StorageClient): |
42 | 42 |
"""GRNet Pithos API client""" |
43 | 43 |
|
44 |
def put_block(self, data): |
|
44 |
def put_block(self, data, hash):
|
|
45 | 45 |
path = '/%s/%s?update' % (self.account, self.container) |
46 |
headers = {'Content-Type': 'application/octet-stream'} |
|
47 |
self.raw_http_cmd('POST', path, data, headers, success=202) |
|
46 |
headers = {'Content-Type': 'application/octet-stream', |
|
47 |
'Content-Length': len(data)} |
|
48 |
resp, reply = self.raw_http_cmd('POST', path, data, headers, |
|
49 |
success=202) |
|
50 |
assert reply.strip() == hash, 'Local hash does not match server' |
|
48 | 51 |
|
49 | 52 |
def create_object(self, object, f): |
50 | 53 |
meta = self.get_container_meta() |
... | ... | |
52 | 55 |
blockhash = meta['block-hash'] |
53 | 56 |
|
54 | 57 |
size = 0 |
55 |
hashes = []
|
|
58 |
hashes = {}
|
|
56 | 59 |
data = f.read(blocksize) |
57 | 60 |
while data: |
58 |
size += len(data)
|
|
61 |
bytes = len(data)
|
|
59 | 62 |
h = hashlib.new(blockhash) |
60 |
h.update(data) |
|
61 |
hashes.append(h.hexdigest()) |
|
63 |
h.update(data.rstrip('\x00')) |
|
64 |
hash = h.hexdigest() |
|
65 |
hashes[hash] = (size, bytes) |
|
66 |
size += bytes |
|
62 | 67 |
data = f.read(blocksize) |
63 | 68 |
|
64 | 69 |
path = '/%s/%s/%s?hashmap&format=json' % (self.account, self.container, |
65 |
object) |
|
70 |
object)
|
|
66 | 71 |
hashmap = dict(bytes=size, hashes=hashes) |
67 | 72 |
req = json.dumps(hashmap) |
68 | 73 |
resp, reply = self.raw_http_cmd('PUT', path, req, success=None) |
... | ... | |
73 | 78 |
if resp.status == 201: |
74 | 79 |
return |
75 | 80 |
|
76 |
hashes = set(json.loads(reply))
|
|
81 |
missing = json.loads(reply)
|
|
77 | 82 |
|
78 |
f.seek(0) |
|
79 |
data = f.read(blocksize) |
|
80 |
while data: |
|
81 |
h = hashlib.new(blockhash) |
|
82 |
h.update(data) |
|
83 |
hash = h.hexdigest() |
|
84 |
if hash in hashes: |
|
85 |
self.put_block(data) |
|
86 |
hashes.remove(hash) |
|
87 |
data = f.read(blocksize) |
|
83 |
for hash in missing: |
|
84 |
offset, bytes = hashes[hash] |
|
85 |
f.seek(offset) |
|
86 |
data = f.read(bytes) |
|
87 |
self.put_block(data, hash) |
|
88 | 88 |
|
89 |
self.http_put(path, req, success=201) |
|
89 |
self.http_put(path, req, success=201) |
Also available in: Unified diff