Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / hashfiler / store.py @ fda50d75

History | View | Annotate | Download (3 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 2715ade4 Sofia Papagiannaki
#
3 7ca7bb08 Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 7ca7bb08 Antony Chazapis
# without modification, are permitted provided that the following
5 7ca7bb08 Antony Chazapis
# conditions are met:
6 2715ade4 Sofia Papagiannaki
#
7 7ca7bb08 Antony Chazapis
#   1. Redistributions of source code must retain the above
8 7ca7bb08 Antony Chazapis
#      copyright notice, this list of conditions and the following
9 7ca7bb08 Antony Chazapis
#      disclaimer.
10 2715ade4 Sofia Papagiannaki
#
11 7ca7bb08 Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 7ca7bb08 Antony Chazapis
#      copyright notice, this list of conditions and the following
13 7ca7bb08 Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 7ca7bb08 Antony Chazapis
#      provided with the distribution.
15 2715ade4 Sofia Papagiannaki
#
16 7ca7bb08 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 7ca7bb08 Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 7ca7bb08 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 7ca7bb08 Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 7ca7bb08 Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 7ca7bb08 Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 7ca7bb08 Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 7ca7bb08 Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 7ca7bb08 Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 7ca7bb08 Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 7ca7bb08 Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 7ca7bb08 Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 2715ade4 Sofia Papagiannaki
#
29 7ca7bb08 Antony Chazapis
# The views and conclusions contained in the software and
30 7ca7bb08 Antony Chazapis
# documentation are those of the authors and should not be
31 7ca7bb08 Antony Chazapis
# interpreted as representing official policies, either expressed
32 7ca7bb08 Antony Chazapis
# or implied, of GRNET S.A.
33 7ca7bb08 Antony Chazapis
34 7ca7bb08 Antony Chazapis
import os
35 7ca7bb08 Antony Chazapis
36 7ca7bb08 Antony Chazapis
from blocker import Blocker
37 7ca7bb08 Antony Chazapis
from mapper import Mapper
38 7ca7bb08 Antony Chazapis
39 2715ade4 Sofia Papagiannaki
40 7ca7bb08 Antony Chazapis
class Store(object):
41 7ca7bb08 Antony Chazapis
    """Store.
42 47462eda Filippos Giannakos
       Required constructor parameters: path, block_size, hash_algorithm,
43 47462eda Filippos Giannakos
       umask, blockpool, mappool.
44 7ca7bb08 Antony Chazapis
    """
45 c30635bf Filippos Giannakos
46 7ca7bb08 Antony Chazapis
    def __init__(self, **params):
47 f3b65e8f Antony Chazapis
        umask = params['umask']
48 f3b65e8f Antony Chazapis
        if umask is not None:
49 f3b65e8f Antony Chazapis
            os.umask(umask)
50 c30635bf Filippos Giannakos
51 fda50d75 Chrysostomos Nanakos
        pb = {'blocksize': params['block_size'],
52 47462eda Filippos Giannakos
             'hashtype': params['hash_algorithm'],
53 fda50d75 Chrysostomos Nanakos
            }
54 fda50d75 Chrysostomos Nanakos
        self.blocker = Blocker(**pb)
55 fda50d75 Chrysostomos Nanakos
        pm = {'namelen': self.blocker.hashlen,
56 fda50d75 Chrysostomos Nanakos
            }
57 fda50d75 Chrysostomos Nanakos
        self.mapper = Mapper(**pm)
58 c30635bf Filippos Giannakos
59 04230536 Antony Chazapis
    def map_get(self, name):
60 04230536 Antony Chazapis
        return self.mapper.map_retr(name)
61 c30635bf Filippos Giannakos
62 fda50d75 Chrysostomos Nanakos
    def map_get_archipelago(self, name, size):
63 fda50d75 Chrysostomos Nanakos
        return self.mapper.map_retr_archipelago(name, size)
64 fda50d75 Chrysostomos Nanakos
65 04230536 Antony Chazapis
    def map_put(self, name, map):
66 04230536 Antony Chazapis
        self.mapper.map_stor(name, map)
67 c30635bf Filippos Giannakos
68 04230536 Antony Chazapis
    def map_delete(self, name):
69 04230536 Antony Chazapis
        pass
70 c30635bf Filippos Giannakos
71 e703fb78 Antony Chazapis
    def block_get(self, hash):
72 7ca7bb08 Antony Chazapis
        blocks = self.blocker.block_retr((hash,))
73 7ca7bb08 Antony Chazapis
        if not blocks:
74 7ca7bb08 Antony Chazapis
            return None
75 7ca7bb08 Antony Chazapis
        return blocks[0]
76 c30635bf Filippos Giannakos
77 fda50d75 Chrysostomos Nanakos
    def block_get_archipelago(self, hash):
78 fda50d75 Chrysostomos Nanakos
        blocks = self.blocker.block_retr_archipelago((hash,))
79 fda50d75 Chrysostomos Nanakos
        if not blocks:
80 fda50d75 Chrysostomos Nanakos
            return None
81 fda50d75 Chrysostomos Nanakos
        return blocks[0]
82 fda50d75 Chrysostomos Nanakos
83 e703fb78 Antony Chazapis
    def block_put(self, data):
84 7ca7bb08 Antony Chazapis
        hashes, absent = self.blocker.block_stor((data,))
85 7ca7bb08 Antony Chazapis
        return hashes[0]
86 c30635bf Filippos Giannakos
87 e703fb78 Antony Chazapis
    def block_update(self, hash, offset, data):
88 c9865fe1 Antony Chazapis
        h, e = self.blocker.block_delta(hash, offset, data)
89 7ca7bb08 Antony Chazapis
        return h
90 c30635bf Filippos Giannakos
91 e703fb78 Antony Chazapis
    def block_search(self, map):
92 7ca7bb08 Antony Chazapis
        return self.blocker.block_ping(map)