comparison src/org/tmatesoft/hg/internal/PhasesHelper.java @ 447:056f724bdc21 smartgit3

Cache earliest phase root revision not to evaluate all the time
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 06 Jun 2012 20:11:17 +0200
parents 9f0e6dfd417e
children 2e402c12ebc6
comparison
equal deleted inserted replaced
446:9f0e6dfd417e 447:056f724bdc21
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import static org.tmatesoft.hg.repo.HgPhase.Draft; 19 import static org.tmatesoft.hg.repo.HgPhase.Draft;
20 import static org.tmatesoft.hg.repo.HgPhase.Secret; 20 import static org.tmatesoft.hg.repo.HgPhase.Secret;
21 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
21 22
22 import java.io.BufferedReader; 23 import java.io.BufferedReader;
23 import java.io.File; 24 import java.io.File;
24 import java.io.FileReader; 25 import java.io.FileReader;
25 import java.io.IOException; 26 import java.io.IOException;
49 private final HgRepository hgRepo; 50 private final HgRepository hgRepo;
50 private final HgChangelog.ParentWalker parentHelper; 51 private final HgChangelog.ParentWalker parentHelper;
51 private Boolean repoSupporsPhases; 52 private Boolean repoSupporsPhases;
52 private List<Nodeid> draftPhaseRoots; 53 private List<Nodeid> draftPhaseRoots;
53 private List<Nodeid> secretPhaseRoots; 54 private List<Nodeid> secretPhaseRoots;
55 private int[] earliestRevIndex = new int[HgPhase.values().length];
54 56
55 public PhasesHelper(HgRepository repo) { 57 public PhasesHelper(HgRepository repo) {
56 this(repo, null); 58 this(repo, null);
57 } 59 }
58 60
59 public PhasesHelper(HgRepository repo, HgChangelog.ParentWalker pw) { 61 public PhasesHelper(HgRepository repo, HgChangelog.ParentWalker pw) {
60 hgRepo = repo; 62 hgRepo = repo;
61 parentHelper = pw; 63 parentHelper = pw;
64 Arrays.fill(earliestRevIndex, BAD_REVISION);
62 } 65 }
63 66
64 public boolean isCapableOfPhases() throws HgInvalidControlFileException { 67 public boolean isCapableOfPhases() throws HgInvalidControlFileException {
65 if (null == repoSupporsPhases) { 68 if (null == repoSupporsPhases) {
66 repoSupporsPhases = readRoots(); 69 repoSupporsPhases = readRoots();
96 return phase; 99 return phase;
97 } 100 }
98 } else { 101 } else {
99 // no parent helper 102 // no parent helper
100 // search all descendants 103 // search all descendants
101 int[] rootIndexes = toIndexes(roots); 104 int earliestRootRevIndex = getEarliestPhaseRevision(phase);
102 Arrays.sort(rootIndexes); 105 if (earliestRootRevIndex > csetRevIndex) {
103 if (rootIndexes[0] > csetRevIndex) {
104 // this phase started later than our changeset was added, try another phase 106 // this phase started later than our changeset was added, try another phase
105 continue; 107 continue;
106 } 108 }
107 /* 109 /*
108 * TODO descendants() method to build a BitSet with 1 at index of those that are descendants 110 * TODO descendants() method to build a BitSet with 1 at index of those that are descendants
110 * (a) collect only for a subset of repository, 112 * (a) collect only for a subset of repository,
111 * (b) be able to answer isDescendant(int csetRevIndex) using absolute indexing (i.e bitAt(csetRevIndex - rootRevIndex)) 113 * (b) be able to answer isDescendant(int csetRevIndex) using absolute indexing (i.e bitAt(csetRevIndex - rootRevIndex))
112 */ 114 */
113 final HashSet<Nodeid> parents2consider = new HashSet<Nodeid>(roots); 115 final HashSet<Nodeid> parents2consider = new HashSet<Nodeid>(roots);
114 final boolean[] result = new boolean[] { false }; 116 final boolean[] result = new boolean[] { false };
115 hgRepo.getChangelog().walk(0/*rootIndexes[0]*/, csetRevIndex, new HgChangelog.ParentInspector() { 117 hgRepo.getChangelog().walk(0/*earlierstRootRevIndex*/, csetRevIndex, new HgChangelog.ParentInspector() {
116 118
117 public void next(int revisionIndex, Nodeid revision, int parent1, int parent2, Nodeid nidParent1, Nodeid nidParent2) { 119 public void next(int revisionIndex, Nodeid revision, int parent1, int parent2, Nodeid nidParent1, Nodeid nidParent2) {
118 boolean descendant = false; 120 boolean descendant = false;
119 if (!nidParent1.isNull() && parents2consider.contains(nidParent1)) { 121 if (!nidParent1.isNull() && parents2consider.contains(nidParent1)) {
120 parents2consider.add(revision); 122 parents2consider.add(revision);
189 case Draft : return draftPhaseRoots; 191 case Draft : return draftPhaseRoots;
190 case Secret : return secretPhaseRoots; 192 case Secret : return secretPhaseRoots;
191 } 193 }
192 return Collections.emptyList(); 194 return Collections.emptyList();
193 } 195 }
196
197 private int getEarliestPhaseRevision(HgPhase phase) throws HgInvalidControlFileException {
198 int ordinal = phase.ordinal();
199 if (earliestRevIndex[ordinal] == BAD_REVISION) {
200 int[] rootIndexes = toIndexes(getPhaseRoots(phase));
201 Arrays.sort(rootIndexes);
202 // instead of MAX_VALUE may use clog.getLastRevision() + 1
203 earliestRevIndex[ordinal] = rootIndexes.length == 0 ? Integer.MAX_VALUE : rootIndexes[0];
204 }
205 return earliestRevIndex[ordinal];
206 }
194 } 207 }