Statistics
| Branch: | Tag: | Revision:

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

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 Ioctx, 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
def file_sync_read_chunks(radosobject, chunksize, nr, offset=0):
84
    """Read and yield groups of chunks from a buffered file object at offset.
85
       Reads never span accros chunksize boundaries.
86
    """
87
    radosobject.seek(offset * chunksize)
88
    while nr:
89
        remains = chunksize
90
        chunk = ''
91
        while 1:
92
            s = radosobject.sync_read(remains)
93
            if not s:
94
                if chunk:
95
                    yield chunk
96
                return
97
            chunk += s
98
            remains -= len(s)
99
            if remains <= 0:
100
                break
101
        yield chunk
102
        nr -= 1
103

    
104
class RadosObject(object):
105
    __slots__ = ("name", "ioctx", "create", "offset")
106

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

    
114
    def __enter__(self):
115
        return self
116

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

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

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

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

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

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

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

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