tikhomirov@432: /*
tikhomirov@628: * Copyright (c) 2011-2013 TMate Software Ltd
tikhomirov@432: *
tikhomirov@432: * This program is free software; you can redistribute it and/or modify
tikhomirov@432: * it under the terms of the GNU General Public License as published by
tikhomirov@432: * the Free Software Foundation; version 2 of the License.
tikhomirov@432: *
tikhomirov@432: * This program is distributed in the hope that it will be useful,
tikhomirov@432: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@432: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@432: * GNU General Public License for more details.
tikhomirov@432: *
tikhomirov@432: * For information on how to redistribute this software under
tikhomirov@432: * the terms of a license other than GNU General Public License
tikhomirov@432: * contact TMate Software at support@hg4j.com
tikhomirov@432: */
tikhomirov@432: package org.tmatesoft.hg.repo;
tikhomirov@432:
tikhomirov@432: import static org.tmatesoft.hg.repo.HgRepository.TIP;
tikhomirov@432:
tikhomirov@432: import java.util.Arrays;
tikhomirov@432: import java.util.Collection;
tikhomirov@432: import java.util.HashSet;
tikhomirov@432: import java.util.LinkedList;
tikhomirov@432: import java.util.List;
tikhomirov@432:
tikhomirov@432: import org.tmatesoft.hg.core.Nodeid;
tikhomirov@432: import org.tmatesoft.hg.repo.Revlog.ParentInspector;
tikhomirov@432:
tikhomirov@432: /**
tikhomirov@432: * Helper class to deal with parent-child relationship between revisions en masse.
tikhomirov@432: * Works in terms of {@link Nodeid nodeids}, there's no need to deal with revision indexes.
tikhomirov@432: * For a given revision, answers questions like "who's my parent and what are my immediate children".
tikhomirov@432: *
tikhomirov@432: *
Comes handy when multiple revisions are analyzed and distinct {@link Revlog#parents(int, int[], byte[], byte[])}
tikhomirov@432: * queries are ineffective.
tikhomirov@432: *
tikhomirov@432: *
Next code snippet shows typical use:
tikhomirov@432: *
tikhomirov@432: * HgChangelog clog = repo.getChangelog();
tikhomirov@432: * ParentWalker<HgChangelog> pw = new ParentWalker<HgChangelog>(clog);
tikhomirov@432: * pw.init();
tikhomirov@432: *
tikhomirov@432: * Nodeid me = Nodeid.fromAscii("...");
tikhomirov@432: * List immediateChildren = pw.directChildren(me);
tikhomirov@432: *
tikhomirov@432: *
tikhomirov@432: *
tikhomirov@432: * Perhaps, later may add alternative way to access (and reuse) map instance, Revlog#getParentWalker(),
tikhomirov@432: * that instantiates and initializes ParentWalker, and keep SoftReference to allow its reuse.
tikhomirov@432: *
tikhomirov@433: * @see HgRevisionMap
tikhomirov@432: *
tikhomirov@432: * @author Artem Tikhomirov
tikhomirov@432: * @author TMate Software Ltd.
tikhomirov@432: */
tikhomirov@432: public final class HgParentChildMap implements ParentInspector {
tikhomirov@432:
tikhomirov@432:
tikhomirov@432: private Nodeid[] sequential; // natural repository order, childrenOf rely on ordering
tikhomirov@432: private Nodeid[] sorted; // for binary search
tikhomirov@432: private int[] sorted2natural;
tikhomirov@432: private Nodeid[] firstParent;
tikhomirov@432: private Nodeid[] secondParent;
tikhomirov@432: private final T revlog;
tikhomirov@432:
tikhomirov@432: // Nodeid instances shall be shared between all arrays
tikhomirov@432:
tikhomirov@432: public HgParentChildMap(T owner) {
tikhomirov@432: revlog = owner;
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: public HgRepository getRepo() {
tikhomirov@432: return revlog.getRepo();
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: public void next(int revisionNumber, Nodeid revision, int parent1Revision, int parent2Revision, Nodeid nidParent1, Nodeid nidParent2) {
tikhomirov@432: if (parent1Revision >= revisionNumber || parent2Revision >= revisionNumber) {
tikhomirov@432: throw new IllegalStateException(); // sanity, revisions are sequential
tikhomirov@432: }
tikhomirov@432: int ix = revisionNumber;
tikhomirov@432: sequential[ix] = sorted[ix] = revision;
tikhomirov@432: if (parent1Revision != -1) {
tikhomirov@432: firstParent[ix] = sequential[parent1Revision];
tikhomirov@432: }
tikhomirov@432: if (parent2Revision != -1) { // revlog of DataAccess.java has p2 set when p1 is -1
tikhomirov@432: secondParent[ix] = sequential[parent2Revision];
tikhomirov@432: }
tikhomirov@432: }
tikhomirov@432:
tikhomirov@628: /**
tikhomirov@628: * Prepare the map
tikhomirov@628: * @throws HgInvalidControlFileException if failed to access revlog index/data entry. Runtime exception
tikhomirov@628: * @throws HgRuntimeException subclass thereof to indicate other issues with the library. Runtime exception
tikhomirov@628: */
tikhomirov@628: public void init() throws HgRuntimeException {
tikhomirov@432: final int revisionCount = revlog.getRevisionCount();
tikhomirov@432: firstParent = new Nodeid[revisionCount];
tikhomirov@432: // TODO [post 1.0] Branches/merges are less frequent, and most of secondParent would be -1/null, hence
tikhomirov@432: // IntMap might be better alternative here, but need to carefully analyze (test) whether this brings
tikhomirov@432: // real improvement (IntMap has 2n capacity, and element lookup is log(n) instead of array's constant)
tikhomirov@432: secondParent = new Nodeid[revisionCount];
tikhomirov@432: //
tikhomirov@432: sequential = new Nodeid[revisionCount];
tikhomirov@432: sorted = new Nodeid[revisionCount];
tikhomirov@432: revlog.indexWalk(0, TIP, this);
tikhomirov@432: Arrays.sort(sorted);
tikhomirov@432: sorted2natural = new int[revisionCount];
tikhomirov@432: for (int i = 0; i < revisionCount; i++) {
tikhomirov@432: Nodeid n = sequential[i];
tikhomirov@432: int x = Arrays.binarySearch(sorted, n);
tikhomirov@432: assertSortedIndex(x);
tikhomirov@432: sorted2natural[x] = i;
tikhomirov@432: }
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: private void assertSortedIndex(int x) {
tikhomirov@432: if (x < 0) {
tikhomirov@432: throw new HgInvalidStateException(String.format("Bad index", x));
tikhomirov@432: }
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: /**
tikhomirov@432: * Tells whether supplied revision is from the walker's associated revlog.
tikhomirov@432: * Note, {@link Nodeid#NULL}, although implicitly present as parent of a first revision, is not recognized as known.
tikhomirov@432: * @param nid revision to check, not null
tikhomirov@432: * @return true
if revision matches any revision in this revlog
tikhomirov@432: */
tikhomirov@432: public boolean knownNode(Nodeid nid) {
tikhomirov@432: return Arrays.binarySearch(sorted, nid) >= 0;
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: /**
tikhomirov@432: * null if none. only known nodes (as per #knownNode) are accepted as arguments
tikhomirov@432: */
tikhomirov@432: public Nodeid firstParent(Nodeid nid) {
tikhomirov@432: int x = Arrays.binarySearch(sorted, nid);
tikhomirov@432: assertSortedIndex(x);
tikhomirov@432: int i = sorted2natural[x];
tikhomirov@432: return firstParent[i];
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: // never null, Nodeid.NULL if none known
tikhomirov@432: public Nodeid safeFirstParent(Nodeid nid) {
tikhomirov@432: Nodeid rv = firstParent(nid);
tikhomirov@432: return rv == null ? Nodeid.NULL : rv;
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: public Nodeid secondParent(Nodeid nid) {
tikhomirov@432: int x = Arrays.binarySearch(sorted, nid);
tikhomirov@432: assertSortedIndex(x);
tikhomirov@432: int i = sorted2natural[x];
tikhomirov@432: return secondParent[i];
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: public Nodeid safeSecondParent(Nodeid nid) {
tikhomirov@432: Nodeid rv = secondParent(nid);
tikhomirov@432: return rv == null ? Nodeid.NULL : rv;
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: public boolean appendParentsOf(Nodeid nid, Collection c) {
tikhomirov@432: int x = Arrays.binarySearch(sorted, nid);
tikhomirov@432: assertSortedIndex(x);
tikhomirov@432: int i = sorted2natural[x];
tikhomirov@432: Nodeid p1 = firstParent[i];
tikhomirov@432: boolean modified = false;
tikhomirov@432: if (p1 != null) {
tikhomirov@432: modified = c.add(p1);
tikhomirov@432: }
tikhomirov@432: Nodeid p2 = secondParent[i];
tikhomirov@432: if (p2 != null) {
tikhomirov@432: modified = c.add(p2) || modified;
tikhomirov@432: }
tikhomirov@432: return modified;
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: // XXX alternative (and perhaps more reliable) approach would be to make a copy of allNodes and remove
tikhomirov@432: // nodes, their parents and so on.
tikhomirov@432:
tikhomirov@432: // @return ordered collection of all children rooted at supplied nodes. Nodes shall not be descendants of each other!
tikhomirov@432: // Nodeids shall belong to this revlog
tikhomirov@432: public List childrenOf(List roots) {
tikhomirov@432: HashSet parents = new HashSet();
tikhomirov@432: LinkedList result = new LinkedList();
tikhomirov@432: int earliestRevision = Integer.MAX_VALUE;
tikhomirov@432: assert sequential.length == firstParent.length && firstParent.length == secondParent.length;
tikhomirov@432: // first, find earliest index of roots in question, as there's no sense
tikhomirov@432: // to check children among nodes prior to branch's root node
tikhomirov@432: for (Nodeid r : roots) {
tikhomirov@432: int x = Arrays.binarySearch(sorted, r);
tikhomirov@432: assertSortedIndex(x);
tikhomirov@432: int i = sorted2natural[x];
tikhomirov@432: if (i < earliestRevision) {
tikhomirov@432: earliestRevision = i;
tikhomirov@432: }
tikhomirov@432: parents.add(sequential[i]); // add canonical instance in hope equals() is bit faster when can do a ==
tikhomirov@432: }
tikhomirov@432: for (int i = earliestRevision + 1; i < sequential.length; i++) {
tikhomirov@432: if (parents.contains(firstParent[i]) || parents.contains(secondParent[i])) {
tikhomirov@432: parents.add(sequential[i]); // to find next child
tikhomirov@432: result.add(sequential[i]);
tikhomirov@432: }
tikhomirov@432: }
tikhomirov@432: return result;
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: /**
tikhomirov@432: * @return revisions that have supplied revision as their immediate parent
tikhomirov@432: */
tikhomirov@432: public List directChildren(Nodeid nid) {
tikhomirov@432: LinkedList result = new LinkedList();
tikhomirov@432: int x = Arrays.binarySearch(sorted, nid);
tikhomirov@432: assertSortedIndex(x);
tikhomirov@432: nid = sorted[x]; // canonical instance
tikhomirov@432: int start = sorted2natural[x];
tikhomirov@432: for (int i = start + 1; i < sequential.length; i++) {
tikhomirov@432: if (nid == firstParent[i] || nid == secondParent[i]) {
tikhomirov@432: result.add(sequential[i]);
tikhomirov@432: }
tikhomirov@432: }
tikhomirov@432: return result;
tikhomirov@432: }
tikhomirov@432:
tikhomirov@432: /**
tikhomirov@432: * @param nid possibly parent node, shall be {@link #knownNode(Nodeid) known} in this revlog.
tikhomirov@432: * @return true
if there's any node in this revlog that has specified node as one of its parents.
tikhomirov@432: */
tikhomirov@432: public boolean hasChildren(Nodeid nid) {
tikhomirov@432: int x = Arrays.binarySearch(sorted, nid);
tikhomirov@432: assertSortedIndex(x);
tikhomirov@432: int i = sorted2natural[x];
tikhomirov@432: assert firstParent.length == secondParent.length; // just in case later I implement sparse array for secondParent
tikhomirov@432: assert firstParent.length == sequential.length;
tikhomirov@432: // to use == instead of equals, take the same Nodeid instance we used to fill all the arrays.
tikhomirov@432: final Nodeid canonicalNode = sequential[i];
tikhomirov@432: i++; // no need to check node itself. child nodes may appear in sequential only after revision in question
tikhomirov@432: for (; i < sequential.length; i++) {
tikhomirov@432: // TODO [post 1.0] likely, not very effective.
tikhomirov@432: // May want to optimize it with another (Tree|Hash)Set, created on demand on first use,
tikhomirov@432: // however, need to be careful with memory usage
tikhomirov@432: if (firstParent[i] == canonicalNode || secondParent[i] == canonicalNode) {
tikhomirov@432: return true;
tikhomirov@432: }
tikhomirov@432: }
tikhomirov@432: return false;
tikhomirov@432: }
tikhomirov@432: }