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

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