comparison mercurial/revlog.py @ 4981:e7131935fbb3

revlog: simplify addrevision - remove unused defaults for p1, p2, and text - reduce some if/else - use better variable names - remove some extra variables - remove some obsolete corner tests - simply first entry handling for revlogng - simply inline vs outofline writeout
author Matt Mackall <mpm@selenic.com>
date Mon, 23 Jul 2007 20:44:08 -0500
parents fc44c8df9d99
children 9672e3c42b0c
comparison
equal deleted inserted replaced
4980:fc44c8df9d99 4981:e7131935fbb3
975 fp.rename() 975 fp.rename()
976 976
977 tr.replace(self.indexfile, trindex * calc) 977 tr.replace(self.indexfile, trindex * calc)
978 self._io.chunkcache = None 978 self._io.chunkcache = None
979 979
980 def addrevision(self, text, transaction, link, p1=None, p2=None, d=None): 980 def addrevision(self, text, transaction, link, p1, p2, d=None):
981 """add a revision to the log 981 """add a revision to the log
982 982
983 text - the revision data to add 983 text - the revision data to add
984 transaction - the transaction object used for rollback 984 transaction - the transaction object used for rollback
985 link - the linkrev data to add 985 link - the linkrev data to add
986 p1, p2 - the parent nodeids of the revision 986 p1, p2 - the parent nodeids of the revision
987 d - an optional precomputed delta 987 d - an optional precomputed delta
988 """ 988 """
989 dfh = None
989 if not self._inline(): 990 if not self._inline():
990 dfh = self.opener(self.datafile, "a") 991 dfh = self.opener(self.datafile, "a")
991 else:
992 dfh = None
993 ifh = self.opener(self.indexfile, "a+") 992 ifh = self.opener(self.indexfile, "a+")
994 return self._addrevision(text, transaction, link, p1, p2, d, ifh, dfh) 993 return self._addrevision(text, transaction, link, p1, p2, d, ifh, dfh)
995 994
996 def _addrevision(self, text, transaction, link, p1, p2, d, ifh, dfh): 995 def _addrevision(self, text, transaction, link, p1, p2, d, ifh, dfh):
997 if text is None:
998 text = ""
999 if p1 is None:
1000 p1 = self.tip()
1001 if p2 is None:
1002 p2 = nullid
1003
1004 node = hash(text, p1, p2) 996 node = hash(text, p1, p2)
1005
1006 if node in self.nodemap: 997 if node in self.nodemap:
1007 return node 998 return node
1008 999
1009 n = self.count() 1000 curr = self.count()
1010 t = n - 1 1001 prev = curr - 1
1011 1002 base = self.base(prev)
1012 if n: 1003 offset = self.end(prev)
1013 base = self.base(t) 1004
1014 start = self.start(base) 1005 if curr:
1015 end = self.end(t)
1016 if not d: 1006 if not d:
1017 prev = self.revision(self.tip()) 1007 ptext = self.revision(self.node(prev))
1018 d = self.diff(prev, text) 1008 d = self.diff(ptext, text)
1019 data = compress(d) 1009 data = compress(d)
1020 l = len(data[1]) + len(data[0]) 1010 l = len(data[1]) + len(data[0])
1021 dist = end - start + l 1011 dist = l + offset - self.start(base)
1022 1012
1023 # full versions are inserted when the needed deltas 1013 # full versions are inserted when the needed deltas
1024 # become comparable to the uncompressed text 1014 # become comparable to the uncompressed text
1025 if not n or dist > len(text) * 2: 1015 if not curr or dist > len(text) * 2:
1026 data = compress(text) 1016 data = compress(text)
1027 l = len(data[1]) + len(data[0]) 1017 l = len(data[1]) + len(data[0])
1028 base = n 1018 base = curr
1029 else:
1030 base = self.base(t)
1031
1032 offset = 0
1033 if t >= 0:
1034 offset = self.end(t)
1035 1019
1036 e = (offset_type(offset, 0), l, len(text), 1020 e = (offset_type(offset, 0), l, len(text),
1037 base, link, self.rev(p1), self.rev(p2), node) 1021 base, link, self.rev(p1), self.rev(p2), node)
1038
1039 self.index.insert(-1, e) 1022 self.index.insert(-1, e)
1040 self.nodemap[node] = n 1023 self.nodemap[node] = curr
1041 1024
1042 if self.version == REVLOGV0: 1025 if self.version == REVLOGV0:
1043 e = (offset, l, base, link, p1, p2, node) 1026 e = (offset, l, base, link, p1, p2, node)
1044 entry = struct.pack(indexformatv0, *e) 1027 entry = struct.pack(indexformatv0, *e)
1045 else: 1028 else:
1046 entry = struct.pack(indexformatng, *e) 1029 entry = struct.pack(indexformatng, *e)
1030 if not curr:
1031 entry = struct.pack(versionformat, self.version) + entry[4:]
1047 1032
1048 if not self._inline(): 1033 if not self._inline():
1049 transaction.add(self.datafile, offset) 1034 transaction.add(self.datafile, offset)
1050 transaction.add(self.indexfile, n * len(entry)) 1035 transaction.add(self.indexfile, curr * len(entry))
1051 if data[0]: 1036 if data[0]:
1052 dfh.write(data[0]) 1037 dfh.write(data[0])
1053 dfh.write(data[1]) 1038 dfh.write(data[1])
1054 dfh.flush() 1039 dfh.flush()
1040 ifh.write(entry)
1055 else: 1041 else:
1056 ifh.seek(0, 2) 1042 ifh.seek(0, 2)
1057 transaction.add(self.indexfile, ifh.tell(), self.count() - 1) 1043 transaction.add(self.indexfile, ifh.tell(), prev)
1058 1044 ifh.write(entry)
1059 if self.count() == 1 and self.version != REVLOGV0:
1060 l = struct.pack(versionformat, self.version)
1061 ifh.write(l)
1062 entry = entry[4:]
1063
1064 ifh.write(entry)
1065
1066 if self._inline():
1067 ifh.write(data[0]) 1045 ifh.write(data[0])
1068 ifh.write(data[1]) 1046 ifh.write(data[1])
1069 self.checkinlinesize(transaction, ifh) 1047 self.checkinlinesize(transaction, ifh)
1070 1048
1071 self.cache = (node, n, text) 1049 self.cache = (node, curr, text)
1072 return node 1050 return node
1073 1051
1074 def ancestor(self, a, b): 1052 def ancestor(self, a, b):
1075 """calculate the least common ancestor of nodes a and b""" 1053 """calculate the least common ancestor of nodes a and b"""
1076 1054