root / snf-pithos-backend / pithos / backends / lib / hashfiler / store.py @ 4a7b190f
History | View | Annotate | Download (3.1 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 | 7ca7bb08 | Antony Chazapis | path = params['path']
|
52 | 7ca7bb08 | Antony Chazapis | if path and not os.path.exists(path): |
53 | 7ca7bb08 | Antony Chazapis | os.makedirs(path) |
54 | 7ca7bb08 | Antony Chazapis | if not os.path.isdir(path): |
55 | 7ca7bb08 | Antony Chazapis | raise RuntimeError("Cannot open path '%s'" % (path,)) |
56 | c30635bf | Filippos Giannakos | |
57 | 7ca7bb08 | Antony Chazapis | p = {'blocksize': params['block_size'], |
58 | 7ca7bb08 | Antony Chazapis | 'blockpath': os.path.join(path + '/blocks'), |
59 | 47462eda | Filippos Giannakos | 'hashtype': params['hash_algorithm'], |
60 | df8debbb | Filippos Giannakos | 'blockpool': params['blockpool']} |
61 | 7ca7bb08 | Antony Chazapis | self.blocker = Blocker(**p)
|
62 | 7ca7bb08 | Antony Chazapis | p = {'mappath': os.path.join(path + '/maps'), |
63 | 47462eda | Filippos Giannakos | 'namelen': self.blocker.hashlen, |
64 | df8debbb | Filippos Giannakos | 'mappool': params['mappool']} |
65 | 7ca7bb08 | Antony Chazapis | self.mapper = Mapper(**p)
|
66 | c30635bf | Filippos Giannakos | |
67 | 04230536 | Antony Chazapis | def map_get(self, name): |
68 | 04230536 | Antony Chazapis | return self.mapper.map_retr(name) |
69 | c30635bf | Filippos Giannakos | |
70 | 04230536 | Antony Chazapis | def map_put(self, name, map): |
71 | 04230536 | Antony Chazapis | self.mapper.map_stor(name, map) |
72 | c30635bf | Filippos Giannakos | |
73 | 04230536 | Antony Chazapis | def map_delete(self, name): |
74 | 04230536 | Antony Chazapis | pass
|
75 | c30635bf | Filippos Giannakos | |
76 | e703fb78 | Antony Chazapis | def block_get(self, hash): |
77 | 7ca7bb08 | Antony Chazapis | blocks = self.blocker.block_retr((hash,)) |
78 | 7ca7bb08 | Antony Chazapis | if not blocks: |
79 | 7ca7bb08 | Antony Chazapis | return None |
80 | 7ca7bb08 | Antony Chazapis | return blocks[0] |
81 | c30635bf | Filippos Giannakos | |
82 | e703fb78 | Antony Chazapis | def block_put(self, data): |
83 | 7ca7bb08 | Antony Chazapis | hashes, absent = self.blocker.block_stor((data,))
|
84 | 7ca7bb08 | Antony Chazapis | return hashes[0] |
85 | c30635bf | Filippos Giannakos | |
86 | e703fb78 | Antony Chazapis | def block_update(self, hash, offset, data): |
87 | c9865fe1 | Antony Chazapis | h, e = self.blocker.block_delta(hash, offset, data) |
88 | 7ca7bb08 | Antony Chazapis | return h
|
89 | c30635bf | Filippos Giannakos | |
90 | e703fb78 | Antony Chazapis | def block_search(self, map): |
91 | 7ca7bb08 | Antony Chazapis | return self.blocker.block_ping(map) |