root / snf-pithos-backend / pithos / backends / lib / hashfiler / blocker.py @ b5636704
History | View | Annotate | Download (3.9 kB)
1 | 2e662088 | Antony Chazapis | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 2715ade4 | Sofia Papagiannaki | #
|
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 | 2715ade4 | Sofia Papagiannaki | #
|
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 | 2715ade4 | Sofia Papagiannaki | #
|
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 | 2715ade4 | Sofia Papagiannaki | #
|
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 | 2715ade4 | Sofia Papagiannaki | #
|
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 | fda50d75 | Chrysostomos Nanakos | from archipelagoblocker import ArchipelagoBlocker |
35 | a9b3f29d | Antony Chazapis | |
36 | a9b3f29d | Antony Chazapis | |
37 | a9b3f29d | Antony Chazapis | class Blocker(object): |
38 | a9b3f29d | Antony Chazapis | """Blocker.
|
39 | 47462eda | Filippos Giannakos | Required constructor parameters: blocksize, blockpath, hashtype.
|
40 | 47462eda | Filippos Giannakos | Optional blockpool.
|
41 | a9b3f29d | Antony Chazapis | """
|
42 | a9b3f29d | Antony Chazapis | |
43 | a9b3f29d | Antony Chazapis | def __init__(self, **params): |
44 | fda50d75 | Chrysostomos Nanakos | self.archip_blocker = ArchipelagoBlocker(**params)
|
45 | fda50d75 | Chrysostomos Nanakos | self.hashlen = self.archip_blocker.hashlen |
46 | 57ccbdbf | Filippos Giannakos | self.blocksize = params['blocksize'] |
47 | a9b3f29d | Antony Chazapis | |
48 | a9b3f29d | Antony Chazapis | def block_hash(self, data): |
49 | a9b3f29d | Antony Chazapis | """Hash a block of data"""
|
50 | fda50d75 | Chrysostomos Nanakos | return self.archip_blocker.block_hash(data) |
51 | a9b3f29d | Antony Chazapis | |
52 | a9b3f29d | Antony Chazapis | def block_ping(self, hashes): |
53 | a9b3f29d | Antony Chazapis | """Check hashes for existence and
|
54 | a9b3f29d | Antony Chazapis | return those missing from block storage.
|
55 | a9b3f29d | Antony Chazapis | """
|
56 | fda50d75 | Chrysostomos Nanakos | return self.archip_blocker.block_ping(hashes) |
57 | a9b3f29d | Antony Chazapis | |
58 | a9b3f29d | Antony Chazapis | def block_retr(self, hashes): |
59 | a9b3f29d | Antony Chazapis | """Retrieve blocks from storage by their hashes."""
|
60 | fda50d75 | Chrysostomos Nanakos | return self.archip_blocker.block_retr(hashes) |
61 | fda50d75 | Chrysostomos Nanakos | |
62 | fda50d75 | Chrysostomos Nanakos | def block_retr_archipelago(self, hashes): |
63 | fda50d75 | Chrysostomos Nanakos | """Retrieve blocks from storage by theri hashes."""
|
64 | fda50d75 | Chrysostomos Nanakos | return self.archip_blocker.block_retr_archipelago(hashes) |
65 | a9b3f29d | Antony Chazapis | |
66 | a9b3f29d | Antony Chazapis | def block_stor(self, blocklist): |
67 | a9b3f29d | Antony Chazapis | """Store a bunch of blocks and return (hashes, missing).
|
68 | a9b3f29d | Antony Chazapis | Hashes is a list of the hashes of the blocks,
|
69 | a9b3f29d | Antony Chazapis | missing is a list of indices in that list indicating
|
70 | a9b3f29d | Antony Chazapis | which blocks were missing from the store.
|
71 | a9b3f29d | Antony Chazapis | """
|
72 | fda50d75 | Chrysostomos Nanakos | (hashes, missing) = self.archip_blocker.block_stor(blocklist)
|
73 | fda50d75 | Chrysostomos Nanakos | return (hashes, missing)
|
74 | a9b3f29d | Antony Chazapis | |
75 | c9865fe1 | Antony Chazapis | def block_delta(self, blkhash, offset, data): |
76 | a9b3f29d | Antony Chazapis | """Construct and store a new block from a given block
|
77 | c9865fe1 | Antony Chazapis | and a data 'patch' applied at offset. Return:
|
78 | a9b3f29d | Antony Chazapis | (the hash of the new block, if the block already existed)
|
79 | a9b3f29d | Antony Chazapis | """
|
80 | 57ccbdbf | Filippos Giannakos | blocksize = self.blocksize
|
81 | fda50d75 | Chrysostomos Nanakos | archip_hash = None
|
82 | fda50d75 | Chrysostomos Nanakos | archip_existed = True
|
83 | fda50d75 | Chrysostomos Nanakos | (archip_hash, archip_existed) = \ |
84 | b5636704 | Chrysostomos Nanakos | self.archip_blocker.block_delta(blkhash, offset, data)
|
85 | fda50d75 | Chrysostomos Nanakos | |
86 | fda50d75 | Chrysostomos Nanakos | if not archip_hash: |
87 | c9865fe1 | Antony Chazapis | return None, None |
88 | fda50d75 | Chrysostomos Nanakos | |
89 | fda50d75 | Chrysostomos Nanakos | if self.archip_blocker and not archip_hash: |
90 | fda50d75 | Chrysostomos Nanakos | block = self.archip_blocker.block_retr((blkhash,))
|
91 | c30635bf | Filippos Giannakos | if not block: |
92 | c30635bf | Filippos Giannakos | return None, None |
93 | c30635bf | Filippos Giannakos | block = block[0]
|
94 | c30635bf | Filippos Giannakos | newblock = block[:offset] + data |
95 | c30635bf | Filippos Giannakos | if len(newblock) > blocksize: |
96 | c30635bf | Filippos Giannakos | newblock = newblock[:blocksize] |
97 | c30635bf | Filippos Giannakos | elif len(newblock) < blocksize: |
98 | c30635bf | Filippos Giannakos | newblock += block[len(newblock):]
|
99 | fda50d75 | Chrysostomos Nanakos | archip_hash, archip_existed = self.rblocker.block_stor((newblock,))
|
100 | c30635bf | Filippos Giannakos | |
101 | fda50d75 | Chrysostomos Nanakos | return archip_hash, 1 if archip_existed else 0 |