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.

AnyLongObjectId.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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.IOException;
  45. import java.io.OutputStream;
  46. import java.io.Writer;
  47. import java.nio.ByteBuffer;
  48. import org.eclipse.jgit.lib.AnyObjectId;
  49. import org.eclipse.jgit.util.NB;
  50. import org.eclipse.jgit.util.References;
  51. /**
  52. * A (possibly mutable) SHA-256 abstraction.
  53. * <p>
  54. * If this is an instance of
  55. * {@link org.eclipse.jgit.lfs.lib.MutableLongObjectId} the concept of equality
  56. * with this instance can alter at any time, if this instance is modified to
  57. * represent a different object name.
  58. *
  59. * Ported to SHA-256 from {@link org.eclipse.jgit.lib.AnyObjectId}
  60. *
  61. * @since 4.3
  62. */
  63. public abstract class AnyLongObjectId implements Comparable<AnyLongObjectId> {
  64. /**
  65. * Compare two object identifier byte sequences for equality.
  66. *
  67. * @param firstObjectId
  68. * the first identifier to compare. Must not be null.
  69. * @param secondObjectId
  70. * the second identifier to compare. Must not be null.
  71. * @return true if the two identifiers are the same.
  72. * @deprecated use {@link #isEqual(AnyLongObjectId, AnyLongObjectId)}
  73. * instead.
  74. */
  75. @Deprecated
  76. @SuppressWarnings("AmbiguousMethodReference")
  77. public static boolean equals(final AnyLongObjectId firstObjectId,
  78. final AnyLongObjectId secondObjectId) {
  79. return isEqual(firstObjectId, secondObjectId);
  80. }
  81. /**
  82. * Compare two object identifier byte sequences for equality.
  83. *
  84. * @param firstObjectId
  85. * the first identifier to compare. Must not be null.
  86. * @param secondObjectId
  87. * the second identifier to compare. Must not be null.
  88. * @return true if the two identifiers are the same.
  89. * @since 5.4
  90. */
  91. public static boolean isEqual(final AnyLongObjectId firstObjectId,
  92. final AnyLongObjectId secondObjectId) {
  93. if (References.isSameObject(firstObjectId, secondObjectId)) {
  94. return true;
  95. }
  96. // We test word 2 first as odds are someone already used our
  97. // word 1 as a hash code, and applying that came up with these
  98. // two instances we are comparing for equality. Therefore the
  99. // first two words are very likely to be identical. We want to
  100. // break away from collisions as quickly as possible.
  101. //
  102. return firstObjectId.w2 == secondObjectId.w2
  103. && firstObjectId.w3 == secondObjectId.w3
  104. && firstObjectId.w4 == secondObjectId.w4
  105. && firstObjectId.w1 == secondObjectId.w1;
  106. }
  107. long w1;
  108. long w2;
  109. long w3;
  110. long w4;
  111. /**
  112. * Get the first 8 bits of the LongObjectId.
  113. *
  114. * This is a faster version of {@code getByte(0)}.
  115. *
  116. * @return a discriminator usable for a fan-out style map. Returned values
  117. * are unsigned and thus are in the range [0,255] rather than the
  118. * signed byte range of [-128, 127].
  119. */
  120. public final int getFirstByte() {
  121. return (int) (w1 >>> 56);
  122. }
  123. /**
  124. * Get the second 8 bits of the LongObjectId.
  125. *
  126. * @return a discriminator usable for a fan-out style map. Returned values
  127. * are unsigned and thus are in the range [0,255] rather than the
  128. * signed byte range of [-128, 127].
  129. */
  130. public final int getSecondByte() {
  131. return (int) ((w1 >>> 48) & 0xff);
  132. }
  133. /**
  134. * Get any byte from the LongObjectId.
  135. *
  136. * Callers hard-coding {@code getByte(0)} should instead use the much faster
  137. * special case variant {@link #getFirstByte()}.
  138. *
  139. * @param index
  140. * index of the byte to obtain from the raw form of the
  141. * LongObjectId. Must be in range [0,
  142. * {@link org.eclipse.jgit.lfs.lib.Constants#LONG_OBJECT_ID_LENGTH}).
  143. * @return the value of the requested byte at {@code index}. Returned values
  144. * are unsigned and thus are in the range [0,255] rather than the
  145. * signed byte range of [-128, 127].
  146. * @throws java.lang.ArrayIndexOutOfBoundsException
  147. * {@code index} is less than 0, equal to
  148. * {@link org.eclipse.jgit.lfs.lib.Constants#LONG_OBJECT_ID_LENGTH},
  149. * or greater than
  150. * {@link org.eclipse.jgit.lfs.lib.Constants#LONG_OBJECT_ID_LENGTH}.
  151. */
  152. public final int getByte(int index) {
  153. long w;
  154. switch (index >> 3) {
  155. case 0:
  156. w = w1;
  157. break;
  158. case 1:
  159. w = w2;
  160. break;
  161. case 2:
  162. w = w3;
  163. break;
  164. case 3:
  165. w = w4;
  166. break;
  167. default:
  168. throw new ArrayIndexOutOfBoundsException(index);
  169. }
  170. return (int) ((w >>> (8 * (15 - (index & 15)))) & 0xff);
  171. }
  172. /**
  173. * {@inheritDoc}
  174. *
  175. * Compare this LongObjectId to another and obtain a sort ordering.
  176. */
  177. @Override
  178. public final int compareTo(AnyLongObjectId other) {
  179. if (this == other)
  180. return 0;
  181. int cmp;
  182. cmp = NB.compareUInt64(w1, other.w1);
  183. if (cmp != 0)
  184. return cmp;
  185. cmp = NB.compareUInt64(w2, other.w2);
  186. if (cmp != 0)
  187. return cmp;
  188. cmp = NB.compareUInt64(w3, other.w3);
  189. if (cmp != 0)
  190. return cmp;
  191. return NB.compareUInt64(w4, other.w4);
  192. }
  193. /**
  194. * Compare this LongObjectId to a network-byte-order LongObjectId.
  195. *
  196. * @param bs
  197. * array containing the other LongObjectId in network byte order.
  198. * @param p
  199. * position within {@code bs} to start the compare at. At least
  200. * 32 bytes, starting at this position are required.
  201. * @return a negative integer, zero, or a positive integer as this object is
  202. * less than, equal to, or greater than the specified object.
  203. */
  204. public final int compareTo(byte[] bs, int p) {
  205. int cmp;
  206. cmp = NB.compareUInt64(w1, NB.decodeInt64(bs, p));
  207. if (cmp != 0)
  208. return cmp;
  209. cmp = NB.compareUInt64(w2, NB.decodeInt64(bs, p + 8));
  210. if (cmp != 0)
  211. return cmp;
  212. cmp = NB.compareUInt64(w3, NB.decodeInt64(bs, p + 16));
  213. if (cmp != 0)
  214. return cmp;
  215. return NB.compareUInt64(w4, NB.decodeInt64(bs, p + 24));
  216. }
  217. /**
  218. * Compare this LongObjectId to a network-byte-order LongObjectId.
  219. *
  220. * @param bs
  221. * array containing the other LongObjectId in network byte order.
  222. * @param p
  223. * position within {@code bs} to start the compare at. At least 4
  224. * longs, starting at this position are required.
  225. * @return a negative integer, zero, or a positive integer as this object is
  226. * less than, equal to, or greater than the specified object.
  227. */
  228. public final int compareTo(long[] bs, int p) {
  229. int cmp;
  230. cmp = NB.compareUInt64(w1, bs[p]);
  231. if (cmp != 0)
  232. return cmp;
  233. cmp = NB.compareUInt64(w2, bs[p + 1]);
  234. if (cmp != 0)
  235. return cmp;
  236. cmp = NB.compareUInt64(w3, bs[p + 2]);
  237. if (cmp != 0)
  238. return cmp;
  239. return NB.compareUInt64(w4, bs[p + 3]);
  240. }
  241. /**
  242. * Tests if this LongObjectId starts with the given abbreviation.
  243. *
  244. * @param abbr
  245. * the abbreviation.
  246. * @return true if this LongObjectId begins with the abbreviation; else
  247. * false.
  248. */
  249. public boolean startsWith(AbbreviatedLongObjectId abbr) {
  250. return abbr.prefixCompare(this) == 0;
  251. }
  252. /** {@inheritDoc} */
  253. @Override
  254. public final int hashCode() {
  255. return (int) (w1 >> 32);
  256. }
  257. /**
  258. * Determine if this LongObjectId has exactly the same value as another.
  259. *
  260. * @param other
  261. * the other id to compare to. May be null.
  262. * @return true only if both LongObjectIds have identical bits.
  263. */
  264. @SuppressWarnings({ "NonOverridingEquals", "AmbiguousMethodReference" })
  265. public final boolean equals(AnyLongObjectId other) {
  266. return other != null ? equals(this, other) : false;
  267. }
  268. /** {@inheritDoc} */
  269. @Override
  270. public final boolean equals(Object o) {
  271. if (o instanceof AnyLongObjectId) {
  272. return equals((AnyLongObjectId) o);
  273. }
  274. return false;
  275. }
  276. /**
  277. * Copy this LongObjectId to an output writer in raw binary.
  278. *
  279. * @param w
  280. * the buffer to copy to. Must be in big endian order.
  281. */
  282. public void copyRawTo(ByteBuffer w) {
  283. w.putLong(w1);
  284. w.putLong(w2);
  285. w.putLong(w3);
  286. w.putLong(w4);
  287. }
  288. /**
  289. * Copy this LongObjectId to a byte array.
  290. *
  291. * @param b
  292. * the buffer to copy to.
  293. * @param o
  294. * the offset within b to write at.
  295. */
  296. public void copyRawTo(byte[] b, int o) {
  297. NB.encodeInt64(b, o, w1);
  298. NB.encodeInt64(b, o + 8, w2);
  299. NB.encodeInt64(b, o + 16, w3);
  300. NB.encodeInt64(b, o + 24, w4);
  301. }
  302. /**
  303. * Copy this LongObjectId to an long array.
  304. *
  305. * @param b
  306. * the buffer to copy to.
  307. * @param o
  308. * the offset within b to write at.
  309. */
  310. public void copyRawTo(long[] b, int o) {
  311. b[o] = w1;
  312. b[o + 1] = w2;
  313. b[o + 2] = w3;
  314. b[o + 3] = w4;
  315. }
  316. /**
  317. * Copy this LongObjectId to an output writer in raw binary.
  318. *
  319. * @param w
  320. * the stream to write to.
  321. * @throws java.io.IOException
  322. * the stream writing failed.
  323. */
  324. public void copyRawTo(OutputStream w) throws IOException {
  325. writeRawLong(w, w1);
  326. writeRawLong(w, w2);
  327. writeRawLong(w, w3);
  328. writeRawLong(w, w4);
  329. }
  330. private static void writeRawLong(OutputStream w, long v)
  331. throws IOException {
  332. w.write((int) (v >>> 56));
  333. w.write((int) (v >>> 48));
  334. w.write((int) (v >>> 40));
  335. w.write((int) (v >>> 32));
  336. w.write((int) (v >>> 24));
  337. w.write((int) (v >>> 16));
  338. w.write((int) (v >>> 8));
  339. w.write((int) v);
  340. }
  341. /**
  342. * Copy this LongObjectId to an output writer in hex format.
  343. *
  344. * @param w
  345. * the stream to copy to.
  346. * @throws java.io.IOException
  347. * the stream writing failed.
  348. */
  349. public void copyTo(OutputStream w) throws IOException {
  350. w.write(toHexByteArray());
  351. }
  352. /**
  353. * Copy this LongObjectId to a byte array in hex format.
  354. *
  355. * @param b
  356. * the buffer to copy to.
  357. * @param o
  358. * the offset within b to write at.
  359. */
  360. public void copyTo(byte[] b, int o) {
  361. formatHexByte(b, o + 0, w1);
  362. formatHexByte(b, o + 16, w2);
  363. formatHexByte(b, o + 32, w3);
  364. formatHexByte(b, o + 48, w4);
  365. }
  366. /**
  367. * Copy this LongObjectId to a ByteBuffer in hex format.
  368. *
  369. * @param b
  370. * the buffer to copy to.
  371. */
  372. public void copyTo(ByteBuffer b) {
  373. b.put(toHexByteArray());
  374. }
  375. private byte[] toHexByteArray() {
  376. final byte[] dst = new byte[Constants.LONG_OBJECT_ID_STRING_LENGTH];
  377. formatHexByte(dst, 0, w1);
  378. formatHexByte(dst, 16, w2);
  379. formatHexByte(dst, 32, w3);
  380. formatHexByte(dst, 48, w4);
  381. return dst;
  382. }
  383. private static final byte[] hexbyte = { '0', '1', '2', '3', '4', '5', '6',
  384. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  385. private static void formatHexByte(byte[] dst, int p, long w) {
  386. int o = p + 15;
  387. while (o >= p && w != 0) {
  388. dst[o--] = hexbyte[(int) (w & 0xf)];
  389. w >>>= 4;
  390. }
  391. while (o >= p)
  392. dst[o--] = '0';
  393. }
  394. /**
  395. * Copy this LongObjectId to an output writer in hex format.
  396. *
  397. * @param w
  398. * the stream to copy to.
  399. * @throws java.io.IOException
  400. * the stream writing failed.
  401. */
  402. public void copyTo(Writer w) throws IOException {
  403. w.write(toHexCharArray());
  404. }
  405. /**
  406. * Copy this LongObjectId to an output writer in hex format.
  407. *
  408. * @param tmp
  409. * temporary char array to buffer construct into before writing.
  410. * Must be at least large enough to hold 2 digits for each byte
  411. * of object id (64 characters or larger).
  412. * @param w
  413. * the stream to copy to.
  414. * @throws java.io.IOException
  415. * the stream writing failed.
  416. */
  417. public void copyTo(char[] tmp, Writer w) throws IOException {
  418. toHexCharArray(tmp);
  419. w.write(tmp, 0, Constants.LONG_OBJECT_ID_STRING_LENGTH);
  420. }
  421. /**
  422. * Copy this LongObjectId to a StringBuilder in hex format.
  423. *
  424. * @param tmp
  425. * temporary char array to buffer construct into before writing.
  426. * Must be at least large enough to hold 2 digits for each byte
  427. * of object id (64 characters or larger).
  428. * @param w
  429. * the string to append onto.
  430. */
  431. public void copyTo(char[] tmp, StringBuilder w) {
  432. toHexCharArray(tmp);
  433. w.append(tmp, 0, Constants.LONG_OBJECT_ID_STRING_LENGTH);
  434. }
  435. char[] toHexCharArray() {
  436. final char[] dst = new char[Constants.LONG_OBJECT_ID_STRING_LENGTH];
  437. toHexCharArray(dst);
  438. return dst;
  439. }
  440. private void toHexCharArray(char[] dst) {
  441. formatHexChar(dst, 0, w1);
  442. formatHexChar(dst, 16, w2);
  443. formatHexChar(dst, 32, w3);
  444. formatHexChar(dst, 48, w4);
  445. }
  446. private static final char[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
  447. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  448. static void formatHexChar(char[] dst, int p, long w) {
  449. int o = p + 15;
  450. while (o >= p && w != 0) {
  451. dst[o--] = hexchar[(int) (w & 0xf)];
  452. w >>>= 4;
  453. }
  454. while (o >= p)
  455. dst[o--] = '0';
  456. }
  457. /** {@inheritDoc} */
  458. @SuppressWarnings("nls")
  459. @Override
  460. public String toString() {
  461. return "AnyLongObjectId[" + name() + "]";
  462. }
  463. /**
  464. * Get string form of the SHA-256
  465. *
  466. * @return string form of the SHA-256, in lower case hexadecimal.
  467. */
  468. public final String name() {
  469. return new String(toHexCharArray());
  470. }
  471. /**
  472. * Get string form of the SHA-256
  473. *
  474. * @return string form of the SHA-256, in lower case hexadecimal.
  475. */
  476. public final String getName() {
  477. return name();
  478. }
  479. /**
  480. * Return an abbreviation (prefix) of this object SHA-256.
  481. * <p>
  482. * This implementation does not guarantee uniqueness. Callers should instead
  483. * use
  484. * {@link org.eclipse.jgit.lib.ObjectReader#abbreviate(AnyObjectId, int)} to
  485. * obtain a unique abbreviation within the scope of a particular object
  486. * database.
  487. *
  488. * @param len
  489. * length of the abbreviated string.
  490. * @return SHA-256 abbreviation.
  491. */
  492. public AbbreviatedLongObjectId abbreviate(int len) {
  493. final long a = AbbreviatedLongObjectId.mask(len, 1, w1);
  494. final long b = AbbreviatedLongObjectId.mask(len, 2, w2);
  495. final long c = AbbreviatedLongObjectId.mask(len, 3, w3);
  496. final long d = AbbreviatedLongObjectId.mask(len, 4, w4);
  497. return new AbbreviatedLongObjectId(len, a, b, c, d);
  498. }
  499. /**
  500. * Obtain an immutable copy of this current object.
  501. * <p>
  502. * Only returns <code>this</code> if this instance is an unsubclassed
  503. * instance of {@link org.eclipse.jgit.lfs.lib.LongObjectId}; otherwise a
  504. * new instance is returned holding the same value.
  505. * <p>
  506. * This method is useful to shed any additional memory that may be tied to
  507. * the subclass, yet retain the unique identity of the object id for future
  508. * lookups within maps and repositories.
  509. *
  510. * @return an immutable copy, using the smallest memory footprint possible.
  511. */
  512. public final LongObjectId copy() {
  513. if (getClass() == LongObjectId.class)
  514. return (LongObjectId) this;
  515. return new LongObjectId(this);
  516. }
  517. /**
  518. * Obtain an immutable copy of this current object.
  519. * <p>
  520. * See {@link #copy()} if <code>this</code> is a possibly subclassed (but
  521. * immutable) identity and the application needs a lightweight identity
  522. * <i>only</i> reference.
  523. *
  524. * @return an immutable copy. May be <code>this</code> if this is already an
  525. * immutable instance.
  526. */
  527. public abstract LongObjectId toObjectId();
  528. }