Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / hashfiler / context_object.py @ 185340e0

History | View | Annotate | Download (4.9 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", "offset")
108

    
109
    def __init__(self, name, ioctx):
110
        self.name = name
111
        self.ioctx = ioctx
112
        self.offset = 0
113
        #self.dirty = 0
114

    
115
    def __enter__(self):
116
        return self
117

    
118
    def __exit__(self, exc, arg, trace):
119
        return False
120

    
121
    def seek(self, offset, whence=SEEK_SET):
122
        if whence == SEEK_CUR:
123
            offset += self.offset
124
        self.offset = offset
125
        return offset
126

    
127
    def tell(self):
128
        return self.offset
129

    
130
    def truncate(self, size):
131
        self.ioctx.trunc(self.name, size)
132

    
133
    def sync_write(self, data):
134
        #self.dirty = 1
135
        self.ioctx.write(self.name, data, self.offset)
136
        self.offset += len(data)
137

    
138
    def sync_write_chunks(self, chunksize, offset, chunks, size=None):
139
        #self.dirty = 1
140
        return file_sync_write_chunks(self, chunksize, offset, chunks, size)
141

    
142
    def sync_read(self, size):
143
        read = self.ioctx.read
144
        data = ''
145
        datalen = 0
146
        while 1:
147
            try:
148
                s = read(self.name, size - datalen, self.offset)
149
            except ObjectNotFound:
150
                s = None
151
            if not s:
152
                break
153
            data += s
154
            datalen += len(s)
155
            self.offset += len(s)
156
            if datalen >= size:
157
                break
158
        return data
159

    
160
    def sync_read_chunks(self, chunksize, nr, offset=0):
161
        return file_sync_read_chunks(self, chunksize, nr, offset)