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.

PackIndex.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.storage.file;
  45. import java.io.File;
  46. import java.io.FileInputStream;
  47. import java.io.FileNotFoundException;
  48. import java.io.IOException;
  49. import java.text.MessageFormat;
  50. import java.util.Iterator;
  51. import java.util.Set;
  52. import org.eclipse.jgit.JGitText;
  53. import org.eclipse.jgit.errors.MissingObjectException;
  54. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  55. import org.eclipse.jgit.lib.AnyObjectId;
  56. import org.eclipse.jgit.lib.MutableObjectId;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.util.IO;
  59. import org.eclipse.jgit.util.NB;
  60. /**
  61. * Access path to locate objects by {@link ObjectId} in a {@link PackFile}.
  62. * <p>
  63. * Indexes are strictly redundant information in that we can rebuild all of the
  64. * data held in the index file from the on disk representation of the pack file
  65. * itself, but it is faster to access for random requests because data is stored
  66. * by ObjectId.
  67. * </p>
  68. */
  69. public abstract class PackIndex implements Iterable<PackIndex.MutableEntry> {
  70. /**
  71. * Open an existing pack <code>.idx</code> file for reading.
  72. * <p>
  73. * The format of the file will be automatically detected and a proper access
  74. * implementation for that format will be constructed and returned to the
  75. * caller. The file may or may not be held open by the returned instance.
  76. * </p>
  77. *
  78. * @param idxFile
  79. * existing pack .idx to read.
  80. * @return access implementation for the requested file.
  81. * @throws FileNotFoundException
  82. * the file does not exist.
  83. * @throws IOException
  84. * the file exists but could not be read due to security errors,
  85. * unrecognized data version, or unexpected data corruption.
  86. */
  87. public static PackIndex open(final File idxFile) throws IOException {
  88. final FileInputStream fd = new FileInputStream(idxFile);
  89. try {
  90. final byte[] hdr = new byte[8];
  91. IO.readFully(fd, hdr, 0, hdr.length);
  92. if (isTOC(hdr)) {
  93. final int v = NB.decodeInt32(hdr, 4);
  94. switch (v) {
  95. case 2:
  96. return new PackIndexV2(fd);
  97. default:
  98. throw new IOException(MessageFormat.format(JGitText.get().unsupportedPackIndexVersion, v));
  99. }
  100. }
  101. return new PackIndexV1(fd, hdr);
  102. } catch (IOException ioe) {
  103. final String path = idxFile.getAbsolutePath();
  104. final IOException err;
  105. err = new IOException(MessageFormat.format(JGitText.get().unreadablePackIndex, path));
  106. err.initCause(ioe);
  107. throw err;
  108. } finally {
  109. try {
  110. fd.close();
  111. } catch (IOException err2) {
  112. // ignore
  113. }
  114. }
  115. }
  116. private static boolean isTOC(final byte[] h) {
  117. final byte[] toc = PackIndexWriter.TOC;
  118. for (int i = 0; i < toc.length; i++)
  119. if (h[i] != toc[i])
  120. return false;
  121. return true;
  122. }
  123. /** Footer checksum applied on the bottom of the pack file. */
  124. protected byte[] packChecksum;
  125. /**
  126. * Determine if an object is contained within the pack file.
  127. *
  128. * @param id
  129. * the object to look for. Must not be null.
  130. * @return true if the object is listed in this index; false otherwise.
  131. */
  132. public boolean hasObject(final AnyObjectId id) {
  133. return findOffset(id) != -1;
  134. }
  135. /**
  136. * Provide iterator that gives access to index entries. Note, that iterator
  137. * returns reference to mutable object, the same reference in each call -
  138. * for performance reason. If client needs immutable objects, it must copy
  139. * returned object on its own.
  140. * <p>
  141. * Iterator returns objects in SHA-1 lexicographical order.
  142. * </p>
  143. *
  144. * @return iterator over pack index entries
  145. */
  146. public abstract Iterator<MutableEntry> iterator();
  147. /**
  148. * Obtain the total number of objects described by this index.
  149. *
  150. * @return number of objects in this index, and likewise in the associated
  151. * pack that this index was generated from.
  152. */
  153. abstract long getObjectCount();
  154. /**
  155. * Obtain the total number of objects needing 64 bit offsets.
  156. *
  157. * @return number of objects in this index using a 64 bit offset; that is an
  158. * object positioned after the 2 GB position within the file.
  159. */
  160. abstract long getOffset64Count();
  161. /**
  162. * Get ObjectId for the n-th object entry returned by {@link #iterator()}.
  163. * <p>
  164. * This method is a constant-time replacement for the following loop:
  165. *
  166. * <pre>
  167. * Iterator&lt;MutableEntry&gt; eItr = index.iterator();
  168. * int curPosition = 0;
  169. * while (eItr.hasNext() &amp;&amp; curPosition++ &lt; nthPosition)
  170. * eItr.next();
  171. * ObjectId result = eItr.next().toObjectId();
  172. * </pre>
  173. *
  174. * @param nthPosition
  175. * position within the traversal of {@link #iterator()} that the
  176. * caller needs the object for. The first returned
  177. * {@link MutableEntry} is 0, the second is 1, etc.
  178. * @return the ObjectId for the corresponding entry.
  179. */
  180. abstract ObjectId getObjectId(long nthPosition);
  181. /**
  182. * Get ObjectId for the n-th object entry returned by {@link #iterator()}.
  183. * <p>
  184. * This method is a constant-time replacement for the following loop:
  185. *
  186. * <pre>
  187. * Iterator&lt;MutableEntry&gt; eItr = index.iterator();
  188. * int curPosition = 0;
  189. * while (eItr.hasNext() &amp;&amp; curPosition++ &lt; nthPosition)
  190. * eItr.next();
  191. * ObjectId result = eItr.next().toObjectId();
  192. * </pre>
  193. *
  194. * @param nthPosition
  195. * unsigned 32 bit position within the traversal of
  196. * {@link #iterator()} that the caller needs the object for. The
  197. * first returned {@link MutableEntry} is 0, the second is 1,
  198. * etc. Positions past 2**31-1 are negative, but still valid.
  199. * @return the ObjectId for the corresponding entry.
  200. */
  201. final ObjectId getObjectId(final int nthPosition) {
  202. if (nthPosition >= 0)
  203. return getObjectId((long) nthPosition);
  204. final int u31 = nthPosition >>> 1;
  205. final int one = nthPosition & 1;
  206. return getObjectId(((long) u31) << 1 | one);
  207. }
  208. /**
  209. * Locate the file offset position for the requested object.
  210. *
  211. * @param objId
  212. * name of the object to locate within the pack.
  213. * @return offset of the object's header and compressed content; -1 if the
  214. * object does not exist in this index and is thus not stored in the
  215. * associated pack.
  216. */
  217. abstract long findOffset(AnyObjectId objId);
  218. /**
  219. * Retrieve stored CRC32 checksum of the requested object raw-data
  220. * (including header).
  221. *
  222. * @param objId
  223. * id of object to look for
  224. * @return CRC32 checksum of specified object (at 32 less significant bits)
  225. * @throws MissingObjectException
  226. * when requested ObjectId was not found in this index
  227. * @throws UnsupportedOperationException
  228. * when this index doesn't support CRC32 checksum
  229. */
  230. abstract long findCRC32(AnyObjectId objId) throws MissingObjectException,
  231. UnsupportedOperationException;
  232. /**
  233. * Check whether this index supports (has) CRC32 checksums for objects.
  234. *
  235. * @return true if CRC32 is stored, false otherwise
  236. */
  237. abstract boolean hasCRC32Support();
  238. abstract void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
  239. int matchLimit) throws IOException;
  240. /**
  241. * Represent mutable entry of pack index consisting of object id and offset
  242. * in pack (both mutable).
  243. *
  244. */
  245. public static class MutableEntry {
  246. final MutableObjectId idBuffer = new MutableObjectId();
  247. long offset;
  248. /**
  249. * Returns offset for this index object entry
  250. *
  251. * @return offset of this object in a pack file
  252. */
  253. public long getOffset() {
  254. return offset;
  255. }
  256. /** @return hex string describing the object id of this entry. */
  257. public String name() {
  258. ensureId();
  259. return idBuffer.name();
  260. }
  261. /** @return a copy of the object id. */
  262. public ObjectId toObjectId() {
  263. ensureId();
  264. return idBuffer.toObjectId();
  265. }
  266. /** @return a complete copy of this entry, that won't modify */
  267. public MutableEntry cloneEntry() {
  268. final MutableEntry r = new MutableEntry();
  269. ensureId();
  270. r.idBuffer.fromObjectId(idBuffer);
  271. r.offset = offset;
  272. return r;
  273. }
  274. void ensureId() {
  275. // Override in implementations.
  276. }
  277. }
  278. abstract class EntriesIterator implements Iterator<MutableEntry> {
  279. protected final MutableEntry entry = initEntry();
  280. protected long returnedNumber = 0;
  281. protected abstract MutableEntry initEntry();
  282. public boolean hasNext() {
  283. return returnedNumber < getObjectCount();
  284. }
  285. /**
  286. * Implementation must update {@link #returnedNumber} before returning
  287. * element.
  288. */
  289. public abstract MutableEntry next();
  290. public void remove() {
  291. throw new UnsupportedOperationException();
  292. }
  293. }
  294. }