kitaev@213: /* kitaev@213: * Copyright (c) 2010-2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.repo; kitaev@213: kitaev@213: import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; kitaev@213: import static org.tmatesoft.hg.repo.HgRepository.TIP; kitaev@213: kitaev@213: import java.io.IOException; kitaev@213: import java.nio.ByteBuffer; kitaev@213: import java.util.Arrays; kitaev@213: import java.util.Collection; kitaev@213: import java.util.HashSet; kitaev@213: import java.util.LinkedList; kitaev@213: import java.util.List; kitaev@213: kitaev@213: import org.tmatesoft.hg.core.HgBadStateException; kitaev@213: import org.tmatesoft.hg.core.HgException; kitaev@213: import org.tmatesoft.hg.core.Nodeid; kitaev@213: import org.tmatesoft.hg.internal.DataAccess; kitaev@213: import org.tmatesoft.hg.internal.RevlogStream; kitaev@213: import org.tmatesoft.hg.util.ByteChannel; kitaev@213: import org.tmatesoft.hg.util.CancelSupport; kitaev@213: import org.tmatesoft.hg.util.CancelledException; kitaev@213: import org.tmatesoft.hg.util.ProgressSupport; kitaev@213: kitaev@213: kitaev@213: /** kitaev@213: * Base class for all Mercurial entities that are serialized in a so called revlog format (changelog, manifest, data files). kitaev@213: * kitaev@213: * Implementation note: kitaev@213: * Hides actual actual revlog stream implementation and its access methods (i.e. RevlogStream.Inspector), iow shall not expose anything internal kitaev@213: * in public methods. kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: abstract class Revlog { kitaev@213: kitaev@213: private final HgRepository repo; kitaev@213: protected final RevlogStream content; kitaev@213: kitaev@213: protected Revlog(HgRepository hgRepo, RevlogStream contentStream) { kitaev@213: if (hgRepo == null) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: if (contentStream == null) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: repo = hgRepo; kitaev@213: content = contentStream; kitaev@213: } kitaev@213: kitaev@213: // invalid Revlog kitaev@213: protected Revlog(HgRepository hgRepo) { kitaev@213: repo = hgRepo; kitaev@213: content = null; kitaev@213: } kitaev@213: kitaev@213: public final HgRepository getRepo() { kitaev@213: return repo; kitaev@213: } kitaev@213: kitaev@213: public final int getRevisionCount() { kitaev@213: return content.revisionCount(); kitaev@213: } kitaev@213: kitaev@213: public final int getLastRevision() { kitaev@213: return content.revisionCount() - 1; kitaev@213: } kitaev@213: kitaev@213: public final Nodeid getRevision(int revision) { kitaev@213: // XXX cache nodeids? kitaev@213: return Nodeid.fromBinary(content.nodeid(revision), 0); kitaev@213: } kitaev@213: kitaev@213: public final int getLocalRevision(Nodeid nid) { kitaev@213: int revision = content.findLocalRevisionNumber(nid); kitaev@213: if (revision == BAD_REVISION) { kitaev@213: throw new IllegalArgumentException(String.format("%s doesn't represent a revision of %s", nid.toString(), this /*XXX HgDataFile.getPath might be more suitable here*/)); kitaev@213: } kitaev@213: return revision; kitaev@213: } kitaev@213: kitaev@213: // Till now, i follow approach that NULL nodeid is never part of revlog kitaev@213: public final boolean isKnown(Nodeid nodeid) { kitaev@213: final int rn = content.findLocalRevisionNumber(nodeid); kitaev@213: if (Integer.MIN_VALUE == rn) { kitaev@213: return false; kitaev@213: } kitaev@213: if (rn < 0 || rn >= content.revisionCount()) { kitaev@213: // Sanity check kitaev@213: throw new IllegalStateException(); kitaev@213: } kitaev@213: return true; kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * Access to revision data as is (decompressed, but otherwise unprocessed, i.e. not parsed for e.g. changeset or manifest entries) kitaev@213: * @param nodeid kitaev@213: */ kitaev@213: protected void rawContent(Nodeid nodeid, ByteChannel sink) throws HgException, IOException, CancelledException { kitaev@213: rawContent(getLocalRevision(nodeid), sink); kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * @param revision - repo-local index of this file change (not a changelog revision number!) kitaev@213: */ kitaev@213: protected void rawContent(int revision, ByteChannel sink) throws HgException, IOException, CancelledException { kitaev@213: if (sink == null) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: ContentPipe insp = new ContentPipe(sink, 0); kitaev@213: insp.checkCancelled(); kitaev@213: content.iterate(revision, revision, true, insp); kitaev@213: insp.checkFailed(); kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * XXX perhaps, return value Nodeid[2] and boolean needNodeids is better (and higher level) API for this query? kitaev@213: * kitaev@213: * @param revision - revision to query parents, or {@link HgRepository#TIP} kitaev@213: * @param parentRevisions - int[2] to get local revision numbers of parents (e.g. {6, -1}) kitaev@213: * @param parent1 - byte[20] or null, if parent's nodeid is not needed kitaev@213: * @param parent2 - byte[20] or null, if second parent's nodeid is not needed kitaev@213: * @return kitaev@213: */ kitaev@213: public void parents(int revision, int[] parentRevisions, byte[] parent1, byte[] parent2) { kitaev@213: if (revision != TIP && !(revision >= 0 && revision < content.revisionCount())) { kitaev@213: throw new IllegalArgumentException(String.valueOf(revision)); kitaev@213: } kitaev@213: if (parentRevisions == null || parentRevisions.length < 2) { kitaev@213: throw new IllegalArgumentException(String.valueOf(parentRevisions)); kitaev@213: } kitaev@213: if (parent1 != null && parent1.length < 20) { kitaev@213: throw new IllegalArgumentException(parent1.toString()); kitaev@213: } kitaev@213: if (parent2 != null && parent2.length < 20) { kitaev@213: throw new IllegalArgumentException(parent2.toString()); kitaev@213: } kitaev@213: class ParentCollector implements RevlogStream.Inspector { kitaev@213: public int p1 = -1; kitaev@213: public int p2 = -1; kitaev@213: public byte[] nodeid; kitaev@213: kitaev@213: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { kitaev@213: p1 = parent1Revision; kitaev@213: p2 = parent2Revision; kitaev@213: this.nodeid = new byte[20]; kitaev@213: // nodeid arg now comes in 32 byte from (as in file format description), however upper 12 bytes are zeros. kitaev@213: System.arraycopy(nodeid, nodeid.length > 20 ? nodeid.length - 20 : 0, this.nodeid, 0, 20); kitaev@213: } kitaev@213: }; kitaev@213: ParentCollector pc = new ParentCollector(); kitaev@213: content.iterate(revision, revision, false, pc); kitaev@213: parentRevisions[0] = pc.p1; kitaev@213: parentRevisions[1] = pc.p2; kitaev@213: if (parent1 != null) { kitaev@213: if (parentRevisions[0] == -1) { kitaev@213: Arrays.fill(parent1, 0, 20, (byte) 0); kitaev@213: } else { kitaev@213: content.iterate(parentRevisions[0], parentRevisions[0], false, pc); kitaev@213: System.arraycopy(pc.nodeid, 0, parent1, 0, 20); kitaev@213: } kitaev@213: } kitaev@213: if (parent2 != null) { kitaev@213: if (parentRevisions[1] == -1) { kitaev@213: Arrays.fill(parent2, 0, 20, (byte) 0); kitaev@213: } else { kitaev@213: content.iterate(parentRevisions[1], parentRevisions[1], false, pc); kitaev@213: System.arraycopy(pc.nodeid, 0, parent2, 0, 20); kitaev@213: } kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: /* kitaev@213: * XXX think over if it's better to do either: kitaev@213: * pw = getChangelog().new ParentWalker(); pw.init() and pass pw instance around as needed kitaev@213: * or kitaev@213: * add Revlog#getParentWalker(), static class, make cons() and #init package-local, and keep SoftReference to allow walker reuse. kitaev@213: * kitaev@213: * and yes, walker is not a proper name kitaev@213: */ kitaev@213: public final class ParentWalker { kitaev@213: kitaev@213: kitaev@213: private Nodeid[] sequential; // natural repository order, childrenOf rely on ordering kitaev@213: private Nodeid[] sorted; // for binary search kitaev@213: private int[] sorted2natural; kitaev@213: private Nodeid[] firstParent; kitaev@213: private Nodeid[] secondParent; kitaev@213: kitaev@213: // Nodeid instances shall be shared between all arrays kitaev@213: kitaev@213: public ParentWalker() { kitaev@213: } kitaev@213: kitaev@213: public HgRepository getRepo() { kitaev@213: return Revlog.this.getRepo(); kitaev@213: } kitaev@213: kitaev@213: public void init() { kitaev@213: final RevlogStream stream = Revlog.this.content; kitaev@213: final int revisionCount = stream.revisionCount(); kitaev@213: firstParent = new Nodeid[revisionCount]; kitaev@213: // although branches/merges are less frequent, and most of secondParent would be -1/null, some sort of kitaev@213: // SparseOrderedList might be handy, provided its inner structures do not overweight simplicity of an array kitaev@213: secondParent = new Nodeid[revisionCount]; kitaev@213: // kitaev@213: sequential = new Nodeid[revisionCount]; kitaev@213: sorted = new Nodeid[revisionCount]; kitaev@213: kitaev@213: RevlogStream.Inspector insp = new RevlogStream.Inspector() { kitaev@213: kitaev@213: int ix = 0; kitaev@213: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { kitaev@213: if (ix != revisionNumber) { kitaev@213: // XXX temp code, just to make sure I understand what's going on here kitaev@213: throw new IllegalStateException(); kitaev@213: } kitaev@213: if (parent1Revision >= revisionNumber || parent2Revision >= revisionNumber) { kitaev@213: throw new IllegalStateException(); // sanity, revisions are sequential kitaev@213: } kitaev@213: final Nodeid nid = new Nodeid(nodeid, true); kitaev@213: sequential[ix] = sorted[ix] = nid; kitaev@213: if (parent1Revision != -1) { kitaev@213: assert parent1Revision < ix; kitaev@213: firstParent[ix] = sequential[parent1Revision]; kitaev@213: } kitaev@213: if (parent2Revision != -1) { // revlog of DataAccess.java has p2 set when p1 is -1 kitaev@213: assert parent2Revision < ix; kitaev@213: secondParent[ix] = sequential[parent2Revision]; kitaev@213: } kitaev@213: ix++; kitaev@213: } kitaev@213: }; kitaev@213: stream.iterate(0, TIP, false, insp); kitaev@213: Arrays.sort(sorted); kitaev@213: sorted2natural = new int[revisionCount]; kitaev@213: for (int i = 0; i < revisionCount; i++) { kitaev@213: Nodeid n = sequential[i]; kitaev@213: int x = Arrays.binarySearch(sorted, n); kitaev@213: assertSortedIndex(x); kitaev@213: sorted2natural[x] = i; kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: private void assertSortedIndex(int x) { kitaev@213: if (x < 0) { kitaev@213: throw new HgBadStateException(); kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: // FIXME need to decide whether Nodeid(00 * 20) is always known or not kitaev@213: // right now Nodeid.NULL is not recognized as known if passed to this method, kitaev@213: // caller is supposed to make explicit check kitaev@213: public boolean knownNode(Nodeid nid) { kitaev@213: return Arrays.binarySearch(sorted, nid) >= 0; kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * null if none. only known nodes (as per #knownNode) are accepted as arguments kitaev@213: */ kitaev@213: public Nodeid firstParent(Nodeid nid) { kitaev@213: int x = Arrays.binarySearch(sorted, nid); kitaev@213: assertSortedIndex(x); kitaev@213: int i = sorted2natural[x]; kitaev@213: return firstParent[i]; kitaev@213: } kitaev@213: kitaev@213: // never null, Nodeid.NULL if none known kitaev@213: public Nodeid safeFirstParent(Nodeid nid) { kitaev@213: Nodeid rv = firstParent(nid); kitaev@213: return rv == null ? Nodeid.NULL : rv; kitaev@213: } kitaev@213: kitaev@213: public Nodeid secondParent(Nodeid nid) { kitaev@213: int x = Arrays.binarySearch(sorted, nid); kitaev@213: assertSortedIndex(x); kitaev@213: int i = sorted2natural[x]; kitaev@213: return secondParent[i]; kitaev@213: } kitaev@213: kitaev@213: public Nodeid safeSecondParent(Nodeid nid) { kitaev@213: Nodeid rv = secondParent(nid); kitaev@213: return rv == null ? Nodeid.NULL : rv; kitaev@213: } kitaev@213: kitaev@213: public boolean appendParentsOf(Nodeid nid, Collection c) { kitaev@213: int x = Arrays.binarySearch(sorted, nid); kitaev@213: assertSortedIndex(x); kitaev@213: int i = sorted2natural[x]; kitaev@213: Nodeid p1 = firstParent[i]; kitaev@213: boolean modified = false; kitaev@213: if (p1 != null) { kitaev@213: modified = c.add(p1); kitaev@213: } kitaev@213: Nodeid p2 = secondParent[i]; kitaev@213: if (p2 != null) { kitaev@213: modified = c.add(p2) || modified; kitaev@213: } kitaev@213: return modified; kitaev@213: } kitaev@213: kitaev@213: // XXX alternative (and perhaps more reliable) approach would be to make a copy of allNodes and remove kitaev@213: // nodes, their parents and so on. kitaev@213: kitaev@213: // @return ordered collection of all children rooted at supplied nodes. Nodes shall not be descendants of each other! kitaev@213: // Nodeids shall belong to this revlog kitaev@213: public List childrenOf(List roots) { kitaev@213: HashSet parents = new HashSet(); kitaev@213: LinkedList result = new LinkedList(); kitaev@213: int earliestRevision = Integer.MAX_VALUE; kitaev@213: assert sequential.length == firstParent.length && firstParent.length == secondParent.length; kitaev@213: // first, find earliest index of roots in question, as there's no sense kitaev@213: // to check children among nodes prior to branch's root node kitaev@213: for (Nodeid r : roots) { kitaev@213: int x = Arrays.binarySearch(sorted, r); kitaev@213: assertSortedIndex(x); kitaev@213: int i = sorted2natural[x]; kitaev@213: if (i < earliestRevision) { kitaev@213: earliestRevision = i; kitaev@213: } kitaev@213: parents.add(sequential[i]); // add canonical instance in hope equals() is bit faster when can do a == kitaev@213: } kitaev@213: for (int i = earliestRevision + 1; i < sequential.length; i++) { kitaev@213: if (parents.contains(firstParent[i]) || parents.contains(secondParent[i])) { kitaev@213: parents.add(sequential[i]); // to find next child kitaev@213: result.add(sequential[i]); kitaev@213: } kitaev@213: } kitaev@213: return result; kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * @param nid possibly parent node, shall be {@link #knownNode(Nodeid) known} in this revlog. kitaev@213: * @return true if there's any node in this revlog that has specified node as one of its parents. kitaev@213: */ kitaev@213: public boolean hasChildren(Nodeid nid) { kitaev@213: int x = Arrays.binarySearch(sorted, nid); kitaev@213: assertSortedIndex(x); kitaev@213: int i = sorted2natural[x]; kitaev@213: assert firstParent.length == secondParent.length; // just in case later I implement sparse array for secondParent kitaev@213: assert firstParent.length == sequential.length; kitaev@213: // to use == instead of equals, take the same Nodeid instance we used to fill all the arrays. kitaev@213: final Nodeid canonicalNode = sequential[i]; kitaev@213: i++; // no need to check node itself. child nodes may appear in sequential only after revision in question kitaev@213: for (; i < sequential.length; i++) { kitaev@213: // FIXME likely, not very effective. kitaev@213: // May want to optimize it with another (Tree|Hash)Set, created on demand on first use, kitaev@213: // however, need to be careful with memory usage kitaev@213: if (firstParent[i] == canonicalNode || secondParent[i] == canonicalNode) { kitaev@213: return true; kitaev@213: } kitaev@213: } kitaev@213: return false; kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: protected static class ContentPipe implements RevlogStream.Inspector, CancelSupport { kitaev@213: private final ByteChannel sink; kitaev@213: private final CancelSupport cancelSupport; kitaev@213: private Exception failure; kitaev@213: private final int offset; kitaev@213: kitaev@213: /** kitaev@213: * @param _sink - cannot be null kitaev@213: * @param seekOffset - when positive, orders to pipe bytes to the sink starting from specified offset, not from the first byte available in DataAccess kitaev@213: */ kitaev@213: public ContentPipe(ByteChannel _sink, int seekOffset) { kitaev@213: assert _sink != null; kitaev@213: sink = _sink; kitaev@213: cancelSupport = CancelSupport.Factory.get(_sink); kitaev@213: offset = seekOffset; kitaev@213: } kitaev@213: kitaev@213: protected void prepare(int revisionNumber, DataAccess da) throws HgException, IOException { kitaev@213: if (offset > 0) { // save few useless reset/rewind operations kitaev@213: da.seek(offset); kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { kitaev@213: try { kitaev@213: prepare(revisionNumber, da); // XXX perhaps, prepare shall return DA (sliced, if needed) kitaev@213: final ProgressSupport progressSupport = ProgressSupport.Factory.get(sink); kitaev@213: ByteBuffer buf = ByteBuffer.allocate(512); kitaev@213: progressSupport.start(da.length()); kitaev@213: while (!da.isEmpty()) { kitaev@213: cancelSupport.checkCancelled(); kitaev@213: da.readBytes(buf); kitaev@213: buf.flip(); kitaev@213: // XXX I may not rely on returned number of bytes but track change in buf position instead. kitaev@213: int consumed = sink.write(buf); kitaev@213: // FIXME in fact, bad sink implementation (that consumes no bytes) would result in endless loop. Need to account for this kitaev@213: buf.compact(); kitaev@213: progressSupport.worked(consumed); kitaev@213: } kitaev@213: progressSupport.done(); // XXX shall specify whether #done() is invoked always or only if completed successfully. kitaev@213: } catch (IOException ex) { kitaev@213: recordFailure(ex); kitaev@213: } catch (CancelledException ex) { kitaev@213: recordFailure(ex); kitaev@213: } catch (HgException ex) { kitaev@213: recordFailure(ex); kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: public void checkCancelled() throws CancelledException { kitaev@213: cancelSupport.checkCancelled(); kitaev@213: } kitaev@213: kitaev@213: protected void recordFailure(Exception ex) { kitaev@213: assert failure == null; kitaev@213: failure = ex; kitaev@213: } kitaev@213: kitaev@213: public void checkFailed() throws HgException, IOException, CancelledException { kitaev@213: if (failure == null) { kitaev@213: return; kitaev@213: } kitaev@213: if (failure instanceof IOException) { kitaev@213: throw (IOException) failure; kitaev@213: } kitaev@213: if (failure instanceof CancelledException) { kitaev@213: throw (CancelledException) failure; kitaev@213: } kitaev@213: if (failure instanceof HgException) { kitaev@213: throw (HgException) failure; kitaev@213: } kitaev@213: throw new HgBadStateException(failure); kitaev@213: } kitaev@213: } kitaev@213: }