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.

CommitGraphDataV1.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (C) 2021, Tencent.
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.commitgraph;
  11. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_COMMIT_DATA;
  12. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_EXTRA_EDGE_LIST;
  13. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_OID_FANOUT;
  14. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_OID_LOOKUP;
  15. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.COMMIT_DATA_EXTRA_LENGTH;
  16. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.COMMIT_GRAPH_MAGIC;
  17. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_CHUNK_LOOKUP_WIDTH;
  18. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_EDGE_LAST_MASK;
  19. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_EXTRA_EDGES_NEEDED;
  20. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_LAST_EDGE;
  21. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_NO_PARENT;
  22. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.text.MessageFormat;
  26. import java.util.Arrays;
  27. import org.eclipse.jgit.errors.CommitGraphFormatException;
  28. import org.eclipse.jgit.internal.JGitText;
  29. import org.eclipse.jgit.lib.AnyObjectId;
  30. import org.eclipse.jgit.lib.CommitGraph;
  31. import org.eclipse.jgit.lib.ObjectId;
  32. import org.eclipse.jgit.util.IO;
  33. import org.eclipse.jgit.util.NB;
  34. /**
  35. * Support for the commit-graph v1 format.
  36. *
  37. * @see CommitGraphData
  38. */
  39. public class CommitGraphDataV1 extends CommitGraphData {
  40. private static final int chunkLookupWidth = GRAPH_CHUNK_LOOKUP_WIDTH;
  41. private static final int FANOUT = 256;
  42. private final long commitCnt;
  43. private final int hashLength;
  44. private final int commitDataLength;
  45. private long[] oidFanout;
  46. private byte[][] oidLookup;
  47. private byte[][] commitData;
  48. private byte[] extraEdgeList;
  49. CommitGraphDataV1(InputStream fd, byte[] hdr) throws IOException {
  50. int magic = NB.decodeInt32(hdr, 0);
  51. if (magic != COMMIT_GRAPH_MAGIC) {
  52. throw new CommitGraphFormatException(
  53. JGitText.get().notACommitGraph);
  54. }
  55. // Read the hash version (1 byte)
  56. // 1 => SHA-1
  57. // 2 => SHA-256 nonsupport now
  58. int hashVersion = hdr[5];
  59. if (hashVersion != 1) {
  60. throw new CommitGraphFormatException(
  61. JGitText.get().incorrectOBJECT_ID_LENGTH);
  62. }
  63. hashLength = OBJECT_ID_LENGTH;
  64. commitDataLength = hashLength + COMMIT_DATA_EXTRA_LENGTH;
  65. // Read the number of "chunkOffsets" (1 byte)
  66. int numberOfChunks = hdr[6];
  67. byte[] chunkLookup = new byte[chunkLookupWidth * (numberOfChunks + 1)];
  68. IO.readFully(fd, chunkLookup, 0, chunkLookup.length);
  69. int[] chunkId = new int[numberOfChunks + 1];
  70. long[] chunkOffset = new long[numberOfChunks + 1];
  71. for (int i = 0; i <= numberOfChunks; i++) {
  72. chunkId[i] = NB.decodeInt32(chunkLookup, i * 12);
  73. for (int j = 0; j < i; j++) {
  74. if (chunkId[i] == chunkId[j]) {
  75. throw new CommitGraphFormatException(MessageFormat.format(
  76. JGitText.get().commitGraphChunkRepeated,
  77. Integer.toHexString(chunkId[i])));
  78. }
  79. }
  80. chunkOffset[i] = NB.decodeInt64(chunkLookup, i * 12 + 4);
  81. }
  82. oidLookup = new byte[FANOUT][];
  83. commitData = new byte[FANOUT][];
  84. for (int i = 0; i < numberOfChunks; i++) {
  85. long length = chunkOffset[i + 1] - chunkOffset[i];
  86. long lengthReaded;
  87. if (chunkOffset[i] < 0
  88. || chunkOffset[i] > chunkOffset[numberOfChunks]) {
  89. throw new CommitGraphFormatException(MessageFormat.format(
  90. JGitText.get().commitGraphChunkImproperOffset,
  91. Integer.toHexString(chunkId[i]), chunkOffset[i]));
  92. }
  93. switch (chunkId[i]) {
  94. case CHUNK_ID_OID_FANOUT:
  95. lengthReaded = loadChunkOidFanout(fd);
  96. break;
  97. case CHUNK_ID_OID_LOOKUP:
  98. lengthReaded = loadChunkDataBasedOnFanout(fd, hashLength,
  99. oidLookup);
  100. break;
  101. case CHUNK_ID_COMMIT_DATA:
  102. lengthReaded = loadChunkDataBasedOnFanout(fd, commitDataLength,
  103. commitData);
  104. break;
  105. case CHUNK_ID_EXTRA_EDGE_LIST:
  106. lengthReaded = loadChunkExtraEdgeList(fd, length);
  107. break;
  108. default:
  109. throw new CommitGraphFormatException(MessageFormat.format(
  110. JGitText.get().commitGraphChunkUnknown,
  111. Integer.toHexString(chunkId[i])));
  112. }
  113. if (length != lengthReaded) {
  114. throw new CommitGraphFormatException(MessageFormat.format(
  115. JGitText.get().commitGraphChunkImproperOffset,
  116. Integer.toHexString(chunkId[i + 1]),
  117. chunkOffset[i + 1]));
  118. }
  119. }
  120. if (oidFanout == null) {
  121. throw new CommitGraphFormatException(
  122. JGitText.get().commitGraphOidFanoutNeeded);
  123. }
  124. commitCnt = oidFanout[FANOUT - 1];
  125. }
  126. private long loadChunkOidFanout(InputStream fd) throws IOException {
  127. int fanoutLen = FANOUT * 4;
  128. byte[] fanoutTable = new byte[fanoutLen];
  129. IO.readFully(fd, fanoutTable, 0, fanoutLen);
  130. oidFanout = new long[256]; // really unsigned 32-bit...
  131. for (int k = 0; k < oidFanout.length; k++) {
  132. oidFanout[k] = NB.decodeUInt32(fanoutTable, k * 4);
  133. }
  134. return fanoutLen;
  135. }
  136. private long loadChunkDataBasedOnFanout(InputStream fd, int itemLength,
  137. byte[][] chunkData) throws IOException {
  138. if (oidFanout == null) {
  139. throw new CommitGraphFormatException(
  140. JGitText.get().commitGraphOidFanoutNeeded);
  141. }
  142. long readedLength = 0;
  143. for (int k = 0; k < oidFanout.length; k++) {
  144. long n;
  145. if (k == 0) {
  146. n = oidFanout[k];
  147. } else {
  148. n = oidFanout[k] - oidFanout[k - 1];
  149. }
  150. if (n > 0) {
  151. long len = n * itemLength;
  152. if (len > Integer.MAX_VALUE - 8) { // http://stackoverflow.com/a/8381338
  153. throw new CommitGraphFormatException(
  154. JGitText.get().commitGraphFileIsTooLargeForJgit);
  155. }
  156. chunkData[k] = new byte[(int) len];
  157. IO.readFully(fd, chunkData[k], 0, chunkData[k].length);
  158. readedLength += len;
  159. }
  160. }
  161. return readedLength;
  162. }
  163. private long loadChunkExtraEdgeList(InputStream fd, long len)
  164. throws IOException {
  165. if (len > Integer.MAX_VALUE - 8) { // http://stackoverflow.com/a/8381338
  166. throw new CommitGraphFormatException(
  167. JGitText.get().commitGraphFileIsTooLargeForJgit);
  168. }
  169. extraEdgeList = new byte[(int) len];
  170. IO.readFully(fd, extraEdgeList, 0, extraEdgeList.length);
  171. return len;
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public int findGraphPosition(AnyObjectId objId) {
  176. int levelOne = objId.getFirstByte();
  177. byte[] data = oidLookup[levelOne];
  178. if (data == null) {
  179. return -1;
  180. }
  181. int high = data.length / (hashLength);
  182. int low = 0;
  183. do {
  184. int mid = (low + high) >>> 1;
  185. int pos = objIdOffset(mid);
  186. int cmp = objId.compareTo(data, pos);
  187. if (cmp < 0) {
  188. high = mid;
  189. } else if (cmp == 0) {
  190. if (levelOne == 0) {
  191. return mid;
  192. }
  193. return (int) (mid + oidFanout[levelOne - 1]);
  194. } else {
  195. low = mid + 1;
  196. }
  197. } while (low < high);
  198. return -1;
  199. }
  200. /** {@inheritDoc} */
  201. @Override
  202. public ObjectId getObjectId(int graphPos) {
  203. if (graphPos < 0 || graphPos > commitCnt) {
  204. return null;
  205. }
  206. int levelOne = findLevelOne(graphPos);
  207. int p = getLevelTwo(graphPos, levelOne);
  208. int dataIdx = objIdOffset(p);
  209. return ObjectId.fromRaw(oidLookup[levelOne], dataIdx);
  210. }
  211. /** {@inheritDoc} */
  212. @Override
  213. public CommitGraph.CommitData getCommitData(int graphPos) {
  214. int levelOne = findLevelOne(graphPos);
  215. int p = getLevelTwo(graphPos, levelOne);
  216. int dataIdx = commitDataOffset(p);
  217. byte[] data = this.commitData[levelOne];
  218. if (graphPos < 0) {
  219. return null;
  220. }
  221. CommitDataImpl commit = new CommitDataImpl();
  222. // parse tree
  223. commit.tree = ObjectId.fromRaw(data, dataIdx);
  224. // parse date
  225. long dateHigh = NB.decodeInt32(data, dataIdx + hashLength + 8) & 0x3;
  226. long dateLow = NB.decodeInt32(data, dataIdx + hashLength + 12);
  227. commit.commitTime = dateHigh << 32 | dateLow;
  228. // parse generation
  229. commit.generation = NB.decodeInt32(data, dataIdx + hashLength + 8) >> 2;
  230. boolean noParents = false;
  231. int[] pList = new int[0];
  232. int edgeValue = NB.decodeInt32(data, dataIdx + hashLength);
  233. if (edgeValue == GRAPH_NO_PARENT) {
  234. noParents = true;
  235. }
  236. // parse parents
  237. if (!noParents) {
  238. pList = new int[1];
  239. int parent = edgeValue;
  240. pList[0] = parent;
  241. edgeValue = NB.decodeInt32(data, dataIdx + hashLength + 4);
  242. if (edgeValue != GRAPH_NO_PARENT) {
  243. if ((edgeValue & GRAPH_EXTRA_EDGES_NEEDED) != 0) {
  244. int pptr = edgeValue & GRAPH_EDGE_LAST_MASK;
  245. int[] extraEdgeList = findExtraEdgeList(pptr);
  246. if (extraEdgeList == null) {
  247. return null;
  248. }
  249. int[] old = pList;
  250. pList = new int[extraEdgeList.length + 1];
  251. pList[0] = old[0];
  252. for (int i = 0; i < extraEdgeList.length; i++) {
  253. parent = extraEdgeList[i];
  254. pList[i + 1] = parent;
  255. }
  256. } else {
  257. parent = edgeValue;
  258. pList = new int[] { pList[0], parent };
  259. }
  260. }
  261. }
  262. commit.parents = pList;
  263. return commit;
  264. }
  265. /**
  266. * Find the list of commit-graph position in extra edge list chunk.
  267. * <p>
  268. * The extra edge list chunk store the second through nth parents for all
  269. * octopus merges.
  270. *
  271. * @param pptr
  272. * the start position to iterate of extra edge list chunk
  273. * @return the list of commit-graph position or null if not found
  274. */
  275. int[] findExtraEdgeList(int pptr) {
  276. if (extraEdgeList == null) {
  277. return null;
  278. }
  279. int maxOffset = extraEdgeList.length - 4;
  280. int offset = pptr * 4;
  281. if (offset < 0 || offset > maxOffset) {
  282. return null;
  283. }
  284. int[] pList = new int[32];
  285. int count = 0;
  286. int parentPosition;
  287. for (;;) {
  288. if (count >= pList.length) {
  289. int[] old = pList;
  290. pList = new int[pList.length + 32];
  291. System.arraycopy(old, 0, pList, 0, count);
  292. }
  293. if (offset > maxOffset) {
  294. return null;
  295. }
  296. parentPosition = NB.decodeInt32(extraEdgeList, offset);
  297. if ((parentPosition & GRAPH_LAST_EDGE) != 0) {
  298. pList[count] = parentPosition & GRAPH_EDGE_LAST_MASK;
  299. count++;
  300. break;
  301. }
  302. pList[count++] = parentPosition;
  303. offset += 4;
  304. }
  305. int[] old = pList;
  306. pList = new int[count];
  307. System.arraycopy(old, 0, pList, 0, count);
  308. return pList;
  309. }
  310. /** {@inheritDoc} */
  311. @Override
  312. public long getCommitCnt() {
  313. return commitCnt;
  314. }
  315. /** {@inheritDoc} */
  316. @Override
  317. public int getHashLength() {
  318. return hashLength;
  319. }
  320. private int findLevelOne(long nthPosition) {
  321. int levelOne = Arrays.binarySearch(oidFanout, nthPosition + 1);
  322. if (levelOne >= 0) {
  323. // If we hit the bucket exactly the item is in the bucket, or
  324. // any bucket before it which has the same object count.
  325. //
  326. long base = oidFanout[levelOne];
  327. while (levelOne > 0 && base == oidFanout[levelOne - 1])
  328. levelOne--;
  329. } else {
  330. // The item is in the bucket we would insert it into.
  331. //
  332. levelOne = -(levelOne + 1);
  333. }
  334. return levelOne;
  335. }
  336. private int getLevelTwo(long nthPosition, int levelOne) {
  337. long base = levelOne > 0 ? oidFanout[levelOne - 1] : 0;
  338. return (int) (nthPosition - base);
  339. }
  340. private int objIdOffset(int mid) {
  341. return hashLength * mid;
  342. }
  343. private int commitDataOffset(int mid) {
  344. return commitDataLength * mid;
  345. }
  346. }