tikhomirov@31: /*
tikhomirov@74: * Copyright (c) 2011 TMate Software Ltd
tikhomirov@74: *
tikhomirov@74: * This program is free software; you can redistribute it and/or modify
tikhomirov@74: * it under the terms of the GNU General Public License as published by
tikhomirov@74: * the Free Software Foundation; version 2 of the License.
tikhomirov@74: *
tikhomirov@74: * This program is distributed in the hope that it will be useful,
tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@74: * GNU General Public License for more details.
tikhomirov@74: *
tikhomirov@74: * For information on how to redistribute this software under
tikhomirov@74: * the terms of a license other than GNU General Public License
tikhomirov@102: * contact TMate Software at support@hg4j.com
tikhomirov@31: */
tikhomirov@74: package org.tmatesoft.hg.console;
tikhomirov@31:
tikhomirov@173: import java.util.Arrays;
tikhomirov@173: import java.util.Collections;
tikhomirov@173: import java.util.Comparator;
tikhomirov@31: import java.util.HashSet;
tikhomirov@173: import java.util.Iterator;
tikhomirov@173: import java.util.LinkedHashMap;
tikhomirov@31: import java.util.LinkedList;
tikhomirov@31: import java.util.List;
tikhomirov@173: import java.util.Map.Entry;
tikhomirov@173:
tikhomirov@206: import org.tmatesoft.hg.core.HgIncomingCommand;
tikhomirov@206: import org.tmatesoft.hg.core.HgRepoFacade;
tikhomirov@74: import org.tmatesoft.hg.core.Nodeid;
tikhomirov@176: import org.tmatesoft.hg.repo.HgLookup;
tikhomirov@176: import org.tmatesoft.hg.repo.HgRemoteRepository;
tikhomirov@74:
tikhomirov@31:
tikhomirov@31: /**
tikhomirov@206: * hg incoming counterpart
tikhomirov@74: *
tikhomirov@74: * @author Artem Tikhomirov
tikhomirov@74: * @author TMate Software Ltd.
tikhomirov@31: */
tikhomirov@31: public class Incoming {
tikhomirov@31:
tikhomirov@31: public static void main(String[] args) throws Exception {
tikhomirov@176: if (Boolean.FALSE.booleanValue()) {
tikhomirov@173: new SequenceConstructor().test();
tikhomirov@173: return;
tikhomirov@173: }
tikhomirov@74: Options cmdLineOpts = Options.parse(args);
tikhomirov@206: HgRepoFacade hgRepo = new HgRepoFacade();
tikhomirov@206: if (!hgRepo.init(cmdLineOpts.findRepository())) {
tikhomirov@206: System.err.printf("Can't find repository in: %s\n", hgRepo.getRepository().getLocation());
tikhomirov@31: return;
tikhomirov@31: }
tikhomirov@206: HgRemoteRepository hgRemote = new HgLookup().detectRemote(cmdLineOpts.getSingle(""), hgRepo.getRepository());
tikhomirov@181: if (hgRemote.isInvalid()) {
tikhomirov@181: System.err.printf("Remote repository %s is not valid", hgRemote.getLocation());
tikhomirov@181: return;
tikhomirov@176: }
tikhomirov@206: HgIncomingCommand cmd = hgRepo.createIncomingCommand();
tikhomirov@206: cmd.against(hgRemote);
tikhomirov@31: //
tikhomirov@215: List missing = cmd.executeLite();
tikhomirov@206: Collections.reverse(missing); // useful to test output, from newer to older
tikhomirov@206: Outgoing.dump("Nodes to fetch:", missing);
tikhomirov@208: System.out.printf("Total: %d\n\n", missing.size());
tikhomirov@184: //
tikhomirov@184: // Complete
tikhomirov@206: final ChangesetDumpHandler h = new ChangesetDumpHandler(hgRepo.getRepository());
tikhomirov@206: h.complete(false); // this option looks up index of parent revision, done via repo.changelog (which doesn't have any of these new revisions)
tikhomirov@206: // this can be fixed by tracking all nodeid->revision idx inside ChangesetDumpHandler, and refer to repo.changelog only when that mapping didn't work
tikhomirov@206: h.verbose(cmdLineOpts.getBoolean("-v", "--verbose"));
tikhomirov@206: cmd.executeFull(h);
tikhomirov@178: }
tikhomirov@178:
tikhomirov@31:
tikhomirov@206: /*
tikhomirov@206: * This is for investigation purposes only
tikhomirov@206: */
tikhomirov@173: private static class SequenceConstructor {
tikhomirov@173:
tikhomirov@173: private int[] between(int root, int head) {
tikhomirov@173: if (head <= (root+1)) {
tikhomirov@173: return new int[0];
tikhomirov@173: }
tikhomirov@173: System.out.printf("[%d, %d]\t\t", root, head);
tikhomirov@173: int size = 1 + (int) Math.floor(Math.log(head-root - 1) / Math.log(2));
tikhomirov@173: int[] rv = new int[size];
tikhomirov@173: for (int v = 1, i = 0; i < rv.length; i++) {
tikhomirov@173: rv[i] = root + v;
tikhomirov@173: v = v << 1;
tikhomirov@173: }
tikhomirov@173: System.out.println(Arrays.toString(rv));
tikhomirov@173: return rv;
tikhomirov@173: }
tikhomirov@173:
tikhomirov@173: public void test() {
tikhomirov@176: int root = 0, head = 126;
tikhomirov@176: int[] data = between(root, head); // max number of elements to recover is 2**data.length-1, when head is exactly
tikhomirov@176: // 2**data.length element of the branch.
tikhomirov@176: // In such case, total number of elements in the branch (including head and root, would be 2**data.length+1
tikhomirov@176: int[] finalSequence = new int[1 + (1 << data.length >>> 5)]; // div 32 - total bits to integers, +1 for possible modulus
tikhomirov@173: int exactNumberOfElements = -1; // exact number of meaningful bits in finalSequence
tikhomirov@173: LinkedHashMap datas = new LinkedHashMap();
tikhomirov@173: datas.put(root, data);
tikhomirov@173: int totalQueries = 1;
tikhomirov@173: HashSet queried = new HashSet();
tikhomirov@173: int[] checkSequence = null;
tikhomirov@173: while(!datas.isEmpty()) {
tikhomirov@173: LinkedList toQuery = new LinkedList();
tikhomirov@173: do {
tikhomirov@173: Iterator> it = datas.entrySet().iterator();
tikhomirov@173: Entry next = it.next();
tikhomirov@173: int r = next.getKey();
tikhomirov@173: data = next.getValue();
tikhomirov@173: it.remove();
tikhomirov@173: populate(r, head, data, finalSequence);
tikhomirov@173: if (checkSequence != null) {
tikhomirov@173: boolean match = true;
tikhomirov@173: // System.out.println("Try to match:");
tikhomirov@173: for (int i = 0; i < checkSequence.length; i++) {
tikhomirov@173: // System.out.println(i);
tikhomirov@173: // System.out.println("control:" + toBinaryString(checkSequence[i], ' '));
tikhomirov@173: // System.out.println("present:" + toBinaryString(finalSequence[i], ' '));
tikhomirov@173: if (checkSequence[i] != finalSequence[i]) {
tikhomirov@173: match = false;
tikhomirov@173: } else {
tikhomirov@173: match &= true;
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@173: System.out.println(match ? "Match, on query:" + totalQueries : "Didn't match");
tikhomirov@173: }
tikhomirov@176: if (data.length > 1) {
tikhomirov@176: /*queries for elements next to head is senseless, hence data.length check above and head-x below*/
tikhomirov@173: for (int x : data) {
tikhomirov@176: if (!queried.contains(x) && head - x > 1) {
tikhomirov@173: toQuery.add(new int[] {x, head});
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@173: } while (!datas.isEmpty()) ;
tikhomirov@173: if (!toQuery.isEmpty()) {
tikhomirov@173: System.out.println();
tikhomirov@173: totalQueries++;
tikhomirov@173: }
tikhomirov@173: Collections.sort(toQuery, new Comparator() {
tikhomirov@173:
tikhomirov@173: public int compare(int[] o1, int[] o2) {
tikhomirov@173: return o1[0] < o2[0] ? -1 : (o1[0] == o2[0] ? 0 : 1);
tikhomirov@173: }
tikhomirov@173: });
tikhomirov@173: for (int[] x : toQuery) {
tikhomirov@173: if (!queried.contains(x[0])) {
tikhomirov@173: queried.add(x[0]);
tikhomirov@173: data = between(x[0], x[1]);
tikhomirov@173: if (exactNumberOfElements == -1 && data.length == 1) {
tikhomirov@173: exactNumberOfElements = x[0] + 1;
tikhomirov@173: System.out.printf("On query %d found out exact number of missing elements: %d\n", totalQueries, exactNumberOfElements);
tikhomirov@173: // get a bit sequence of exactNumberOfElements, 0111..110
tikhomirov@173: // to 'and' it with finalSequence later
tikhomirov@173: int totalInts = (exactNumberOfElements + 2 /*heading and tailing zero bits*/) >>> 5;
tikhomirov@173: int trailingBits = (exactNumberOfElements + 2) & 0x1f;
tikhomirov@173: if (trailingBits != 0) {
tikhomirov@173: totalInts++;
tikhomirov@173: }
tikhomirov@173: checkSequence = new int[totalInts];
tikhomirov@173: Arrays.fill(checkSequence, 0xffffffff);
tikhomirov@173: checkSequence[0] &= 0x7FFFFFFF;
tikhomirov@173: if (trailingBits == 0) {
tikhomirov@173: checkSequence[totalInts-1] &= 0xFFFFFFFE;
tikhomirov@173: } else if (trailingBits == 1) {
tikhomirov@173: checkSequence[totalInts-1] = 0;
tikhomirov@173: } else {
tikhomirov@173: // trailingBits include heading and trailing zero bits
tikhomirov@173: int mask = 0x80000000 >> trailingBits-2; // with sign!
tikhomirov@173: checkSequence[totalInts - 1] &= mask;
tikhomirov@173: }
tikhomirov@173: for (int e : checkSequence) {
tikhomirov@173: System.out.print(toBinaryString(e, ' '));
tikhomirov@173: }
tikhomirov@173: System.out.println();
tikhomirov@173: }
tikhomirov@173: datas.put(x[0], data);
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@173:
tikhomirov@173: System.out.println("Total queries:" + totalQueries);
tikhomirov@173: for (int x : finalSequence) {
tikhomirov@173: System.out.print(toBinaryString(x, ' '));
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@173:
tikhomirov@173: private void populate(int root, int head, int[] data, int[] finalSequence) {
tikhomirov@173: for (int i = 1, x = 0; root+i < head; i = i << 1, x++) {
tikhomirov@173: int value = data[x];
tikhomirov@173: int value_check = root+i;
tikhomirov@174: if (value != value_check) {
tikhomirov@174: throw new IllegalStateException();
tikhomirov@174: }
tikhomirov@173: int wordIx = (root + i) >>> 5;
tikhomirov@173: int bitIx = (root + i) & 0x1f;
tikhomirov@173: finalSequence[wordIx] |= 1 << (31-bitIx);
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@173:
tikhomirov@173: private static String toBinaryString(int x, char byteSeparator) {
tikhomirov@173: StringBuilder sb = new StringBuilder(4*8+4);
tikhomirov@173: sb.append(toBinaryString((byte) (x >>> 24)));
tikhomirov@173: sb.append(byteSeparator);
tikhomirov@173: sb.append(toBinaryString((byte) ((x & 0x00ff0000) >>> 16)));
tikhomirov@173: sb.append(byteSeparator);
tikhomirov@173: sb.append(toBinaryString((byte) ((x & 0x00ff00) >>> 8)));
tikhomirov@173: sb.append(byteSeparator);
tikhomirov@173: sb.append(toBinaryString((byte) (x & 0x00ff)));
tikhomirov@173: sb.append(byteSeparator);
tikhomirov@173: return sb.toString();
tikhomirov@173: }
tikhomirov@173:
tikhomirov@173: private static String toBinaryString(byte b) {
tikhomirov@173: final String nibbles = "0000000100100011010001010110011110001001101010111100110111101111";
tikhomirov@173: assert nibbles.length() == 16*4;
tikhomirov@173: int x1 = (b >>> 4) & 0x0f, x2 = b & 0x0f;
tikhomirov@173: x1 *= 4; x2 *= 4; // 4 characters per nibble
tikhomirov@173: return nibbles.substring(x1, x1+4).concat(nibbles.substring(x2, x2+4));
tikhomirov@173: }
tikhomirov@173: }
tikhomirov@31: }