root / snf-pithos-backend / pithos / backends / lib / hashfiler / context_object.py @ 29148653
History | View | Annotate | Download (5 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from os import SEEK_CUR, SEEK_SET |
35 |
from rados import ObjectNotFound |
36 |
|
37 |
_zeros = ''
|
38 |
|
39 |
|
40 |
def zeros(nr): |
41 |
global _zeros
|
42 |
size = len(_zeros)
|
43 |
if nr == size:
|
44 |
return _zeros
|
45 |
|
46 |
if nr > size:
|
47 |
_zeros += '\0' * (nr - size)
|
48 |
return _zeros
|
49 |
|
50 |
if nr < size:
|
51 |
_zeros = _zeros[:nr] |
52 |
return _zeros
|
53 |
|
54 |
|
55 |
def file_sync_write_chunks(radosobject, chunksize, offset, chunks, size=None): |
56 |
"""Write given chunks to the given buffered file object.
|
57 |
Writes never span across chunk boundaries.
|
58 |
If size is given stop after or pad until size bytes have been written.
|
59 |
"""
|
60 |
padding = 0
|
61 |
cursize = chunksize * offset |
62 |
radosobject.seek(cursize) |
63 |
for chunk in chunks: |
64 |
if padding:
|
65 |
radosobject.sync_write(buffer(zeros(chunksize), 0, padding)) |
66 |
if size is not None and cursize + chunksize >= size: |
67 |
chunk = chunk[:chunksize - (cursize - size)] |
68 |
radosobject.sync_write(chunk) |
69 |
cursize += len(chunk)
|
70 |
break
|
71 |
radosobject.sync_write(chunk) |
72 |
padding = chunksize - len(chunk)
|
73 |
|
74 |
padding = size - cursize if size is not None else 0 |
75 |
if padding <= 0: |
76 |
return
|
77 |
|
78 |
q, r = divmod(padding, chunksize)
|
79 |
for x in xrange(q): |
80 |
radosobject.sunc_write(zeros(chunksize)) |
81 |
radosobject.sync_write(buffer(zeros(chunksize), 0, r)) |
82 |
|
83 |
|
84 |
def file_sync_read_chunks(radosobject, chunksize, nr, offset=0): |
85 |
"""Read and yield groups of chunks from a buffered file object at offset.
|
86 |
Reads never span accros chunksize boundaries.
|
87 |
"""
|
88 |
radosobject.seek(offset * chunksize) |
89 |
while nr:
|
90 |
remains = chunksize |
91 |
chunk = ''
|
92 |
while 1: |
93 |
s = radosobject.sync_read(remains) |
94 |
if not s: |
95 |
if chunk:
|
96 |
yield chunk
|
97 |
return
|
98 |
chunk += s |
99 |
remains -= len(s)
|
100 |
if remains <= 0: |
101 |
break
|
102 |
yield chunk
|
103 |
nr -= 1
|
104 |
|
105 |
|
106 |
class RadosObject(object): |
107 |
__slots__ = ("name", "ioctx", "create", "offset") |
108 |
|
109 |
def __init__(self, name, ioctx, create=0): |
110 |
self.name = name
|
111 |
self.ioctx = ioctx
|
112 |
self.create = create
|
113 |
self.offset = 0 |
114 |
#self.dirty = 0
|
115 |
|
116 |
def __enter__(self): |
117 |
return self |
118 |
|
119 |
def __exit__(self, exc, arg, trace): |
120 |
return False |
121 |
|
122 |
def seek(self, offset, whence=SEEK_SET): |
123 |
if whence == SEEK_CUR:
|
124 |
offset += self.offset
|
125 |
self.offset = offset
|
126 |
return offset
|
127 |
|
128 |
def tell(self): |
129 |
return self.offset |
130 |
|
131 |
def truncate(self, size): |
132 |
self.ioctx.trunc(self.name, size) |
133 |
|
134 |
def sync_write(self, data): |
135 |
#self.dirty = 1
|
136 |
self.ioctx.write(self.name, data, self.offset) |
137 |
self.offset += len(data) |
138 |
|
139 |
def sync_write_chunks(self, chunksize, offset, chunks, size=None): |
140 |
#self.dirty = 1
|
141 |
return file_sync_write_chunks(self, chunksize, offset, chunks, size) |
142 |
|
143 |
def sync_read(self, size): |
144 |
read = self.ioctx.read
|
145 |
data = ''
|
146 |
datalen = 0
|
147 |
while 1: |
148 |
try:
|
149 |
s = read(self.name, size - datalen, self.offset) |
150 |
except ObjectNotFound:
|
151 |
s = None
|
152 |
if not s: |
153 |
break
|
154 |
data += s |
155 |
datalen += len(s)
|
156 |
self.offset += len(s) |
157 |
if datalen >= size:
|
158 |
break
|
159 |
return data
|
160 |
|
161 |
def sync_read_chunks(self, chunksize, nr, offset=0): |
162 |
return file_sync_read_chunks(self, chunksize, nr, offset) |