|
| 1 | +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors |
| 2 | +# |
| 3 | +# This module is part of GitDB and is released under |
| 4 | +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php |
| 5 | +"""Module with basic data structures - they are designed to be lightweight and fast""" |
| 6 | +from util import ( |
| 7 | + bin_to_hex, |
| 8 | + zlib |
| 9 | + ) |
| 10 | + |
| 11 | +from fun import ( |
| 12 | + type_id_to_type_map, |
| 13 | + type_to_type_id_map |
| 14 | + ) |
| 15 | + |
| 16 | +__all__ = ('OInfo', 'OPackInfo', 'ODeltaPackInfo', |
| 17 | + 'OStream', 'OPackStream', 'ODeltaPackStream', |
| 18 | + 'IStream', 'InvalidOInfo', 'InvalidOStream' ) |
| 19 | + |
| 20 | +#{ ODB Bases |
| 21 | + |
| 22 | +class OInfo(tuple): |
| 23 | + """Carries information about an object in an ODB, provding information |
| 24 | + about the binary sha of the object, the type_string as well as the uncompressed size |
| 25 | + in bytes. |
| 26 | + |
| 27 | + It can be accessed using tuple notation and using attribute access notation:: |
| 28 | + |
| 29 | + assert dbi[0] == dbi.binsha |
| 30 | + assert dbi[1] == dbi.type |
| 31 | + assert dbi[2] == dbi.size |
| 32 | + |
| 33 | + The type is designed to be as lighteight as possible.""" |
| 34 | + __slots__ = tuple() |
| 35 | + |
| 36 | + def __new__(cls, sha, type, size): |
| 37 | + return tuple.__new__(cls, (sha, type, size)) |
| 38 | + |
| 39 | + def __init__(self, *args): |
| 40 | + tuple.__init__(self) |
| 41 | + |
| 42 | + #{ Interface |
| 43 | + @property |
| 44 | + def binsha(self): |
| 45 | + """:return: our sha as binary, 20 bytes""" |
| 46 | + return self[0] |
| 47 | + |
| 48 | + @property |
| 49 | + def hexsha(self): |
| 50 | + """:return: our sha, hex encoded, 40 bytes""" |
| 51 | + return bin_to_hex(self[0]) |
| 52 | + |
| 53 | + @property |
| 54 | + def type(self): |
| 55 | + return self[1] |
| 56 | + |
| 57 | + @property |
| 58 | + def type_id(self): |
| 59 | + return type_to_type_id_map[self[1]] |
| 60 | + |
| 61 | + @property |
| 62 | + def size(self): |
| 63 | + return self[2] |
| 64 | + #} END interface |
| 65 | + |
| 66 | + |
| 67 | +class OPackInfo(tuple): |
| 68 | + """As OInfo, but provides a type_id property to retrieve the numerical type id, and |
| 69 | + does not include a sha. |
| 70 | + |
| 71 | + Additionally, the pack_offset is the absolute offset into the packfile at which |
| 72 | + all object information is located. The data_offset property points to the abosolute |
| 73 | + location in the pack at which that actual data stream can be found.""" |
| 74 | + __slots__ = tuple() |
| 75 | + |
| 76 | + def __new__(cls, packoffset, type, size): |
| 77 | + return tuple.__new__(cls, (packoffset,type, size)) |
| 78 | + |
| 79 | + def __init__(self, *args): |
| 80 | + tuple.__init__(self) |
| 81 | + |
| 82 | + #{ Interface |
| 83 | + |
| 84 | + @property |
| 85 | + def pack_offset(self): |
| 86 | + return self[0] |
| 87 | + |
| 88 | + @property |
| 89 | + def type(self): |
| 90 | + return type_id_to_type_map[self[1]] |
| 91 | + |
| 92 | + @property |
| 93 | + def type_id(self): |
| 94 | + return self[1] |
| 95 | + |
| 96 | + @property |
| 97 | + def size(self): |
| 98 | + return self[2] |
| 99 | + |
| 100 | + #} END interface |
| 101 | + |
| 102 | + |
| 103 | +class ODeltaPackInfo(OPackInfo): |
| 104 | + """Adds delta specific information, |
| 105 | + Either the 20 byte sha which points to some object in the database, |
| 106 | + or the negative offset from the pack_offset, so that pack_offset - delta_info yields |
| 107 | + the pack offset of the base object""" |
| 108 | + __slots__ = tuple() |
| 109 | + |
| 110 | + def __new__(cls, packoffset, type, size, delta_info): |
| 111 | + return tuple.__new__(cls, (packoffset, type, size, delta_info)) |
| 112 | + |
| 113 | + #{ Interface |
| 114 | + @property |
| 115 | + def delta_info(self): |
| 116 | + return self[3] |
| 117 | + #} END interface |
| 118 | + |
| 119 | + |
| 120 | +class OStream(OInfo): |
| 121 | + """Base for object streams retrieved from the database, providing additional |
| 122 | + information about the stream. |
| 123 | + Generally, ODB streams are read-only as objects are immutable""" |
| 124 | + __slots__ = tuple() |
| 125 | + |
| 126 | + def __new__(cls, sha, type, size, stream, *args, **kwargs): |
| 127 | + """Helps with the initialization of subclasses""" |
| 128 | + return tuple.__new__(cls, (sha, type, size, stream)) |
| 129 | + |
| 130 | + |
| 131 | + def __init__(self, *args, **kwargs): |
| 132 | + tuple.__init__(self) |
| 133 | + |
| 134 | + #{ Stream Reader Interface |
| 135 | + |
| 136 | + def read(self, size=-1): |
| 137 | + return self[3].read(size) |
| 138 | + |
| 139 | + @property |
| 140 | + def stream(self): |
| 141 | + return self[3] |
| 142 | + |
| 143 | + #} END stream reader interface |
| 144 | + |
| 145 | + |
| 146 | +class ODeltaStream(OStream): |
| 147 | + """Uses size info of its stream, delaying reads""" |
| 148 | + |
| 149 | + def __new__(cls, sha, type, size, stream, *args, **kwargs): |
| 150 | + """Helps with the initialization of subclasses""" |
| 151 | + return tuple.__new__(cls, (sha, type, size, stream)) |
| 152 | + |
| 153 | + #{ Stream Reader Interface |
| 154 | + |
| 155 | + @property |
| 156 | + def size(self): |
| 157 | + return self[3].size |
| 158 | + |
| 159 | + #} END stream reader interface |
| 160 | + |
| 161 | + |
| 162 | +class OPackStream(OPackInfo): |
| 163 | + """Next to pack object information, a stream outputting an undeltified base object |
| 164 | + is provided""" |
| 165 | + __slots__ = tuple() |
| 166 | + |
| 167 | + def __new__(cls, packoffset, type, size, stream, *args): |
| 168 | + """Helps with the initialization of subclasses""" |
| 169 | + return tuple.__new__(cls, (packoffset, type, size, stream)) |
| 170 | + |
| 171 | + #{ Stream Reader Interface |
| 172 | + def read(self, size=-1): |
| 173 | + return self[3].read(size) |
| 174 | + |
| 175 | + @property |
| 176 | + def stream(self): |
| 177 | + return self[3] |
| 178 | + #} END stream reader interface |
| 179 | + |
| 180 | + |
| 181 | +class ODeltaPackStream(ODeltaPackInfo): |
| 182 | + """Provides a stream outputting the uncompressed offset delta information""" |
| 183 | + __slots__ = tuple() |
| 184 | + |
| 185 | + def __new__(cls, packoffset, type, size, delta_info, stream): |
| 186 | + return tuple.__new__(cls, (packoffset, type, size, delta_info, stream)) |
| 187 | + |
| 188 | + |
| 189 | + #{ Stream Reader Interface |
| 190 | + def read(self, size=-1): |
| 191 | + return self[4].read(size) |
| 192 | + |
| 193 | + @property |
| 194 | + def stream(self): |
| 195 | + return self[4] |
| 196 | + #} END stream reader interface |
| 197 | + |
| 198 | + |
| 199 | +class IStream(list): |
| 200 | + """Represents an input content stream to be fed into the ODB. It is mutable to allow |
| 201 | + the ODB to record information about the operations outcome right in this instance. |
| 202 | + |
| 203 | + It provides interfaces for the OStream and a StreamReader to allow the instance |
| 204 | + to blend in without prior conversion. |
| 205 | + |
| 206 | + The only method your content stream must support is 'read'""" |
| 207 | + __slots__ = tuple() |
| 208 | + |
| 209 | + def __new__(cls, type, size, stream, sha=None): |
| 210 | + return list.__new__(cls, (sha, type, size, stream, None)) |
| 211 | + |
| 212 | + def __init__(self, type, size, stream, sha=None): |
| 213 | + list.__init__(self, (sha, type, size, stream, None)) |
| 214 | + |
| 215 | + #{ Interface |
| 216 | + @property |
| 217 | + def hexsha(self): |
| 218 | + """:return: our sha, hex encoded, 40 bytes""" |
| 219 | + return bin_to_hex(self[0]) |
| 220 | + |
| 221 | + def _error(self): |
| 222 | + """:return: the error that occurred when processing the stream, or None""" |
| 223 | + return self[4] |
| 224 | + |
| 225 | + def _set_error(self, exc): |
| 226 | + """Set this input stream to the given exc, may be None to reset the error""" |
| 227 | + self[4] = exc |
| 228 | + |
| 229 | + error = property(_error, _set_error) |
| 230 | + |
| 231 | + #} END interface |
| 232 | + |
| 233 | + #{ Stream Reader Interface |
| 234 | + |
| 235 | + def read(self, size=-1): |
| 236 | + """Implements a simple stream reader interface, passing the read call on |
| 237 | + to our internal stream""" |
| 238 | + return self[3].read(size) |
| 239 | + |
| 240 | + #} END stream reader interface |
| 241 | + |
| 242 | + #{ interface |
| 243 | + |
| 244 | + def _set_binsha(self, binsha): |
| 245 | + self[0] = binsha |
| 246 | + |
| 247 | + def _binsha(self): |
| 248 | + return self[0] |
| 249 | + |
| 250 | + binsha = property(_binsha, _set_binsha) |
| 251 | + |
| 252 | + |
| 253 | + def _type(self): |
| 254 | + return self[1] |
| 255 | + |
| 256 | + def _set_type(self, type): |
| 257 | + self[1] = type |
| 258 | + |
| 259 | + type = property(_type, _set_type) |
| 260 | + |
| 261 | + def _size(self): |
| 262 | + return self[2] |
| 263 | + |
| 264 | + def _set_size(self, size): |
| 265 | + self[2] = size |
| 266 | + |
| 267 | + size = property(_size, _set_size) |
| 268 | + |
| 269 | + def _stream(self): |
| 270 | + return self[3] |
| 271 | + |
| 272 | + def _set_stream(self, stream): |
| 273 | + self[3] = stream |
| 274 | + |
| 275 | + stream = property(_stream, _set_stream) |
| 276 | + |
| 277 | + #} END odb info interface |
| 278 | + |
| 279 | + |
| 280 | +class InvalidOInfo(tuple): |
| 281 | + """Carries information about a sha identifying an object which is invalid in |
| 282 | + the queried database. The exception attribute provides more information about |
| 283 | + the cause of the issue""" |
| 284 | + __slots__ = tuple() |
| 285 | + |
| 286 | + def __new__(cls, sha, exc): |
| 287 | + return tuple.__new__(cls, (sha, exc)) |
| 288 | + |
| 289 | + def __init__(self, sha, exc): |
| 290 | + tuple.__init__(self, (sha, exc)) |
| 291 | + |
| 292 | + @property |
| 293 | + def binsha(self): |
| 294 | + return self[0] |
| 295 | + |
| 296 | + @property |
| 297 | + def hexsha(self): |
| 298 | + return bin_to_hex(self[0]) |
| 299 | + |
| 300 | + @property |
| 301 | + def error(self): |
| 302 | + """:return: exception instance explaining the failure""" |
| 303 | + return self[1] |
| 304 | + |
| 305 | + |
| 306 | +class InvalidOStream(InvalidOInfo): |
| 307 | + """Carries information about an invalid ODB stream""" |
| 308 | + __slots__ = tuple() |
| 309 | + |
| 310 | +#} END ODB Bases |
| 311 | + |
0 commit comments