Mercurial > jhg
annotate cmdline/org/tmatesoft/hg/console/Incoming.java @ 174:b1de83ffa7f8
Build shall succeed with no precompiled classes, too
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 30 Mar 2011 02:43:15 +0200 |
parents | 4bf061a7c001 |
children | a8df7162ec75 |
rev | line source |
---|---|
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
2 * Copyright (c) 2011 TMate Software Ltd |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
3 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
7 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
11 * GNU General Public License for more details. |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
12 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
13 * For information on how to redistribute this software under |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
102
a3a2e5deb320
Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
98
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
17 package org.tmatesoft.hg.console; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
171
2c3e96674e2a
Towards outgoing changes - initial detection logic, get connected with remote repo stub
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
19 import static org.tmatesoft.hg.core.Nodeid.NULL; |
2c3e96674e2a
Towards outgoing changes - initial detection logic, get connected with remote repo stub
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
20 |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
21 import java.util.Arrays; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.util.Collection; |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
23 import java.util.Collections; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
24 import java.util.Comparator; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 import java.util.HashSet; |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
26 import java.util.Iterator; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
27 import java.util.LinkedHashMap; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 import java.util.LinkedHashSet; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 import java.util.LinkedList; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 import java.util.List; |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
31 import java.util.Map.Entry; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
32 |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
33 import org.tmatesoft.hg.core.Nodeid; |
97
ee2c750b036d
Changelog to HgChangelog
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
34 import org.tmatesoft.hg.repo.HgChangelog; |
171
2c3e96674e2a
Towards outgoing changes - initial detection logic, get connected with remote repo stub
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
35 import org.tmatesoft.hg.repo.HgRemoteRepository.RemoteBranch; |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
36 import org.tmatesoft.hg.repo.HgRepository; |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
37 |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 /** |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
40 * WORK IN PROGRESS, DO NOT USE |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
41 * hg incoming counterpart |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
42 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
43 * @author Artem Tikhomirov |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
44 * @author TMate Software Ltd. |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45 */ |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 public class Incoming { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 public static void main(String[] args) throws Exception { |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
49 if (Boolean.TRUE.booleanValue()) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
50 new SequenceConstructor().test(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
51 return; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
52 } |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
53 Options cmdLineOpts = Options.parse(args); |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
54 HgRepository hgRepo = cmdLineOpts.findRepository(); |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 if (hgRepo.isInvalid()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 return; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 // in fact, all we need from changelog is set of all nodeids. However, since ParentWalker reuses same Nodeids, it's not too expensive |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 // to reuse it here, XXX although later this may need to be refactored |
98
225c48d964ed
Changelog to HgChangelog, Refactoring doesn't recognize name with inner class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
97
diff
changeset
|
61 final HgChangelog.ParentWalker pw = hgRepo.getChangelog().new ParentWalker(); |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 pw.init(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 HashSet<Nodeid> base = new HashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 HashSet<Nodeid> unknownRemoteHeads = new HashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 // imagine empty repository - any nodeid from remote heads would be unknown |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 unknownRemoteHeads.add(Nodeid.fromAscii("382cfe9463db0484a14136e4b38407419525f0c0".getBytes(), 0, 40)); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 LinkedList<RemoteBranch> remoteBranches = new LinkedList<RemoteBranch>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 remoteBranches(unknownRemoteHeads, remoteBranches); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 HashSet<Nodeid> visited = new HashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 HashSet<RemoteBranch> processed = new HashSet<RemoteBranch>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 LinkedList<Nodeid[]> toScan = new LinkedList<Nodeid[]>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 LinkedHashSet<Nodeid> toFetch = new LinkedHashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 // next one seems to track heads we've asked (or plan to ask) remote.branches for |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 HashSet<Nodeid> unknownHeads /*req*/ = new HashSet<Nodeid>(unknownRemoteHeads); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 while (!remoteBranches.isEmpty()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 LinkedList<Nodeid> toQueryRemote = new LinkedList<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 while (!remoteBranches.isEmpty()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 RemoteBranch next = remoteBranches.removeFirst(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 if (visited.contains(next.head) || processed.contains(next)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 continue; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 if (Nodeid.NULL.equals(next.head)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 // it's discovery.py that expects next.head to be nullid here, I can't imagine how this may happen, hence this exception |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 throw new IllegalStateException("I wonder if null if may ever get here with remote branches"); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 } else if (pw.knownNode(next.root)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 // root of the remote change is known locally, analyze to find exact missing changesets |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 toScan.addLast(new Nodeid[] { next.head, next.root }); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 processed.add(next); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
92 } else { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 if (!visited.contains(next.root) && !toFetch.contains(next.root)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 // if parents are locally known, this is new branch (sequence of changes) (sequence sprang out of known parents) |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 if ((next.p1 == null || pw.knownNode(next.p1)) && (next.p2 == null || pw.knownNode(next.p2))) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 toFetch.add(next.root); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 // XXX perhaps, may combine this parent processing below (I don't understand what this code is exactly about) |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 if (pw.knownNode(next.p1)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 base.add(next.p1); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
101 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 if (pw.knownNode(next.p2)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 base.add(next.p2); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
106 if (next.p1 != null && !pw.knownNode(next.p1) && !unknownHeads.contains(next.p1)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
107 toQueryRemote.add(next.p1); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
108 unknownHeads.add(next.p1); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 if (next.p2 != null && !pw.knownNode(next.p2) && !unknownHeads.contains(next.p2)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 toQueryRemote.add(next.p2); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 unknownHeads.add(next.p2); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 visited.add(next.head); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
117 if (!toQueryRemote.isEmpty()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
118 // discovery.py in fact does this in batches of 10 revisions a time. |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 // however, this slicing may be done in remoteBranches call instead (if needed) |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
120 remoteBranches(toQueryRemote, remoteBranches); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
121 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
122 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 while (!toScan.isEmpty()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 Nodeid[] head_root = toScan.removeFirst(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
125 List<Nodeid> nodesBetween = remoteBetween(head_root[0], head_root[1], new LinkedList<Nodeid>()); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
126 nodesBetween.add(head_root[1]); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 int x = 1; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
128 Nodeid p = head_root[0]; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 for (Nodeid i : nodesBetween) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
130 System.out.println("narrowing " + x + ":" + nodesBetween.size() + " " + i.shortNotation()); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
131 if (pw.knownNode(i)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 if (x <= 2) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 toFetch.add(p); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 base.add(i); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 } else { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
136 // XXX original discovery.py collects new elements to scan separately |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
137 // likely to "batch" calls to server |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
138 System.out.println("narrowed branch search to " + p.shortNotation() + ":" + i.shortNotation()); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
139 toScan.addLast(new Nodeid[] { p, i }); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
140 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
141 break; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
142 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
143 x = x << 1; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
144 p = i; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
146 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
147 for (Nodeid n : toFetch) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
148 if (pw.knownNode(n)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
149 System.out.println("Erroneous to fetch:" + n); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 } else { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
151 System.out.println(n); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
152 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
153 } |
33
565ce0835674
TODO added, to try stream for unzip in revlog
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
154 |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
155 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
156 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
157 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
158 private static void remoteBranches(Collection<Nodeid> unknownRemoteHeads, List<RemoteBranch> remoteBranches) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
159 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
160 // TODO implement this with remote access |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
161 // |
171
2c3e96674e2a
Towards outgoing changes - initial detection logic, get connected with remote repo stub
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
162 RemoteBranch rb = new RemoteBranch(unknownRemoteHeads.iterator().next(), Nodeid.fromAscii("dbd663faec1f0175619cf7668bddc6350548b8d6"), NULL, NULL); |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
163 remoteBranches.add(rb); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
164 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
165 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
166 private static List<Nodeid> remoteBetween(Nodeid nodeid1, Nodeid nodeid2, List<Nodeid> list) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
167 // sent: cmd=between&pairs=d6d2a630f4a6d670c90a5ca909150f2b426ec88f-dbd663faec1f0175619cf7668bddc6350548b8d6 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
168 // received: a78c980749e3ccebb47138b547e9b644a22797a9 286d221f6c28cbfce25ea314e1f46a23b7f979d3 fc265ddeab262ff5c34b4cf4e2522d8d41f1f05b a3576694a4d1edaa681cab15b89d6b556b02aff4 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
169 // 1st, 2nd, fourth and eights of total 8 changes between rev9 and rev0 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
170 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
171 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
172 // a78c980749e3ccebb47138b547e9b644a22797a9 286d221f6c28cbfce25ea314e1f46a23b7f979d3 fc265ddeab262ff5c34b4cf4e2522d8d41f1f05b a3576694a4d1edaa681cab15b89d6b556b02aff4 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
173 //d6d2a630f4a6d670c90a5ca909150f2b426ec88f a78c980749e3ccebb47138b547e9b644a22797a9 5abe5af181bd6a6d3e94c378376c901f0f80da50 08db726a0fb7914ac9d27ba26dc8bbf6385a0554 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
174 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
175 // TODO implement with remote access |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
176 String response = null; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
177 if (nodeid1.equals(Nodeid.fromAscii("382cfe9463db0484a14136e4b38407419525f0c0".getBytes(), 0, 40)) && nodeid2.equals(Nodeid.fromAscii("dbd663faec1f0175619cf7668bddc6350548b8d6".getBytes(), 0, 40))) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
178 response = "d6d2a630f4a6d670c90a5ca909150f2b426ec88f a78c980749e3ccebb47138b547e9b644a22797a9 5abe5af181bd6a6d3e94c378376c901f0f80da50 08db726a0fb7914ac9d27ba26dc8bbf6385a0554"; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
179 } else if (nodeid1.equals(Nodeid.fromAscii("a78c980749e3ccebb47138b547e9b644a22797a9".getBytes(), 0, 40)) && nodeid2.equals(Nodeid.fromAscii("5abe5af181bd6a6d3e94c378376c901f0f80da50".getBytes(), 0, 40))) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
180 response = "286d221f6c28cbfce25ea314e1f46a23b7f979d3"; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
181 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
182 if (response == null) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
183 throw HgRepository.notImplemented(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
184 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
185 for (String s : response.split(" ")) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
186 list.add(Nodeid.fromAscii(s.getBytes(), 0, 40)); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
187 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
188 return list; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
189 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
190 |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
191 private static class SequenceConstructor { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
192 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
193 private int[] between(int root, int head) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
194 if (head <= (root+1)) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
195 return new int[0]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
196 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
197 System.out.printf("[%d, %d]\t\t", root, head); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
198 int size = 1 + (int) Math.floor(Math.log(head-root - 1) / Math.log(2)); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
199 int[] rv = new int[size]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
200 for (int v = 1, i = 0; i < rv.length; i++) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
201 rv[i] = root + v; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
202 v = v << 1; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
203 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
204 System.out.println(Arrays.toString(rv)); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
205 return rv; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
206 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
207 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
208 public void test() { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
209 int root = 0, head = 1000; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
210 int[] data = between(root, head); // max number of elements to recover is 2**(1+data.length)-1, need room for |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
211 // as much elements, hence 2**(data.length+1). In worst case, when there are onlu 2**data.length + 1 missing element, |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
212 // almost half of the finalSequence would be empty |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
213 int[] finalSequence = new int[1 << (data.length+1) >>> 5]; // div 32 - total bits to integers |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
214 int exactNumberOfElements = -1; // exact number of meaningful bits in finalSequence |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
215 LinkedHashMap<Integer, int[]> datas = new LinkedHashMap<Integer, int[]>(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
216 datas.put(root, data); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
217 int totalQueries = 1; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
218 HashSet<Integer> queried = new HashSet<Integer>(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
219 int[] checkSequence = null; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
220 while(!datas.isEmpty()) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
221 LinkedList<int[]> toQuery = new LinkedList<int[]>(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
222 do { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
223 Iterator<Entry<Integer, int[]>> it = datas.entrySet().iterator(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
224 Entry<Integer, int[]> next = it.next(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
225 int r = next.getKey(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
226 data = next.getValue(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
227 it.remove(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
228 populate(r, head, data, finalSequence); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
229 if (checkSequence != null) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
230 boolean match = true; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
231 // System.out.println("Try to match:"); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
232 for (int i = 0; i < checkSequence.length; i++) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
233 // System.out.println(i); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
234 // System.out.println("control:" + toBinaryString(checkSequence[i], ' ')); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
235 // System.out.println("present:" + toBinaryString(finalSequence[i], ' ')); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
236 if (checkSequence[i] != finalSequence[i]) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
237 match = false; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
238 } else { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
239 match &= true; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
240 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
241 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
242 System.out.println(match ? "Match, on query:" + totalQueries : "Didn't match"); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
243 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
244 if (data.length > 2) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
245 for (int x : data) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
246 if (!queried.contains(x) && head - x > 1) { /*queries for neighboring elements is senseless*/ |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
247 toQuery.add(new int[] {x, head}); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
248 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
249 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
250 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
251 } while (!datas.isEmpty()) ; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
252 if (!toQuery.isEmpty()) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
253 System.out.println(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
254 totalQueries++; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
255 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
256 Collections.sort(toQuery, new Comparator<int[]>() { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
257 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
258 public int compare(int[] o1, int[] o2) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
259 return o1[0] < o2[0] ? -1 : (o1[0] == o2[0] ? 0 : 1); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
260 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
261 }); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
262 for (int[] x : toQuery) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
263 if (!queried.contains(x[0])) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
264 queried.add(x[0]); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
265 data = between(x[0], x[1]); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
266 if (exactNumberOfElements == -1 && data.length == 1) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
267 exactNumberOfElements = x[0] + 1; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
268 System.out.printf("On query %d found out exact number of missing elements: %d\n", totalQueries, exactNumberOfElements); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
269 // get a bit sequence of exactNumberOfElements, 0111..110 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
270 // to 'and' it with finalSequence later |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
271 int totalInts = (exactNumberOfElements + 2 /*heading and tailing zero bits*/) >>> 5; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
272 int trailingBits = (exactNumberOfElements + 2) & 0x1f; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
273 if (trailingBits != 0) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
274 totalInts++; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
275 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
276 checkSequence = new int[totalInts]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
277 Arrays.fill(checkSequence, 0xffffffff); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
278 checkSequence[0] &= 0x7FFFFFFF; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
279 if (trailingBits == 0) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
280 checkSequence[totalInts-1] &= 0xFFFFFFFE; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
281 } else if (trailingBits == 1) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
282 checkSequence[totalInts-1] = 0; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
283 } else { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
284 // trailingBits include heading and trailing zero bits |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
285 int mask = 0x80000000 >> trailingBits-2; // with sign! |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
286 checkSequence[totalInts - 1] &= mask; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
287 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
288 for (int e : checkSequence) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
289 System.out.print(toBinaryString(e, ' ')); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
290 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
291 System.out.println(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
292 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
293 datas.put(x[0], data); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
294 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
295 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
296 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
297 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
298 System.out.println("Total queries:" + totalQueries); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
299 for (int x : finalSequence) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
300 System.out.print(toBinaryString(x, ' ')); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
301 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
302 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
303 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
304 private void populate(int root, int head, int[] data, int[] finalSequence) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
305 for (int i = 1, x = 0; root+i < head; i = i << 1, x++) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
306 int value = data[x]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
307 int value_check = root+i; |
174
b1de83ffa7f8
Build shall succeed with no precompiled classes, too
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
173
diff
changeset
|
308 if (value != value_check) { |
b1de83ffa7f8
Build shall succeed with no precompiled classes, too
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
173
diff
changeset
|
309 throw new IllegalStateException(); |
b1de83ffa7f8
Build shall succeed with no precompiled classes, too
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
173
diff
changeset
|
310 } |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
311 int wordIx = (root + i) >>> 5; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
312 int bitIx = (root + i) & 0x1f; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
313 finalSequence[wordIx] |= 1 << (31-bitIx); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
314 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
315 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
316 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
317 private static String toBinaryString(int x, char byteSeparator) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
318 StringBuilder sb = new StringBuilder(4*8+4); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
319 sb.append(toBinaryString((byte) (x >>> 24))); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
320 sb.append(byteSeparator); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
321 sb.append(toBinaryString((byte) ((x & 0x00ff0000) >>> 16))); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
322 sb.append(byteSeparator); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
323 sb.append(toBinaryString((byte) ((x & 0x00ff00) >>> 8))); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
324 sb.append(byteSeparator); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
325 sb.append(toBinaryString((byte) (x & 0x00ff))); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
326 sb.append(byteSeparator); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
327 return sb.toString(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
328 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
329 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
330 private static String toBinaryString(byte b) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
331 final String nibbles = "0000000100100011010001010110011110001001101010111100110111101111"; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
332 assert nibbles.length() == 16*4; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
333 int x1 = (b >>> 4) & 0x0f, x2 = b & 0x0f; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
334 x1 *= 4; x2 *= 4; // 4 characters per nibble |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
335 return nibbles.substring(x1, x1+4).concat(nibbles.substring(x2, x2+4)); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
336 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
337 } |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
338 } |