tikhomirov@13: /* tikhomirov@537: * 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@74: tikhomirov@390: import static org.tmatesoft.hg.core.Nodeid.NULL; tikhomirov@423: import static org.tmatesoft.hg.repo.HgRepository.*; tikhomirov@456: import static org.tmatesoft.hg.util.LogFacility.Severity.Info; tikhomirov@218: tikhomirov@157: import java.io.IOException; tikhomirov@218: import java.util.Arrays; tikhomirov@157: tikhomirov@417: import org.tmatesoft.hg.core.HgChangesetFileSneaker; tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@574: import org.tmatesoft.hg.internal.ByteVector; tikhomirov@423: import org.tmatesoft.hg.internal.Callback; tikhomirov@157: import org.tmatesoft.hg.internal.DataAccess; tikhomirov@266: import org.tmatesoft.hg.internal.DigestHelper; tikhomirov@320: import org.tmatesoft.hg.internal.EncodingHelper; tikhomirov@574: import org.tmatesoft.hg.internal.IdentityPool; tikhomirov@415: import org.tmatesoft.hg.internal.IntMap; tikhomirov@597: import org.tmatesoft.hg.internal.IntVector; tikhomirov@312: import org.tmatesoft.hg.internal.IterateControlMediator; tikhomirov@218: import org.tmatesoft.hg.internal.Lifecycle; tikhomirov@600: import org.tmatesoft.hg.internal.RevisionLookup; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@597: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; tikhomirov@312: import org.tmatesoft.hg.util.CancelSupport; tikhomirov@574: import org.tmatesoft.hg.util.LogFacility.Severity; tikhomirov@232: import org.tmatesoft.hg.util.Path; tikhomirov@312: import org.tmatesoft.hg.util.ProgressSupport; tikhomirov@74: tikhomirov@2: tikhomirov@2: /** tikhomirov@425: * Representation of Mercurial manifest file (list of file names and their revisions in a particular changeset) tikhomirov@425: * tikhomirov@425: * @see http://mercurial.selenic.com/wiki/Manifest tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@2: */ tikhomirov@426: public final class HgManifest extends Revlog { tikhomirov@218: private RevisionMapper revisionMap; tikhomirov@574: private final EncodingHelper encodingHelper; tikhomirov@574: private final Path.Source pathFactory; tikhomirov@285: tikhomirov@415: /** tikhomirov@415: * File flags recorded in manifest tikhomirov@415: */ tikhomirov@285: public enum Flags { tikhomirov@415: /** tikhomirov@415: * Executable bit set tikhomirov@415: */ tikhomirov@415: Exec, tikhomirov@415: /** tikhomirov@415: * Symbolic link tikhomirov@415: */ tikhomirov@415: Link, tikhomirov@415: /** tikhomirov@415: * Regular file tikhomirov@415: */ tikhomirov@415: RegularFile; tikhomirov@285: tikhomirov@285: static Flags parse(String flags) { tikhomirov@285: if ("x".equalsIgnoreCase(flags)) { tikhomirov@285: return Exec; tikhomirov@285: } tikhomirov@285: if ("l".equalsIgnoreCase(flags)) { tikhomirov@285: return Link; tikhomirov@285: } tikhomirov@285: if (flags == null) { tikhomirov@415: return RegularFile; tikhomirov@285: } tikhomirov@285: throw new IllegalStateException(flags); tikhomirov@285: } tikhomirov@285: tikhomirov@285: static Flags parse(byte[] data, int start, int length) { tikhomirov@285: if (length == 0) { tikhomirov@415: return RegularFile; tikhomirov@285: } tikhomirov@285: if (length == 1) { tikhomirov@285: if (data[start] == 'x') { tikhomirov@285: return Exec; tikhomirov@285: } tikhomirov@285: if (data[start] == 'l') { tikhomirov@285: return Link; tikhomirov@285: } tikhomirov@285: // FALL THROUGH tikhomirov@285: } tikhomirov@285: throw new IllegalStateException(new String(data, start, length)); tikhomirov@285: } tikhomirov@285: tikhomirov@285: String nativeString() { tikhomirov@285: if (this == Exec) { tikhomirov@285: return "x"; tikhomirov@285: } tikhomirov@285: if (this == Link) { tikhomirov@285: return "l"; tikhomirov@285: } tikhomirov@415: if (this == RegularFile) { tikhomirov@415: return ""; tikhomirov@415: } tikhomirov@285: throw new IllegalStateException(toString()); tikhomirov@285: } tikhomirov@574: tikhomirov@574: public int fsMode() { tikhomirov@574: if (this == Exec) { tikhomirov@574: return 0755; tikhomirov@574: } tikhomirov@574: return 0644; tikhomirov@574: } tikhomirov@285: } tikhomirov@2: tikhomirov@412: /*package-local*/ HgManifest(HgRepository hgRepo, RevlogStream content, EncodingHelper eh) { tikhomirov@600: super(hgRepo, content, true); tikhomirov@412: encodingHelper = eh; tikhomirov@574: pathFactory = hgRepo.getSessionContext().getPathFactory(); tikhomirov@13: } tikhomirov@13: tikhomirov@218: /** tikhomirov@390: * Walks manifest revisions that correspond to specified range of changesets. The order in which manifest versions get reported tikhomirov@390: * to the inspector corresponds to physical order of manifest revisions, not that of changesets (with few exceptions as noted below). tikhomirov@390: * That is, for cset-manifest revision pairs: tikhomirov@390: *
tikhomirov@390: 	 *   3  8
tikhomirov@390: 	 *   4  7
tikhomirov@390: 	 *   5  9
tikhomirov@390: 	 * 
tikhomirov@390: * call walk(3,5, insp) would yield (4,7), (3,8) and (5,9) to the inspector; tikhomirov@390: * different order of arguments, walk(5, 3, insp), makes no difference. tikhomirov@390: * tikhomirov@390: *

Physical layout of mercurial files (revlog) doesn't impose any restriction on whether manifest and changeset revisions shall go tikhomirov@390: * incrementally, nor it mandates presence of manifest version for a changeset. Thus, there might be changesets that record {@link Nodeid#NULL} tikhomirov@390: * as corresponding manifest revision. This situation is deemed exceptional now and what would inspector get depends on whether tikhomirov@390: * start or end arguments point to such changeset, or such changeset happen to be somewhere inside the range tikhomirov@423: * [start..end]. Implementation does it best to report empty manifests tikhomirov@423: * (Inspector.begin(HgRepository.NO_REVISION, NULL, csetRevIndex); tikhomirov@423: * followed immediately by Inspector.end(HgRepository.NO_REVISION) tikhomirov@423: * when start and/or end point to changeset with no associated tikhomirov@390: * manifest revision. However, if changeset-manifest revision pairs look like: tikhomirov@390: *

tikhomirov@390: 	 *   3  8
tikhomirov@390: 	 *   4  -1 (cset records null revision for manifest)
tikhomirov@390: 	 *   5  9
tikhomirov@390: 	 * 
tikhomirov@390: * call walk(3,5, insp) would yield only (3,8) and (5,9) to the inspector, without additional empty tikhomirov@390: * Inspector.begin(); Inspector.end() call pair. tikhomirov@218: * tikhomirov@423: * @see HgRepository#NO_REVISION tikhomirov@218: * @param start changelog (not manifest!) revision to begin with tikhomirov@218: * @param end changelog (not manifest!) revision to stop, inclusive. tikhomirov@390: * @param inspector manifest revision visitor, can't be null tikhomirov@423: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@423: * @throws IllegalArgumentException if inspector callback is null tikhomirov@218: */ tikhomirov@423: public void walk(int start, int end, final Inspector inspector) throws HgRuntimeException, IllegalArgumentException { tikhomirov@196: if (inspector == null) { tikhomirov@196: throw new IllegalArgumentException(); tikhomirov@196: } tikhomirov@390: final int csetFirst = start <= end ? start : end, csetLast = start > end ? start : end; tikhomirov@390: int manifestFirst, manifestLast, i = 0; tikhomirov@390: do { tikhomirov@390: manifestFirst = fromChangelog(csetFirst+i); tikhomirov@403: if (manifestFirst == BAD_REVISION) { tikhomirov@423: inspector.begin(NO_REVISION, NULL, csetFirst+i); tikhomirov@423: inspector.end(NO_REVISION); tikhomirov@390: } tikhomirov@390: i++; tikhomirov@403: } while (manifestFirst == BAD_REVISION && csetFirst+i <= csetLast); tikhomirov@403: if (manifestFirst == BAD_REVISION) { tikhomirov@490: getRepo().getSessionContext().getLog().dump(getClass(), Info, "None of changesets [%d..%d] have associated manifest revision", csetFirst, csetLast); tikhomirov@390: // we ran through all revisions in [start..end] and none of them had manifest. tikhomirov@390: // we reported that to inspector and proceeding is done now. tikhomirov@390: return; tikhomirov@390: } tikhomirov@390: i = 0; tikhomirov@390: do { tikhomirov@390: manifestLast = fromChangelog(csetLast-i); tikhomirov@403: if (manifestLast == BAD_REVISION) { tikhomirov@423: inspector.begin(NO_REVISION, NULL, csetLast-i); tikhomirov@423: inspector.end(NO_REVISION); tikhomirov@390: } tikhomirov@390: i++; tikhomirov@403: } while (manifestLast == BAD_REVISION && csetLast-i >= csetFirst); tikhomirov@403: if (manifestLast == BAD_REVISION) { tikhomirov@423: // hmm, manifestFirst != BAD_REVISION here, hence there's i from [csetFirst..csetLast] for which manifest entry exists, tikhomirov@423: // and thus it's impossible to run into manifestLast == BAD_REVISION. Nevertheless, never hurts to check. tikhomirov@423: throw new HgInvalidStateException(String.format("Manifest %d-%d(!) for cset range [%d..%d] ", manifestFirst, manifestLast, csetFirst, csetLast)); tikhomirov@390: } tikhomirov@390: if (manifestLast < manifestFirst) { tikhomirov@300: // there are tool-constructed repositories that got order of changeset revisions completely different from that of manifest tikhomirov@390: int x = manifestLast; tikhomirov@390: manifestLast = manifestFirst; tikhomirov@390: manifestFirst = x; tikhomirov@300: } tikhomirov@571: content.iterate(manifestFirst, manifestLast, true, new ManifestParser(inspector)); tikhomirov@19: } tikhomirov@218: tikhomirov@254: /** tikhomirov@390: * "Sparse" iteration of the manifest, more effective than accessing revisions one by one. tikhomirov@390: *

Inspector is invoked for each changeset revision supplied, even when there's no manifest tikhomirov@390: * revision associated with a changeset (@see {@link #walk(int, int, Inspector)} for more details when it happens). Order inspector tikhomirov@390: * gets invoked doesn't resemble order of changeset revisions supplied, manifest revisions are reported in the order they appear tikhomirov@390: * in manifest revlog (with exception of changesets with missing manifest that may be reported in any order). tikhomirov@254: * tikhomirov@390: * @param inspector manifest revision visitor, can't be null tikhomirov@390: * @param revisionIndexes local indexes of changesets to visit, non-null tikhomirov@423: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@423: * @throws InvalidArgumentException if supplied arguments are nulls tikhomirov@254: */ tikhomirov@423: public void walk(final Inspector inspector, int... revisionIndexes) throws HgRuntimeException, IllegalArgumentException { tikhomirov@367: if (inspector == null || revisionIndexes == null) { tikhomirov@254: throw new IllegalArgumentException(); tikhomirov@254: } tikhomirov@390: int[] manifestRevs = toManifestRevisionIndexes(revisionIndexes, inspector); tikhomirov@571: content.iterate(manifestRevs, true, new ManifestParser(inspector)); tikhomirov@254: } tikhomirov@254: tikhomirov@390: // tikhomirov@390: /** tikhomirov@403: * Tells manifest revision number that corresponds to the given changeset. May return {@link HgRepository#BAD_REVISION} tikhomirov@403: * if changeset has no associated manifest (cset records NULL nodeid for manifest). tikhomirov@403: * @return manifest revision index, non-negative, or {@link HgRepository#BAD_REVISION}. tikhomirov@403: * @throws HgInvalidRevisionException if method argument specifies non-existent revision index tikhomirov@403: * @throws HgInvalidControlFileException if access to revlog index/data entry failed tikhomirov@390: */ tikhomirov@403: /*package-local*/ int fromChangelog(int changesetRevisionIndex) throws HgInvalidRevisionException, HgInvalidControlFileException { tikhomirov@368: if (HgInternals.wrongRevisionIndex(changesetRevisionIndex)) { tikhomirov@425: throw new HgInvalidRevisionException(changesetRevisionIndex); tikhomirov@218: } tikhomirov@368: if (changesetRevisionIndex == HgRepository.WORKING_COPY || changesetRevisionIndex == HgRepository.BAD_REVISION) { tikhomirov@425: throw new HgInvalidRevisionException("Can't use constants like WORKING_COPY or BAD_REVISION", null, changesetRevisionIndex); tikhomirov@257: } tikhomirov@257: // revisionNumber == TIP is processed by RevisionMapper tikhomirov@218: if (revisionMap == null) { tikhomirov@600: revisionMap = new RevisionMapper(super.revisionLookup == null); tikhomirov@218: content.iterate(0, TIP, false, revisionMap); tikhomirov@600: revisionMap.fixReusedManifests(); tikhomirov@600: if (super.useRevisionLookup && super.revisionLookup == null) { tikhomirov@600: // reuse RevisionLookup if there's none yet tikhomirov@600: super.revisionLookup = revisionMap.manifestNodeids; tikhomirov@600: } tikhomirov@600: revisionMap.manifestNodeids = null; tikhomirov@218: } tikhomirov@368: return revisionMap.at(changesetRevisionIndex); tikhomirov@218: } tikhomirov@218: tikhomirov@232: /** tikhomirov@232: * Extracts file revision as it was known at the time of given changeset. tikhomirov@426: *

For more thorough details about file at specific changeset, use {@link HgChangesetFileSneaker}. tikhomirov@426: *

To visit few changesets for the same file, use {@link #walkFileRevisions(Path, Inspector, int...)} tikhomirov@232: * tikhomirov@426: * @see #walkFileRevisions(Path, Inspector, int...) tikhomirov@417: * @see HgChangesetFileSneaker tikhomirov@367: * @param changelogRevisionIndex local changeset index tikhomirov@232: * @param file path to file in question tikhomirov@232: * @return file revision or null if manifest at specified revision doesn't list such file tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@232: */ tikhomirov@403: public Nodeid getFileRevision(int changelogRevisionIndex, final Path file) throws HgInvalidRevisionException, HgInvalidControlFileException { tikhomirov@417: // there's no need for HgDataFile to own this method, or get a delegate tikhomirov@417: // as most of HgDataFile API is using file revision indexes, and there's easy step from file revision index to tikhomirov@417: // both file revision and changeset revision index. But there's no easy way to go from changesetRevisionIndex to tikhomirov@547: // file revision (the task this method solves), except for HgFileInformer tikhomirov@417: // I feel methods dealing with changeset indexes shall be more exposed in HgChangelog and HgManifest API. tikhomirov@547: // TODO need tests (e.g. pass TIP here to see resMap.get(-1) doesn't fail) tikhomirov@426: int manifestRevIndex = fromChangelog(changelogRevisionIndex); tikhomirov@426: if (manifestRevIndex == BAD_REVISION) { tikhomirov@426: return null; tikhomirov@426: } tikhomirov@426: IntMap resMap = new IntMap(3); tikhomirov@426: FileLookupInspector parser = new FileLookupInspector(encodingHelper, file, resMap, null); tikhomirov@426: parser.walk(manifestRevIndex, content); tikhomirov@550: assert resMap.size() <= 1; // size() == 0 if file wasn't found tikhomirov@547: // can't use changelogRevisionIndex as key - it might have been TIP tikhomirov@550: return resMap.size() == 0 ? null : resMap.get(resMap.firstKey()); tikhomirov@326: } tikhomirov@426: tikhomirov@426: /** tikhomirov@426: * Visit file revisions as they were recorded at the time of given changesets. Same file revision may be reported as many times as tikhomirov@426: * there are changesets that refer to that revision. Both {@link Inspector#begin(int, Nodeid, int)} and {@link Inspector#end(int)} tikhomirov@426: * with appropriate values are invoked around {@link Inspector#next(Nodeid, Path, Flags)} call for the supplied file tikhomirov@426: * tikhomirov@426: *

NOTE, this method doesn't respect return values from callback (i.e. to stop iteration), as it's lookup of a single file tikhomirov@426: * and canceling it seems superfluous. However, this may change in future and it's recommended to return true from tikhomirov@426: * all {@link Inspector} methods. tikhomirov@426: * tikhomirov@426: * @see #getFileRevision(int, Path) tikhomirov@426: * @param file path of interest tikhomirov@426: * @param inspector callback to receive details about selected file tikhomirov@426: * @param changelogRevisionIndexes changeset indexes to visit tikhomirov@426: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@426: */ tikhomirov@426: public void walkFileRevisions(Path file, Inspector inspector, int... changelogRevisionIndexes) throws HgRuntimeException { tikhomirov@426: if (file == null || inspector == null || changelogRevisionIndexes == null) { tikhomirov@426: throw new IllegalArgumentException(); tikhomirov@426: } tikhomirov@426: // TODO [post-1.0] need tests. There's Main#checkWalkFileRevisions that may be a starting point tikhomirov@426: int[] manifestRevIndexes = toManifestRevisionIndexes(changelogRevisionIndexes, null); tikhomirov@426: FileLookupInspector parser = new FileLookupInspector(encodingHelper, file, inspector); tikhomirov@426: parser.walk(manifestRevIndexes, content); tikhomirov@232: } tikhomirov@326: tikhomirov@415: /** tikhomirov@417: * Extract file {@link Flags flags} as they were recorded in appropriate manifest version. tikhomirov@417: * tikhomirov@417: * @see HgDataFile#getFlags(int) tikhomirov@415: * @param changesetRevIndex changeset revision index tikhomirov@415: * @param file path to look up tikhomirov@417: * @return one of predefined enum values, or null if file was not known in the specified revision tikhomirov@425: * @throws HgRuntimeException subclass thereof to indicate issues with the library. Runtime exception tikhomirov@415: */ tikhomirov@417: public Flags getFileFlags(int changesetRevIndex, Path file) throws HgInvalidRevisionException, HgInvalidControlFileException { tikhomirov@415: int manifestRevIdx = fromChangelog(changesetRevIndex); tikhomirov@415: IntMap resMap = new IntMap(2); tikhomirov@426: FileLookupInspector parser = new FileLookupInspector(encodingHelper, file, null, resMap); tikhomirov@426: parser.walk(manifestRevIdx, content); tikhomirov@550: assert resMap.size() <= 1; // size() == 0 if not found tikhomirov@547: // can't use changesetRevIndex as key - it might have been TIP tikhomirov@550: return resMap.size() == 0 ? null : resMap.get(resMap.firstKey()); tikhomirov@415: } tikhomirov@415: tikhomirov@326: tikhomirov@390: /** tikhomirov@390: * @param changelogRevisionIndexes non-null tikhomirov@390: * @param inspector may be null if reporting of missing manifests is not needed tikhomirov@403: * @throws HgInvalidRevisionException if arguments specify non-existent revision index tikhomirov@403: * @throws IllegalArgumentException if any index argument is not a revision index tikhomirov@403: * @throws HgInvalidControlFileException if access to revlog index/data entry failed tikhomirov@390: */ tikhomirov@403: private int[] toManifestRevisionIndexes(int[] changelogRevisionIndexes, Inspector inspector) throws HgInvalidRevisionException, HgInvalidControlFileException { tikhomirov@368: int[] manifestRevs = new int[changelogRevisionIndexes.length]; tikhomirov@326: boolean needsSort = false; tikhomirov@390: int j = 0; tikhomirov@367: for (int i = 0; i < changelogRevisionIndexes.length; i++) { tikhomirov@367: final int manifestRevisionIndex = fromChangelog(changelogRevisionIndexes[i]); tikhomirov@403: if (manifestRevisionIndex == BAD_REVISION) { tikhomirov@390: if (inspector != null) { tikhomirov@423: inspector.begin(NO_REVISION, NULL, changelogRevisionIndexes[i]); tikhomirov@423: inspector.end(NO_REVISION); tikhomirov@390: } tikhomirov@390: // othrwise, ignore changeset without manifest tikhomirov@390: } else { tikhomirov@390: manifestRevs[j] = manifestRevisionIndex; tikhomirov@390: if (j > 0 && manifestRevs[j-1] > manifestRevisionIndex) { tikhomirov@390: needsSort = true; tikhomirov@390: } tikhomirov@390: j++; tikhomirov@326: } tikhomirov@326: } tikhomirov@326: if (needsSort) { tikhomirov@390: Arrays.sort(manifestRevs, 0, j); tikhomirov@326: } tikhomirov@390: if (j == manifestRevs.length) { tikhomirov@390: return manifestRevs; tikhomirov@390: } else { tikhomirov@390: int[] rv = new int[j]; tikhomirov@390: //Arrays.copyOfRange tikhomirov@390: System.arraycopy(manifestRevs, 0, rv, 0, j); tikhomirov@390: return rv; tikhomirov@390: } tikhomirov@326: } tikhomirov@326: tikhomirov@423: @Callback tikhomirov@19: public interface Inspector { tikhomirov@285: /** tikhomirov@537: * Denotes entering specific manifest revision, separate entries are tikhomirov@537: * reported with subsequence {@link #next(Nodeid, Path, Flags)} calls. tikhomirov@537: * tikhomirov@583: * @param manifestRevisionIndex local revision index of the inspected revision tikhomirov@537: * @param manifestRevision revision of the manifest we're about to iterate through tikhomirov@537: * @param changelogRevisionIndex local revision index of changelog this manifest points to tikhomirov@537: * @return true to continue iteration, false to stop tikhomirov@537: */ tikhomirov@583: boolean begin(int manifestRevisionIndex, Nodeid manifestRevision, int changelogRevisionIndex); tikhomirov@537: tikhomirov@537: tikhomirov@537: /** tikhomirov@537: * Reports each manifest entry tikhomirov@537: * tikhomirov@415: * @param nid file revision tikhomirov@415: * @param fname file name tikhomirov@415: * @param flags one of {@link HgManifest.Flags} constants, not null tikhomirov@415: * @return true to continue iteration, false to stop tikhomirov@415: */ tikhomirov@285: boolean next(Nodeid nid, Path fname, Flags flags); tikhomirov@537: tikhomirov@537: /** tikhomirov@537: * Denotes leaving specific manifest revision, after all entries were reported using {@link #next(Nodeid, Path, Flags)} tikhomirov@537: * tikhomirov@537: * @param manifestRevisionIndex indicates manifest revision, corresponds to opening {@link #begin(int, Nodeid, int)} tikhomirov@537: * @return true to continue iteration, false to stop tikhomirov@537: */ tikhomirov@537: boolean end(int manifestRevisionIndex); tikhomirov@267: } tikhomirov@424: tikhomirov@268: /** tikhomirov@268: * When Pool uses Strings directly, tikhomirov@268: * ManifestParser creates new String instance with new char[] value, and does byte->char conversion. tikhomirov@268: * For cpython repo, walk(0..10k), there are over 16 million filenames, of them only 3020 unique. tikhomirov@268: * This means there are 15.9 million useless char[] instances and byte->char conversions tikhomirov@268: * tikhomirov@285: * When String (Path) is wrapped into {@link PathProxy}, there's extra overhead of byte[] representation tikhomirov@285: * of the String, but these are only for unique Strings (Paths) (3020 in the example above). Besides, I save tikhomirov@268: * useless char[] and byte->char conversions. tikhomirov@268: */ tikhomirov@571: private final class PathProxy { tikhomirov@267: private byte[] data; tikhomirov@268: private int start; tikhomirov@268: private final int hash, length; tikhomirov@285: private Path result; tikhomirov@267: tikhomirov@571: public PathProxy(byte[] data, int start, int length) { tikhomirov@267: this.data = data; tikhomirov@267: this.start = start; tikhomirov@267: this.length = length; tikhomirov@267: tikhomirov@268: // copy from String.hashCode(). In fact, not necessarily match result of String(data).hashCode tikhomirov@268: // just need some nice algorithm here tikhomirov@267: int h = 0; tikhomirov@267: byte[] d = data; tikhomirov@267: for (int i = 0, off = start, len = length; i < len; i++) { tikhomirov@267: h = 31 * h + d[off++]; tikhomirov@267: } tikhomirov@267: hash = h; tikhomirov@267: } tikhomirov@267: tikhomirov@267: @Override tikhomirov@267: public boolean equals(Object obj) { tikhomirov@285: if (false == obj instanceof PathProxy) { tikhomirov@267: return false; tikhomirov@267: } tikhomirov@285: PathProxy o = (PathProxy) obj; tikhomirov@267: if (o.result != null && result != null) { tikhomirov@267: return result.equals(o.result); tikhomirov@267: } tikhomirov@268: if (o.length != length || o.hash != hash) { tikhomirov@267: return false; tikhomirov@267: } tikhomirov@267: for (int i = 0, x = o.start, y = start; i < length; i++) { tikhomirov@267: if (o.data[x++] != data[y++]) { tikhomirov@267: return false; tikhomirov@267: } tikhomirov@267: } tikhomirov@267: return true; tikhomirov@267: } tikhomirov@267: @Override tikhomirov@267: public int hashCode() { tikhomirov@267: return hash; tikhomirov@267: } tikhomirov@267: tikhomirov@285: public Path freeze() { tikhomirov@267: if (result == null) { tikhomirov@574: Path.Source pf = HgManifest.this.pathFactory; tikhomirov@574: result = pf.path(HgManifest.this.encodingHelper.fromManifest(data, start, length)); tikhomirov@268: // release reference to bigger data array, make a copy of relevant part only tikhomirov@285: // use original bytes, not those from String above to avoid cache misses due to different encodings tikhomirov@268: byte[] d = new byte[length]; tikhomirov@268: System.arraycopy(data, start, d, 0, length); tikhomirov@268: data = d; tikhomirov@268: start = 0; tikhomirov@267: } tikhomirov@267: return result; tikhomirov@267: } tikhomirov@267: } tikhomirov@196: tikhomirov@571: private class ManifestParser implements RevlogStream.Inspector, Lifecycle { tikhomirov@196: private final Inspector inspector; tikhomirov@431: private IdentityPool nodeidPool, thisRevPool; tikhomirov@431: private final IdentityPool fnamePool; tikhomirov@266: private byte[] nodeidLookupBuffer = new byte[20]; // get reassigned each time new Nodeid is added to pool tikhomirov@312: private final ProgressSupport progressHelper; tikhomirov@312: private IterateControlMediator iterateControl; tikhomirov@262: tikhomirov@571: public ManifestParser(Inspector delegate) { tikhomirov@196: assert delegate != null; tikhomirov@196: inspector = delegate; tikhomirov@431: nodeidPool = new IdentityPool(); tikhomirov@431: fnamePool = new IdentityPool(); tikhomirov@431: thisRevPool = new IdentityPool(); tikhomirov@312: progressHelper = ProgressSupport.Factory.get(delegate); tikhomirov@196: } tikhomirov@268: tikhomirov@423: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) { tikhomirov@196: try { tikhomirov@312: if (!inspector.begin(revisionNumber, new Nodeid(nodeid, true), linkRevision)) { tikhomirov@312: iterateControl.stop(); tikhomirov@312: return; tikhomirov@312: } tikhomirov@372: if (!da.isEmpty()) { tikhomirov@372: // although unlikely, manifest entry may be empty, when all files have been deleted from the repository tikhomirov@372: Path fname = null; tikhomirov@372: Flags flags = null; tikhomirov@372: Nodeid nid = null; tikhomirov@372: int i; tikhomirov@372: byte[] data = da.byteArray(); tikhomirov@372: for (i = 0; i < actualLen; i++) { tikhomirov@372: int x = i; tikhomirov@372: for( ; data[i] != '\n' && i < actualLen; i++) { tikhomirov@372: if (fname == null && data[i] == 0) { tikhomirov@571: PathProxy px = fnamePool.unify(new PathProxy(data, x, i - x)); tikhomirov@372: // if (cached = fnamePool.unify(px))== px then cacheMiss, else cacheHit tikhomirov@372: // cpython 0..10k: hits: 15 989 152, misses: 3020 tikhomirov@372: fname = px.freeze(); tikhomirov@372: x = i+1; tikhomirov@372: } tikhomirov@196: } tikhomirov@372: if (i < actualLen) { tikhomirov@372: assert data[i] == '\n'; tikhomirov@372: int nodeidLen = i - x < 40 ? i-x : 40; // if > 40, there are flags tikhomirov@372: DigestHelper.ascii2bin(data, x, nodeidLen, nodeidLookupBuffer); // ignore return value as it's unlikely to have NULL in manifest tikhomirov@372: nid = new Nodeid(nodeidLookupBuffer, false); // this Nodeid is for pool lookup only, mock object tikhomirov@372: Nodeid cached = nodeidPool.unify(nid); tikhomirov@372: if (cached == nid) { tikhomirov@372: // buffer now belongs to the cached nodeid tikhomirov@372: nodeidLookupBuffer = new byte[20]; tikhomirov@372: } else { tikhomirov@372: nid = cached; // use existing version, discard the lookup object tikhomirov@372: } // for cpython 0..10k, cache hits are 15 973 301, vs 18871 misses. tikhomirov@372: thisRevPool.record(nid); // memorize revision for the next iteration. tikhomirov@372: if (nodeidLen + x < i) { tikhomirov@372: // 'x' and 'l' for executable bits and symlinks? tikhomirov@372: // hg --debug manifest shows 644 for each regular file in my repo tikhomirov@372: // for cpython 0..10k, there are 4361062 flag checks, and there's only 1 unique flag tikhomirov@372: flags = Flags.parse(data, x + nodeidLen, i-x-nodeidLen); tikhomirov@372: } else { tikhomirov@415: flags = Flags.RegularFile; tikhomirov@372: } tikhomirov@424: boolean good2go = inspector.next(nid, fname, flags); tikhomirov@372: if (!good2go) { tikhomirov@372: iterateControl.stop(); tikhomirov@372: return; tikhomirov@372: } tikhomirov@372: } tikhomirov@372: nid = null; tikhomirov@372: fname = null; tikhomirov@372: flags = null; tikhomirov@196: } tikhomirov@196: } tikhomirov@312: if (!inspector.end(revisionNumber)) { tikhomirov@312: iterateControl.stop(); tikhomirov@312: return; tikhomirov@312: } tikhomirov@262: // tikhomirov@262: // keep only actual file revisions, found at this version tikhomirov@262: // (next manifest is likely to refer to most of them, although in specific cases tikhomirov@262: // like commit in another branch a lot may be useless) tikhomirov@262: nodeidPool.clear(); tikhomirov@431: IdentityPool t = nodeidPool; tikhomirov@262: nodeidPool = thisRevPool; tikhomirov@264: thisRevPool = t; tikhomirov@313: iterateControl.checkCancelled(); tikhomirov@312: progressHelper.worked(1); tikhomirov@196: } catch (IOException ex) { tikhomirov@423: throw new HgInvalidControlFileException("Failed reading manifest", ex, null).setRevisionIndex(revisionNumber); tikhomirov@196: } tikhomirov@196: } tikhomirov@312: tikhomirov@312: public void start(int count, Callback callback, Object token) { tikhomirov@312: CancelSupport cs = CancelSupport.Factory.get(inspector, null); tikhomirov@312: iterateControl = new IterateControlMediator(cs, callback); tikhomirov@312: progressHelper.start(count); tikhomirov@312: } tikhomirov@312: tikhomirov@312: public void finish(Object token) { tikhomirov@312: progressHelper.done(); tikhomirov@312: } tikhomirov@196: } tikhomirov@218: tikhomirov@600: private class RevisionMapper implements RevlogStream.Inspector, Lifecycle { tikhomirov@218: tikhomirov@403: private final int changelogRevisionCount; tikhomirov@218: private int[] changelog2manifest; tikhomirov@600: RevisionLookup manifestNodeids; tikhomirov@218: tikhomirov@600: private RevisionMapper(boolean useOwnRevisionLookup) { tikhomirov@600: changelogRevisionCount = HgManifest.this.getRepo().getChangelog().getRevisionCount(); tikhomirov@600: if (useOwnRevisionLookup) { tikhomirov@600: manifestNodeids = new RevisionLookup(HgManifest.this.content); tikhomirov@600: } tikhomirov@218: } tikhomirov@600: tikhomirov@403: /** tikhomirov@403: * Get index of manifest revision that corresponds to specified changeset tikhomirov@403: * @param changesetRevisionIndex non-negative index of changelog revision, or {@link HgRepository#TIP} tikhomirov@403: * @return index of manifest revision, or {@link HgRepository#BAD_REVISION} if changeset doesn't reference a valid manifest tikhomirov@403: * @throws HgInvalidRevisionException if method argument specifies non-existent revision index tikhomirov@403: */ tikhomirov@403: public int at(int changesetRevisionIndex) throws HgInvalidRevisionException { tikhomirov@403: if (changesetRevisionIndex == TIP) { tikhomirov@403: changesetRevisionIndex = changelogRevisionCount - 1; tikhomirov@403: } tikhomirov@403: if (changesetRevisionIndex >= changelogRevisionCount) { tikhomirov@403: throw new HgInvalidRevisionException(changesetRevisionIndex); tikhomirov@257: } tikhomirov@218: if (changelog2manifest != null) { tikhomirov@403: return changelog2manifest[changesetRevisionIndex]; tikhomirov@218: } tikhomirov@403: return changesetRevisionIndex; tikhomirov@218: } tikhomirov@218: tikhomirov@600: // XXX can be replaced with Revlog.RevisionInspector, but I don't want Nodeid instances tikhomirov@218: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) { tikhomirov@218: if (changelog2manifest != null) { tikhomirov@220: // next assertion is not an error, rather assumption check, which is too development-related to be explicit exception - tikhomirov@220: // I just wonder if there are manifests that have two entries pointing to single changeset. It seems unrealistic, though - tikhomirov@220: // changeset records one and only one manifest nodeid tikhomirov@403: assert changelog2manifest[linkRevision] == BAD_REVISION : String.format("revision:%d, link:%d, already linked to revision:%d", revisionNumber, linkRevision, changelog2manifest[linkRevision]); tikhomirov@218: changelog2manifest[linkRevision] = revisionNumber; tikhomirov@218: } else { tikhomirov@218: if (revisionNumber != linkRevision) { tikhomirov@403: changelog2manifest = new int[changelogRevisionCount]; tikhomirov@403: Arrays.fill(changelog2manifest, BAD_REVISION); tikhomirov@218: for (int i = 0; i < revisionNumber; changelog2manifest[i] = i, i++) tikhomirov@218: ; tikhomirov@218: changelog2manifest[linkRevision] = revisionNumber; tikhomirov@218: } tikhomirov@218: } tikhomirov@600: if (manifestNodeids != null) { tikhomirov@600: manifestNodeids.next(revisionNumber, nodeid); tikhomirov@600: } tikhomirov@218: } tikhomirov@218: tikhomirov@218: public void start(int count, Callback callback, Object token) { tikhomirov@403: if (count != changelogRevisionCount) { tikhomirov@403: assert count < changelogRevisionCount; // no idea what to do if manifest has more revisions than changelog tikhomirov@218: // the way how manifest may contain more revisions than changelog, as I can imagine, is a result of tikhomirov@218: // some kind of an import tool (e.g. from SVN or CVS), that creates manifest and changelog independently. tikhomirov@218: // Note, it's pure guess, I didn't see such repository yet (although the way manifest revisions tikhomirov@218: // in cpython repo are numbered makes me think aforementioned way) tikhomirov@403: changelog2manifest = new int[changelogRevisionCount]; tikhomirov@403: Arrays.fill(changelog2manifest, BAD_REVISION); tikhomirov@218: } tikhomirov@600: if (manifestNodeids != null) { tikhomirov@600: manifestNodeids.prepare(count); tikhomirov@600: } tikhomirov@218: } tikhomirov@218: tikhomirov@218: public void finish(Object token) { tikhomirov@600: // it's not a nice idea to fix changesets that reuse existing manifest entries from inside tikhomirov@600: // #finish, as the manifest read operation is not complete at the moment. tikhomirov@600: } tikhomirov@600: tikhomirov@600: public void fixReusedManifests() { tikhomirov@218: if (changelog2manifest == null) { tikhomirov@600: // direct, 1-1 mapping of changeset indexes to manifest tikhomirov@218: return; tikhomirov@218: } tikhomirov@218: // I assume there'd be not too many revisions we don't know manifest of tikhomirov@597: IntVector undefinedChangelogRevision = new IntVector(); tikhomirov@218: for (int i = 0; i < changelog2manifest.length; i++) { tikhomirov@403: if (changelog2manifest[i] == BAD_REVISION) { tikhomirov@218: undefinedChangelogRevision.add(i); tikhomirov@218: } tikhomirov@218: } tikhomirov@597: if (undefinedChangelogRevision.size() > 0) { tikhomirov@598: final long t1 = System.nanoTime(); tikhomirov@597: final IntMap missingCsetToManifest = new IntMap(undefinedChangelogRevision.size()); tikhomirov@597: int[] undefinedClogRevs = undefinedChangelogRevision.toArray(); tikhomirov@597: // undefinedChangelogRevision is sorted by the nature it's created tikhomirov@600: HgManifest.this.getRepo().getChangelog().rangeInternal(new HgChangelog.Inspector() { tikhomirov@597: tikhomirov@597: public void next(int revisionIndex, Nodeid nodeid, RawChangeset cset) { tikhomirov@597: missingCsetToManifest.put(revisionIndex, cset.manifest()); tikhomirov@597: } tikhomirov@597: }, undefinedClogRevs); tikhomirov@597: assert missingCsetToManifest.size() == undefinedChangelogRevision.size(); tikhomirov@598: final long t2 = System.nanoTime(); tikhomirov@597: for (int u : undefinedClogRevs) { tikhomirov@597: Nodeid manifest = missingCsetToManifest.get(u); tikhomirov@597: if (manifest == null || manifest.isNull()) { tikhomirov@600: HgManifest.this.getRepo().getSessionContext().getLog().dump(getClass(), Severity.Warn, "Changeset %d has no associated manifest entry", u); tikhomirov@600: // keep BAD_REVISION in the changelog2manifest map. tikhomirov@598: continue; tikhomirov@598: } tikhomirov@600: if (manifestNodeids != null) { tikhomirov@600: int manifestRevIndex = manifestNodeids.findIndex(manifest); tikhomirov@600: // mimic HgManifest#getRevisionIndex() to keep behavior the same tikhomirov@600: if (manifestRevIndex == BAD_REVISION) { tikhomirov@600: throw new HgInvalidRevisionException(String.format("Can't find index of revision %s", manifest.shortNotation()), manifest, null); tikhomirov@598: } tikhomirov@600: changelog2manifest[u] = manifestRevIndex; tikhomirov@600: } else { tikhomirov@600: changelog2manifest[u] = HgManifest.this.getRevisionIndex(manifest); tikhomirov@597: } tikhomirov@354: } tikhomirov@598: final long t3 = System.nanoTime(); tikhomirov@598: System.out.printf("\tRevisionMapper#finish(), %d missing revs: %d + %d us\n", undefinedChangelogRevision.size(), (t2-t1) / 1000, (t3-t2) / 1000); tikhomirov@218: } tikhomirov@218: } tikhomirov@218: } tikhomirov@415: tikhomirov@415: /** tikhomirov@415: * Look up specified file in possibly multiple manifest revisions, collect file revision and flags. tikhomirov@415: */ tikhomirov@415: private static class FileLookupInspector implements RevlogStream.Inspector { tikhomirov@415: tikhomirov@426: private final Path filename; tikhomirov@415: private final byte[] filenameAsBytes; tikhomirov@415: private final IntMap csetIndex2FileRev; tikhomirov@415: private final IntMap csetIndex2Flags; tikhomirov@426: private final Inspector delegate; tikhomirov@415: tikhomirov@415: public FileLookupInspector(EncodingHelper eh, Path fileToLookUp, IntMap csetIndex2FileRevMap, IntMap csetIndex2FlagsMap) { tikhomirov@415: assert fileToLookUp != null; tikhomirov@415: // need at least one map for the inspector to make any sense tikhomirov@415: assert csetIndex2FileRevMap != null || csetIndex2FlagsMap != null; tikhomirov@426: filename = fileToLookUp; tikhomirov@426: filenameAsBytes = eh.toManifest(fileToLookUp.toString()); tikhomirov@426: delegate = null; tikhomirov@415: csetIndex2FileRev = csetIndex2FileRevMap; tikhomirov@415: csetIndex2Flags = csetIndex2FlagsMap; tikhomirov@426: } tikhomirov@426: tikhomirov@426: public FileLookupInspector(EncodingHelper eh, Path fileToLookUp, Inspector delegateInspector) { tikhomirov@426: assert fileToLookUp != null; tikhomirov@426: assert delegateInspector != null; tikhomirov@426: filename = fileToLookUp; tikhomirov@415: filenameAsBytes = eh.toManifest(fileToLookUp.toString()); tikhomirov@426: delegate = delegateInspector; tikhomirov@426: csetIndex2FileRev = null; tikhomirov@426: csetIndex2Flags = null; tikhomirov@426: } tikhomirov@426: tikhomirov@426: void walk(int manifestRevIndex, RevlogStream content) { tikhomirov@426: content.iterate(manifestRevIndex, manifestRevIndex, true, this); tikhomirov@426: } tikhomirov@426: tikhomirov@426: void walk(int[] manifestRevIndexes, RevlogStream content) { tikhomirov@426: content.iterate(manifestRevIndexes, true, this); tikhomirov@415: } tikhomirov@415: tikhomirov@423: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) { tikhomirov@574: ByteVector byteVector = new ByteVector(256, 128); // allocate for long paths right away tikhomirov@415: try { tikhomirov@415: byte b; tikhomirov@415: while (!data.isEmpty() && (b = data.readByte()) != '\n') { tikhomirov@415: if (b != 0) { tikhomirov@574: byteVector.add(b); tikhomirov@415: } else { tikhomirov@574: if (byteVector.equalsTo(filenameAsBytes)) { tikhomirov@426: Nodeid fileRev = null; tikhomirov@426: Flags flags = null; tikhomirov@426: if (csetIndex2FileRev != null || delegate != null) { tikhomirov@415: byte[] nid = new byte[40]; tikhomirov@415: data.readBytes(nid, 0, 40); tikhomirov@426: fileRev = Nodeid.fromAscii(nid, 0, 40); tikhomirov@415: } else { tikhomirov@415: data.skip(40); tikhomirov@415: } tikhomirov@426: if (csetIndex2Flags != null || delegate != null) { tikhomirov@574: byteVector.clear(); tikhomirov@415: while (!data.isEmpty() && (b = data.readByte()) != '\n') { tikhomirov@574: byteVector.add(b); tikhomirov@415: } tikhomirov@574: if (byteVector.size() == 0) { tikhomirov@415: flags = Flags.RegularFile; tikhomirov@415: } else { tikhomirov@574: flags = Flags.parse(byteVector.toByteArray(), 0, byteVector.size()); tikhomirov@415: } tikhomirov@426: } tikhomirov@426: if (delegate != null) { tikhomirov@426: assert flags != null; tikhomirov@426: assert fileRev != null; tikhomirov@426: delegate.begin(revisionNumber, Nodeid.fromBinary(nodeid, 0), linkRevision); tikhomirov@426: delegate.next(fileRev, filename, flags); tikhomirov@426: delegate.end(revisionNumber); tikhomirov@426: tikhomirov@426: } else { tikhomirov@426: if (csetIndex2FileRev != null) { tikhomirov@426: csetIndex2FileRev.put(linkRevision, fileRev); tikhomirov@426: } tikhomirov@426: if (csetIndex2Flags != null) { tikhomirov@426: csetIndex2Flags.put(linkRevision, flags); tikhomirov@426: } tikhomirov@415: } tikhomirov@415: break; tikhomirov@415: } else { tikhomirov@415: data.skip(40); tikhomirov@415: } tikhomirov@415: // else skip to the end of line tikhomirov@415: while (!data.isEmpty() && (b = data.readByte()) != '\n') tikhomirov@415: ; tikhomirov@574: tikhomirov@574: byteVector.clear(); tikhomirov@415: } tikhomirov@415: } tikhomirov@415: } catch (IOException ex) { tikhomirov@423: throw new HgInvalidControlFileException("Failed reading manifest", ex, null); tikhomirov@415: } tikhomirov@415: } tikhomirov@415: } tikhomirov@2: }