Mercurial > jhg
comparison hg4j/src/main/java/org/tmatesoft/hg/repo/Revlog.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
author | Alexander Kitaev <kitaev@gmail.com> |
---|---|
date | Tue, 10 May 2011 10:52:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
212:edb2e2829352 | 213:6ec4af642ba8 |
---|---|
1 /* | |
2 * Copyright (c) 2010-2011 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.repo; | |
18 | |
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; | |
20 import static org.tmatesoft.hg.repo.HgRepository.TIP; | |
21 | |
22 import java.io.IOException; | |
23 import java.nio.ByteBuffer; | |
24 import java.util.Arrays; | |
25 import java.util.Collection; | |
26 import java.util.HashSet; | |
27 import java.util.LinkedList; | |
28 import java.util.List; | |
29 | |
30 import org.tmatesoft.hg.core.HgBadStateException; | |
31 import org.tmatesoft.hg.core.HgException; | |
32 import org.tmatesoft.hg.core.Nodeid; | |
33 import org.tmatesoft.hg.internal.DataAccess; | |
34 import org.tmatesoft.hg.internal.RevlogStream; | |
35 import org.tmatesoft.hg.util.ByteChannel; | |
36 import org.tmatesoft.hg.util.CancelSupport; | |
37 import org.tmatesoft.hg.util.CancelledException; | |
38 import org.tmatesoft.hg.util.ProgressSupport; | |
39 | |
40 | |
41 /** | |
42 * Base class for all Mercurial entities that are serialized in a so called revlog format (changelog, manifest, data files). | |
43 * | |
44 * Implementation note: | |
45 * Hides actual actual revlog stream implementation and its access methods (i.e. RevlogStream.Inspector), iow shall not expose anything internal | |
46 * in public methods. | |
47 * | |
48 * @author Artem Tikhomirov | |
49 * @author TMate Software Ltd. | |
50 */ | |
51 abstract class Revlog { | |
52 | |
53 private final HgRepository repo; | |
54 protected final RevlogStream content; | |
55 | |
56 protected Revlog(HgRepository hgRepo, RevlogStream contentStream) { | |
57 if (hgRepo == null) { | |
58 throw new IllegalArgumentException(); | |
59 } | |
60 if (contentStream == null) { | |
61 throw new IllegalArgumentException(); | |
62 } | |
63 repo = hgRepo; | |
64 content = contentStream; | |
65 } | |
66 | |
67 // invalid Revlog | |
68 protected Revlog(HgRepository hgRepo) { | |
69 repo = hgRepo; | |
70 content = null; | |
71 } | |
72 | |
73 public final HgRepository getRepo() { | |
74 return repo; | |
75 } | |
76 | |
77 public final int getRevisionCount() { | |
78 return content.revisionCount(); | |
79 } | |
80 | |
81 public final int getLastRevision() { | |
82 return content.revisionCount() - 1; | |
83 } | |
84 | |
85 public final Nodeid getRevision(int revision) { | |
86 // XXX cache nodeids? | |
87 return Nodeid.fromBinary(content.nodeid(revision), 0); | |
88 } | |
89 | |
90 public final int getLocalRevision(Nodeid nid) { | |
91 int revision = content.findLocalRevisionNumber(nid); | |
92 if (revision == BAD_REVISION) { | |
93 throw new IllegalArgumentException(String.format("%s doesn't represent a revision of %s", nid.toString(), this /*XXX HgDataFile.getPath might be more suitable here*/)); | |
94 } | |
95 return revision; | |
96 } | |
97 | |
98 // Till now, i follow approach that NULL nodeid is never part of revlog | |
99 public final boolean isKnown(Nodeid nodeid) { | |
100 final int rn = content.findLocalRevisionNumber(nodeid); | |
101 if (Integer.MIN_VALUE == rn) { | |
102 return false; | |
103 } | |
104 if (rn < 0 || rn >= content.revisionCount()) { | |
105 // Sanity check | |
106 throw new IllegalStateException(); | |
107 } | |
108 return true; | |
109 } | |
110 | |
111 /** | |
112 * Access to revision data as is (decompressed, but otherwise unprocessed, i.e. not parsed for e.g. changeset or manifest entries) | |
113 * @param nodeid | |
114 */ | |
115 protected void rawContent(Nodeid nodeid, ByteChannel sink) throws HgException, IOException, CancelledException { | |
116 rawContent(getLocalRevision(nodeid), sink); | |
117 } | |
118 | |
119 /** | |
120 * @param revision - repo-local index of this file change (not a changelog revision number!) | |
121 */ | |
122 protected void rawContent(int revision, ByteChannel sink) throws HgException, IOException, CancelledException { | |
123 if (sink == null) { | |
124 throw new IllegalArgumentException(); | |
125 } | |
126 ContentPipe insp = new ContentPipe(sink, 0); | |
127 insp.checkCancelled(); | |
128 content.iterate(revision, revision, true, insp); | |
129 insp.checkFailed(); | |
130 } | |
131 | |
132 /** | |
133 * XXX perhaps, return value Nodeid[2] and boolean needNodeids is better (and higher level) API for this query? | |
134 * | |
135 * @param revision - revision to query parents, or {@link HgRepository#TIP} | |
136 * @param parentRevisions - int[2] to get local revision numbers of parents (e.g. {6, -1}) | |
137 * @param parent1 - byte[20] or null, if parent's nodeid is not needed | |
138 * @param parent2 - byte[20] or null, if second parent's nodeid is not needed | |
139 * @return | |
140 */ | |
141 public void parents(int revision, int[] parentRevisions, byte[] parent1, byte[] parent2) { | |
142 if (revision != TIP && !(revision >= 0 && revision < content.revisionCount())) { | |
143 throw new IllegalArgumentException(String.valueOf(revision)); | |
144 } | |
145 if (parentRevisions == null || parentRevisions.length < 2) { | |
146 throw new IllegalArgumentException(String.valueOf(parentRevisions)); | |
147 } | |
148 if (parent1 != null && parent1.length < 20) { | |
149 throw new IllegalArgumentException(parent1.toString()); | |
150 } | |
151 if (parent2 != null && parent2.length < 20) { | |
152 throw new IllegalArgumentException(parent2.toString()); | |
153 } | |
154 class ParentCollector implements RevlogStream.Inspector { | |
155 public int p1 = -1; | |
156 public int p2 = -1; | |
157 public byte[] nodeid; | |
158 | |
159 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { | |
160 p1 = parent1Revision; | |
161 p2 = parent2Revision; | |
162 this.nodeid = new byte[20]; | |
163 // nodeid arg now comes in 32 byte from (as in file format description), however upper 12 bytes are zeros. | |
164 System.arraycopy(nodeid, nodeid.length > 20 ? nodeid.length - 20 : 0, this.nodeid, 0, 20); | |
165 } | |
166 }; | |
167 ParentCollector pc = new ParentCollector(); | |
168 content.iterate(revision, revision, false, pc); | |
169 parentRevisions[0] = pc.p1; | |
170 parentRevisions[1] = pc.p2; | |
171 if (parent1 != null) { | |
172 if (parentRevisions[0] == -1) { | |
173 Arrays.fill(parent1, 0, 20, (byte) 0); | |
174 } else { | |
175 content.iterate(parentRevisions[0], parentRevisions[0], false, pc); | |
176 System.arraycopy(pc.nodeid, 0, parent1, 0, 20); | |
177 } | |
178 } | |
179 if (parent2 != null) { | |
180 if (parentRevisions[1] == -1) { | |
181 Arrays.fill(parent2, 0, 20, (byte) 0); | |
182 } else { | |
183 content.iterate(parentRevisions[1], parentRevisions[1], false, pc); | |
184 System.arraycopy(pc.nodeid, 0, parent2, 0, 20); | |
185 } | |
186 } | |
187 } | |
188 | |
189 /* | |
190 * XXX think over if it's better to do either: | |
191 * pw = getChangelog().new ParentWalker(); pw.init() and pass pw instance around as needed | |
192 * or | |
193 * add Revlog#getParentWalker(), static class, make cons() and #init package-local, and keep SoftReference to allow walker reuse. | |
194 * | |
195 * and yes, walker is not a proper name | |
196 */ | |
197 public final class ParentWalker { | |
198 | |
199 | |
200 private Nodeid[] sequential; // natural repository order, childrenOf rely on ordering | |
201 private Nodeid[] sorted; // for binary search | |
202 private int[] sorted2natural; | |
203 private Nodeid[] firstParent; | |
204 private Nodeid[] secondParent; | |
205 | |
206 // Nodeid instances shall be shared between all arrays | |
207 | |
208 public ParentWalker() { | |
209 } | |
210 | |
211 public HgRepository getRepo() { | |
212 return Revlog.this.getRepo(); | |
213 } | |
214 | |
215 public void init() { | |
216 final RevlogStream stream = Revlog.this.content; | |
217 final int revisionCount = stream.revisionCount(); | |
218 firstParent = new Nodeid[revisionCount]; | |
219 // although branches/merges are less frequent, and most of secondParent would be -1/null, some sort of | |
220 // SparseOrderedList might be handy, provided its inner structures do not overweight simplicity of an array | |
221 secondParent = new Nodeid[revisionCount]; | |
222 // | |
223 sequential = new Nodeid[revisionCount]; | |
224 sorted = new Nodeid[revisionCount]; | |
225 | |
226 RevlogStream.Inspector insp = new RevlogStream.Inspector() { | |
227 | |
228 int ix = 0; | |
229 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { | |
230 if (ix != revisionNumber) { | |
231 // XXX temp code, just to make sure I understand what's going on here | |
232 throw new IllegalStateException(); | |
233 } | |
234 if (parent1Revision >= revisionNumber || parent2Revision >= revisionNumber) { | |
235 throw new IllegalStateException(); // sanity, revisions are sequential | |
236 } | |
237 final Nodeid nid = new Nodeid(nodeid, true); | |
238 sequential[ix] = sorted[ix] = nid; | |
239 if (parent1Revision != -1) { | |
240 assert parent1Revision < ix; | |
241 firstParent[ix] = sequential[parent1Revision]; | |
242 } | |
243 if (parent2Revision != -1) { // revlog of DataAccess.java has p2 set when p1 is -1 | |
244 assert parent2Revision < ix; | |
245 secondParent[ix] = sequential[parent2Revision]; | |
246 } | |
247 ix++; | |
248 } | |
249 }; | |
250 stream.iterate(0, TIP, false, insp); | |
251 Arrays.sort(sorted); | |
252 sorted2natural = new int[revisionCount]; | |
253 for (int i = 0; i < revisionCount; i++) { | |
254 Nodeid n = sequential[i]; | |
255 int x = Arrays.binarySearch(sorted, n); | |
256 assertSortedIndex(x); | |
257 sorted2natural[x] = i; | |
258 } | |
259 } | |
260 | |
261 private void assertSortedIndex(int x) { | |
262 if (x < 0) { | |
263 throw new HgBadStateException(); | |
264 } | |
265 } | |
266 | |
267 // FIXME need to decide whether Nodeid(00 * 20) is always known or not | |
268 // right now Nodeid.NULL is not recognized as known if passed to this method, | |
269 // caller is supposed to make explicit check | |
270 public boolean knownNode(Nodeid nid) { | |
271 return Arrays.binarySearch(sorted, nid) >= 0; | |
272 } | |
273 | |
274 /** | |
275 * null if none. only known nodes (as per #knownNode) are accepted as arguments | |
276 */ | |
277 public Nodeid firstParent(Nodeid nid) { | |
278 int x = Arrays.binarySearch(sorted, nid); | |
279 assertSortedIndex(x); | |
280 int i = sorted2natural[x]; | |
281 return firstParent[i]; | |
282 } | |
283 | |
284 // never null, Nodeid.NULL if none known | |
285 public Nodeid safeFirstParent(Nodeid nid) { | |
286 Nodeid rv = firstParent(nid); | |
287 return rv == null ? Nodeid.NULL : rv; | |
288 } | |
289 | |
290 public Nodeid secondParent(Nodeid nid) { | |
291 int x = Arrays.binarySearch(sorted, nid); | |
292 assertSortedIndex(x); | |
293 int i = sorted2natural[x]; | |
294 return secondParent[i]; | |
295 } | |
296 | |
297 public Nodeid safeSecondParent(Nodeid nid) { | |
298 Nodeid rv = secondParent(nid); | |
299 return rv == null ? Nodeid.NULL : rv; | |
300 } | |
301 | |
302 public boolean appendParentsOf(Nodeid nid, Collection<Nodeid> c) { | |
303 int x = Arrays.binarySearch(sorted, nid); | |
304 assertSortedIndex(x); | |
305 int i = sorted2natural[x]; | |
306 Nodeid p1 = firstParent[i]; | |
307 boolean modified = false; | |
308 if (p1 != null) { | |
309 modified = c.add(p1); | |
310 } | |
311 Nodeid p2 = secondParent[i]; | |
312 if (p2 != null) { | |
313 modified = c.add(p2) || modified; | |
314 } | |
315 return modified; | |
316 } | |
317 | |
318 // XXX alternative (and perhaps more reliable) approach would be to make a copy of allNodes and remove | |
319 // nodes, their parents and so on. | |
320 | |
321 // @return ordered collection of all children rooted at supplied nodes. Nodes shall not be descendants of each other! | |
322 // Nodeids shall belong to this revlog | |
323 public List<Nodeid> childrenOf(List<Nodeid> roots) { | |
324 HashSet<Nodeid> parents = new HashSet<Nodeid>(); | |
325 LinkedList<Nodeid> result = new LinkedList<Nodeid>(); | |
326 int earliestRevision = Integer.MAX_VALUE; | |
327 assert sequential.length == firstParent.length && firstParent.length == secondParent.length; | |
328 // first, find earliest index of roots in question, as there's no sense | |
329 // to check children among nodes prior to branch's root node | |
330 for (Nodeid r : roots) { | |
331 int x = Arrays.binarySearch(sorted, r); | |
332 assertSortedIndex(x); | |
333 int i = sorted2natural[x]; | |
334 if (i < earliestRevision) { | |
335 earliestRevision = i; | |
336 } | |
337 parents.add(sequential[i]); // add canonical instance in hope equals() is bit faster when can do a == | |
338 } | |
339 for (int i = earliestRevision + 1; i < sequential.length; i++) { | |
340 if (parents.contains(firstParent[i]) || parents.contains(secondParent[i])) { | |
341 parents.add(sequential[i]); // to find next child | |
342 result.add(sequential[i]); | |
343 } | |
344 } | |
345 return result; | |
346 } | |
347 | |
348 /** | |
349 * @param nid possibly parent node, shall be {@link #knownNode(Nodeid) known} in this revlog. | |
350 * @return <code>true</code> if there's any node in this revlog that has specified node as one of its parents. | |
351 */ | |
352 public boolean hasChildren(Nodeid nid) { | |
353 int x = Arrays.binarySearch(sorted, nid); | |
354 assertSortedIndex(x); | |
355 int i = sorted2natural[x]; | |
356 assert firstParent.length == secondParent.length; // just in case later I implement sparse array for secondParent | |
357 assert firstParent.length == sequential.length; | |
358 // to use == instead of equals, take the same Nodeid instance we used to fill all the arrays. | |
359 final Nodeid canonicalNode = sequential[i]; | |
360 i++; // no need to check node itself. child nodes may appear in sequential only after revision in question | |
361 for (; i < sequential.length; i++) { | |
362 // FIXME likely, not very effective. | |
363 // May want to optimize it with another (Tree|Hash)Set, created on demand on first use, | |
364 // however, need to be careful with memory usage | |
365 if (firstParent[i] == canonicalNode || secondParent[i] == canonicalNode) { | |
366 return true; | |
367 } | |
368 } | |
369 return false; | |
370 } | |
371 } | |
372 | |
373 protected static class ContentPipe implements RevlogStream.Inspector, CancelSupport { | |
374 private final ByteChannel sink; | |
375 private final CancelSupport cancelSupport; | |
376 private Exception failure; | |
377 private final int offset; | |
378 | |
379 /** | |
380 * @param _sink - cannot be <code>null</code> | |
381 * @param seekOffset - when positive, orders to pipe bytes to the sink starting from specified offset, not from the first byte available in DataAccess | |
382 */ | |
383 public ContentPipe(ByteChannel _sink, int seekOffset) { | |
384 assert _sink != null; | |
385 sink = _sink; | |
386 cancelSupport = CancelSupport.Factory.get(_sink); | |
387 offset = seekOffset; | |
388 } | |
389 | |
390 protected void prepare(int revisionNumber, DataAccess da) throws HgException, IOException { | |
391 if (offset > 0) { // save few useless reset/rewind operations | |
392 da.seek(offset); | |
393 } | |
394 } | |
395 | |
396 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { | |
397 try { | |
398 prepare(revisionNumber, da); // XXX perhaps, prepare shall return DA (sliced, if needed) | |
399 final ProgressSupport progressSupport = ProgressSupport.Factory.get(sink); | |
400 ByteBuffer buf = ByteBuffer.allocate(512); | |
401 progressSupport.start(da.length()); | |
402 while (!da.isEmpty()) { | |
403 cancelSupport.checkCancelled(); | |
404 da.readBytes(buf); | |
405 buf.flip(); | |
406 // XXX I may not rely on returned number of bytes but track change in buf position instead. | |
407 int consumed = sink.write(buf); | |
408 // FIXME in fact, bad sink implementation (that consumes no bytes) would result in endless loop. Need to account for this | |
409 buf.compact(); | |
410 progressSupport.worked(consumed); | |
411 } | |
412 progressSupport.done(); // XXX shall specify whether #done() is invoked always or only if completed successfully. | |
413 } catch (IOException ex) { | |
414 recordFailure(ex); | |
415 } catch (CancelledException ex) { | |
416 recordFailure(ex); | |
417 } catch (HgException ex) { | |
418 recordFailure(ex); | |
419 } | |
420 } | |
421 | |
422 public void checkCancelled() throws CancelledException { | |
423 cancelSupport.checkCancelled(); | |
424 } | |
425 | |
426 protected void recordFailure(Exception ex) { | |
427 assert failure == null; | |
428 failure = ex; | |
429 } | |
430 | |
431 public void checkFailed() throws HgException, IOException, CancelledException { | |
432 if (failure == null) { | |
433 return; | |
434 } | |
435 if (failure instanceof IOException) { | |
436 throw (IOException) failure; | |
437 } | |
438 if (failure instanceof CancelledException) { | |
439 throw (CancelledException) failure; | |
440 } | |
441 if (failure instanceof HgException) { | |
442 throw (HgException) failure; | |
443 } | |
444 throw new HgBadStateException(failure); | |
445 } | |
446 } | |
447 } |