Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / pithos.py @ 188f23b9

History | View | Annotate | Download (4.2 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import hashlib
35
import os
36

    
37
from ..utils import OrderedDict
38

    
39
from .storage import StorageClient
40

    
41

    
42
def pithos_hash(block, blockhash):
43
    h = hashlib.new(blockhash)
44
    h.update(block.rstrip('\x00'))
45
    return h.hexdigest()
46

    
47

    
48
class PithosClient(StorageClient):
49
    """GRNet Pithos API client"""
50
    
51
    def put_block(self, data, hash):
52
        path = '/%s/%s' % (self.account, self.container)
53
        params = {'update': ''}
54
        headers = {'Content-Type': 'application/octet-stream',
55
                   'Content-Length': str(len(data))}
56
        r = self.post(path, params=params, data=data, headers=headers,
57
                      success=202)
58
        assert r.text.strip() == hash, 'Local hash does not match server'
59
    
60
    def create_object(self, object, f, size=None, hash_cb=None,
61
                      upload_cb=None):
62
        """Create an object by uploading only the missing blocks
63
        
64
        hash_cb is a generator function taking the total number of blocks to
65
        be hashed as an argument. Its next() will be called every time a block
66
        is hashed.
67
        
68
        upload_cb is a generator function with the same properties that is
69
        called every time a block is uploaded.
70
        """
71
        self.assert_container()
72
        
73
        meta = self.get_container_meta(self.container)
74
        blocksize = int(meta['block-size'])
75
        blockhash = meta['block-hash']
76
        
77
        file_size = size if size is not None else os.fstat(f.fileno()).st_size
78
        nblocks = 1 + (file_size - 1) // blocksize
79
        hashes = OrderedDict()
80
        
81
        size = 0
82
        
83
        if hash_cb:
84
            hash_gen = hash_cb(nblocks)
85
            hash_gen.next()
86
        for i in range(nblocks):
87
            block = f.read(blocksize)
88
            bytes = len(block)
89
            hash = pithos_hash(block, blockhash)
90
            hashes[hash] = (size, bytes)
91
            size += bytes
92
            if hash_cb:
93
                hash_gen.next()
94
        
95
        assert size == file_size
96
                
97
        path = '/%s/%s/%s' % (self.account, self.container, object)
98
        params = dict(format='json', hashmap='')
99
        hashmap = dict(bytes=size, hashes=hashes.keys())
100
        r = self.put(path, params=params, json=hashmap, success=(201, 409))
101
        
102
        if r.status_code == 201:
103
            return
104
        
105
        missing = r.json
106
        
107
        if upload_cb:
108
            upload_gen = upload_cb(len(missing))
109
            upload_gen.next()
110
        for hash in missing:
111
            offset, bytes = hashes[hash]
112
            f.seek(offset)
113
            data = f.read(bytes)
114
            self.put_block(data, hash)
115
            if upload_cb:
116
                upload_gen.next()
117
        
118
        self.put(path, params=params, json=hashmap, success=201)