Revision c30635bf snf-pithos-backend/pithos/backends/lib/hashfiler/mapper.py
b/snf-pithos-backend/pithos/backends/lib/hashfiler/mapper.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
from os import makedirs, unlink |
|
35 |
from os.path import isdir, realpath, exists, join |
|
36 | 34 |
from binascii import hexlify |
37 | 35 |
|
38 |
from context_file import ContextFile
|
|
39 |
|
|
36 |
from radosmapper import RadosMapper
|
|
37 |
from filemapper import FileMapper |
|
40 | 38 |
|
41 | 39 |
class Mapper(object): |
42 | 40 |
"""Mapper. |
43 |
Required constructor parameters: mappath, namelen. |
|
41 |
Required constructor parameters: mappath, namelen, mappool.
|
|
44 | 42 |
""" |
45 |
|
|
46 |
mappath = None |
|
47 |
namelen = None |
|
48 | 43 |
|
49 | 44 |
def __init__(self, **params): |
50 |
self.params = params |
|
51 |
self.namelen = params['namelen'] |
|
52 |
mappath = realpath(params['mappath']) |
|
53 |
if not isdir(mappath): |
|
54 |
if not exists(mappath): |
|
55 |
makedirs(mappath) |
|
56 |
else: |
|
57 |
raise ValueError("Variable mappath '%s' is not a directory" % (mappath,)) |
|
58 |
self.mappath = mappath |
|
45 |
params['mappool'] = 'maps' |
|
46 |
self.rmap = RadosMapper(**params) |
|
47 |
self.fmap = FileMapper(**params) |
|
59 | 48 |
|
60 |
def _get_rear_map(self, maphash, create=0): |
|
61 |
filename = hexlify(maphash) |
|
62 |
dir = join(self.mappath, filename[0:2], filename[2:4], filename[4:6]) |
|
63 |
if not exists(dir): |
|
64 |
makedirs(dir) |
|
65 |
name = join(dir, filename) |
|
66 |
return ContextFile(name, create) |
|
49 |
# def _get_rear_map(self, maphash, create=0): |
|
50 |
# return self.fmap._get_rear_map(maphash, create) |
|
67 | 51 |
|
68 |
def _check_rear_map(self, maphash): |
|
69 |
filename = hexlify(maphash) |
|
70 |
dir = join(self.mappath, filename[0:2], filename[2:4], filename[4:6]) |
|
71 |
name = join(dir, filename) |
|
72 |
return exists(name) |
|
52 |
# def _check_rear_map(self, maphash): |
|
53 |
# return self.rmap._check_rear_map(maphash) |
|
54 |
# return self.rmap._check_rear_map(maphash) and |
|
55 |
# self.fmap._check_rear_map(maphash) |
|
73 | 56 |
|
74 | 57 |
def map_retr(self, maphash, blkoff=0, nr=100000000000000): |
75 | 58 |
"""Return as a list, part of the hashes map of an object |
76 | 59 |
at the given block offset. |
77 | 60 |
By default, return the whole hashes map. |
78 | 61 |
""" |
79 |
namelen = self.namelen |
|
80 |
hashes = () |
|
81 |
|
|
82 |
with self._get_rear_map(maphash, 0) as rmap: |
|
83 |
if rmap: |
|
84 |
hashes = list(rmap.sync_read_chunks(namelen, nr, blkoff)) |
|
85 |
return hashes |
|
62 |
return self.fmap.map_retr(maphash, blkoff, nr) |
|
86 | 63 |
|
87 | 64 |
def map_stor(self, maphash, hashes=(), blkoff=0, create=1): |
88 | 65 |
"""Store hashes in the given hashes map.""" |
89 |
namelen = self.namelen |
|
90 |
if self._check_rear_map(maphash): |
|
91 |
return |
|
92 |
with self._get_rear_map(maphash, 1) as rmap: |
|
93 |
rmap.sync_write_chunks(namelen, blkoff, hashes, None) |
|
94 |
|
|
66 |
self.rmap.map_stor(maphash, hashes, blkoff, create) |
|
67 |
self.fmap.map_stor(maphash, hashes, blkoff, create) |
Also available in: Unified diff