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.

PackIndexV2.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.internal.storage.file;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.text.MessageFormat;
  47. import java.util.Arrays;
  48. import java.util.Iterator;
  49. import java.util.NoSuchElementException;
  50. import java.util.Set;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  54. import org.eclipse.jgit.lib.AnyObjectId;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.util.IO;
  58. import org.eclipse.jgit.util.NB;
  59. /** Support for the pack index v2 format. */
  60. class PackIndexV2 extends PackIndex {
  61. private static final long IS_O64 = 1L << 31;
  62. private static final int FANOUT = 256;
  63. private static final int[] NO_INTS = {};
  64. private static final byte[] NO_BYTES = {};
  65. private long objectCnt;
  66. private final long[] fanoutTable;
  67. /** 256 arrays of contiguous object names. */
  68. private int[][] names;
  69. /** 256 arrays of the 32 bit offset data, matching {@link #names}. */
  70. private byte[][] offset32;
  71. /** 256 arrays of the CRC-32 of objects, matching {@link #names}. */
  72. private byte[][] crc32;
  73. /** 64 bit offset table. */
  74. private byte[] offset64;
  75. PackIndexV2(final InputStream fd) throws IOException {
  76. final byte[] fanoutRaw = new byte[4 * FANOUT];
  77. IO.readFully(fd, fanoutRaw, 0, fanoutRaw.length);
  78. fanoutTable = new long[FANOUT];
  79. for (int k = 0; k < FANOUT; k++)
  80. fanoutTable[k] = NB.decodeUInt32(fanoutRaw, k * 4);
  81. objectCnt = fanoutTable[FANOUT - 1];
  82. names = new int[FANOUT][];
  83. offset32 = new byte[FANOUT][];
  84. crc32 = new byte[FANOUT][];
  85. // Object name table. The size we can permit per fan-out bucket
  86. // is limited to Java's 2 GB per byte array limitation. That is
  87. // no more than 107,374,182 objects per fan-out.
  88. //
  89. for (int k = 0; k < FANOUT; k++) {
  90. final long bucketCnt;
  91. if (k == 0)
  92. bucketCnt = fanoutTable[k];
  93. else
  94. bucketCnt = fanoutTable[k] - fanoutTable[k - 1];
  95. if (bucketCnt == 0) {
  96. names[k] = NO_INTS;
  97. offset32[k] = NO_BYTES;
  98. crc32[k] = NO_BYTES;
  99. continue;
  100. } else if (bucketCnt < 0)
  101. throw new IOException(MessageFormat.format(
  102. JGitText.get().indexFileCorruptedNegativeBucketCount,
  103. Long.valueOf(bucketCnt)));
  104. final long nameLen = bucketCnt * Constants.OBJECT_ID_LENGTH;
  105. if (nameLen > Integer.MAX_VALUE - 8) // see http://stackoverflow.com/a/8381338
  106. throw new IOException(JGitText.get().indexFileIsTooLargeForJgit);
  107. final int intNameLen = (int) nameLen;
  108. final byte[] raw = new byte[intNameLen];
  109. final int[] bin = new int[intNameLen >>> 2];
  110. IO.readFully(fd, raw, 0, raw.length);
  111. for (int i = 0; i < bin.length; i++)
  112. bin[i] = NB.decodeInt32(raw, i << 2);
  113. names[k] = bin;
  114. offset32[k] = new byte[(int) (bucketCnt * 4)];
  115. crc32[k] = new byte[(int) (bucketCnt * 4)];
  116. }
  117. // CRC32 table.
  118. for (int k = 0; k < FANOUT; k++)
  119. IO.readFully(fd, crc32[k], 0, crc32[k].length);
  120. // 32 bit offset table. Any entries with the most significant bit
  121. // set require a 64 bit offset entry in another table.
  122. //
  123. int o64cnt = 0;
  124. for (int k = 0; k < FANOUT; k++) {
  125. final byte[] ofs = offset32[k];
  126. IO.readFully(fd, ofs, 0, ofs.length);
  127. for (int p = 0; p < ofs.length; p += 4)
  128. if (ofs[p] < 0)
  129. o64cnt++;
  130. }
  131. // 64 bit offset table. Most objects should not require an entry.
  132. //
  133. if (o64cnt > 0) {
  134. offset64 = new byte[o64cnt * 8];
  135. IO.readFully(fd, offset64, 0, offset64.length);
  136. } else {
  137. offset64 = NO_BYTES;
  138. }
  139. packChecksum = new byte[20];
  140. IO.readFully(fd, packChecksum, 0, packChecksum.length);
  141. }
  142. @Override
  143. public long getObjectCount() {
  144. return objectCnt;
  145. }
  146. @Override
  147. public long getOffset64Count() {
  148. return offset64.length / 8;
  149. }
  150. private int findLevelOne(final long nthPosition) {
  151. int levelOne = Arrays.binarySearch(fanoutTable, nthPosition + 1);
  152. if (levelOne >= 0) {
  153. // If we hit the bucket exactly the item is in the bucket, or
  154. // any bucket before it which has the same object count.
  155. //
  156. long base = fanoutTable[levelOne];
  157. while (levelOne > 0 && base == fanoutTable[levelOne - 1])
  158. levelOne--;
  159. } else {
  160. // The item is in the bucket we would insert it into.
  161. //
  162. levelOne = -(levelOne + 1);
  163. }
  164. return levelOne;
  165. }
  166. private int getLevelTwo(final long nthPosition, final int levelOne) {
  167. final long base = levelOne > 0 ? fanoutTable[levelOne - 1] : 0;
  168. return (int) (nthPosition - base);
  169. }
  170. @Override
  171. public ObjectId getObjectId(final long nthPosition) {
  172. final int levelOne = findLevelOne(nthPosition);
  173. final int p = getLevelTwo(nthPosition, levelOne);
  174. final int p4 = p << 2;
  175. return ObjectId.fromRaw(names[levelOne], p4 + p); // p * 5
  176. }
  177. @Override
  178. public long getOffset(final long nthPosition) {
  179. final int levelOne = findLevelOne(nthPosition);
  180. final int levelTwo = getLevelTwo(nthPosition, levelOne);
  181. return getOffset(levelOne, levelTwo);
  182. }
  183. @Override
  184. public long findOffset(final AnyObjectId objId) {
  185. final int levelOne = objId.getFirstByte();
  186. final int levelTwo = binarySearchLevelTwo(objId, levelOne);
  187. if (levelTwo == -1)
  188. return -1;
  189. return getOffset(levelOne, levelTwo);
  190. }
  191. private long getOffset(final int levelOne, final int levelTwo) {
  192. final long p = NB.decodeUInt32(offset32[levelOne], levelTwo << 2);
  193. if ((p & IS_O64) != 0)
  194. return NB.decodeUInt64(offset64, (8 * (int) (p & ~IS_O64)));
  195. return p;
  196. }
  197. @Override
  198. public long findCRC32(AnyObjectId objId) throws MissingObjectException {
  199. final int levelOne = objId.getFirstByte();
  200. final int levelTwo = binarySearchLevelTwo(objId, levelOne);
  201. if (levelTwo == -1)
  202. throw new MissingObjectException(objId.copy(), "unknown");
  203. return NB.decodeUInt32(crc32[levelOne], levelTwo << 2);
  204. }
  205. @Override
  206. public boolean hasCRC32Support() {
  207. return true;
  208. }
  209. @Override
  210. public Iterator<MutableEntry> iterator() {
  211. return new EntriesIteratorV2();
  212. }
  213. @Override
  214. public void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
  215. int matchLimit) throws IOException {
  216. int[] data = names[id.getFirstByte()];
  217. int max = offset32[id.getFirstByte()].length >>> 2;
  218. int high = max;
  219. if (high == 0)
  220. return;
  221. int low = 0;
  222. do {
  223. int p = (low + high) >>> 1;
  224. final int cmp = id.prefixCompare(data, idOffset(p));
  225. if (cmp < 0)
  226. high = p;
  227. else if (cmp == 0) {
  228. // We may have landed in the middle of the matches. Move
  229. // backwards to the start of matches, then walk forwards.
  230. //
  231. while (0 < p && id.prefixCompare(data, idOffset(p - 1)) == 0)
  232. p--;
  233. for (; p < max && id.prefixCompare(data, idOffset(p)) == 0; p++) {
  234. matches.add(ObjectId.fromRaw(data, idOffset(p)));
  235. if (matches.size() > matchLimit)
  236. break;
  237. }
  238. return;
  239. } else
  240. low = p + 1;
  241. } while (low < high);
  242. }
  243. private static int idOffset(int p) {
  244. return (p << 2) + p; // p * 5
  245. }
  246. private int binarySearchLevelTwo(final AnyObjectId objId, final int levelOne) {
  247. final int[] data = names[levelOne];
  248. int high = offset32[levelOne].length >>> 2;
  249. if (high == 0)
  250. return -1;
  251. int low = 0;
  252. do {
  253. final int mid = (low + high) >>> 1;
  254. final int mid4 = mid << 2;
  255. final int cmp;
  256. cmp = objId.compareTo(data, mid4 + mid); // mid * 5
  257. if (cmp < 0)
  258. high = mid;
  259. else if (cmp == 0) {
  260. return mid;
  261. } else
  262. low = mid + 1;
  263. } while (low < high);
  264. return -1;
  265. }
  266. private class EntriesIteratorV2 extends EntriesIterator {
  267. private int levelOne;
  268. private int levelTwo;
  269. @Override
  270. protected MutableEntry initEntry() {
  271. return new MutableEntry() {
  272. protected void ensureId() {
  273. idBuffer.fromRaw(names[levelOne], levelTwo
  274. - Constants.OBJECT_ID_LENGTH / 4);
  275. }
  276. };
  277. }
  278. public MutableEntry next() {
  279. for (; levelOne < names.length; levelOne++) {
  280. if (levelTwo < names[levelOne].length) {
  281. int idx = levelTwo / (Constants.OBJECT_ID_LENGTH / 4) * 4;
  282. long offset = NB.decodeUInt32(offset32[levelOne], idx);
  283. if ((offset & IS_O64) != 0) {
  284. idx = (8 * (int) (offset & ~IS_O64));
  285. offset = NB.decodeUInt64(offset64, idx);
  286. }
  287. entry.offset = offset;
  288. levelTwo += Constants.OBJECT_ID_LENGTH / 4;
  289. returnedNumber++;
  290. return entry;
  291. }
  292. levelTwo = 0;
  293. }
  294. throw new NoSuchElementException();
  295. }
  296. }
  297. }