private void collectTagsPerFile_Approach_2(HgRepository repository, final int[] tagLocalRevs, final IntMap<List<TagInfo>> tagRevIndex2TagInfo, Path targetPath) throws HgException, HgRuntimeException {
//
// Approach 2. No all-file map. Collect file revisions recorded at the time of tagging,
// then for each file revision check if it is among those above, and if yes, take corresponding tags
HgDataFile fileNode = repository.getFileNode(targetPath);
final long start2 = System.nanoTime();
final Map<Integer, Nodeid> fileRevisionAtTagRevision = new HashMap<Integer, Nodeid>();
final Map<Nodeid, List<String>> fileRev2TagNames = new HashMap<Nodeid, List<String>>();
HgManifest.Inspector collectFileRevAtCset = new HgManifest.Inspector() {
private int csetRevIndex;
public boolean next(Nodeid nid, Path fname, Flags flags) {
fileRevisionAtTagRevision.put(csetRevIndex, nid);
if (tagRevIndex2TagInfo.containsKey(csetRevIndex)) {
List<String> tags = fileRev2TagNames.get(nid);
if (tags == null) {
fileRev2TagNames.put(nid, tags = new ArrayList<String>(3));
}
for (TagInfo ti : tagRevIndex2TagInfo.get(csetRevIndex)) {
tags.add(ti.name());
}
}
return true;
}
public boolean end(int manifestRevision) {
return true;
}
public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
csetRevIndex = changelogRevision;
return true;
}
};
repository.getManifest().walkFileRevisions(targetPath, collectFileRevAtCset,tagLocalRevs);
final long start2a = System.nanoTime();
fileNode.indexWalk(0, TIP, new HgDataFile.RevisionInspector() {
public void next(int fileRevisionIndex, Nodeid fileRevision, int changesetRevisionIndex) {
List<String> associatedTags = new LinkedList<String>();
for (int taggedRevision : tagLocalRevs) {
// current file revision can't appear in tags that point to earlier changelog revisions (they got own file revision)
if (taggedRevision >= changesetRevisionIndex) {
// z points to some changeset with tag
Nodeid wasKnownAs = fileRevisionAtTagRevision.get(taggedRevision);
if (wasKnownAs.equals(fileRevision)) {
// has tag associated with changeset at index z
List<TagInfo> tagsAtRev = tagRevIndex2TagInfo.get(taggedRevision);
assert tagsAtRev != null;
for (TagInfo ti : tagsAtRev) {
associatedTags.add(ti.name());
}
}
}
}
//
System.out.printf("%3d%7d%s\n", fileRevisionIndex, changesetRevisionIndex, associatedTags);
}
});
for (int i = 0, lastRev = fileNode.getLastRevision(); i <= lastRev; i++) {
Nodeid fileRevision = fileNode.getRevision(i);
List<String> associatedTags2 = fileRev2TagNames.get(fileRevision);
int changesetRevIndex = fileNode.getChangesetRevisionIndex(i);
System.out.printf("%3d%7d%s\n", i, changesetRevIndex, associatedTags2 == null ? Collections.emptyList() : associatedTags2);
}
System.out.printf("Alternative total time: %d ms, of that init: %d ms\n", (System.nanoTime() - start2)/1000000, (start2a-start2)/1000000);
System.out.printf("Free mem: %,d\n", Runtime.getRuntime().freeMemory());
}