You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbbreviatedLongObjectId.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lfs.lib;
  44. import java.io.Serializable;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
  47. import org.eclipse.jgit.lfs.internal.LfsText;
  48. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.util.NB;
  52. import org.eclipse.jgit.util.RawParseUtils;
  53. /**
  54. * A prefix abbreviation of an {@link LongObjectId}.
  55. * <p>
  56. * Enable abbreviating SHA-256 strings used by Git LFS, using sufficient leading
  57. * digits from the LongObjectId name to still be unique within the repository
  58. * the string was generated from. These ids are likely to be unique for a useful
  59. * period of time, especially if they contain at least 6-10 hex digits.
  60. * <p>
  61. * This class converts the hex string into a binary form, to make it more
  62. * efficient for matching against an object.
  63. *
  64. * Ported to SHA-256 from {@link AbbreviatedObjectId}
  65. *
  66. * @since 4.3
  67. */
  68. public final class AbbreviatedLongObjectId implements Serializable {
  69. private static final long serialVersionUID = 1L;
  70. /**
  71. * Test a string of characters to verify it is a hex format.
  72. * <p>
  73. * If true the string can be parsed with {@link #fromString(String)}.
  74. *
  75. * @param id
  76. * the string to test.
  77. * @return true if the string can converted into an AbbreviatedObjectId.
  78. */
  79. public static final boolean isId(final String id) {
  80. if (id.length() < 2
  81. || Constants.LONG_OBJECT_ID_STRING_LENGTH < id.length())
  82. return false;
  83. try {
  84. for (int i = 0; i < id.length(); i++)
  85. RawParseUtils.parseHexInt4((byte) id.charAt(i));
  86. return true;
  87. } catch (ArrayIndexOutOfBoundsException e) {
  88. return false;
  89. }
  90. }
  91. /**
  92. * Convert an AbbreviatedObjectId from hex characters (US-ASCII).
  93. *
  94. * @param buf
  95. * the US-ASCII buffer to read from.
  96. * @param offset
  97. * position to read the first character from.
  98. * @param end
  99. * one past the last position to read (<code>end-offset</code> is
  100. * the length of the string).
  101. * @return the converted object id.
  102. */
  103. public static final AbbreviatedLongObjectId fromString(final byte[] buf,
  104. final int offset, final int end) {
  105. if (end - offset > Constants.LONG_OBJECT_ID_STRING_LENGTH)
  106. throw new IllegalArgumentException(MessageFormat.format(
  107. LfsText.get().invalidLongIdLength,
  108. Integer.valueOf(end - offset),
  109. Integer.valueOf(Constants.LONG_OBJECT_ID_STRING_LENGTH)));
  110. return fromHexString(buf, offset, end);
  111. }
  112. /**
  113. * Convert an AbbreviatedObjectId from an {@link AnyObjectId}.
  114. * <p>
  115. * This method copies over all bits of the Id, and is therefore complete
  116. * (see {@link #isComplete()}).
  117. *
  118. * @param id
  119. * the {@link ObjectId} to convert from.
  120. * @return the converted object id.
  121. */
  122. public static final AbbreviatedLongObjectId fromLongObjectId(
  123. AnyLongObjectId id) {
  124. return new AbbreviatedLongObjectId(
  125. Constants.LONG_OBJECT_ID_STRING_LENGTH, id.w1, id.w2, id.w3,
  126. id.w4);
  127. }
  128. /**
  129. * Convert an AbbreviatedLongObjectId from hex characters.
  130. *
  131. * @param str
  132. * the string to read from. Must be &lt;= 64 characters.
  133. * @return the converted object id.
  134. */
  135. public static final AbbreviatedLongObjectId fromString(final String str) {
  136. if (str.length() > Constants.LONG_OBJECT_ID_STRING_LENGTH)
  137. throw new IllegalArgumentException(
  138. MessageFormat.format(LfsText.get().invalidLongId, str));
  139. final byte[] b = org.eclipse.jgit.lib.Constants.encodeASCII(str);
  140. return fromHexString(b, 0, b.length);
  141. }
  142. private static final AbbreviatedLongObjectId fromHexString(final byte[] bs,
  143. int ptr, final int end) {
  144. try {
  145. final long a = hexUInt64(bs, ptr, end);
  146. final long b = hexUInt64(bs, ptr + 16, end);
  147. final long c = hexUInt64(bs, ptr + 32, end);
  148. final long d = hexUInt64(bs, ptr + 48, end);
  149. return new AbbreviatedLongObjectId(end - ptr, a, b, c, d);
  150. } catch (ArrayIndexOutOfBoundsException e1) {
  151. throw new InvalidLongObjectIdException(bs, ptr, end - ptr);
  152. }
  153. }
  154. private static final long hexUInt64(final byte[] bs, int p, final int end) {
  155. if (16 <= end - p)
  156. return RawParseUtils.parseHexInt64(bs, p);
  157. long r = 0;
  158. int n = 0;
  159. while (n < 16 && p < end) {
  160. r <<= 4;
  161. r |= RawParseUtils.parseHexInt4(bs[p++]);
  162. n++;
  163. }
  164. return r << (16 - n) * 4;
  165. }
  166. static long mask(final int nibbles, final long word, final long v) {
  167. final long b = (word - 1) * 16;
  168. if (b + 16 <= nibbles) {
  169. // We have all of the bits required for this word.
  170. //
  171. return v;
  172. }
  173. if (nibbles <= b) {
  174. // We have none of the bits required for this word.
  175. //
  176. return 0;
  177. }
  178. final long s = 64 - (nibbles - b) * 4;
  179. return (v >>> s) << s;
  180. }
  181. /** Number of half-bytes used by this id. */
  182. final int nibbles;
  183. final long w1;
  184. final long w2;
  185. final long w3;
  186. final long w4;
  187. AbbreviatedLongObjectId(final int n, final long new_1, final long new_2,
  188. final long new_3, final long new_4) {
  189. nibbles = n;
  190. w1 = new_1;
  191. w2 = new_2;
  192. w3 = new_3;
  193. w4 = new_4;
  194. }
  195. /** @return number of hex digits appearing in this id */
  196. public int length() {
  197. return nibbles;
  198. }
  199. /** @return true if this ObjectId is actually a complete id. */
  200. public boolean isComplete() {
  201. return length() == Constants.LONG_OBJECT_ID_STRING_LENGTH;
  202. }
  203. /** @return a complete ObjectId; null if {@link #isComplete()} is false */
  204. public LongObjectId toLongObjectId() {
  205. return isComplete() ? new LongObjectId(w1, w2, w3, w4) : null;
  206. }
  207. /**
  208. * Compares this abbreviation to a full object id.
  209. *
  210. * @param other
  211. * the other object id.
  212. * @return &lt;0 if this abbreviation names an object that is less than
  213. * <code>other</code>; 0 if this abbreviation exactly matches the
  214. * first {@link #length()} digits of <code>other.name()</code>;
  215. * &gt;0 if this abbreviation names an object that is after
  216. * <code>other</code>.
  217. */
  218. public final int prefixCompare(final AnyLongObjectId other) {
  219. int cmp;
  220. cmp = NB.compareUInt64(w1, mask(1, other.w1));
  221. if (cmp != 0)
  222. return cmp;
  223. cmp = NB.compareUInt64(w2, mask(2, other.w2));
  224. if (cmp != 0)
  225. return cmp;
  226. cmp = NB.compareUInt64(w3, mask(3, other.w3));
  227. if (cmp != 0)
  228. return cmp;
  229. return NB.compareUInt64(w4, mask(4, other.w4));
  230. }
  231. /**
  232. * Compare this abbreviation to a network-byte-order LongObjectId.
  233. *
  234. * @param bs
  235. * array containing the other LongObjectId in network byte order.
  236. * @param p
  237. * position within {@code bs} to start the compare at. At least
  238. * 32 bytes, starting at this position are required.
  239. * @return &lt;0 if this abbreviation names an object that is less than
  240. * <code>other</code>; 0 if this abbreviation exactly matches the
  241. * first {@link #length()} digits of <code>other.name()</code>;
  242. * &gt;0 if this abbreviation names an object that is after
  243. * <code>other</code>.
  244. */
  245. public final int prefixCompare(final byte[] bs, final int p) {
  246. int cmp;
  247. cmp = NB.compareUInt64(w1, mask(1, NB.decodeInt64(bs, p)));
  248. if (cmp != 0)
  249. return cmp;
  250. cmp = NB.compareUInt64(w2, mask(2, NB.decodeInt64(bs, p + 8)));
  251. if (cmp != 0)
  252. return cmp;
  253. cmp = NB.compareUInt64(w3, mask(3, NB.decodeInt64(bs, p + 16)));
  254. if (cmp != 0)
  255. return cmp;
  256. return NB.compareUInt64(w4, mask(4, NB.decodeInt64(bs, p + 24)));
  257. }
  258. /**
  259. * Compare this abbreviation to a network-byte-order LongObjectId.
  260. *
  261. * @param bs
  262. * array containing the other LongObjectId in network byte order.
  263. * @param p
  264. * position within {@code bs} to start the compare at. At least 4
  265. * longs, starting at this position are required.
  266. * @return &lt;0 if this abbreviation names an object that is less than
  267. * <code>other</code>; 0 if this abbreviation exactly matches the
  268. * first {@link #length()} digits of <code>other.name()</code>;
  269. * &gt;0 if this abbreviation names an object that is after
  270. * <code>other</code>.
  271. */
  272. public final int prefixCompare(final long[] bs, final int p) {
  273. int cmp;
  274. cmp = NB.compareUInt64(w1, mask(1, bs[p]));
  275. if (cmp != 0)
  276. return cmp;
  277. cmp = NB.compareUInt64(w2, mask(2, bs[p + 1]));
  278. if (cmp != 0)
  279. return cmp;
  280. cmp = NB.compareUInt64(w3, mask(3, bs[p + 2]));
  281. if (cmp != 0)
  282. return cmp;
  283. return NB.compareUInt64(w4, mask(4, bs[p + 3]));
  284. }
  285. /** @return value for a fan-out style map, only valid of length &gt;= 2. */
  286. public final int getFirstByte() {
  287. return (int) (w1 >>> 56);
  288. }
  289. private long mask(final long word, final long v) {
  290. return mask(nibbles, word, v);
  291. }
  292. @Override
  293. public int hashCode() {
  294. return (int) (w1 >> 32);
  295. }
  296. @Override
  297. public boolean equals(final Object o) {
  298. if (o instanceof AbbreviatedLongObjectId) {
  299. final AbbreviatedLongObjectId b = (AbbreviatedLongObjectId) o;
  300. return nibbles == b.nibbles && w1 == b.w1 && w2 == b.w2
  301. && w3 == b.w3 && w4 == b.w4;
  302. }
  303. return false;
  304. }
  305. /**
  306. * @return string form of the abbreviation, in lower case hexadecimal.
  307. */
  308. public final String name() {
  309. final char[] b = new char[Constants.LONG_OBJECT_ID_STRING_LENGTH];
  310. AnyLongObjectId.formatHexChar(b, 0, w1);
  311. if (nibbles <= 16)
  312. return new String(b, 0, nibbles);
  313. AnyLongObjectId.formatHexChar(b, 16, w2);
  314. if (nibbles <= 32)
  315. return new String(b, 0, nibbles);
  316. AnyLongObjectId.formatHexChar(b, 32, w3);
  317. if (nibbles <= 48)
  318. return new String(b, 0, nibbles);
  319. AnyLongObjectId.formatHexChar(b, 48, w4);
  320. return new String(b, 0, nibbles);
  321. }
  322. @SuppressWarnings("nls")
  323. @Override
  324. public String toString() {
  325. return "AbbreviatedLongObjectId[" + name() + "]"; //$NON-NLS-1$
  326. }
  327. }