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