tikhomirov@22: /* tikhomirov@534: * Copyright (c) 2010-2013 TMate Software Ltd tikhomirov@74: * tikhomirov@74: * This program is free software; you can redistribute it and/or modify tikhomirov@74: * it under the terms of the GNU General Public License as published by tikhomirov@74: * the Free Software Foundation; version 2 of the License. tikhomirov@74: * tikhomirov@74: * This program is distributed in the hope that it will be useful, tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@74: * GNU General Public License for more details. tikhomirov@74: * tikhomirov@74: * For information on how to redistribute this software under tikhomirov@74: * the terms of a license other than GNU General Public License tikhomirov@102: * contact TMate Software at support@hg4j.com tikhomirov@2: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@2: tikhomirov@425: import static org.tmatesoft.hg.repo.HgRepository.*; tikhomirov@456: import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; tikhomirov@56: tikhomirov@157: import java.io.IOException; tikhomirov@157: import java.nio.ByteBuffer; tikhomirov@317: import java.util.ArrayList; tikhomirov@56: import java.util.Arrays; tikhomirov@171: import java.util.List; tikhomirov@29: tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@157: import org.tmatesoft.hg.internal.DataAccess; tikhomirov@324: import org.tmatesoft.hg.internal.Experimental; tikhomirov@448: import org.tmatesoft.hg.internal.IntMap; tikhomirov@355: import org.tmatesoft.hg.internal.Preview; tikhomirov@600: import org.tmatesoft.hg.internal.RevisionLookup; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@324: import org.tmatesoft.hg.util.Adaptable; tikhomirov@157: import org.tmatesoft.hg.util.ByteChannel; tikhomirov@157: import org.tmatesoft.hg.util.CancelSupport; tikhomirov@157: import org.tmatesoft.hg.util.CancelledException; tikhomirov@355: import org.tmatesoft.hg.util.LogFacility; tikhomirov@157: import org.tmatesoft.hg.util.ProgressSupport; tikhomirov@74: tikhomirov@74: tikhomirov@2: /** tikhomirov@157: * Base class for all Mercurial entities that are serialized in a so called revlog format (changelog, manifest, data files). tikhomirov@157: * tikhomirov@157: * Implementation note: tikhomirov@157: * Hides actual actual revlog stream implementation and its access methods (i.e. RevlogStream.Inspector), iow shall not expose anything internal tikhomirov@157: * in public methods. tikhomirov@157: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@2: */ tikhomirov@74: abstract class Revlog { tikhomirov@2: tikhomirov@115: private final HgRepository repo; tikhomirov@21: protected final RevlogStream content; tikhomirov@600: protected final boolean useRevisionLookup; tikhomirov@600: protected RevisionLookup revisionLookup; tikhomirov@2: tikhomirov@600: protected Revlog(HgRepository hgRepo, RevlogStream contentStream, boolean needRevisionLookup) { tikhomirov@2: if (hgRepo == null) { tikhomirov@74: throw new IllegalArgumentException(); tikhomirov@74: } tikhomirov@115: if (contentStream == null) { tikhomirov@74: throw new IllegalArgumentException(); tikhomirov@2: } tikhomirov@115: repo = hgRepo; tikhomirov@115: content = contentStream; tikhomirov@600: useRevisionLookup = needRevisionLookup; tikhomirov@115: } tikhomirov@115: tikhomirov@115: // invalid Revlog tikhomirov@115: protected Revlog(HgRepository hgRepo) { tikhomirov@115: repo = hgRepo; tikhomirov@115: content = null; tikhomirov@600: useRevisionLookup = false; tikhomirov@2: } tikhomirov@2: tikhomirov@2: public final HgRepository getRepo() { tikhomirov@115: return repo; tikhomirov@2: } tikhomirov@2: tikhomirov@425: /** tikhomirov@425: * @return total number of revisions kept in this revlog tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@425: */ tikhomirov@425: public final int getRevisionCount() throws HgRuntimeException { tikhomirov@21: return content.revisionCount(); tikhomirov@21: } tikhomirov@80: tikhomirov@425: /** tikhomirov@534: * @return index of last known revision, a.k.a. {@link HgRepository#TIP}, or {@link HgRepository#NO_REVISION} if revlog is empty tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@425: */ tikhomirov@425: public final int getLastRevision() throws HgRuntimeException { tikhomirov@534: // although old code gives correct result when revlog is empty (NO_REVISION deliberately == -1), tikhomirov@534: // it's still better to be explicit tikhomirov@534: int revCount = content.revisionCount(); tikhomirov@534: return revCount == 0 ? NO_REVISION : revCount - 1; tikhomirov@135: } tikhomirov@354: tikhomirov@354: /** tikhomirov@388: * Map revision index to unique revision identifier (nodeid). tikhomirov@354: * tikhomirov@437: * @param revisionIndex index of the entry in this revlog, may be {@link HgRepository#TIP} tikhomirov@354: * @return revision nodeid of the entry tikhomirov@354: * tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@354: */ tikhomirov@437: public final Nodeid getRevision(int revisionIndex) throws HgRuntimeException { tikhomirov@328: // XXX cache nodeids? Rather, if context.getCache(this).getRevisionMap(create == false) != null, use it tikhomirov@437: return Nodeid.fromBinary(content.nodeid(revisionIndex), 0); tikhomirov@80: } tikhomirov@317: tikhomirov@328: /** tikhomirov@425: * Effective alternative to map few revision indexes to corresponding nodeids at once. tikhomirov@425: *

Note, there are few aspects to be careful about when using this method

tikhomirov@425: * @return list of mapped revisions in no particular order tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@328: */ tikhomirov@425: public final List getRevisions(int... revisions) throws HgRuntimeException { tikhomirov@317: ArrayList rv = new ArrayList(revisions.length); tikhomirov@317: Arrays.sort(revisions); tikhomirov@317: getRevisionsInternal(rv, revisions); tikhomirov@317: return rv; tikhomirov@317: } tikhomirov@317: tikhomirov@366: /*package-local*/ void getRevisionsInternal(final List retVal, int[] sortedRevs) throws HgInvalidRevisionException, HgInvalidControlFileException { tikhomirov@317: // once I have getRevisionMap and may find out whether it is avalable from cache, tikhomirov@317: // may use it, perhaps only for small number of revisions tikhomirov@317: content.iterate(sortedRevs, false, new RevlogStream.Inspector() { tikhomirov@317: tikhomirov@317: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) { tikhomirov@317: retVal.add(Nodeid.fromBinary(nodeid, 0)); tikhomirov@317: } tikhomirov@317: }); tikhomirov@317: } tikhomirov@37: tikhomirov@243: /** tikhomirov@368: * Get local index of the specified revision. tikhomirov@347: * If unsure, use {@link #isKnown(Nodeid)} to find out whether nodeid belongs to this revlog. tikhomirov@243: * tikhomirov@243: * For occasional queries, this method works with decent performance, despite its O(n/2) approach. tikhomirov@433: * Alternatively, if you need to perform multiple queries (e.g. at least 15-20), {@link HgRevisionMap} may come handy. tikhomirov@347: * tikhomirov@243: * @param nid revision to look up tikhomirov@347: * @return revision local index in this revlog tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@243: */ tikhomirov@425: public final int getRevisionIndex(Nodeid nid) throws HgRuntimeException { tikhomirov@600: // final long t1 = System.nanoTime(); tikhomirov@600: int revision; tikhomirov@600: if (useRevisionLookup) { tikhomirov@600: if (revisionLookup == null) { tikhomirov@600: revisionLookup = RevisionLookup.createFor(content); tikhomirov@600: } tikhomirov@600: revision = revisionLookup.findIndex(nid); tikhomirov@600: } else { tikhomirov@600: revision = content.findRevisionIndex(nid); tikhomirov@600: } tikhomirov@80: if (revision == BAD_REVISION) { tikhomirov@393: // using toString() to identify revlog. HgDataFile.toString includes path, HgManifest and HgChangelog instances tikhomirov@393: // are fine with default (class name) tikhomirov@393: // Perhaps, more tailored description method would be suitable here tikhomirov@393: throw new HgInvalidRevisionException(String.format("Can't find revision %s in %s", nid.shortNotation(), this), nid, null); tikhomirov@49: } tikhomirov@600: // final long t2 = System.nanoTime(); tikhomirov@600: // System.out.printf("\tgetRevisionIndex(%s): %d us\n", nid.shortNotation(), (t2-t1)/1000); tikhomirov@49: return revision; tikhomirov@49: } tikhomirov@367: tikhomirov@367: /** tikhomirov@354: * Note, {@link Nodeid#NULL} nodeid is not reported as known in any revlog. tikhomirov@354: * tikhomirov@354: * @param nodeid tikhomirov@419: * @return true if revision is part of this revlog tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@354: */ tikhomirov@425: public final boolean isKnown(Nodeid nodeid) throws HgRuntimeException { tikhomirov@367: final int rn = content.findRevisionIndex(nodeid); tikhomirov@218: if (BAD_REVISION == rn) { tikhomirov@39: return false; tikhomirov@39: } tikhomirov@49: if (rn < 0 || rn >= content.revisionCount()) { tikhomirov@49: // Sanity check tikhomirov@423: throw new HgInvalidStateException(String.format("Revision index %d found for nodeid %s is not from the range [0..%d]", rn, nodeid.shortNotation(), content.revisionCount()-1)); tikhomirov@49: } tikhomirov@49: return true; tikhomirov@39: } tikhomirov@49: tikhomirov@37: /** tikhomirov@394: * Access to revision data as is, equivalent to rawContent(getRevisionIndex(nodeid), sink) tikhomirov@394: * tikhomirov@394: * @param nodeid revision to retrieve tikhomirov@394: * @param sink data destination tikhomirov@394: * tikhomirov@394: * @throws HgInvalidRevisionException if supplied argument doesn't represent revision index in this revlog tikhomirov@394: * @throws HgInvalidControlFileException if access to revlog index/data entry failed tikhomirov@394: * @throws CancelledException if content retrieval operation was cancelled tikhomirov@394: * tikhomirov@394: * @see #rawContent(int, ByteChannel) tikhomirov@37: */ tikhomirov@394: protected void rawContent(Nodeid nodeid, ByteChannel sink) throws HgInvalidControlFileException, CancelledException, HgInvalidRevisionException { tikhomirov@367: rawContent(getRevisionIndex(nodeid), sink); tikhomirov@37: } tikhomirov@29: tikhomirov@37: /** tikhomirov@394: * Access to revision data as is (decompressed, but otherwise unprocessed, i.e. not parsed for e.g. changeset or manifest entries). tikhomirov@394: * tikhomirov@416: * @param revisionIndex index of this revlog change (not a changelog revision index), non-negative. From predefined constants, only {@link HgRepository#TIP} makes sense. tikhomirov@394: * @param sink data destination tikhomirov@394: * tikhomirov@394: * @throws HgInvalidRevisionException if supplied argument doesn't represent revision index in this revlog tikhomirov@394: * @throws HgInvalidControlFileException if access to revlog index/data entry failed tikhomirov@394: * @throws CancelledException if content retrieval operation was cancelled tikhomirov@37: */ tikhomirov@416: protected void rawContent(int revisionIndex, ByteChannel sink) throws HgInvalidControlFileException, CancelledException, HgInvalidRevisionException { tikhomirov@157: if (sink == null) { tikhomirov@157: throw new IllegalArgumentException(); tikhomirov@157: } tikhomirov@394: try { tikhomirov@490: ContentPipe insp = new ContentPipe(sink, 0, repo.getSessionContext().getLog()); tikhomirov@394: insp.checkCancelled(); tikhomirov@416: content.iterate(revisionIndex, revisionIndex, true, insp); tikhomirov@394: insp.checkFailed(); tikhomirov@394: } catch (IOException ex) { tikhomirov@416: HgInvalidControlFileException e = new HgInvalidControlFileException(String.format("Access to revision %d content failed", revisionIndex), ex, null); tikhomirov@416: e.setRevisionIndex(revisionIndex); tikhomirov@418: // TODO post 1.0 e.setFileName(content.getIndexFile() or this.getHumanFriendlyPath()) - shall decide whether tikhomirov@418: // protected abstract getHFPath() with impl in HgDataFile, HgManifest and HgChangelog or path is data of either Revlog or RevlogStream tikhomirov@394: // Do the same (add file name) below tikhomirov@394: throw e; tikhomirov@394: } catch (HgInvalidControlFileException ex) { tikhomirov@425: throw ex.isRevisionIndexSet() ? ex : ex.setRevisionIndex(revisionIndex); tikhomirov@394: } tikhomirov@37: } tikhomirov@37: tikhomirov@56: /** tikhomirov@405: * Fills supplied arguments with information about revision parents. tikhomirov@56: * tikhomirov@56: * @param revision - revision to query parents, or {@link HgRepository#TIP} tikhomirov@405: * @param parentRevisions - int[2] to get local revision numbers of parents (e.g. {6, -1}), {@link HgRepository#NO_REVISION} indicates parent not set tikhomirov@56: * @param parent1 - byte[20] or null, if parent's nodeid is not needed tikhomirov@56: * @param parent2 - byte[20] or null, if second parent's nodeid is not needed tikhomirov@425: * @throws IllegalArgumentException if passed arrays can't fit requested data tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@56: */ tikhomirov@425: public void parents(int revision, int[] parentRevisions, byte[] parent1, byte[] parent2) throws HgRuntimeException, IllegalArgumentException { tikhomirov@56: if (revision != TIP && !(revision >= 0 && revision < content.revisionCount())) { tikhomirov@347: throw new HgInvalidRevisionException(revision); tikhomirov@56: } tikhomirov@56: if (parentRevisions == null || parentRevisions.length < 2) { tikhomirov@56: throw new IllegalArgumentException(String.valueOf(parentRevisions)); tikhomirov@56: } tikhomirov@56: if (parent1 != null && parent1.length < 20) { tikhomirov@56: throw new IllegalArgumentException(parent1.toString()); tikhomirov@56: } tikhomirov@56: if (parent2 != null && parent2.length < 20) { tikhomirov@56: throw new IllegalArgumentException(parent2.toString()); tikhomirov@56: } tikhomirov@77: class ParentCollector implements RevlogStream.Inspector { tikhomirov@56: public int p1 = -1; tikhomirov@56: public int p2 = -1; tikhomirov@56: public byte[] nodeid; tikhomirov@56: tikhomirov@157: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { tikhomirov@56: p1 = parent1Revision; tikhomirov@56: p2 = parent2Revision; tikhomirov@56: this.nodeid = new byte[20]; tikhomirov@56: // nodeid arg now comes in 32 byte from (as in file format description), however upper 12 bytes are zeros. tikhomirov@56: System.arraycopy(nodeid, nodeid.length > 20 ? nodeid.length - 20 : 0, this.nodeid, 0, 20); tikhomirov@56: } tikhomirov@56: }; tikhomirov@56: ParentCollector pc = new ParentCollector(); tikhomirov@56: content.iterate(revision, revision, false, pc); tikhomirov@405: // although next code looks odd (NO_REVISION *is* -1), it's safer to be explicit tikhomirov@405: parentRevisions[0] = pc.p1 == -1 ? NO_REVISION : pc.p1; tikhomirov@405: parentRevisions[1] = pc.p2 == -1 ? NO_REVISION : pc.p2; tikhomirov@56: if (parent1 != null) { tikhomirov@405: if (parentRevisions[0] == NO_REVISION) { tikhomirov@56: Arrays.fill(parent1, 0, 20, (byte) 0); tikhomirov@56: } else { tikhomirov@56: content.iterate(parentRevisions[0], parentRevisions[0], false, pc); tikhomirov@56: System.arraycopy(pc.nodeid, 0, parent1, 0, 20); tikhomirov@56: } tikhomirov@56: } tikhomirov@56: if (parent2 != null) { tikhomirov@405: if (parentRevisions[1] == NO_REVISION) { tikhomirov@56: Arrays.fill(parent2, 0, 20, (byte) 0); tikhomirov@56: } else { tikhomirov@56: content.iterate(parentRevisions[1], parentRevisions[1], false, pc); tikhomirov@56: System.arraycopy(pc.nodeid, 0, parent2, 0, 20); tikhomirov@56: } tikhomirov@56: } tikhomirov@56: } tikhomirov@425: tikhomirov@425: /** tikhomirov@425: * EXPERIMENTAL CODE, DO NOT USE tikhomirov@425: * tikhomirov@425: * Alternative revlog iteration tikhomirov@425: * tikhomirov@425: * @param start tikhomirov@425: * @param end tikhomirov@425: * @param inspector tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@425: */ tikhomirov@324: @Experimental tikhomirov@471: public final void indexWalk(int start, int end, final Revlog.Inspector inspector) throws HgRuntimeException { tikhomirov@324: int lastRev = getLastRevision(); tikhomirov@448: final int _start = start == TIP ? lastRev : start; tikhomirov@324: if (end == TIP) { tikhomirov@324: end = lastRev; tikhomirov@324: } tikhomirov@356: final RevisionInspector revisionInsp = Adaptable.Factory.getAdapter(inspector, RevisionInspector.class, null); tikhomirov@356: final ParentInspector parentInsp = Adaptable.Factory.getAdapter(inspector, ParentInspector.class, null); tikhomirov@448: final Nodeid[] allRevisions = parentInsp == null ? null : new Nodeid[end - _start + 1]; tikhomirov@448: // next are to build set of parent indexes that are not part of the range iteration tikhomirov@448: // i.e. those parents we need to read separately. See Issue 31 for details. tikhomirov@448: final int[] firstParentIndexes = parentInsp == null || _start == 0 ? null : new int[allRevisions.length]; tikhomirov@448: final int[] secondParentIndexes = parentInsp == null || _start == 0 ? null : new int[allRevisions.length]; tikhomirov@448: final IntMap missingParents = parentInsp == null || _start == 0 ? null : new IntMap(16); tikhomirov@324: tikhomirov@448: content.iterate(_start, end, false, new RevlogStream.Inspector() { tikhomirov@448: private int i = 0; tikhomirov@324: tikhomirov@448: public void next(int revisionIndex, int actualLen, int baseRevIndex, int linkRevIndex, int parent1RevIndex, int parent2RevIndex, byte[] nodeid, DataAccess data) { tikhomirov@324: Nodeid nid = Nodeid.fromBinary(nodeid, 0); tikhomirov@324: if (revisionInsp != null) { tikhomirov@448: revisionInsp.next(revisionIndex, nid, linkRevIndex); tikhomirov@324: } tikhomirov@324: if (parentInsp != null) { tikhomirov@448: allRevisions[i] = nid; tikhomirov@448: if (_start > 0) { tikhomirov@448: firstParentIndexes[i] = parent1RevIndex; tikhomirov@448: secondParentIndexes[i] = parent2RevIndex; tikhomirov@448: if (parent1RevIndex < _start && parent1RevIndex >= 0) { tikhomirov@448: missingParents.put(parent1RevIndex, null); tikhomirov@448: } tikhomirov@448: if (parent2RevIndex < _start && parent2RevIndex >= 0) { tikhomirov@448: missingParents.put(parent2RevIndex, null); tikhomirov@448: } tikhomirov@448: } else { tikhomirov@448: Nodeid p1 = parent1RevIndex == -1 ? Nodeid.NULL : allRevisions[parent1RevIndex]; tikhomirov@448: Nodeid p2 = parent2RevIndex == -1 ? Nodeid.NULL : allRevisions[parent2RevIndex]; tikhomirov@448: parentInsp.next(revisionIndex, allRevisions[i], parent1RevIndex, parent2RevIndex, p1, p2); tikhomirov@448: } tikhomirov@448: i++; tikhomirov@324: } tikhomirov@324: } tikhomirov@324: }); tikhomirov@448: if (parentInsp != null && _start > 0) { tikhomirov@448: assert missingParents.size() > 0; // in fact, more relaxed than assert. rather 'assume' tikhomirov@448: // TODO int[] IntMap#keys() or even sort of iterator that can modify values tikhomirov@448: for (int k = missingParents.firstKey(), l = missingParents.lastKey(); k <= l; k++) { tikhomirov@448: if (missingParents.containsKey(k)) { tikhomirov@448: Nodeid nid = getRepo().getChangelog().getRevision(k); tikhomirov@448: missingParents.put(k, nid); tikhomirov@448: } tikhomirov@448: } tikhomirov@448: tikhomirov@448: for (int i = 0, revNum = _start; i < allRevisions.length; i++, revNum++) { tikhomirov@448: int riP1 = firstParentIndexes[i]; tikhomirov@448: int riP2 = secondParentIndexes[i]; tikhomirov@448: Nodeid p1, p2; tikhomirov@448: p1 = p2 = Nodeid.NULL; tikhomirov@448: if (riP1 >= _start) { tikhomirov@448: // p1 of revNum's revision is out of iterated range tikhomirov@448: // (don't check for riP1=0 && riP1 < _start; tikhomirov@448: p1 = missingParents.get(riP1); tikhomirov@448: } tikhomirov@448: // same for Pp2 tikhomirov@448: if (riP2 >= _start) { tikhomirov@448: p2 = allRevisions[riP2 - start]; tikhomirov@448: } else if (riP2 != -1) { tikhomirov@448: assert riP2 >= 0 && riP2 < _start; tikhomirov@448: p2 = missingParents.get(riP2); tikhomirov@448: } tikhomirov@448: parentInsp.next(revNum, allRevisions[i], riP1, riP2, p1, p2); tikhomirov@448: } tikhomirov@448: } tikhomirov@324: } tikhomirov@324: tikhomirov@324: /** tikhomirov@324: * MARKER tikhomirov@324: */ tikhomirov@324: @Experimental tikhomirov@324: public interface Inspector { tikhomirov@324: } tikhomirov@324: tikhomirov@324: @Experimental tikhomirov@324: public interface RevisionInspector extends Inspector { tikhomirov@368: void next(int revisionIndex, Nodeid revision, int linkedRevisionIndex); tikhomirov@324: } tikhomirov@324: tikhomirov@324: @Experimental tikhomirov@324: public interface ParentInspector extends Inspector { tikhomirov@327: // XXX document whether parentX is -1 or a constant (BAD_REVISION? or dedicated?) tikhomirov@367: void next(int revisionIndex, Nodeid revision, int parent1, int parent2, Nodeid nidParent1, Nodeid nidParent2); tikhomirov@324: } tikhomirov@324: tikhomirov@432: protected HgParentChildMap getParentWalker() { tikhomirov@432: HgParentChildMap pw = new HgParentChildMap(this); tikhomirov@432: pw.init(); tikhomirov@432: return pw; tikhomirov@29: } tikhomirov@538: tikhomirov@433: /* tikhomirov@433: * class with cancel and few other exceptions support. TODO consider general superclass to share with e.g. HgManifestCommand.Mediator tikhomirov@243: */ tikhomirov@277: protected abstract static class ErrorHandlingInspector implements RevlogStream.Inspector, CancelSupport { tikhomirov@157: private Exception failure; tikhomirov@277: private CancelSupport cancelSupport; tikhomirov@157: tikhomirov@277: protected void setCancelSupport(CancelSupport cs) { tikhomirov@277: assert cancelSupport == null; // no reason to set it twice tikhomirov@277: cancelSupport = cs; tikhomirov@157: } tikhomirov@157: tikhomirov@157: protected void recordFailure(Exception ex) { tikhomirov@157: assert failure == null; tikhomirov@157: failure = ex; tikhomirov@157: } tikhomirov@157: tikhomirov@425: public void checkFailed() throws HgRuntimeException, IOException, CancelledException { tikhomirov@157: if (failure == null) { tikhomirov@157: return; tikhomirov@157: } tikhomirov@157: if (failure instanceof IOException) { tikhomirov@157: throw (IOException) failure; tikhomirov@157: } tikhomirov@157: if (failure instanceof CancelledException) { tikhomirov@157: throw (CancelledException) failure; tikhomirov@157: } tikhomirov@425: if (failure instanceof HgRuntimeException) { tikhomirov@425: throw (HgRuntimeException) failure; tikhomirov@157: } tikhomirov@423: throw new HgInvalidStateException(failure.toString()); tikhomirov@157: } tikhomirov@277: tikhomirov@277: public void checkCancelled() throws CancelledException { tikhomirov@277: if (cancelSupport != null) { tikhomirov@277: cancelSupport.checkCancelled(); tikhomirov@277: } tikhomirov@277: } tikhomirov@277: } tikhomirov@277: tikhomirov@277: protected static class ContentPipe extends ErrorHandlingInspector implements RevlogStream.Inspector, CancelSupport { tikhomirov@277: private final ByteChannel sink; tikhomirov@277: private final int offset; tikhomirov@355: private final LogFacility logFacility; tikhomirov@277: tikhomirov@277: /** tikhomirov@277: * @param _sink - cannot be null tikhomirov@277: * @param seekOffset - when positive, orders to pipe bytes to the sink starting from specified offset, not from the first byte available in DataAccess tikhomirov@355: * @param log optional facility to put warnings/debug messages into, may be null. tikhomirov@277: */ tikhomirov@355: public ContentPipe(ByteChannel _sink, int seekOffset, LogFacility log) { tikhomirov@277: assert _sink != null; tikhomirov@277: sink = _sink; tikhomirov@277: setCancelSupport(CancelSupport.Factory.get(_sink)); tikhomirov@277: offset = seekOffset; tikhomirov@355: logFacility = log; tikhomirov@277: } tikhomirov@277: tikhomirov@425: protected void prepare(int revisionNumber, DataAccess da) throws IOException { tikhomirov@277: if (offset > 0) { // save few useless reset/rewind operations tikhomirov@277: da.seek(offset); tikhomirov@277: } tikhomirov@277: } tikhomirov@277: tikhomirov@277: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { tikhomirov@277: try { tikhomirov@277: prepare(revisionNumber, da); // XXX perhaps, prepare shall return DA (sliced, if needed) tikhomirov@277: final ProgressSupport progressSupport = ProgressSupport.Factory.get(sink); tikhomirov@355: ByteBuffer buf = ByteBuffer.allocate(actualLen > 8192 ? 8192 : actualLen); tikhomirov@356: Preview p = Adaptable.Factory.getAdapter(sink, Preview.class, null); tikhomirov@355: if (p != null) { tikhomirov@355: progressSupport.start(2 * da.length()); tikhomirov@355: while (!da.isEmpty()) { tikhomirov@355: checkCancelled(); tikhomirov@355: da.readBytes(buf); tikhomirov@355: p.preview(buf); tikhomirov@355: buf.clear(); tikhomirov@355: } tikhomirov@355: da.reset(); tikhomirov@355: prepare(revisionNumber, da); tikhomirov@355: progressSupport.worked(da.length()); tikhomirov@355: buf.clear(); tikhomirov@355: } else { tikhomirov@355: progressSupport.start(da.length()); tikhomirov@355: } tikhomirov@277: while (!da.isEmpty()) { tikhomirov@277: checkCancelled(); tikhomirov@277: da.readBytes(buf); tikhomirov@355: buf.flip(); // post: position == 0 tikhomirov@277: // XXX I may not rely on returned number of bytes but track change in buf position instead. tikhomirov@355: tikhomirov@355: int consumed = sink.write(buf); tikhomirov@355: if ((consumed == 0 || consumed != buf.position()) && logFacility != null) { tikhomirov@456: logFacility.dump(getClass(), Warn, "Bad data sink when reading revision %d. Reported %d bytes consumed, byt actually read %d", revisionNumber, consumed, buf.position()); tikhomirov@355: } tikhomirov@355: if (buf.position() == 0) { tikhomirov@423: throw new HgInvalidStateException("Bad sink implementation (consumes no bytes) results in endless loop"); tikhomirov@355: } tikhomirov@355: buf.compact(); // ensure (a) there's space for new (b) data starts at 0 tikhomirov@277: progressSupport.worked(consumed); tikhomirov@277: } tikhomirov@277: progressSupport.done(); // XXX shall specify whether #done() is invoked always or only if completed successfully. tikhomirov@277: } catch (IOException ex) { tikhomirov@277: recordFailure(ex); tikhomirov@277: } catch (CancelledException ex) { tikhomirov@277: recordFailure(ex); tikhomirov@277: } tikhomirov@277: } tikhomirov@157: } tikhomirov@2: }