Revision 6f08f063
b/snf-pithos-backend/pithos/backends/lib/hashfiler/fileblocker.py | ||
---|---|---|
37 | 37 |
from binascii import hexlify |
38 | 38 |
|
39 | 39 |
from context_file import ContextFile, file_sync_read_chunks |
40 |
from os import O_RDONLY, O_WRONLY |
|
40 | 41 |
|
41 | 42 |
|
42 | 43 |
class FileBlocker(object): |
... | ... | |
78 | 79 |
def _pad(self, block): |
79 | 80 |
return block + ('\x00' * (self.blocksize - len(block))) |
80 | 81 |
|
81 |
def _get_rear_block(self, blkhash, create=0): |
|
82 |
def _read_rear_block(self, blkhash): |
|
83 |
filename = hexlify(blkhash) |
|
84 |
dir = join(self.blockpath, filename[0:2], filename[2:4], filename[4:6]) |
|
85 |
name = join(dir, filename) |
|
86 |
return ContextFile(name, O_RDONLY) |
|
87 |
|
|
88 |
def _write_rear_block(self, blkhash): |
|
82 | 89 |
filename = hexlify(blkhash) |
83 | 90 |
dir = join(self.blockpath, filename[0:2], filename[2:4], filename[4:6]) |
84 | 91 |
if not exists(dir): |
85 | 92 |
makedirs(dir) |
86 | 93 |
name = join(dir, filename) |
87 |
return ContextFile(name, create)
|
|
94 |
return ContextFile(name, O_WRONLY)
|
|
88 | 95 |
|
89 | 96 |
def _check_rear_block(self, blkhash): |
90 | 97 |
filename = hexlify(blkhash) |
... | ... | |
122 | 129 |
if h == self.emptyhash: |
123 | 130 |
append(self._pad('')) |
124 | 131 |
continue |
125 |
with self._get_rear_block(h, 0) as rbl:
|
|
132 |
with self._read_rear_block(h) as rbl:
|
|
126 | 133 |
if not rbl: |
127 | 134 |
break |
128 | 135 |
for block in rbl.sync_read_chunks(blocksize, 1, 0): |
... | ... | |
144 | 151 |
missing = [i for i, h in enumerate(hashlist) if not |
145 | 152 |
self._check_rear_block(h)] |
146 | 153 |
for i in missing: |
147 |
with self._get_rear_block(hashlist[i], 1) as rbl:
|
|
154 |
with self._write_rear_block(hashlist[i]) as rbl:
|
|
148 | 155 |
rbl.sync_write(blocklist[i]) # XXX: verify? |
149 | 156 |
|
150 | 157 |
return hashlist, missing |
b/snf-pithos-backend/pithos/backends/lib/hashfiler/filemapper.py | ||
---|---|---|
36 | 36 |
from binascii import hexlify |
37 | 37 |
|
38 | 38 |
from context_file import ContextFile |
39 |
from os import O_RDONLY, O_WRONLY |
|
39 | 40 |
|
40 | 41 |
|
41 | 42 |
class FileMapper(object): |
... | ... | |
58 | 59 |
(mappath,)) |
59 | 60 |
self.mappath = mappath |
60 | 61 |
|
61 |
def _get_rear_map(self, maphash, create=0):
|
|
62 |
def _read_rear_map(self, maphash):
|
|
62 | 63 |
filename = hexlify(maphash) |
63 | 64 |
dir = join(self.mappath, filename[0:2], filename[2:4], filename[4:6]) |
64 | 65 |
if not exists(dir): |
65 | 66 |
makedirs(dir) |
66 | 67 |
name = join(dir, filename) |
67 |
return ContextFile(name, create) |
|
68 |
return ContextFile(name, O_RDONLY) |
|
69 |
|
|
70 |
def _write_rear_map(self, maphash): |
|
71 |
filename = hexlify(maphash) |
|
72 |
dir = join(self.mappath, filename[0:2], filename[2:4], filename[4:6]) |
|
73 |
if not exists(dir): |
|
74 |
makedirs(dir) |
|
75 |
name = join(dir, filename) |
|
76 |
return ContextFile(name, O_WRONLY) |
|
68 | 77 |
|
69 | 78 |
def _check_rear_map(self, maphash): |
70 | 79 |
filename = hexlify(maphash) |
... | ... | |
80 | 89 |
namelen = self.namelen |
81 | 90 |
hashes = () |
82 | 91 |
|
83 |
with self._get_rear_map(maphash, 0) as rmap:
|
|
92 |
with self._read_rear_map(maphash) as rmap:
|
|
84 | 93 |
if rmap: |
85 | 94 |
hashes = list(rmap.sync_read_chunks(namelen, nr, blkoff)) |
86 | 95 |
return hashes |
87 | 96 |
|
88 |
def map_stor(self, maphash, hashes=(), blkoff=0, create=1):
|
|
97 |
def map_stor(self, maphash, hashes=(), blkoff=0): |
|
89 | 98 |
"""Store hashes in the given hashes map.""" |
90 | 99 |
namelen = self.namelen |
91 | 100 |
if self._check_rear_map(maphash): |
92 | 101 |
return |
93 |
with self._get_rear_map(maphash, 1) as rmap:
|
|
102 |
with self._write_rear_map(maphash) as rmap:
|
|
94 | 103 |
rmap.sync_write_chunks(namelen, blkoff, hashes, None) |
Also available in: Unified diff