root / pithos / backends / lib / hashfiler / mapper.py @ a9b3f29d
History | View | Annotate | Download (3.6 kB)
1 | a9b3f29d | Antony Chazapis | # Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | a9b3f29d | Antony Chazapis | #
|
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 | a9b3f29d | Antony Chazapis | #
|
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 | a9b3f29d | Antony Chazapis | #
|
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 | a9b3f29d | Antony Chazapis | #
|
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 | a9b3f29d | Antony Chazapis | #
|
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 | a9b3f29d | Antony Chazapis | from os.path import realpath, join, exists, isdir |
35 | a9b3f29d | Antony Chazapis | from os import makedirs, unlink |
36 | a9b3f29d | Antony Chazapis | from errno import ENOENT |
37 | a9b3f29d | Antony Chazapis | |
38 | a9b3f29d | Antony Chazapis | from pithos.lib.hashfiler.context_file import ContextFile |
39 | a9b3f29d | Antony Chazapis | |
40 | a9b3f29d | Antony Chazapis | |
41 | a9b3f29d | Antony Chazapis | class Mapper(object): |
42 | a9b3f29d | Antony Chazapis | """Mapper.
|
43 | a9b3f29d | Antony Chazapis | Required contstructor parameters: mappath, namelen.
|
44 | a9b3f29d | Antony Chazapis | """
|
45 | a9b3f29d | Antony Chazapis | |
46 | a9b3f29d | Antony Chazapis | mappath = None
|
47 | a9b3f29d | Antony Chazapis | namelen = None
|
48 | a9b3f29d | Antony Chazapis | |
49 | a9b3f29d | Antony Chazapis | def __init__(self, **params): |
50 | a9b3f29d | Antony Chazapis | self.params = params
|
51 | a9b3f29d | Antony Chazapis | self.namelen = params['namelen'] |
52 | a9b3f29d | Antony Chazapis | mappath = realpath(params['mappath'])
|
53 | a9b3f29d | Antony Chazapis | if not isdir(mappath): |
54 | a9b3f29d | Antony Chazapis | if not exists(mappath): |
55 | a9b3f29d | Antony Chazapis | makedirs(mappath) |
56 | a9b3f29d | Antony Chazapis | else:
|
57 | a9b3f29d | Antony Chazapis | raise ValueError("Variable mappath '%s' is not a directory" % (mappath,)) |
58 | a9b3f29d | Antony Chazapis | self.mappath = mappath
|
59 | a9b3f29d | Antony Chazapis | |
60 | a9b3f29d | Antony Chazapis | def get_rear_map(self, name, create=0): |
61 | a9b3f29d | Antony Chazapis | name = join(self.mappath, hex(int(name))) |
62 | a9b3f29d | Antony Chazapis | return ContextFile(name, create)
|
63 | a9b3f29d | Antony Chazapis | |
64 | a9b3f29d | Antony Chazapis | def delete_rear_map(self, name): |
65 | a9b3f29d | Antony Chazapis | name = join(self.mappath, hex(int(name))) |
66 | a9b3f29d | Antony Chazapis | try:
|
67 | a9b3f29d | Antony Chazapis | unlink(name) |
68 | a9b3f29d | Antony Chazapis | return 1 |
69 | a9b3f29d | Antony Chazapis | except OSError, e: |
70 | a9b3f29d | Antony Chazapis | if e.errno != ENOENT:
|
71 | a9b3f29d | Antony Chazapis | raise
|
72 | a9b3f29d | Antony Chazapis | return 0 |
73 | a9b3f29d | Antony Chazapis | |
74 | a9b3f29d | Antony Chazapis | def map_retr(self, name, blkoff=0, nr=100000000000000): |
75 | a9b3f29d | Antony Chazapis | """Return as a list, part of the hashes map of an object
|
76 | a9b3f29d | Antony Chazapis | at the given block offset.
|
77 | a9b3f29d | Antony Chazapis | By default, return the whole hashes map.
|
78 | a9b3f29d | Antony Chazapis | """
|
79 | a9b3f29d | Antony Chazapis | namelen = self.namelen
|
80 | a9b3f29d | Antony Chazapis | hashes = () |
81 | a9b3f29d | Antony Chazapis | |
82 | a9b3f29d | Antony Chazapis | with self.get_rear_map(name, 0) as rmap: |
83 | a9b3f29d | Antony Chazapis | if rmap:
|
84 | a9b3f29d | Antony Chazapis | hashes = list(rmap.sync_read_chunks(namelen, nr, blkoff))
|
85 | a9b3f29d | Antony Chazapis | return hashes
|
86 | a9b3f29d | Antony Chazapis | |
87 | a9b3f29d | Antony Chazapis | def map_stor(self, name, hashes=(), blkoff=0, create=1): |
88 | a9b3f29d | Antony Chazapis | """Store hashes in the given hashes map, replacing the old ones."""
|
89 | a9b3f29d | Antony Chazapis | namelen = self.namelen
|
90 | a9b3f29d | Antony Chazapis | with self.get_rear_map(name, 1) as rmap: |
91 | a9b3f29d | Antony Chazapis | rmap.sync_write_chunks(namelen, blkoff, hashes, None)
|
92 | a9b3f29d | Antony Chazapis | |
93 | a9b3f29d | Antony Chazapis | # def map_copy(self, src, dst):
|
94 | a9b3f29d | Antony Chazapis | # """Copy a hashes map to another one, replacing it."""
|
95 | a9b3f29d | Antony Chazapis | # with self.get_rear_map(src, 0) as rmap:
|
96 | a9b3f29d | Antony Chazapis | # if rmap:
|
97 | a9b3f29d | Antony Chazapis | # rmap.copy_to(dst)
|
98 | a9b3f29d | Antony Chazapis | |
99 | a9b3f29d | Antony Chazapis | def map_remv(self, name): |
100 | a9b3f29d | Antony Chazapis | """Remove a hashes map. Returns true if the map was found and removed."""
|
101 | a9b3f29d | Antony Chazapis | return self.delete_rear_map(name) |