comparison mercurial/revlog.py @ 4985:e6525e459157

revlog: simplify revlog.__init__ - move stat into io helper - get rid of self.defversion and self.indexstat - fold _load into __init__
author Matt Mackall <mpm@selenic.com>
date Mon, 23 Jul 2007 20:44:08 -0500
parents b4066fcbd6ba
children 58cc017ec7e0
comparison
equal deleted inserted replaced
4984:b4066fcbd6ba 4985:e6525e459157
307 307
308 class revlogoldio(object): 308 class revlogoldio(object):
309 def __init__(self): 309 def __init__(self):
310 self.size = struct.calcsize(indexformatv0) 310 self.size = struct.calcsize(indexformatv0)
311 311
312 def parseindex(self, fp, st, inline): 312 def parseindex(self, fp, inline):
313 s = self.size 313 s = self.size
314 index = [] 314 index = []
315 nodemap = {nullid: nullrev} 315 nodemap = {nullid: nullrev}
316 n = off = 0 316 n = off = 0
317 data = fp.read() 317 data = fp.read()
331 331
332 class revlogio(object): 332 class revlogio(object):
333 def __init__(self): 333 def __init__(self):
334 self.size = struct.calcsize(indexformatng) 334 self.size = struct.calcsize(indexformatng)
335 335
336 def parseindex(self, fp, st, inline): 336 def parseindex(self, fp, inline):
337 if (lazyparser.safe_to_use and not inline and 337 try:
338 st and st.st_size > 1000000): 338 size = util.fstat(fp).st_size
339 except AttributeError:
340 size = 0
341
342 if lazyparser.safe_to_use and not inline and size > 1000000:
339 # big index, let's parse it on demand 343 # big index, let's parse it on demand
340 parser = lazyparser(fp, st.st_size) 344 parser = lazyparser(fp, size)
341 index = lazyindex(parser) 345 index = lazyindex(parser)
342 nodemap = lazymap(parser) 346 nodemap = lazymap(parser)
343 e = list(index[0]) 347 e = list(index[0])
344 type = gettype(e[0]) 348 type = gettype(e[0])
345 e[0] = offset_type(0, type) 349 e[0] = offset_type(0, type)
407 and can be used to implement COW semantics or the like. 411 and can be used to implement COW semantics or the like.
408 """ 412 """
409 self.indexfile = indexfile 413 self.indexfile = indexfile
410 self.datafile = indexfile[:-2] + ".d" 414 self.datafile = indexfile[:-2] + ".d"
411 self.opener = opener 415 self.opener = opener
412 self.indexstat = None
413 self._cache = None 416 self._cache = None
414 self._chunkcache = None 417 self._chunkcache = None
415 self.defversion = REVLOG_DEFAULT_VERSION 418 self.nodemap = {nullid: nullrev}
419 self.index = []
420
421 v = REVLOG_DEFAULT_VERSION
416 if hasattr(opener, "defversion"): 422 if hasattr(opener, "defversion"):
417 self.defversion = opener.defversion 423 v = opener.defversion
418 if self.defversion & REVLOGNG: 424 if v & REVLOGNG:
419 self.defversion |= REVLOGNGINLINEDATA 425 v |= REVLOGNGINLINEDATA
420 self._load() 426
421 427 i = ""
422 def _load(self):
423 v = self.defversion
424 try: 428 try:
425 f = self.opener(self.indexfile) 429 f = self.opener(self.indexfile)
426 i = f.read(4) 430 i = f.read(4)
427 f.seek(0) 431 f.seek(0)
428 if len(i) > 0: 432 if len(i) > 0:
429 v = struct.unpack(versionformat, i)[0] 433 v = struct.unpack(versionformat, i)[0]
430 except IOError, inst: 434 except IOError, inst:
431 if inst.errno != errno.ENOENT: 435 if inst.errno != errno.ENOENT:
432 raise 436 raise
433 i = "" 437
434 else: 438 self.version = v
435 try: 439 self._inline = v & REVLOGNGINLINEDATA
436 st = util.fstat(f)
437 except AttributeError, inst:
438 st = None
439 else:
440 oldst = self.indexstat
441 if (oldst and st.st_dev == oldst.st_dev
442 and st.st_ino == oldst.st_ino
443 and st.st_mtime == oldst.st_mtime
444 and st.st_ctime == oldst.st_ctime
445 and st.st_size == oldst.st_size):
446 return
447 self.indexstat = st
448 flags = v & ~0xFFFF 440 flags = v & ~0xFFFF
449 fmt = v & 0xFFFF 441 fmt = v & 0xFFFF
450 if fmt == REVLOGV0: 442 if fmt == REVLOGV0 and flags:
451 if flags: 443 raise RevlogError(_("index %s unknown flags %#04x for format v0")
452 raise RevlogError(_("index %s unknown flags %#04x for format v0") 444 % (self.indexfile, flags >> 16))
453 % (self.indexfile, flags >> 16)) 445 elif fmt == REVLOGNG and flags & ~REVLOGNGINLINEDATA:
454 elif fmt == REVLOGNG: 446 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
455 if flags & ~REVLOGNGINLINEDATA: 447 % (self.indexfile, flags >> 16))
456 raise RevlogError(_("index %s unknown flags %#04x for revlogng") 448 elif fmt > REVLOGNG:
457 % (self.indexfile, flags >> 16))
458 else:
459 raise RevlogError(_("index %s unknown format %d") 449 raise RevlogError(_("index %s unknown format %d")
460 % (self.indexfile, fmt)) 450 % (self.indexfile, fmt))
461 self.version = v 451
462 self._inline = v & REVLOGNGINLINEDATA
463 self.nodemap = {nullid: nullrev}
464 self.index = []
465 self._io = revlogio() 452 self._io = revlogio()
466 if self.version == REVLOGV0: 453 if self.version == REVLOGV0:
467 self._io = revlogoldio() 454 self._io = revlogoldio()
468 if i: 455 if i:
469 d = self._io.parseindex(f, st, self._inline) 456 d = self._io.parseindex(f, self._inline)
470 self.index, self.nodemap, self._chunkcache = d 457 self.index, self.nodemap, self._chunkcache = d
458
471 # add the magic null revision at -1 459 # add the magic null revision at -1
472 self.index.append((0, 0, 0, -1, -1, -1, -1, nullid)) 460 self.index.append((0, 0, 0, -1, -1, -1, -1, nullid))
473 461
474 def _loadindex(self, start, end): 462 def _loadindex(self, start, end):
475 """load a block of indexes all at once from the lazy parser""" 463 """load a block of indexes all at once from the lazy parser"""