Mercurial > hg4j
annotate src/com/tmate/hgkit/ll/Nodeid.java @ 20:11cfabe692b3
Status operation for two repository revisions (no local dir involved)
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 04 Jan 2011 02:08:25 +0100 |
parents | df8c67f3006a |
children | 603806cd2dc6 |
rev | line source |
---|---|
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
1 /* |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
2 * Copyright (c) 2010, 2011 Artem Tikhomirov |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 */ |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 package com.tmate.hgkit.ll; |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
6 import java.util.Arrays; |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
7 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 /** |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
10 * Whether to store fixed size array (20 bytes) - ease of manipulation (e.g. hashcode/equals), or |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
11 * memory effective - reuse supplied array, keep significant bits only? |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
12 * Fixed size array looks most appealing to me now - I doubt one can save any significant amount of memory. |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
13 * There'd always 20 non-zero bytes, the difference is only for any extra bytes one may pass to constructor |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * @author artem |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
17 public final class Nodeid implements Comparable<Nodeid> { |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 public static int NULLREV = -1; |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 private final byte[] binaryData; |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 public Nodeid(byte[] binaryRepresentation) { |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
23 // 5 int fields => 32 bytes |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 // byte[20] => 48 bytes |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 this.binaryData = binaryRepresentation; |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 } |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
28 // instead of hashCode/equals |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
29 public int compareTo(Nodeid o) { |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
30 byte[] a1, a2; |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
31 if (this.binaryData.length != 20) { |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
32 a1 = new byte[20]; |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
33 System.arraycopy(binaryData, 0, a1, 20 - binaryData.length, binaryData.length); |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
34 } else { |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
35 a1 = this.binaryData; |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
36 } |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
37 |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
38 if (o.binaryData.length != 20) { |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
39 a2 = new byte[20]; |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
40 System.arraycopy(o.binaryData, 0, a2, 20 - o.binaryData.length, o.binaryData.length); |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
41 } else { |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
42 a2 = o.binaryData; |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
43 } |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
44 return Arrays.equals(a1, a2) ? 0 : -1; |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
45 } |
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
46 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 @Override |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 public String toString() { |
13
df8c67f3006a
Basic manifest parsing to analyze what's in there
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
9
diff
changeset
|
49 return new DigestHelper().toHexString(binaryData, 0, binaryData.length); |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 } |
20
11cfabe692b3
Status operation for two repository revisions (no local dir involved)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
13
diff
changeset
|
51 |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 // binascii.unhexlify() |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 assert length % 2 == 0; // Python's binascii.hexlify convert each byte into 2 digits |
9
d6d2a630f4a6
Access to underlaying file data wrapped into own Access object, implemented with FileChannel and ByteBuffer
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
0
diff
changeset
|
55 byte[] data = new byte[length >>> 1]; // XXX use known size instead? nodeid is always 20 bytes |
0
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 for (int i = 0, j = offset; i < data.length; i++) { |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 int hiNibble = Character.digit(asciiRepresentation[j++], 16); |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 int lowNibble = Character.digit(asciiRepresentation[j++], 16); |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 } |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 return new Nodeid(data); |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 } |
dbd663faec1f
Basic changelog parsing
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 } |