##// END OF EJS Templates
rbc: empty (and invalid) rbc-names file should give an empty name list...
Mads Kiilerich -
r31371:7dd2f51f default
parent child Browse files
Show More
@@ -1,516 +1,518 b''
1 # branchmap.py - logic to computes, maintain and stores branchmap for local repo
1 # branchmap.py - logic to computes, maintain and stores branchmap for local repo
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import struct
10 import struct
11
11
12 from .node import (
12 from .node import (
13 bin,
13 bin,
14 hex,
14 hex,
15 nullid,
15 nullid,
16 nullrev,
16 nullrev,
17 )
17 )
18 from . import (
18 from . import (
19 encoding,
19 encoding,
20 error,
20 error,
21 scmutil,
21 scmutil,
22 util,
22 util,
23 )
23 )
24
24
25 calcsize = struct.calcsize
25 calcsize = struct.calcsize
26 pack_into = struct.pack_into
26 pack_into = struct.pack_into
27 unpack_from = struct.unpack_from
27 unpack_from = struct.unpack_from
28
28
29 def _filename(repo):
29 def _filename(repo):
30 """name of a branchcache file for a given repo or repoview"""
30 """name of a branchcache file for a given repo or repoview"""
31 filename = "cache/branch2"
31 filename = "cache/branch2"
32 if repo.filtername:
32 if repo.filtername:
33 filename = '%s-%s' % (filename, repo.filtername)
33 filename = '%s-%s' % (filename, repo.filtername)
34 return filename
34 return filename
35
35
36 def read(repo):
36 def read(repo):
37 try:
37 try:
38 f = repo.vfs(_filename(repo))
38 f = repo.vfs(_filename(repo))
39 lines = f.read().split('\n')
39 lines = f.read().split('\n')
40 f.close()
40 f.close()
41 except (IOError, OSError):
41 except (IOError, OSError):
42 return None
42 return None
43
43
44 try:
44 try:
45 cachekey = lines.pop(0).split(" ", 2)
45 cachekey = lines.pop(0).split(" ", 2)
46 last, lrev = cachekey[:2]
46 last, lrev = cachekey[:2]
47 last, lrev = bin(last), int(lrev)
47 last, lrev = bin(last), int(lrev)
48 filteredhash = None
48 filteredhash = None
49 if len(cachekey) > 2:
49 if len(cachekey) > 2:
50 filteredhash = bin(cachekey[2])
50 filteredhash = bin(cachekey[2])
51 partial = branchcache(tipnode=last, tiprev=lrev,
51 partial = branchcache(tipnode=last, tiprev=lrev,
52 filteredhash=filteredhash)
52 filteredhash=filteredhash)
53 if not partial.validfor(repo):
53 if not partial.validfor(repo):
54 # invalidate the cache
54 # invalidate the cache
55 raise ValueError('tip differs')
55 raise ValueError('tip differs')
56 cl = repo.changelog
56 cl = repo.changelog
57 for l in lines:
57 for l in lines:
58 if not l:
58 if not l:
59 continue
59 continue
60 node, state, label = l.split(" ", 2)
60 node, state, label = l.split(" ", 2)
61 if state not in 'oc':
61 if state not in 'oc':
62 raise ValueError('invalid branch state')
62 raise ValueError('invalid branch state')
63 label = encoding.tolocal(label.strip())
63 label = encoding.tolocal(label.strip())
64 node = bin(node)
64 node = bin(node)
65 if not cl.hasnode(node):
65 if not cl.hasnode(node):
66 raise ValueError('node %s does not exist' % hex(node))
66 raise ValueError('node %s does not exist' % hex(node))
67 partial.setdefault(label, []).append(node)
67 partial.setdefault(label, []).append(node)
68 if state == 'c':
68 if state == 'c':
69 partial._closednodes.add(node)
69 partial._closednodes.add(node)
70 except KeyboardInterrupt:
70 except KeyboardInterrupt:
71 raise
71 raise
72 except Exception as inst:
72 except Exception as inst:
73 if repo.ui.debugflag:
73 if repo.ui.debugflag:
74 msg = 'invalid branchheads cache'
74 msg = 'invalid branchheads cache'
75 if repo.filtername is not None:
75 if repo.filtername is not None:
76 msg += ' (%s)' % repo.filtername
76 msg += ' (%s)' % repo.filtername
77 msg += ': %s\n'
77 msg += ': %s\n'
78 repo.ui.debug(msg % inst)
78 repo.ui.debug(msg % inst)
79 partial = None
79 partial = None
80 return partial
80 return partial
81
81
82 ### Nearest subset relation
82 ### Nearest subset relation
83 # Nearest subset of filter X is a filter Y so that:
83 # Nearest subset of filter X is a filter Y so that:
84 # * Y is included in X,
84 # * Y is included in X,
85 # * X - Y is as small as possible.
85 # * X - Y is as small as possible.
86 # This create and ordering used for branchmap purpose.
86 # This create and ordering used for branchmap purpose.
87 # the ordering may be partial
87 # the ordering may be partial
88 subsettable = {None: 'visible',
88 subsettable = {None: 'visible',
89 'visible': 'served',
89 'visible': 'served',
90 'served': 'immutable',
90 'served': 'immutable',
91 'immutable': 'base'}
91 'immutable': 'base'}
92
92
93 def updatecache(repo):
93 def updatecache(repo):
94 cl = repo.changelog
94 cl = repo.changelog
95 filtername = repo.filtername
95 filtername = repo.filtername
96 partial = repo._branchcaches.get(filtername)
96 partial = repo._branchcaches.get(filtername)
97
97
98 revs = []
98 revs = []
99 if partial is None or not partial.validfor(repo):
99 if partial is None or not partial.validfor(repo):
100 partial = read(repo)
100 partial = read(repo)
101 if partial is None:
101 if partial is None:
102 subsetname = subsettable.get(filtername)
102 subsetname = subsettable.get(filtername)
103 if subsetname is None:
103 if subsetname is None:
104 partial = branchcache()
104 partial = branchcache()
105 else:
105 else:
106 subset = repo.filtered(subsetname)
106 subset = repo.filtered(subsetname)
107 partial = subset.branchmap().copy()
107 partial = subset.branchmap().copy()
108 extrarevs = subset.changelog.filteredrevs - cl.filteredrevs
108 extrarevs = subset.changelog.filteredrevs - cl.filteredrevs
109 revs.extend(r for r in extrarevs if r <= partial.tiprev)
109 revs.extend(r for r in extrarevs if r <= partial.tiprev)
110 revs.extend(cl.revs(start=partial.tiprev + 1))
110 revs.extend(cl.revs(start=partial.tiprev + 1))
111 if revs:
111 if revs:
112 partial.update(repo, revs)
112 partial.update(repo, revs)
113 partial.write(repo)
113 partial.write(repo)
114
114
115 assert partial.validfor(repo), filtername
115 assert partial.validfor(repo), filtername
116 repo._branchcaches[repo.filtername] = partial
116 repo._branchcaches[repo.filtername] = partial
117
117
118 def replacecache(repo, bm):
118 def replacecache(repo, bm):
119 """Replace the branchmap cache for a repo with a branch mapping.
119 """Replace the branchmap cache for a repo with a branch mapping.
120
120
121 This is likely only called during clone with a branch map from a remote.
121 This is likely only called during clone with a branch map from a remote.
122 """
122 """
123 rbheads = []
123 rbheads = []
124 closed = []
124 closed = []
125 for bheads in bm.itervalues():
125 for bheads in bm.itervalues():
126 rbheads.extend(bheads)
126 rbheads.extend(bheads)
127 for h in bheads:
127 for h in bheads:
128 r = repo.changelog.rev(h)
128 r = repo.changelog.rev(h)
129 b, c = repo.changelog.branchinfo(r)
129 b, c = repo.changelog.branchinfo(r)
130 if c:
130 if c:
131 closed.append(h)
131 closed.append(h)
132
132
133 if rbheads:
133 if rbheads:
134 rtiprev = max((int(repo.changelog.rev(node))
134 rtiprev = max((int(repo.changelog.rev(node))
135 for node in rbheads))
135 for node in rbheads))
136 cache = branchcache(bm,
136 cache = branchcache(bm,
137 repo[rtiprev].node(),
137 repo[rtiprev].node(),
138 rtiprev,
138 rtiprev,
139 closednodes=closed)
139 closednodes=closed)
140
140
141 # Try to stick it as low as possible
141 # Try to stick it as low as possible
142 # filter above served are unlikely to be fetch from a clone
142 # filter above served are unlikely to be fetch from a clone
143 for candidate in ('base', 'immutable', 'served'):
143 for candidate in ('base', 'immutable', 'served'):
144 rview = repo.filtered(candidate)
144 rview = repo.filtered(candidate)
145 if cache.validfor(rview):
145 if cache.validfor(rview):
146 repo._branchcaches[candidate] = cache
146 repo._branchcaches[candidate] = cache
147 cache.write(rview)
147 cache.write(rview)
148 break
148 break
149
149
150 class branchcache(dict):
150 class branchcache(dict):
151 """A dict like object that hold branches heads cache.
151 """A dict like object that hold branches heads cache.
152
152
153 This cache is used to avoid costly computations to determine all the
153 This cache is used to avoid costly computations to determine all the
154 branch heads of a repo.
154 branch heads of a repo.
155
155
156 The cache is serialized on disk in the following format:
156 The cache is serialized on disk in the following format:
157
157
158 <tip hex node> <tip rev number> [optional filtered repo hex hash]
158 <tip hex node> <tip rev number> [optional filtered repo hex hash]
159 <branch head hex node> <open/closed state> <branch name>
159 <branch head hex node> <open/closed state> <branch name>
160 <branch head hex node> <open/closed state> <branch name>
160 <branch head hex node> <open/closed state> <branch name>
161 ...
161 ...
162
162
163 The first line is used to check if the cache is still valid. If the
163 The first line is used to check if the cache is still valid. If the
164 branch cache is for a filtered repo view, an optional third hash is
164 branch cache is for a filtered repo view, an optional third hash is
165 included that hashes the hashes of all filtered revisions.
165 included that hashes the hashes of all filtered revisions.
166
166
167 The open/closed state is represented by a single letter 'o' or 'c'.
167 The open/closed state is represented by a single letter 'o' or 'c'.
168 This field can be used to avoid changelog reads when determining if a
168 This field can be used to avoid changelog reads when determining if a
169 branch head closes a branch or not.
169 branch head closes a branch or not.
170 """
170 """
171
171
172 def __init__(self, entries=(), tipnode=nullid, tiprev=nullrev,
172 def __init__(self, entries=(), tipnode=nullid, tiprev=nullrev,
173 filteredhash=None, closednodes=None):
173 filteredhash=None, closednodes=None):
174 super(branchcache, self).__init__(entries)
174 super(branchcache, self).__init__(entries)
175 self.tipnode = tipnode
175 self.tipnode = tipnode
176 self.tiprev = tiprev
176 self.tiprev = tiprev
177 self.filteredhash = filteredhash
177 self.filteredhash = filteredhash
178 # closednodes is a set of nodes that close their branch. If the branch
178 # closednodes is a set of nodes that close their branch. If the branch
179 # cache has been updated, it may contain nodes that are no longer
179 # cache has been updated, it may contain nodes that are no longer
180 # heads.
180 # heads.
181 if closednodes is None:
181 if closednodes is None:
182 self._closednodes = set()
182 self._closednodes = set()
183 else:
183 else:
184 self._closednodes = closednodes
184 self._closednodes = closednodes
185
185
186 def validfor(self, repo):
186 def validfor(self, repo):
187 """Is the cache content valid regarding a repo
187 """Is the cache content valid regarding a repo
188
188
189 - False when cached tipnode is unknown or if we detect a strip.
189 - False when cached tipnode is unknown or if we detect a strip.
190 - True when cache is up to date or a subset of current repo."""
190 - True when cache is up to date or a subset of current repo."""
191 try:
191 try:
192 return ((self.tipnode == repo.changelog.node(self.tiprev))
192 return ((self.tipnode == repo.changelog.node(self.tiprev))
193 and (self.filteredhash == \
193 and (self.filteredhash == \
194 scmutil.filteredhash(repo, self.tiprev)))
194 scmutil.filteredhash(repo, self.tiprev)))
195 except IndexError:
195 except IndexError:
196 return False
196 return False
197
197
198 def _branchtip(self, heads):
198 def _branchtip(self, heads):
199 '''Return tuple with last open head in heads and false,
199 '''Return tuple with last open head in heads and false,
200 otherwise return last closed head and true.'''
200 otherwise return last closed head and true.'''
201 tip = heads[-1]
201 tip = heads[-1]
202 closed = True
202 closed = True
203 for h in reversed(heads):
203 for h in reversed(heads):
204 if h not in self._closednodes:
204 if h not in self._closednodes:
205 tip = h
205 tip = h
206 closed = False
206 closed = False
207 break
207 break
208 return tip, closed
208 return tip, closed
209
209
210 def branchtip(self, branch):
210 def branchtip(self, branch):
211 '''Return the tipmost open head on branch head, otherwise return the
211 '''Return the tipmost open head on branch head, otherwise return the
212 tipmost closed head on branch.
212 tipmost closed head on branch.
213 Raise KeyError for unknown branch.'''
213 Raise KeyError for unknown branch.'''
214 return self._branchtip(self[branch])[0]
214 return self._branchtip(self[branch])[0]
215
215
216 def branchheads(self, branch, closed=False):
216 def branchheads(self, branch, closed=False):
217 heads = self[branch]
217 heads = self[branch]
218 if not closed:
218 if not closed:
219 heads = [h for h in heads if h not in self._closednodes]
219 heads = [h for h in heads if h not in self._closednodes]
220 return heads
220 return heads
221
221
222 def iterbranches(self):
222 def iterbranches(self):
223 for bn, heads in self.iteritems():
223 for bn, heads in self.iteritems():
224 yield (bn, heads) + self._branchtip(heads)
224 yield (bn, heads) + self._branchtip(heads)
225
225
226 def copy(self):
226 def copy(self):
227 """return an deep copy of the branchcache object"""
227 """return an deep copy of the branchcache object"""
228 return branchcache(self, self.tipnode, self.tiprev, self.filteredhash,
228 return branchcache(self, self.tipnode, self.tiprev, self.filteredhash,
229 self._closednodes)
229 self._closednodes)
230
230
231 def write(self, repo):
231 def write(self, repo):
232 try:
232 try:
233 f = repo.vfs(_filename(repo), "w", atomictemp=True)
233 f = repo.vfs(_filename(repo), "w", atomictemp=True)
234 cachekey = [hex(self.tipnode), '%d' % self.tiprev]
234 cachekey = [hex(self.tipnode), '%d' % self.tiprev]
235 if self.filteredhash is not None:
235 if self.filteredhash is not None:
236 cachekey.append(hex(self.filteredhash))
236 cachekey.append(hex(self.filteredhash))
237 f.write(" ".join(cachekey) + '\n')
237 f.write(" ".join(cachekey) + '\n')
238 nodecount = 0
238 nodecount = 0
239 for label, nodes in sorted(self.iteritems()):
239 for label, nodes in sorted(self.iteritems()):
240 for node in nodes:
240 for node in nodes:
241 nodecount += 1
241 nodecount += 1
242 if node in self._closednodes:
242 if node in self._closednodes:
243 state = 'c'
243 state = 'c'
244 else:
244 else:
245 state = 'o'
245 state = 'o'
246 f.write("%s %s %s\n" % (hex(node), state,
246 f.write("%s %s %s\n" % (hex(node), state,
247 encoding.fromlocal(label)))
247 encoding.fromlocal(label)))
248 f.close()
248 f.close()
249 repo.ui.log('branchcache',
249 repo.ui.log('branchcache',
250 'wrote %s branch cache with %d labels and %d nodes\n',
250 'wrote %s branch cache with %d labels and %d nodes\n',
251 repo.filtername, len(self), nodecount)
251 repo.filtername, len(self), nodecount)
252 except (IOError, OSError, error.Abort) as inst:
252 except (IOError, OSError, error.Abort) as inst:
253 repo.ui.debug("couldn't write branch cache: %s\n" % inst)
253 repo.ui.debug("couldn't write branch cache: %s\n" % inst)
254 # Abort may be raise by read only opener
254 # Abort may be raise by read only opener
255 pass
255 pass
256
256
257 def update(self, repo, revgen):
257 def update(self, repo, revgen):
258 """Given a branchhead cache, self, that may have extra nodes or be
258 """Given a branchhead cache, self, that may have extra nodes or be
259 missing heads, and a generator of nodes that are strictly a superset of
259 missing heads, and a generator of nodes that are strictly a superset of
260 heads missing, this function updates self to be correct.
260 heads missing, this function updates self to be correct.
261 """
261 """
262 starttime = util.timer()
262 starttime = util.timer()
263 cl = repo.changelog
263 cl = repo.changelog
264 # collect new branch entries
264 # collect new branch entries
265 newbranches = {}
265 newbranches = {}
266 getbranchinfo = repo.revbranchcache().branchinfo
266 getbranchinfo = repo.revbranchcache().branchinfo
267 for r in revgen:
267 for r in revgen:
268 branch, closesbranch = getbranchinfo(r)
268 branch, closesbranch = getbranchinfo(r)
269 newbranches.setdefault(branch, []).append(r)
269 newbranches.setdefault(branch, []).append(r)
270 if closesbranch:
270 if closesbranch:
271 self._closednodes.add(cl.node(r))
271 self._closednodes.add(cl.node(r))
272
272
273 # fetch current topological heads to speed up filtering
273 # fetch current topological heads to speed up filtering
274 topoheads = set(cl.headrevs())
274 topoheads = set(cl.headrevs())
275
275
276 # if older branchheads are reachable from new ones, they aren't
276 # if older branchheads are reachable from new ones, they aren't
277 # really branchheads. Note checking parents is insufficient:
277 # really branchheads. Note checking parents is insufficient:
278 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
278 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
279 for branch, newheadrevs in newbranches.iteritems():
279 for branch, newheadrevs in newbranches.iteritems():
280 bheads = self.setdefault(branch, [])
280 bheads = self.setdefault(branch, [])
281 bheadset = set(cl.rev(node) for node in bheads)
281 bheadset = set(cl.rev(node) for node in bheads)
282
282
283 # This have been tested True on all internal usage of this function.
283 # This have been tested True on all internal usage of this function.
284 # run it again in case of doubt
284 # run it again in case of doubt
285 # assert not (set(bheadrevs) & set(newheadrevs))
285 # assert not (set(bheadrevs) & set(newheadrevs))
286 newheadrevs.sort()
286 newheadrevs.sort()
287 bheadset.update(newheadrevs)
287 bheadset.update(newheadrevs)
288
288
289 # This prunes out two kinds of heads - heads that are superseded by
289 # This prunes out two kinds of heads - heads that are superseded by
290 # a head in newheadrevs, and newheadrevs that are not heads because
290 # a head in newheadrevs, and newheadrevs that are not heads because
291 # an existing head is their descendant.
291 # an existing head is their descendant.
292 uncertain = bheadset - topoheads
292 uncertain = bheadset - topoheads
293 if uncertain:
293 if uncertain:
294 floorrev = min(uncertain)
294 floorrev = min(uncertain)
295 ancestors = set(cl.ancestors(newheadrevs, floorrev))
295 ancestors = set(cl.ancestors(newheadrevs, floorrev))
296 bheadset -= ancestors
296 bheadset -= ancestors
297 bheadrevs = sorted(bheadset)
297 bheadrevs = sorted(bheadset)
298 self[branch] = [cl.node(rev) for rev in bheadrevs]
298 self[branch] = [cl.node(rev) for rev in bheadrevs]
299 tiprev = bheadrevs[-1]
299 tiprev = bheadrevs[-1]
300 if tiprev > self.tiprev:
300 if tiprev > self.tiprev:
301 self.tipnode = cl.node(tiprev)
301 self.tipnode = cl.node(tiprev)
302 self.tiprev = tiprev
302 self.tiprev = tiprev
303
303
304 if not self.validfor(repo):
304 if not self.validfor(repo):
305 # cache key are not valid anymore
305 # cache key are not valid anymore
306 self.tipnode = nullid
306 self.tipnode = nullid
307 self.tiprev = nullrev
307 self.tiprev = nullrev
308 for heads in self.values():
308 for heads in self.values():
309 tiprev = max(cl.rev(node) for node in heads)
309 tiprev = max(cl.rev(node) for node in heads)
310 if tiprev > self.tiprev:
310 if tiprev > self.tiprev:
311 self.tipnode = cl.node(tiprev)
311 self.tipnode = cl.node(tiprev)
312 self.tiprev = tiprev
312 self.tiprev = tiprev
313 self.filteredhash = scmutil.filteredhash(repo, self.tiprev)
313 self.filteredhash = scmutil.filteredhash(repo, self.tiprev)
314
314
315 duration = util.timer() - starttime
315 duration = util.timer() - starttime
316 repo.ui.log('branchcache', 'updated %s branch cache in %.4f seconds\n',
316 repo.ui.log('branchcache', 'updated %s branch cache in %.4f seconds\n',
317 repo.filtername, duration)
317 repo.filtername, duration)
318
318
319 # Revision branch info cache
319 # Revision branch info cache
320
320
321 _rbcversion = '-v1'
321 _rbcversion = '-v1'
322 _rbcnames = 'cache/rbc-names' + _rbcversion
322 _rbcnames = 'cache/rbc-names' + _rbcversion
323 _rbcrevs = 'cache/rbc-revs' + _rbcversion
323 _rbcrevs = 'cache/rbc-revs' + _rbcversion
324 # [4 byte hash prefix][4 byte branch name number with sign bit indicating open]
324 # [4 byte hash prefix][4 byte branch name number with sign bit indicating open]
325 _rbcrecfmt = '>4sI'
325 _rbcrecfmt = '>4sI'
326 _rbcrecsize = calcsize(_rbcrecfmt)
326 _rbcrecsize = calcsize(_rbcrecfmt)
327 _rbcnodelen = 4
327 _rbcnodelen = 4
328 _rbcbranchidxmask = 0x7fffffff
328 _rbcbranchidxmask = 0x7fffffff
329 _rbccloseflag = 0x80000000
329 _rbccloseflag = 0x80000000
330
330
331 class revbranchcache(object):
331 class revbranchcache(object):
332 """Persistent cache, mapping from revision number to branch name and close.
332 """Persistent cache, mapping from revision number to branch name and close.
333 This is a low level cache, independent of filtering.
333 This is a low level cache, independent of filtering.
334
334
335 Branch names are stored in rbc-names in internal encoding separated by 0.
335 Branch names are stored in rbc-names in internal encoding separated by 0.
336 rbc-names is append-only, and each branch name is only stored once and will
336 rbc-names is append-only, and each branch name is only stored once and will
337 thus have a unique index.
337 thus have a unique index.
338
338
339 The branch info for each revision is stored in rbc-revs as constant size
339 The branch info for each revision is stored in rbc-revs as constant size
340 records. The whole file is read into memory, but it is only 'parsed' on
340 records. The whole file is read into memory, but it is only 'parsed' on
341 demand. The file is usually append-only but will be truncated if repo
341 demand. The file is usually append-only but will be truncated if repo
342 modification is detected.
342 modification is detected.
343 The record for each revision contains the first 4 bytes of the
343 The record for each revision contains the first 4 bytes of the
344 corresponding node hash, and the record is only used if it still matches.
344 corresponding node hash, and the record is only used if it still matches.
345 Even a completely trashed rbc-revs fill thus still give the right result
345 Even a completely trashed rbc-revs fill thus still give the right result
346 while converging towards full recovery ... assuming no incorrectly matching
346 while converging towards full recovery ... assuming no incorrectly matching
347 node hashes.
347 node hashes.
348 The record also contains 4 bytes where 31 bits contains the index of the
348 The record also contains 4 bytes where 31 bits contains the index of the
349 branch and the last bit indicate that it is a branch close commit.
349 branch and the last bit indicate that it is a branch close commit.
350 The usage pattern for rbc-revs is thus somewhat similar to 00changelog.i
350 The usage pattern for rbc-revs is thus somewhat similar to 00changelog.i
351 and will grow with it but be 1/8th of its size.
351 and will grow with it but be 1/8th of its size.
352 """
352 """
353
353
354 def __init__(self, repo, readonly=True):
354 def __init__(self, repo, readonly=True):
355 assert repo.filtername is None
355 assert repo.filtername is None
356 self._repo = repo
356 self._repo = repo
357 self._names = [] # branch names in local encoding with static index
357 self._names = [] # branch names in local encoding with static index
358 self._rbcrevs = bytearray()
358 self._rbcrevs = bytearray()
359 self._rbcsnameslen = 0 # length of names read at _rbcsnameslen
359 self._rbcsnameslen = 0 # length of names read at _rbcsnameslen
360 try:
360 try:
361 bndata = repo.vfs.read(_rbcnames)
361 bndata = repo.vfs.read(_rbcnames)
362 self._rbcsnameslen = len(bndata) # for verification before writing
362 self._rbcsnameslen = len(bndata) # for verification before writing
363 self._names = [encoding.tolocal(bn) for bn in bndata.split('\0')]
363 if bndata:
364 self._names = [encoding.tolocal(bn)
365 for bn in bndata.split('\0')]
364 except (IOError, OSError):
366 except (IOError, OSError):
365 if readonly:
367 if readonly:
366 # don't try to use cache - fall back to the slow path
368 # don't try to use cache - fall back to the slow path
367 self.branchinfo = self._branchinfo
369 self.branchinfo = self._branchinfo
368
370
369 if self._names:
371 if self._names:
370 try:
372 try:
371 data = repo.vfs.read(_rbcrevs)
373 data = repo.vfs.read(_rbcrevs)
372 self._rbcrevs[:] = data
374 self._rbcrevs[:] = data
373 except (IOError, OSError) as inst:
375 except (IOError, OSError) as inst:
374 repo.ui.debug("couldn't read revision branch cache: %s\n" %
376 repo.ui.debug("couldn't read revision branch cache: %s\n" %
375 inst)
377 inst)
376 # remember number of good records on disk
378 # remember number of good records on disk
377 self._rbcrevslen = min(len(self._rbcrevs) // _rbcrecsize,
379 self._rbcrevslen = min(len(self._rbcrevs) // _rbcrecsize,
378 len(repo.changelog))
380 len(repo.changelog))
379 if self._rbcrevslen == 0:
381 if self._rbcrevslen == 0:
380 self._names = []
382 self._names = []
381 self._rbcnamescount = len(self._names) # number of names read at
383 self._rbcnamescount = len(self._names) # number of names read at
382 # _rbcsnameslen
384 # _rbcsnameslen
383 self._namesreverse = dict((b, r) for r, b in enumerate(self._names))
385 self._namesreverse = dict((b, r) for r, b in enumerate(self._names))
384
386
385 def _clear(self):
387 def _clear(self):
386 self._rbcsnameslen = 0
388 self._rbcsnameslen = 0
387 del self._names[:]
389 del self._names[:]
388 self._rbcnamescount = 0
390 self._rbcnamescount = 0
389 self._namesreverse.clear()
391 self._namesreverse.clear()
390 self._rbcrevslen = len(self._repo.changelog)
392 self._rbcrevslen = len(self._repo.changelog)
391 self._rbcrevs = bytearray(self._rbcrevslen * _rbcrecsize)
393 self._rbcrevs = bytearray(self._rbcrevslen * _rbcrecsize)
392
394
393 def branchinfo(self, rev):
395 def branchinfo(self, rev):
394 """Return branch name and close flag for rev, using and updating
396 """Return branch name and close flag for rev, using and updating
395 persistent cache."""
397 persistent cache."""
396 changelog = self._repo.changelog
398 changelog = self._repo.changelog
397 rbcrevidx = rev * _rbcrecsize
399 rbcrevidx = rev * _rbcrecsize
398
400
399 # avoid negative index, changelog.read(nullrev) is fast without cache
401 # avoid negative index, changelog.read(nullrev) is fast without cache
400 if rev == nullrev:
402 if rev == nullrev:
401 return changelog.branchinfo(rev)
403 return changelog.branchinfo(rev)
402
404
403 # if requested rev isn't allocated, grow and cache the rev info
405 # if requested rev isn't allocated, grow and cache the rev info
404 if len(self._rbcrevs) < rbcrevidx + _rbcrecsize:
406 if len(self._rbcrevs) < rbcrevidx + _rbcrecsize:
405 return self._branchinfo(rev)
407 return self._branchinfo(rev)
406
408
407 # fast path: extract data from cache, use it if node is matching
409 # fast path: extract data from cache, use it if node is matching
408 reponode = changelog.node(rev)[:_rbcnodelen]
410 reponode = changelog.node(rev)[:_rbcnodelen]
409 cachenode, branchidx = unpack_from(_rbcrecfmt, self._rbcrevs, rbcrevidx)
411 cachenode, branchidx = unpack_from(_rbcrecfmt, self._rbcrevs, rbcrevidx)
410 close = bool(branchidx & _rbccloseflag)
412 close = bool(branchidx & _rbccloseflag)
411 if close:
413 if close:
412 branchidx &= _rbcbranchidxmask
414 branchidx &= _rbcbranchidxmask
413 if cachenode == '\0\0\0\0':
415 if cachenode == '\0\0\0\0':
414 pass
416 pass
415 elif cachenode == reponode:
417 elif cachenode == reponode:
416 try:
418 try:
417 return self._names[branchidx], close
419 return self._names[branchidx], close
418 except IndexError:
420 except IndexError:
419 # recover from invalid reference to unknown branch
421 # recover from invalid reference to unknown branch
420 self._repo.ui.debug("referenced branch names not found"
422 self._repo.ui.debug("referenced branch names not found"
421 " - rebuilding revision branch cache from scratch\n")
423 " - rebuilding revision branch cache from scratch\n")
422 self._clear()
424 self._clear()
423 else:
425 else:
424 # rev/node map has changed, invalidate the cache from here up
426 # rev/node map has changed, invalidate the cache from here up
425 self._repo.ui.debug("history modification detected - truncating "
427 self._repo.ui.debug("history modification detected - truncating "
426 "revision branch cache to revision %s\n" % rev)
428 "revision branch cache to revision %s\n" % rev)
427 truncate = rbcrevidx + _rbcrecsize
429 truncate = rbcrevidx + _rbcrecsize
428 del self._rbcrevs[truncate:]
430 del self._rbcrevs[truncate:]
429 self._rbcrevslen = min(self._rbcrevslen, truncate)
431 self._rbcrevslen = min(self._rbcrevslen, truncate)
430
432
431 # fall back to slow path and make sure it will be written to disk
433 # fall back to slow path and make sure it will be written to disk
432 return self._branchinfo(rev)
434 return self._branchinfo(rev)
433
435
434 def _branchinfo(self, rev):
436 def _branchinfo(self, rev):
435 """Retrieve branch info from changelog and update _rbcrevs"""
437 """Retrieve branch info from changelog and update _rbcrevs"""
436 changelog = self._repo.changelog
438 changelog = self._repo.changelog
437 b, close = changelog.branchinfo(rev)
439 b, close = changelog.branchinfo(rev)
438 if b in self._namesreverse:
440 if b in self._namesreverse:
439 branchidx = self._namesreverse[b]
441 branchidx = self._namesreverse[b]
440 else:
442 else:
441 branchidx = len(self._names)
443 branchidx = len(self._names)
442 self._names.append(b)
444 self._names.append(b)
443 self._namesreverse[b] = branchidx
445 self._namesreverse[b] = branchidx
444 reponode = changelog.node(rev)
446 reponode = changelog.node(rev)
445 if close:
447 if close:
446 branchidx |= _rbccloseflag
448 branchidx |= _rbccloseflag
447 self._setcachedata(rev, reponode, branchidx)
449 self._setcachedata(rev, reponode, branchidx)
448 return b, close
450 return b, close
449
451
450 def _setcachedata(self, rev, node, branchidx):
452 def _setcachedata(self, rev, node, branchidx):
451 """Writes the node's branch data to the in-memory cache data."""
453 """Writes the node's branch data to the in-memory cache data."""
452 rbcrevidx = rev * _rbcrecsize
454 rbcrevidx = rev * _rbcrecsize
453 if len(self._rbcrevs) < rbcrevidx + _rbcrecsize:
455 if len(self._rbcrevs) < rbcrevidx + _rbcrecsize:
454 self._rbcrevs.extend('\0' *
456 self._rbcrevs.extend('\0' *
455 (len(self._repo.changelog) * _rbcrecsize -
457 (len(self._repo.changelog) * _rbcrecsize -
456 len(self._rbcrevs)))
458 len(self._rbcrevs)))
457 pack_into(_rbcrecfmt, self._rbcrevs, rbcrevidx, node, branchidx)
459 pack_into(_rbcrecfmt, self._rbcrevs, rbcrevidx, node, branchidx)
458 self._rbcrevslen = min(self._rbcrevslen, rev)
460 self._rbcrevslen = min(self._rbcrevslen, rev)
459
461
460 tr = self._repo.currenttransaction()
462 tr = self._repo.currenttransaction()
461 if tr:
463 if tr:
462 tr.addfinalize('write-revbranchcache', self.write)
464 tr.addfinalize('write-revbranchcache', self.write)
463
465
464 def write(self, tr=None):
466 def write(self, tr=None):
465 """Save branch cache if it is dirty."""
467 """Save branch cache if it is dirty."""
466 repo = self._repo
468 repo = self._repo
467 wlock = None
469 wlock = None
468 step = ''
470 step = ''
469 try:
471 try:
470 if self._rbcnamescount < len(self._names):
472 if self._rbcnamescount < len(self._names):
471 step = ' names'
473 step = ' names'
472 wlock = repo.wlock(wait=False)
474 wlock = repo.wlock(wait=False)
473 if self._rbcnamescount != 0:
475 if self._rbcnamescount != 0:
474 f = repo.vfs.open(_rbcnames, 'ab')
476 f = repo.vfs.open(_rbcnames, 'ab')
475 if f.tell() == self._rbcsnameslen:
477 if f.tell() == self._rbcsnameslen:
476 f.write('\0')
478 f.write('\0')
477 else:
479 else:
478 f.close()
480 f.close()
479 repo.ui.debug("%s changed - rewriting it\n" % _rbcnames)
481 repo.ui.debug("%s changed - rewriting it\n" % _rbcnames)
480 self._rbcnamescount = 0
482 self._rbcnamescount = 0
481 self._rbcrevslen = 0
483 self._rbcrevslen = 0
482 if self._rbcnamescount == 0:
484 if self._rbcnamescount == 0:
483 # before rewriting names, make sure references are removed
485 # before rewriting names, make sure references are removed
484 repo.vfs.unlinkpath(_rbcrevs, ignoremissing=True)
486 repo.vfs.unlinkpath(_rbcrevs, ignoremissing=True)
485 f = repo.vfs.open(_rbcnames, 'wb')
487 f = repo.vfs.open(_rbcnames, 'wb')
486 f.write('\0'.join(encoding.fromlocal(b)
488 f.write('\0'.join(encoding.fromlocal(b)
487 for b in self._names[self._rbcnamescount:]))
489 for b in self._names[self._rbcnamescount:]))
488 self._rbcsnameslen = f.tell()
490 self._rbcsnameslen = f.tell()
489 f.close()
491 f.close()
490 self._rbcnamescount = len(self._names)
492 self._rbcnamescount = len(self._names)
491
493
492 start = self._rbcrevslen * _rbcrecsize
494 start = self._rbcrevslen * _rbcrecsize
493 if start != len(self._rbcrevs):
495 if start != len(self._rbcrevs):
494 step = ''
496 step = ''
495 if wlock is None:
497 if wlock is None:
496 wlock = repo.wlock(wait=False)
498 wlock = repo.wlock(wait=False)
497 revs = min(len(repo.changelog),
499 revs = min(len(repo.changelog),
498 len(self._rbcrevs) // _rbcrecsize)
500 len(self._rbcrevs) // _rbcrecsize)
499 f = repo.vfs.open(_rbcrevs, 'ab')
501 f = repo.vfs.open(_rbcrevs, 'ab')
500 if f.tell() != start:
502 if f.tell() != start:
501 repo.ui.debug("truncating %s to %s\n" % (_rbcrevs, start))
503 repo.ui.debug("truncating %s to %s\n" % (_rbcrevs, start))
502 f.seek(start)
504 f.seek(start)
503 if f.tell() != start:
505 if f.tell() != start:
504 start = 0
506 start = 0
505 f.seek(start)
507 f.seek(start)
506 f.truncate()
508 f.truncate()
507 end = revs * _rbcrecsize
509 end = revs * _rbcrecsize
508 f.write(self._rbcrevs[start:end])
510 f.write(self._rbcrevs[start:end])
509 f.close()
511 f.close()
510 self._rbcrevslen = revs
512 self._rbcrevslen = revs
511 except (IOError, OSError, error.Abort, error.LockError) as inst:
513 except (IOError, OSError, error.Abort, error.LockError) as inst:
512 repo.ui.debug("couldn't write revision branch cache%s: %s\n"
514 repo.ui.debug("couldn't write revision branch cache%s: %s\n"
513 % (step, inst))
515 % (step, inst))
514 finally:
516 finally:
515 if wlock is not None:
517 if wlock is not None:
516 wlock.release()
518 wlock.release()
General Comments 0
You need to be logged in to leave comments. Login now