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 9.2KB

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