Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.9 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 7ca7bb08 Antony Chazapis
# 
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 7ca7bb08 Antony Chazapis
# 
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 7ca7bb08 Antony Chazapis
# 
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 7ca7bb08 Antony Chazapis
# 
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 7ca7bb08 Antony Chazapis
# 
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 7ca7bb08 Antony Chazapis
class Store(object):
40 7ca7bb08 Antony Chazapis
    """Store.
41 94f33595 Antony Chazapis
       Required constructor parameters: path, block_size, hash_algorithm.
42 7ca7bb08 Antony Chazapis
    """
43 7ca7bb08 Antony Chazapis
    
44 7ca7bb08 Antony Chazapis
    def __init__(self, **params):
45 7ca7bb08 Antony Chazapis
        path = params['path']
46 7ca7bb08 Antony Chazapis
        if path and not os.path.exists(path):
47 7ca7bb08 Antony Chazapis
            os.makedirs(path)
48 7ca7bb08 Antony Chazapis
        if not os.path.isdir(path):
49 7ca7bb08 Antony Chazapis
            raise RuntimeError("Cannot open path '%s'" % (path,))
50 7ca7bb08 Antony Chazapis
        
51 7ca7bb08 Antony Chazapis
        p = {'blocksize': params['block_size'],
52 7ca7bb08 Antony Chazapis
             'blockpath': os.path.join(path + '/blocks'),
53 7ca7bb08 Antony Chazapis
             'hashtype': params['hash_algorithm']}
54 7ca7bb08 Antony Chazapis
        self.blocker = Blocker(**p)
55 7ca7bb08 Antony Chazapis
        p = {'mappath': os.path.join(path + '/maps'),
56 7ca7bb08 Antony Chazapis
             'namelen': self.blocker.hashlen}
57 7ca7bb08 Antony Chazapis
        self.mapper = Mapper(**p)
58 7ca7bb08 Antony Chazapis
    
59 04230536 Antony Chazapis
    def map_get(self, name):
60 04230536 Antony Chazapis
        return self.mapper.map_retr(name)
61 7ca7bb08 Antony Chazapis
    
62 04230536 Antony Chazapis
    def map_put(self, name, map):
63 04230536 Antony Chazapis
        self.mapper.map_stor(name, map)
64 04230536 Antony Chazapis
    
65 04230536 Antony Chazapis
    def map_delete(self, name):
66 04230536 Antony Chazapis
        pass
67 7ca7bb08 Antony Chazapis
    
68 e703fb78 Antony Chazapis
    def block_get(self, hash):
69 7ca7bb08 Antony Chazapis
        blocks = self.blocker.block_retr((hash,))
70 7ca7bb08 Antony Chazapis
        if not blocks:
71 7ca7bb08 Antony Chazapis
            return None
72 7ca7bb08 Antony Chazapis
        return blocks[0]
73 7ca7bb08 Antony Chazapis
    
74 e703fb78 Antony Chazapis
    def block_put(self, data):
75 7ca7bb08 Antony Chazapis
        hashes, absent = self.blocker.block_stor((data,))
76 7ca7bb08 Antony Chazapis
        return hashes[0]
77 7ca7bb08 Antony Chazapis
    
78 e703fb78 Antony Chazapis
    def block_update(self, hash, offset, data):
79 c9865fe1 Antony Chazapis
        h, e = self.blocker.block_delta(hash, offset, data)
80 7ca7bb08 Antony Chazapis
        return h
81 7ca7bb08 Antony Chazapis
    
82 e703fb78 Antony Chazapis
    def block_search(self, map):
83 7ca7bb08 Antony Chazapis
        return self.blocker.block_ping(map)