|
| 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 | +fromutilimport ( |
| 7 | +bin_to_hex, |
| 8 | +zlib |
| 9 | +) |
| 10 | + |
| 11 | +fromfunimport ( |
| 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 | +classOInfo(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 | +returntuple.__new__(cls, (sha,type,size)) |
| 38 | + |
| 39 | +def__init__(self,*args): |
| 40 | +tuple.__init__(self) |
| 41 | + |
| 42 | +#{ Interface |
| 43 | +@property |
| 44 | +defbinsha(self): |
| 45 | +""":return: our sha as binary, 20 bytes""" |
| 46 | +returnself[0] |
| 47 | + |
| 48 | +@property |
| 49 | +defhexsha(self): |
| 50 | +""":return: our sha, hex encoded, 40 bytes""" |
| 51 | +returnbin_to_hex(self[0]) |
| 52 | + |
| 53 | +@property |
| 54 | +deftype(self): |
| 55 | +returnself[1] |
| 56 | + |
| 57 | +@property |
| 58 | +deftype_id(self): |
| 59 | +returntype_to_type_id_map[self[1]] |
| 60 | + |
| 61 | +@property |
| 62 | +defsize(self): |
| 63 | +returnself[2] |
| 64 | +#} END interface |
| 65 | + |
| 66 | + |
| 67 | +classOPackInfo(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 | +returntuple.__new__(cls, (packoffset,type,size)) |
| 78 | + |
| 79 | +def__init__(self,*args): |
| 80 | +tuple.__init__(self) |
| 81 | + |
| 82 | +#{ Interface |
| 83 | + |
| 84 | +@property |
| 85 | +defpack_offset(self): |
| 86 | +returnself[0] |
| 87 | + |
| 88 | +@property |
| 89 | +deftype(self): |
| 90 | +returntype_id_to_type_map[self[1]] |
| 91 | + |
| 92 | +@property |
| 93 | +deftype_id(self): |
| 94 | +returnself[1] |
| 95 | + |
| 96 | +@property |
| 97 | +defsize(self): |
| 98 | +returnself[2] |
| 99 | + |
| 100 | +#} END interface |
| 101 | + |
| 102 | + |
| 103 | +classODeltaPackInfo(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 | +returntuple.__new__(cls, (packoffset,type,size,delta_info)) |
| 112 | + |
| 113 | +#{ Interface |
| 114 | +@property |
| 115 | +defdelta_info(self): |
| 116 | +returnself[3] |
| 117 | +#} END interface |
| 118 | + |
| 119 | + |
| 120 | +classOStream(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 | +returntuple.__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 | +defread(self,size=-1): |
| 137 | +returnself[3].read(size) |
| 138 | + |
| 139 | +@property |
| 140 | +defstream(self): |
| 141 | +returnself[3] |
| 142 | + |
| 143 | +#} END stream reader interface |
| 144 | + |
| 145 | + |
| 146 | +classODeltaStream(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 | +returntuple.__new__(cls, (sha,type,size,stream)) |
| 152 | + |
| 153 | +#{ Stream Reader Interface |
| 154 | + |
| 155 | +@property |
| 156 | +defsize(self): |
| 157 | +returnself[3].size |
| 158 | + |
| 159 | +#} END stream reader interface |
| 160 | + |
| 161 | + |
| 162 | +classOPackStream(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 | +returntuple.__new__(cls, (packoffset,type,size,stream)) |
| 170 | + |
| 171 | +#{ Stream Reader Interface |
| 172 | +defread(self,size=-1): |
| 173 | +returnself[3].read(size) |
| 174 | + |
| 175 | +@property |
| 176 | +defstream(self): |
| 177 | +returnself[3] |
| 178 | +#} END stream reader interface |
| 179 | + |
| 180 | + |
| 181 | +classODeltaPackStream(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 | +returntuple.__new__(cls, (packoffset,type,size,delta_info,stream)) |
| 187 | + |
| 188 | + |
| 189 | +#{ Stream Reader Interface |
| 190 | +defread(self,size=-1): |
| 191 | +returnself[4].read(size) |
| 192 | + |
| 193 | +@property |
| 194 | +defstream(self): |
| 195 | +returnself[4] |
| 196 | +#} END stream reader interface |
| 197 | + |
| 198 | + |
| 199 | +classIStream(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 | +returnlist.__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 | +defhexsha(self): |
| 218 | +""":return: our sha, hex encoded, 40 bytes""" |
| 219 | +returnbin_to_hex(self[0]) |
| 220 | + |
| 221 | +def_error(self): |
| 222 | +""":return: the error that occurred when processing the stream, or None""" |
| 223 | +returnself[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 | +defread(self,size=-1): |
| 236 | +"""Implements a simple stream reader interface, passing the read call on |
| 237 | +to our internal stream""" |
| 238 | +returnself[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 | +returnself[0] |
| 249 | + |
| 250 | +binsha=property(_binsha,_set_binsha) |
| 251 | + |
| 252 | + |
| 253 | +def_type(self): |
| 254 | +returnself[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 | +returnself[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 | +returnself[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 | +classInvalidOInfo(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 | +returntuple.__new__(cls, (sha,exc)) |
| 288 | + |
| 289 | +def__init__(self,sha,exc): |
| 290 | +tuple.__init__(self, (sha,exc)) |
| 291 | + |
| 292 | +@property |
| 293 | +defbinsha(self): |
| 294 | +returnself[0] |
| 295 | + |
| 296 | +@property |
| 297 | +defhexsha(self): |
| 298 | +returnbin_to_hex(self[0]) |
| 299 | + |
| 300 | +@property |
| 301 | +deferror(self): |
| 302 | +""":return: exception instance explaining the failure""" |
| 303 | +returnself[1] |
| 304 | + |
| 305 | + |
| 306 | +classInvalidOStream(InvalidOInfo): |
| 307 | +"""Carries information about an invalid ODB stream""" |
| 308 | +__slots__=tuple() |
| 309 | + |
| 310 | +#} END ODB Bases |
| 311 | + |