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.

PackIndexV1.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2007-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.internal.storage.file;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.util.Arrays;
  50. import java.util.Iterator;
  51. import java.util.NoSuchElementException;
  52. import java.util.Set;
  53. import org.eclipse.jgit.errors.CorruptObjectException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  56. import org.eclipse.jgit.lib.AnyObjectId;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.util.IO;
  60. import org.eclipse.jgit.util.NB;
  61. class PackIndexV1 extends PackIndex {
  62. private static final int IDX_HDR_LEN = 256 * 4;
  63. private final long[] idxHeader;
  64. private byte[][] idxdata;
  65. private long objectCnt;
  66. PackIndexV1(final InputStream fd, final byte[] hdr)
  67. throws CorruptObjectException, IOException {
  68. final byte[] fanoutTable = new byte[IDX_HDR_LEN];
  69. System.arraycopy(hdr, 0, fanoutTable, 0, hdr.length);
  70. IO.readFully(fd, fanoutTable, hdr.length, IDX_HDR_LEN - hdr.length);
  71. idxHeader = new long[256]; // really unsigned 32-bit...
  72. for (int k = 0; k < idxHeader.length; k++)
  73. idxHeader[k] = NB.decodeUInt32(fanoutTable, k * 4);
  74. idxdata = new byte[idxHeader.length][];
  75. for (int k = 0; k < idxHeader.length; k++) {
  76. int n;
  77. if (k == 0) {
  78. n = (int) (idxHeader[k]);
  79. } else {
  80. n = (int) (idxHeader[k] - idxHeader[k - 1]);
  81. }
  82. if (n > 0) {
  83. final long len = n * (Constants.OBJECT_ID_LENGTH + 4);
  84. if (len > Integer.MAX_VALUE - 8) // http://stackoverflow.com/a/8381338
  85. throw new IOException(JGitText.get().indexFileIsTooLargeForJgit);
  86. idxdata[k] = new byte[(int) len];
  87. IO.readFully(fd, idxdata[k], 0, idxdata[k].length);
  88. }
  89. }
  90. objectCnt = idxHeader[255];
  91. packChecksum = new byte[20];
  92. IO.readFully(fd, packChecksum, 0, packChecksum.length);
  93. }
  94. @Override
  95. public long getObjectCount() {
  96. return objectCnt;
  97. }
  98. @Override
  99. public long getOffset64Count() {
  100. long n64 = 0;
  101. for (final MutableEntry e : this) {
  102. if (e.getOffset() >= Integer.MAX_VALUE)
  103. n64++;
  104. }
  105. return n64;
  106. }
  107. private int findLevelOne(final long nthPosition) {
  108. int levelOne = Arrays.binarySearch(idxHeader, nthPosition + 1);
  109. if (levelOne >= 0) {
  110. // If we hit the bucket exactly the item is in the bucket, or
  111. // any bucket before it which has the same object count.
  112. //
  113. long base = idxHeader[levelOne];
  114. while (levelOne > 0 && base == idxHeader[levelOne - 1])
  115. levelOne--;
  116. } else {
  117. // The item is in the bucket we would insert it into.
  118. //
  119. levelOne = -(levelOne + 1);
  120. }
  121. return levelOne;
  122. }
  123. private int getLevelTwo(final long nthPosition, final int levelOne) {
  124. final long base = levelOne > 0 ? idxHeader[levelOne - 1] : 0;
  125. return (int) (nthPosition - base);
  126. }
  127. @Override
  128. public ObjectId getObjectId(final long nthPosition) {
  129. final int levelOne = findLevelOne(nthPosition);
  130. final int p = getLevelTwo(nthPosition, levelOne);
  131. final int dataIdx = idOffset(p);
  132. return ObjectId.fromRaw(idxdata[levelOne], dataIdx);
  133. }
  134. @Override
  135. long getOffset(long nthPosition) {
  136. final int levelOne = findLevelOne(nthPosition);
  137. final int levelTwo = getLevelTwo(nthPosition, levelOne);
  138. final int p = (4 + Constants.OBJECT_ID_LENGTH) * levelTwo;
  139. return NB.decodeUInt32(idxdata[levelOne], p);
  140. }
  141. @Override
  142. public long findOffset(final AnyObjectId objId) {
  143. final int levelOne = objId.getFirstByte();
  144. byte[] data = idxdata[levelOne];
  145. if (data == null)
  146. return -1;
  147. int high = data.length / (4 + Constants.OBJECT_ID_LENGTH);
  148. int low = 0;
  149. do {
  150. final int mid = (low + high) >>> 1;
  151. final int pos = idOffset(mid);
  152. final int cmp = objId.compareTo(data, pos);
  153. if (cmp < 0)
  154. high = mid;
  155. else if (cmp == 0) {
  156. int b0 = data[pos - 4] & 0xff;
  157. int b1 = data[pos - 3] & 0xff;
  158. int b2 = data[pos - 2] & 0xff;
  159. int b3 = data[pos - 1] & 0xff;
  160. return (((long) b0) << 24) | (b1 << 16) | (b2 << 8) | (b3);
  161. } else
  162. low = mid + 1;
  163. } while (low < high);
  164. return -1;
  165. }
  166. @Override
  167. public long findCRC32(AnyObjectId objId) {
  168. throw new UnsupportedOperationException();
  169. }
  170. @Override
  171. public boolean hasCRC32Support() {
  172. return false;
  173. }
  174. @Override
  175. public Iterator<MutableEntry> iterator() {
  176. return new IndexV1Iterator();
  177. }
  178. @Override
  179. public void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
  180. int matchLimit) throws IOException {
  181. byte[] data = idxdata[id.getFirstByte()];
  182. if (data == null)
  183. return;
  184. int max = data.length / (4 + Constants.OBJECT_ID_LENGTH);
  185. int high = max;
  186. int low = 0;
  187. do {
  188. int p = (low + high) >>> 1;
  189. final int cmp = id.prefixCompare(data, idOffset(p));
  190. if (cmp < 0)
  191. high = p;
  192. else if (cmp == 0) {
  193. // We may have landed in the middle of the matches. Move
  194. // backwards to the start of matches, then walk forwards.
  195. //
  196. while (0 < p && id.prefixCompare(data, idOffset(p - 1)) == 0)
  197. p--;
  198. for (; p < max && id.prefixCompare(data, idOffset(p)) == 0; p++) {
  199. matches.add(ObjectId.fromRaw(data, idOffset(p)));
  200. if (matches.size() > matchLimit)
  201. break;
  202. }
  203. return;
  204. } else
  205. low = p + 1;
  206. } while (low < high);
  207. }
  208. private static int idOffset(int mid) {
  209. return ((4 + Constants.OBJECT_ID_LENGTH) * mid) + 4;
  210. }
  211. private class IndexV1Iterator extends EntriesIterator {
  212. private int levelOne;
  213. private int levelTwo;
  214. @Override
  215. protected MutableEntry initEntry() {
  216. return new MutableEntry() {
  217. protected void ensureId() {
  218. idBuffer.fromRaw(idxdata[levelOne], levelTwo
  219. - Constants.OBJECT_ID_LENGTH);
  220. }
  221. };
  222. }
  223. public MutableEntry next() {
  224. for (; levelOne < idxdata.length; levelOne++) {
  225. if (idxdata[levelOne] == null)
  226. continue;
  227. if (levelTwo < idxdata[levelOne].length) {
  228. entry.offset = NB.decodeUInt32(idxdata[levelOne], levelTwo);
  229. levelTwo += Constants.OBJECT_ID_LENGTH + 4;
  230. returnedNumber++;
  231. return entry;
  232. }
  233. levelTwo = 0;
  234. }
  235. throw new NoSuchElementException();
  236. }
  237. }
  238. }