root / snf-pithos-backend / pithos / backends / lib / hashfiler / blocker.py @ c30635bf
History | View | Annotate | Download (4.6 kB)
1 | 2e662088 | Antony Chazapis | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | a9b3f29d | Antony Chazapis | #
|
3 | a9b3f29d | Antony Chazapis | # Redistribution and use in source and binary forms, with or
|
4 | a9b3f29d | Antony Chazapis | # without modification, are permitted provided that the following
|
5 | a9b3f29d | Antony Chazapis | # conditions are met:
|
6 | a9b3f29d | Antony Chazapis | #
|
7 | a9b3f29d | Antony Chazapis | # 1. Redistributions of source code must retain the above
|
8 | a9b3f29d | Antony Chazapis | # copyright notice, this list of conditions and the following
|
9 | a9b3f29d | Antony Chazapis | # disclaimer.
|
10 | a9b3f29d | Antony Chazapis | #
|
11 | a9b3f29d | Antony Chazapis | # 2. Redistributions in binary form must reproduce the above
|
12 | a9b3f29d | Antony Chazapis | # copyright notice, this list of conditions and the following
|
13 | a9b3f29d | Antony Chazapis | # disclaimer in the documentation and/or other materials
|
14 | a9b3f29d | Antony Chazapis | # provided with the distribution.
|
15 | a9b3f29d | Antony Chazapis | #
|
16 | a9b3f29d | Antony Chazapis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | a9b3f29d | Antony Chazapis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | a9b3f29d | Antony Chazapis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | a9b3f29d | Antony Chazapis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | a9b3f29d | Antony Chazapis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | a9b3f29d | Antony Chazapis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | a9b3f29d | Antony Chazapis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | a9b3f29d | Antony Chazapis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | a9b3f29d | Antony Chazapis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | a9b3f29d | Antony Chazapis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | a9b3f29d | Antony Chazapis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | a9b3f29d | Antony Chazapis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | a9b3f29d | Antony Chazapis | #
|
29 | a9b3f29d | Antony Chazapis | # The views and conclusions contained in the software and
|
30 | a9b3f29d | Antony Chazapis | # documentation are those of the authors and should not be
|
31 | a9b3f29d | Antony Chazapis | # interpreted as representing official policies, either expressed
|
32 | a9b3f29d | Antony Chazapis | # or implied, of GRNET S.A.
|
33 | a9b3f29d | Antony Chazapis | |
34 | a9b3f29d | Antony Chazapis | from hashlib import new as newhasher |
35 | a9b3f29d | Antony Chazapis | from binascii import hexlify |
36 | a9b3f29d | Antony Chazapis | |
37 | c30635bf | Filippos Giannakos | from radosblocker import RadosBlocker |
38 | c30635bf | Filippos Giannakos | from fileblocker import FileBlocker |
39 | c30635bf | Filippos Giannakos | |
40 | c30635bf | Filippos Giannakos | def intersect(a, b): |
41 | c30635bf | Filippos Giannakos | """ return the intersection of two lists """
|
42 | c30635bf | Filippos Giannakos | return list(set(a) & set(b)) |
43 | c30635bf | Filippos Giannakos | |
44 | c30635bf | Filippos Giannakos | def union(a, b): |
45 | c30635bf | Filippos Giannakos | """ return the union of two lists """
|
46 | c30635bf | Filippos Giannakos | return list(set(a) | set(b)) |
47 | a9b3f29d | Antony Chazapis | |
48 | a9b3f29d | Antony Chazapis | |
49 | a9b3f29d | Antony Chazapis | class Blocker(object): |
50 | a9b3f29d | Antony Chazapis | """Blocker.
|
51 | c30635bf | Filippos Giannakos | Required constructor parameters: blocksize, blockpath, hashtype,
|
52 | c30635bf | Filippos Giannakos | blockpool.
|
53 | a9b3f29d | Antony Chazapis | """
|
54 | a9b3f29d | Antony Chazapis | |
55 | a9b3f29d | Antony Chazapis | def __init__(self, **params): |
56 | c30635bf | Filippos Giannakos | params['blockpool'] = 'blocks' |
57 | c30635bf | Filippos Giannakos | self.rblocker = RadosBlocker(**params)
|
58 | c30635bf | Filippos Giannakos | self.fblocker = FileBlocker(**params)
|
59 | c30635bf | Filippos Giannakos | self.hashlen = self.rblocker.hashlen |
60 | c30635bf | Filippos Giannakos | |
61 | c30635bf | Filippos Giannakos | # def _get_rear_block(self, blkhash, create=0):
|
62 | c30635bf | Filippos Giannakos | # return self.rblocker._get_rear_block(blkhash, create)
|
63 | c30635bf | Filippos Giannakos | |
64 | c30635bf | Filippos Giannakos | # def _check_rear_block(self, blkhash):
|
65 | c30635bf | Filippos Giannakos | # return self.rblocker._check_rear_block(blkhash)
|
66 | c30635bf | Filippos Giannakos | # return self.rblocker._check_rear_block(blkhash) and
|
67 | c30635bf | Filippos Giannakos | # self.fblocker._check_rear_block(blkhash)
|
68 | a9b3f29d | Antony Chazapis | |
69 | a9b3f29d | Antony Chazapis | def block_hash(self, data): |
70 | a9b3f29d | Antony Chazapis | """Hash a block of data"""
|
71 | c30635bf | Filippos Giannakos | return self.rblocker.block_hash(data) |
72 | a9b3f29d | Antony Chazapis | |
73 | a9b3f29d | Antony Chazapis | def block_ping(self, hashes): |
74 | a9b3f29d | Antony Chazapis | """Check hashes for existence and
|
75 | a9b3f29d | Antony Chazapis | return those missing from block storage.
|
76 | a9b3f29d | Antony Chazapis | """
|
77 | c30635bf | Filippos Giannakos | # return self.rblocker.block_ping(hashes)
|
78 | c30635bf | Filippos Giannakos | r = self.rblocker.block_ping(hashes)
|
79 | c30635bf | Filippos Giannakos | f = self.fblocker.block_ping(hashes)
|
80 | c30635bf | Filippos Giannakos | return union(r, f)
|
81 | a9b3f29d | Antony Chazapis | |
82 | a9b3f29d | Antony Chazapis | def block_retr(self, hashes): |
83 | a9b3f29d | Antony Chazapis | """Retrieve blocks from storage by their hashes."""
|
84 | c30635bf | Filippos Giannakos | return self.fblocker.block_retr(hashes) |
85 | a9b3f29d | Antony Chazapis | |
86 | a9b3f29d | Antony Chazapis | def block_stor(self, blocklist): |
87 | a9b3f29d | Antony Chazapis | """Store a bunch of blocks and return (hashes, missing).
|
88 | a9b3f29d | Antony Chazapis | Hashes is a list of the hashes of the blocks,
|
89 | a9b3f29d | Antony Chazapis | missing is a list of indices in that list indicating
|
90 | a9b3f29d | Antony Chazapis | which blocks were missing from the store.
|
91 | a9b3f29d | Antony Chazapis | """
|
92 | c30635bf | Filippos Giannakos | # return self.rblocker.block_stor(blocklist)
|
93 | c30635bf | Filippos Giannakos | (hashes, r_missing) = self.rblocker.block_stor(blocklist)
|
94 | c30635bf | Filippos Giannakos | (_, f_missing) = self.fblocker.block_stor(blocklist)
|
95 | c30635bf | Filippos Giannakos | return (hashes, union(r_missing, f_missing))
|
96 | a9b3f29d | Antony Chazapis | |
97 | a9b3f29d | Antony Chazapis | |
98 | c9865fe1 | Antony Chazapis | def block_delta(self, blkhash, offset, data): |
99 | a9b3f29d | Antony Chazapis | """Construct and store a new block from a given block
|
100 | c9865fe1 | Antony Chazapis | and a data 'patch' applied at offset. Return:
|
101 | a9b3f29d | Antony Chazapis | (the hash of the new block, if the block already existed)
|
102 | a9b3f29d | Antony Chazapis | """
|
103 | c30635bf | Filippos Giannakos | # return self.rblocker.block_delta(blkhash, offset, data)
|
104 | a9b3f29d | Antony Chazapis | |
105 | c30635bf | Filippos Giannakos | (f_hash, f_existed) = self.fblocker.block_delta(blkhash, offset, data)
|
106 | c30635bf | Filippos Giannakos | (r_hash, r_existed) = self.rblocker.block_delta(blkhash, offset, data)
|
107 | c30635bf | Filippos Giannakos | if not r_hash and not f_hash: |
108 | c9865fe1 | Antony Chazapis | return None, None |
109 | c30635bf | Filippos Giannakos | if not r_hash: |
110 | c30635bf | Filippos Giannakos | block = self.fblocker.block_retr((blkhash,))
|
111 | c30635bf | Filippos Giannakos | if not block: |
112 | c30635bf | Filippos Giannakos | return None, None |
113 | c30635bf | Filippos Giannakos | block = block[0]
|
114 | c30635bf | Filippos Giannakos | newblock = block[:offset] + data |
115 | c30635bf | Filippos Giannakos | if len(newblock) > blocksize: |
116 | c30635bf | Filippos Giannakos | newblock = newblock[:blocksize] |
117 | c30635bf | Filippos Giannakos | elif len(newblock) < blocksize: |
118 | c30635bf | Filippos Giannakos | newblock += block[len(newblock):]
|
119 | c30635bf | Filippos Giannakos | r_hash, r_existed = self.rblocker.block_stor((newblock,))
|
120 | c30635bf | Filippos Giannakos | |
121 | c30635bf | Filippos Giannakos | return f_hash, 1 if r_existed and f_existed else 0 |