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

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