comparison src/org/tmatesoft/hg/internal/RevisionDescendants.java @ 449:5787e912f60e smartgit3

Speed up changeset phase detection when no parent cache is avalable
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 07 Jun 2012 16:01:09 +0200
parents
children 03fd8d079e9c
comparison
equal deleted inserted replaced
448:2e402c12ebc6 449:5787e912f60e
1 /*
2 * Copyright (c) 2012 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.util.BitSet;
20
21 import org.tmatesoft.hg.core.HgBadStateException;
22 import org.tmatesoft.hg.core.HgInvalidControlFileException;
23 import org.tmatesoft.hg.core.Nodeid;
24 import org.tmatesoft.hg.repo.HgChangelog;
25 import org.tmatesoft.hg.repo.HgRepository;
26
27 /**
28 * Represent indicators which revisions are descendants of the supplied root revision
29 * This is sort of lightweight alternative to ParentWalker#childrenOf
30 *
31 * @author Artem Tikhomirov
32 * @author TMate Software Ltd.
33 */
34 public class RevisionDescendants {
35
36 private final HgRepository repo;
37 private final int rootRevIndex;
38 private final int tipRevIndex; // this is the last revision we cache to
39 private final BitSet descendants;
40
41 // in fact, may be refactored to deal not only with changelog, but any revlog (not sure what would be the usecase, though)
42 public RevisionDescendants(HgRepository hgRepo, int revisionIndex) {
43 repo = hgRepo;
44 rootRevIndex = revisionIndex;
45 // even if tip moves, we still answer correctly for those isCandidate()
46 tipRevIndex = repo.getChangelog().getLastRevision();
47 if (revisionIndex < 0 || revisionIndex > tipRevIndex) {
48 String m = "Revision to build descendants for shall be in range [%d,%d], not %d";
49 throw new IllegalArgumentException(String.format(m, 0, tipRevIndex, revisionIndex));
50 }
51 descendants = new BitSet(tipRevIndex - rootRevIndex + 1);
52 }
53
54 public void build() throws HgInvalidControlFileException {
55 final BitSet result = descendants;
56 result.set(0);
57 repo.getChangelog().walk(rootRevIndex+1, tipRevIndex, new HgChangelog.ParentInspector() {
58 // TODO ParentRevisionInspector, with no parent nodeids, just indexes?
59
60 private int i = 1; // above we start with revision next to rootRevIndex, which is at offset 0
61 public void next(int revisionIndex, Nodeid revision, int parent1, int parent2, Nodeid nidParent1, Nodeid nidParent2) {
62 int p1x = parent1 - rootRevIndex;
63 int p2x = parent2 - rootRevIndex;
64 boolean p1IsDescendant = false, p2IsDescendant = false;
65 if (p1x >= 0) { // parent1 is among descendants candidates
66 assert p1x < result.size();
67 p1IsDescendant = result.get(p1x);
68 }
69 if (p2x >= 0) {
70 assert p2x < result.size();
71 p2IsDescendant = result.get(p2x);
72 }
73 //
74 int rx = revisionIndex -rootRevIndex;
75 if (rx != i) {
76 throw new HgBadStateException();
77 }
78 // current revision is descentand if any of its parents is descendant
79 result.set(rx, p1IsDescendant || p2IsDescendant);
80 i++;
81 }
82 });
83 }
84
85 // deliberately doesn't allow TIP
86 public boolean isCandidate(int revIndex) {
87 return (revIndex >= rootRevIndex && revIndex <= tipRevIndex) ;
88 }
89
90 public boolean hasDescendants() { // isEmpty is better name?
91 // bit at rootRevIndex is always set
92 return descendants.nextSetBit(rootRevIndex+1) != -1;
93 }
94
95 public boolean isDescendant(int revisionIndex) {
96 assert isCandidate(revisionIndex);
97 int ix = revisionIndex - rootRevIndex;
98 assert ix < descendants.size();
99 return descendants.get(ix);
100 }
101 }