Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / hashfiler / blocker.py @ c30635bf

History | View | Annotate | Download (4.6 kB)

1
# Copyright 2011-2012 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
from hashlib import new as newhasher
35
from binascii import hexlify
36

    
37
from radosblocker import RadosBlocker
38
from fileblocker import FileBlocker
39

    
40
def intersect(a, b):
41
    """ return the intersection of two lists """
42
    return list(set(a) & set(b))
43

    
44
def union(a, b):
45
    """ return the union of two lists """
46
    return list(set(a) | set(b))
47

    
48

    
49
class Blocker(object):
50
    """Blocker.
51
       Required constructor parameters: blocksize, blockpath, hashtype,
52
       blockpool.
53
    """
54

    
55
    def __init__(self, **params):
56
        params['blockpool'] = 'blocks'
57
        self.rblocker = RadosBlocker(**params)
58
        self.fblocker = FileBlocker(**params)
59
        self.hashlen = self.rblocker.hashlen
60

    
61
#    def _get_rear_block(self, blkhash, create=0):
62
#        return self.rblocker._get_rear_block(blkhash, create)
63

    
64
#    def _check_rear_block(self, blkhash):
65
#        return self.rblocker._check_rear_block(blkhash)
66
#        return self.rblocker._check_rear_block(blkhash) and
67
#                self.fblocker._check_rear_block(blkhash)
68

    
69
    def block_hash(self, data):
70
        """Hash a block of data"""
71
        return self.rblocker.block_hash(data)
72

    
73
    def block_ping(self, hashes):
74
        """Check hashes for existence and
75
           return those missing from block storage.
76
        """
77
#        return self.rblocker.block_ping(hashes)
78
        r = self.rblocker.block_ping(hashes)
79
        f = self.fblocker.block_ping(hashes)
80
        return union(r, f)
81

    
82
    def block_retr(self, hashes):
83
        """Retrieve blocks from storage by their hashes."""
84
        return self.fblocker.block_retr(hashes)
85

    
86
    def block_stor(self, blocklist):
87
        """Store a bunch of blocks and return (hashes, missing).
88
           Hashes is a list of the hashes of the blocks,
89
           missing is a list of indices in that list indicating
90
           which blocks were missing from the store.
91
        """
92
#        return self.rblocker.block_stor(blocklist)
93
        (hashes, r_missing) = self.rblocker.block_stor(blocklist)
94
        (_, f_missing) = self.fblocker.block_stor(blocklist)
95
        return (hashes, union(r_missing, f_missing))
96

    
97

    
98
    def block_delta(self, blkhash, offset, data):
99
        """Construct and store a new block from a given block
100
           and a data 'patch' applied at offset. Return:
101
           (the hash of the new block, if the block already existed)
102
        """
103
#        return self.rblocker.block_delta(blkhash, offset, data)
104

    
105
        (f_hash, f_existed) = self.fblocker.block_delta(blkhash, offset, data)
106
        (r_hash, r_existed) = self.rblocker.block_delta(blkhash, offset, data)
107
        if not r_hash and not f_hash:
108
            return None, None
109
        if not r_hash:
110
            block = self.fblocker.block_retr((blkhash,))
111
            if not block:
112
                return None, None
113
            block = block[0]
114
            newblock = block[:offset] + data
115
            if len(newblock) > blocksize:
116
                newblock = newblock[:blocksize]
117
            elif len(newblock) < blocksize:
118
                newblock += block[len(newblock):]
119
            r_hash, r_existed = self.rblocker.block_stor((newblock,))
120

    
121
        return f_hash, 1 if r_existed and f_existed else 0