tikhomirov@22: /* tikhomirov@22: * Copyright (c) 2010, 2011 Artem Tikhomirov tikhomirov@2: */ tikhomirov@2: package com.tmate.hgkit.ll; tikhomirov@2: tikhomirov@29: import java.util.Collection; tikhomirov@29: import java.util.Collections; tikhomirov@29: import java.util.HashMap; tikhomirov@29: import java.util.LinkedHashSet; tikhomirov@29: import java.util.Map; tikhomirov@29: import java.util.Set; tikhomirov@29: tikhomirov@2: /** tikhomirov@2: * tikhomirov@2: * @author artem tikhomirov@2: */ tikhomirov@2: public abstract class Revlog { tikhomirov@2: tikhomirov@2: private final HgRepository hgRepo; tikhomirov@21: protected final RevlogStream content; tikhomirov@2: tikhomirov@21: protected Revlog(HgRepository hgRepo, RevlogStream content) { tikhomirov@2: if (hgRepo == null) { tikhomirov@2: throw new NullPointerException(); tikhomirov@2: } tikhomirov@2: this.hgRepo = hgRepo; tikhomirov@21: this.content = content; tikhomirov@2: } tikhomirov@2: tikhomirov@2: public final HgRepository getRepo() { tikhomirov@2: return hgRepo; tikhomirov@2: } tikhomirov@2: tikhomirov@21: public int getRevisionCount() { tikhomirov@21: return content.revisionCount(); tikhomirov@21: } tikhomirov@37: tikhomirov@49: public int getLocalRevisionNumber(Nodeid nid) { tikhomirov@49: int revision = content.findLocalRevisionNumber(nid); tikhomirov@49: if (revision == Integer.MIN_VALUE) { tikhomirov@49: throw new IllegalArgumentException(String.format("%s doesn't represent a revision of %s", nid.toString(), this /*XXX HgDataFile.getPath might be more suitable here*/)); tikhomirov@49: } tikhomirov@49: return revision; tikhomirov@49: } tikhomirov@49: tikhomirov@39: // Till now, i follow approach that NULL nodeid is never part of revlog tikhomirov@39: public boolean isKnown(Nodeid nodeid) { tikhomirov@49: final int rn = content.findLocalRevisionNumber(nodeid); tikhomirov@49: if (Integer.MIN_VALUE == rn) { tikhomirov@39: return false; tikhomirov@39: } tikhomirov@49: if (rn < 0 || rn >= content.revisionCount()) { tikhomirov@49: // Sanity check tikhomirov@49: throw new IllegalStateException(); tikhomirov@49: } tikhomirov@49: return true; tikhomirov@39: } tikhomirov@49: tikhomirov@37: /** tikhomirov@37: * Access to revision data as is (decompressed, but otherwise unprocessed, i.e. not parsed for e.g. changeset or manifest entries) tikhomirov@37: * @param nodeid tikhomirov@37: */ tikhomirov@37: public byte[] content(Nodeid nodeid) { tikhomirov@49: return content(getLocalRevisionNumber(nodeid)); tikhomirov@37: } tikhomirov@29: tikhomirov@37: /** tikhomirov@37: * @param revision - repo-local index of this file change (not a changelog revision number!) tikhomirov@37: */ tikhomirov@37: public byte[] content(int revision) { tikhomirov@37: final byte[][] dataPtr = new byte[1][]; tikhomirov@37: Revlog.Inspector insp = new Revlog.Inspector() { tikhomirov@37: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { tikhomirov@37: dataPtr[0] = data; tikhomirov@37: } tikhomirov@37: }; tikhomirov@37: content.iterate(revision, revision, true, insp); tikhomirov@37: return dataPtr[0]; tikhomirov@37: } tikhomirov@37: tikhomirov@22: // FIXME byte[] data might be too expensive, for few usecases it may be better to have intermediate Access object (when we don't need full data tikhomirov@22: // instantly - e.g. calculate hash, or comparing two revisions tikhomirov@29: // XXX seems that RevlogStream is better place for this class. tikhomirov@2: public interface Inspector { tikhomirov@3: // XXX boolean retVal to indicate whether to continue? tikhomirov@24: // TODO specify nodeid and data length, and reuse policy (i.e. if revlog stream doesn't reuse nodeid[] for each call) tikhomirov@24: void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[/*20*/] nodeid, byte[] data); tikhomirov@2: } tikhomirov@29: tikhomirov@29: /* tikhomirov@29: * XXX think over if it's better to do either: tikhomirov@29: * pw = getChangelog().new ParentWalker(); pw.init() and pass pw instance around as needed tikhomirov@29: * or tikhomirov@29: * add Revlog#getParentWalker(), static class, make cons() and #init package-local, and keep SoftReference to allow walker reuse. tikhomirov@29: * tikhomirov@29: * and yes, walker is not a proper name tikhomirov@29: */ tikhomirov@29: public final class ParentWalker { tikhomirov@29: private Map firstParent; tikhomirov@29: private Map secondParent; tikhomirov@29: private Set allNodes; tikhomirov@29: tikhomirov@29: public ParentWalker() { tikhomirov@29: firstParent = secondParent = Collections.emptyMap(); tikhomirov@29: allNodes = Collections.emptySet(); tikhomirov@29: } tikhomirov@29: tikhomirov@29: public void init() { tikhomirov@29: final RevlogStream stream = Revlog.this.content; tikhomirov@29: final int revisionCount = stream.revisionCount(); tikhomirov@29: firstParent = new HashMap(revisionCount); tikhomirov@29: secondParent = new HashMap(firstParent.size() >> 1); // assume branches/merges are less frequent tikhomirov@29: allNodes = new LinkedHashSet(); tikhomirov@29: tikhomirov@29: Inspector insp = new Inspector() { tikhomirov@29: final Nodeid[] sequentialRevisionNodeids = new Nodeid[revisionCount]; tikhomirov@29: int ix = 0; tikhomirov@29: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { tikhomirov@29: if (ix != revisionNumber) { tikhomirov@29: // XXX temp code, just to make sure I understand what's going on here tikhomirov@29: throw new IllegalStateException(); tikhomirov@29: } tikhomirov@29: if (parent1Revision >= revisionNumber || parent2Revision >= revisionNumber) { tikhomirov@29: throw new IllegalStateException(); // sanity, revisions are sequential tikhomirov@29: } tikhomirov@29: final Nodeid nid = new Nodeid(nodeid, true); tikhomirov@29: sequentialRevisionNodeids[ix++] = nid; tikhomirov@29: allNodes.add(nid); tikhomirov@29: if (parent1Revision != -1) { tikhomirov@29: firstParent.put(nid, sequentialRevisionNodeids[parent1Revision]); tikhomirov@29: if (parent2Revision != -1) { tikhomirov@29: secondParent.put(nid, sequentialRevisionNodeids[parent2Revision]); tikhomirov@29: } tikhomirov@29: } tikhomirov@29: } tikhomirov@29: }; tikhomirov@29: stream.iterate(0, -1, false, insp); tikhomirov@29: } tikhomirov@29: tikhomirov@29: public Set allNodes() { tikhomirov@29: return Collections.unmodifiableSet(allNodes); tikhomirov@29: } tikhomirov@31: tikhomirov@31: // FIXME need to decide whether Nodeid(00 * 20) is always known or not tikhomirov@31: public boolean knownNode(Nodeid nid) { tikhomirov@31: return allNodes.contains(nid); tikhomirov@31: } tikhomirov@29: tikhomirov@29: // null if none tikhomirov@29: public Nodeid firstParent(Nodeid nid) { tikhomirov@29: return firstParent.get(nid); tikhomirov@29: } tikhomirov@49: tikhomirov@49: // never null, Nodeid.NULL if none known tikhomirov@49: public Nodeid safeFirstParent(Nodeid nid) { tikhomirov@49: Nodeid rv = firstParent(nid); tikhomirov@49: return rv == null ? Nodeid.NULL : rv; tikhomirov@49: } tikhomirov@29: tikhomirov@29: public Nodeid secondParent(Nodeid nid) { tikhomirov@29: return secondParent.get(nid); tikhomirov@29: } tikhomirov@29: tikhomirov@49: public Nodeid safeSecondParent(Nodeid nid) { tikhomirov@49: Nodeid rv = secondParent(nid); tikhomirov@49: return rv == null ? Nodeid.NULL : rv; tikhomirov@49: } tikhomirov@49: tikhomirov@29: public boolean appendParentsOf(Nodeid nid, Collection c) { tikhomirov@29: Nodeid p1 = firstParent(nid); tikhomirov@29: boolean modified = false; tikhomirov@29: if (p1 != null) { tikhomirov@29: modified = c.add(p1); tikhomirov@29: Nodeid p2 = secondParent(nid); tikhomirov@29: if (p2 != null) { tikhomirov@29: modified = c.add(p2) || modified; tikhomirov@29: } tikhomirov@29: } tikhomirov@29: return modified; tikhomirov@29: } tikhomirov@29: } tikhomirov@2: }