Mercurial > jhg
annotate cmdline/org/tmatesoft/hg/console/Incoming.java @ 173:4bf061a7c001
Test algorithm to build sequence of missing revisions
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 29 Mar 2011 02:20:02 +0200 |
parents | 2c3e96674e2a |
children | b1de83ffa7f8 |
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 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
33 import junit.framework.Assert; |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
35 import org.tmatesoft.hg.core.Nodeid; |
97
ee2c750b036d
Changelog to HgChangelog
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
74
diff
changeset
|
36 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
|
37 import org.tmatesoft.hg.repo.HgRemoteRepository.RemoteBranch; |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
38 import org.tmatesoft.hg.repo.HgRepository; |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
39 |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 /** |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
42 * 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
|
43 * hg incoming counterpart |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
44 * |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
45 * @author Artem Tikhomirov |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
46 * @author TMate Software Ltd. |
31
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 class Incoming { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 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
|
51 if (Boolean.TRUE.booleanValue()) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
52 new SequenceConstructor().test(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
53 return; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
54 } |
74
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
55 Options cmdLineOpts = Options.parse(args); |
6f1b88693d48
Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
33
diff
changeset
|
56 HgRepository hgRepo = cmdLineOpts.findRepository(); |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 if (hgRepo.isInvalid()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 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
|
59 return; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 // 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
|
62 // 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
|
63 final HgChangelog.ParentWalker pw = hgRepo.getChangelog().new ParentWalker(); |
31
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 pw.init(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 HashSet<Nodeid> base = new HashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 HashSet<Nodeid> unknownRemoteHeads = new HashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 // 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
|
69 unknownRemoteHeads.add(Nodeid.fromAscii("382cfe9463db0484a14136e4b38407419525f0c0".getBytes(), 0, 40)); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 LinkedList<RemoteBranch> remoteBranches = new LinkedList<RemoteBranch>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 remoteBranches(unknownRemoteHeads, remoteBranches); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 HashSet<Nodeid> visited = new HashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 HashSet<RemoteBranch> processed = new HashSet<RemoteBranch>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 LinkedList<Nodeid[]> toScan = new LinkedList<Nodeid[]>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 LinkedHashSet<Nodeid> toFetch = new LinkedHashSet<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 // 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
|
79 HashSet<Nodeid> unknownHeads /*req*/ = new HashSet<Nodeid>(unknownRemoteHeads); |
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 LinkedList<Nodeid> toQueryRemote = new LinkedList<Nodeid>(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 while (!remoteBranches.isEmpty()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 RemoteBranch next = remoteBranches.removeFirst(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 if (visited.contains(next.head) || processed.contains(next)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 continue; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 if (Nodeid.NULL.equals(next.head)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 // 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
|
89 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
|
90 } else if (pw.knownNode(next.root)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 // 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
|
92 toScan.addLast(new Nodeid[] { next.head, next.root }); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 processed.add(next); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 } else { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 if (!visited.contains(next.root) && !toFetch.contains(next.root)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 // 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
|
97 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
|
98 toFetch.add(next.root); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 // 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
|
101 if (pw.knownNode(next.p1)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 base.add(next.p1); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 if (pw.knownNode(next.p2)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 base.add(next.p2); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
106 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
107 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
108 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
|
109 toQueryRemote.add(next.p1); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 unknownHeads.add(next.p1); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 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
|
113 toQueryRemote.add(next.p2); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 unknownHeads.add(next.p2); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 } |
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 visited.add(next.head); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
118 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 if (!toQueryRemote.isEmpty()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
120 // 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
|
121 // 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
|
122 remoteBranches(toQueryRemote, remoteBranches); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
125 while (!toScan.isEmpty()) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
126 Nodeid[] head_root = toScan.removeFirst(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 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
|
128 nodesBetween.add(head_root[1]); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 int x = 1; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
130 Nodeid p = head_root[0]; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
131 for (Nodeid i : nodesBetween) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 System.out.println("narrowing " + x + ":" + nodesBetween.size() + " " + i.shortNotation()); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 if (pw.knownNode(i)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 if (x <= 2) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 toFetch.add(p); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
136 base.add(i); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
137 } else { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
138 // 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
|
139 // likely to "batch" calls to server |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
140 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
|
141 toScan.addLast(new Nodeid[] { p, i }); |
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 break; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
144 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 x = x << 1; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
146 p = i; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
147 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
148 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
149 for (Nodeid n : toFetch) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 if (pw.knownNode(n)) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
151 System.out.println("Erroneous to fetch:" + n); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
152 } else { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
153 System.out.println(n); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
154 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
155 } |
33
565ce0835674
TODO added, to try stream for unzip in revlog
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
31
diff
changeset
|
156 |
31
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 |
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 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
|
161 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
162 // TODO implement this with remote access |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
163 // |
171
2c3e96674e2a
Towards outgoing changes - initial detection logic, get connected with remote repo stub
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
102
diff
changeset
|
164 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
|
165 remoteBranches.add(rb); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
166 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
167 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
168 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
|
169 // sent: cmd=between&pairs=d6d2a630f4a6d670c90a5ca909150f2b426ec88f-dbd663faec1f0175619cf7668bddc6350548b8d6 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
170 // received: a78c980749e3ccebb47138b547e9b644a22797a9 286d221f6c28cbfce25ea314e1f46a23b7f979d3 fc265ddeab262ff5c34b4cf4e2522d8d41f1f05b a3576694a4d1edaa681cab15b89d6b556b02aff4 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
171 // 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
|
172 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
173 // |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
174 // a78c980749e3ccebb47138b547e9b644a22797a9 286d221f6c28cbfce25ea314e1f46a23b7f979d3 fc265ddeab262ff5c34b4cf4e2522d8d41f1f05b a3576694a4d1edaa681cab15b89d6b556b02aff4 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
175 //d6d2a630f4a6d670c90a5ca909150f2b426ec88f a78c980749e3ccebb47138b547e9b644a22797a9 5abe5af181bd6a6d3e94c378376c901f0f80da50 08db726a0fb7914ac9d27ba26dc8bbf6385a0554 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
176 |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
177 // TODO implement with remote access |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
178 String response = null; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
179 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
|
180 response = "d6d2a630f4a6d670c90a5ca909150f2b426ec88f a78c980749e3ccebb47138b547e9b644a22797a9 5abe5af181bd6a6d3e94c378376c901f0f80da50 08db726a0fb7914ac9d27ba26dc8bbf6385a0554"; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
181 } 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
|
182 response = "286d221f6c28cbfce25ea314e1f46a23b7f979d3"; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
183 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
184 if (response == null) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
185 throw HgRepository.notImplemented(); |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
186 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
187 for (String s : response.split(" ")) { |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
188 list.add(Nodeid.fromAscii(s.getBytes(), 0, 40)); |
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 return list; |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
191 } |
346b66add79d
Basic lookup for incoming changes
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
192 |
173
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
193 private static class SequenceConstructor { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
194 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
195 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
|
196 if (head <= (root+1)) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
197 return new int[0]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
198 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
199 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
|
200 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
|
201 int[] rv = new int[size]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
202 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
|
203 rv[i] = root + v; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
204 v = v << 1; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
205 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
206 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
|
207 return rv; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
208 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
209 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
210 public void test() { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
211 int root = 0, head = 1000; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
212 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
|
213 // 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
|
214 // 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
|
215 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
|
216 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
|
217 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
|
218 datas.put(root, data); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
219 int totalQueries = 1; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
220 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
|
221 int[] checkSequence = null; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
222 while(!datas.isEmpty()) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
223 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
|
224 do { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
225 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
|
226 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
|
227 int r = next.getKey(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
228 data = next.getValue(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
229 it.remove(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
230 populate(r, head, data, finalSequence); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
231 if (checkSequence != null) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
232 boolean match = true; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
233 // 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
|
234 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
|
235 // System.out.println(i); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
236 // 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
|
237 // 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
|
238 if (checkSequence[i] != finalSequence[i]) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
239 match = false; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
240 } else { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
241 match &= true; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
242 } |
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 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
|
245 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
246 if (data.length > 2) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
247 for (int x : data) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
248 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
|
249 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
|
250 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
251 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
252 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
253 } while (!datas.isEmpty()) ; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
254 if (!toQuery.isEmpty()) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
255 System.out.println(); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
256 totalQueries++; |
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 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
|
259 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
260 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
|
261 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
|
262 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
263 }); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
264 for (int[] x : toQuery) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
265 if (!queried.contains(x[0])) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
266 queried.add(x[0]); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
267 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
|
268 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
|
269 exactNumberOfElements = x[0] + 1; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
270 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
|
271 // 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
|
272 // 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
|
273 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
|
274 int trailingBits = (exactNumberOfElements + 2) & 0x1f; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
275 if (trailingBits != 0) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
276 totalInts++; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
277 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
278 checkSequence = new int[totalInts]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
279 Arrays.fill(checkSequence, 0xffffffff); |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
280 checkSequence[0] &= 0x7FFFFFFF; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
281 if (trailingBits == 0) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
282 checkSequence[totalInts-1] &= 0xFFFFFFFE; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
283 } else if (trailingBits == 1) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
284 checkSequence[totalInts-1] = 0; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
285 } else { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
286 // 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
|
287 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
|
288 checkSequence[totalInts - 1] &= mask; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
289 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
290 for (int e : checkSequence) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
291 System.out.print(toBinaryString(e, ' ')); |
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 System.out.println(); |
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 datas.put(x[0], data); |
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 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
299 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
300 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
|
301 for (int x : finalSequence) { |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
302 System.out.print(toBinaryString(x, ' ')); |
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 } |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
305 |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
306 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
|
307 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
|
308 int value = data[x]; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
309 int value_check = root+i; |
4bf061a7c001
Test algorithm to build sequence of missing revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
171
diff
changeset
|
310 Assert.assertEquals(value, value_check); |
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 } |