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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 org.eclipse.jgit.JGitText;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. import org.eclipse.jgit.lib.AnyObjectId;
  54. import org.eclipse.jgit.lib.MutableObjectId;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.util.IO;
  57. import org.eclipse.jgit.util.NB;
  58. /**
  59. * Access path to locate objects by {@link ObjectId} in a {@link PackFile}.
  60. * <p>
  61. * Indexes are strictly redundant information in that we can rebuild all of the
  62. * data held in the index file from the on disk representation of the pack file
  63. * itself, but it is faster to access for random requests because data is stored
  64. * by ObjectId.
  65. * </p>
  66. */
  67. public abstract class PackIndex implements Iterable<PackIndex.MutableEntry> {
  68. /**
  69. * Open an existing pack <code>.idx</code> file for reading.
  70. * <p>
  71. * The format of the file will be automatically detected and a proper access
  72. * implementation for that format will be constructed and returned to the
  73. * caller. The file may or may not be held open by the returned instance.
  74. * </p>
  75. *
  76. * @param idxFile
  77. * existing pack .idx to read.
  78. * @return access implementation for the requested file.
  79. * @throws FileNotFoundException
  80. * the file does not exist.
  81. * @throws IOException
  82. * the file exists but could not be read due to security errors,
  83. * unrecognized data version, or unexpected data corruption.
  84. */
  85. public static PackIndex open(final File idxFile) throws IOException {
  86. final FileInputStream fd = new FileInputStream(idxFile);
  87. try {
  88. final byte[] hdr = new byte[8];
  89. IO.readFully(fd, hdr, 0, hdr.length);
  90. if (isTOC(hdr)) {
  91. final int v = NB.decodeInt32(hdr, 4);
  92. switch (v) {
  93. case 2:
  94. return new PackIndexV2(fd);
  95. default:
  96. throw new IOException(MessageFormat.format(JGitText.get().unsupportedPackIndexVersion, v));
  97. }
  98. }
  99. return new PackIndexV1(fd, hdr);
  100. } catch (IOException ioe) {
  101. final String path = idxFile.getAbsolutePath();
  102. final IOException err;
  103. err = new IOException(MessageFormat.format(JGitText.get().unreadablePackIndex, path));
  104. err.initCause(ioe);
  105. throw err;
  106. } finally {
  107. try {
  108. fd.close();
  109. } catch (IOException err2) {
  110. // ignore
  111. }
  112. }
  113. }
  114. private static boolean isTOC(final byte[] h) {
  115. final byte[] toc = PackIndexWriter.TOC;
  116. for (int i = 0; i < toc.length; i++)
  117. if (h[i] != toc[i])
  118. return false;
  119. return true;
  120. }
  121. /** Footer checksum applied on the bottom of the pack file. */
  122. protected byte[] packChecksum;
  123. /**
  124. * Determine if an object is contained within the pack file.
  125. *
  126. * @param id
  127. * the object to look for. Must not be null.
  128. * @return true if the object is listed in this index; false otherwise.
  129. */
  130. public boolean hasObject(final AnyObjectId id) {
  131. return findOffset(id) != -1;
  132. }
  133. /**
  134. * Provide iterator that gives access to index entries. Note, that iterator
  135. * returns reference to mutable object, the same reference in each call -
  136. * for performance reason. If client needs immutable objects, it must copy
  137. * returned object on its own.
  138. * <p>
  139. * Iterator returns objects in SHA-1 lexicographical order.
  140. * </p>
  141. *
  142. * @return iterator over pack index entries
  143. */
  144. public abstract Iterator<MutableEntry> iterator();
  145. /**
  146. * Obtain the total number of objects described by this index.
  147. *
  148. * @return number of objects in this index, and likewise in the associated
  149. * pack that this index was generated from.
  150. */
  151. abstract long getObjectCount();
  152. /**
  153. * Obtain the total number of objects needing 64 bit offsets.
  154. *
  155. * @return number of objects in this index using a 64 bit offset; that is an
  156. * object positioned after the 2 GB position within the file.
  157. */
  158. abstract long getOffset64Count();
  159. /**
  160. * Get ObjectId for the n-th object entry returned by {@link #iterator()}.
  161. * <p>
  162. * This method is a constant-time replacement for the following loop:
  163. *
  164. * <pre>
  165. * Iterator&lt;MutableEntry&gt; eItr = index.iterator();
  166. * int curPosition = 0;
  167. * while (eItr.hasNext() &amp;&amp; curPosition++ &lt; nthPosition)
  168. * eItr.next();
  169. * ObjectId result = eItr.next().toObjectId();
  170. * </pre>
  171. *
  172. * @param nthPosition
  173. * position within the traversal of {@link #iterator()} that the
  174. * caller needs the object for. The first returned
  175. * {@link MutableEntry} is 0, the second is 1, etc.
  176. * @return the ObjectId for the corresponding entry.
  177. */
  178. abstract ObjectId getObjectId(long nthPosition);
  179. /**
  180. * Get ObjectId for the n-th object entry returned by {@link #iterator()}.
  181. * <p>
  182. * This method is a constant-time replacement for the following loop:
  183. *
  184. * <pre>
  185. * Iterator&lt;MutableEntry&gt; eItr = index.iterator();
  186. * int curPosition = 0;
  187. * while (eItr.hasNext() &amp;&amp; curPosition++ &lt; nthPosition)
  188. * eItr.next();
  189. * ObjectId result = eItr.next().toObjectId();
  190. * </pre>
  191. *
  192. * @param nthPosition
  193. * unsigned 32 bit position within the traversal of
  194. * {@link #iterator()} that the caller needs the object for. The
  195. * first returned {@link MutableEntry} is 0, the second is 1,
  196. * etc. Positions past 2**31-1 are negative, but still valid.
  197. * @return the ObjectId for the corresponding entry.
  198. */
  199. final ObjectId getObjectId(final int nthPosition) {
  200. if (nthPosition >= 0)
  201. return getObjectId((long) nthPosition);
  202. final int u31 = nthPosition >>> 1;
  203. final int one = nthPosition & 1;
  204. return getObjectId(((long) u31) << 1 | one);
  205. }
  206. /**
  207. * Locate the file offset position for the requested object.
  208. *
  209. * @param objId
  210. * name of the object to locate within the pack.
  211. * @return offset of the object's header and compressed content; -1 if the
  212. * object does not exist in this index and is thus not stored in the
  213. * associated pack.
  214. */
  215. abstract long findOffset(AnyObjectId objId);
  216. /**
  217. * Retrieve stored CRC32 checksum of the requested object raw-data
  218. * (including header).
  219. *
  220. * @param objId
  221. * id of object to look for
  222. * @return CRC32 checksum of specified object (at 32 less significant bits)
  223. * @throws MissingObjectException
  224. * when requested ObjectId was not found in this index
  225. * @throws UnsupportedOperationException
  226. * when this index doesn't support CRC32 checksum
  227. */
  228. abstract long findCRC32(AnyObjectId objId) throws MissingObjectException,
  229. UnsupportedOperationException;
  230. /**
  231. * Check whether this index supports (has) CRC32 checksums for objects.
  232. *
  233. * @return true if CRC32 is stored, false otherwise
  234. */
  235. abstract boolean hasCRC32Support();
  236. /**
  237. * Represent mutable entry of pack index consisting of object id and offset
  238. * in pack (both mutable).
  239. *
  240. */
  241. public static class MutableEntry {
  242. final MutableObjectId idBuffer = new MutableObjectId();
  243. long offset;
  244. /**
  245. * Returns offset for this index object entry
  246. *
  247. * @return offset of this object in a pack file
  248. */
  249. public long getOffset() {
  250. return offset;
  251. }
  252. /** @return hex string describing the object id of this entry. */
  253. public String name() {
  254. ensureId();
  255. return idBuffer.name();
  256. }
  257. /** @return a copy of the object id. */
  258. public ObjectId toObjectId() {
  259. ensureId();
  260. return idBuffer.toObjectId();
  261. }
  262. /** @return a complete copy of this entry, that won't modify */
  263. public MutableEntry cloneEntry() {
  264. final MutableEntry r = new MutableEntry();
  265. ensureId();
  266. r.idBuffer.fromObjectId(idBuffer);
  267. r.offset = offset;
  268. return r;
  269. }
  270. void ensureId() {
  271. // Override in implementations.
  272. }
  273. }
  274. abstract class EntriesIterator implements Iterator<MutableEntry> {
  275. protected final MutableEntry entry = initEntry();
  276. protected long returnedNumber = 0;
  277. protected abstract MutableEntry initEntry();
  278. public boolean hasNext() {
  279. return returnedNumber < getObjectCount();
  280. }
  281. /**
  282. * Implementation must update {@link #returnedNumber} before returning
  283. * element.
  284. */
  285. public abstract MutableEntry next();
  286. public void remove() {
  287. throw new UnsupportedOperationException();
  288. }
  289. }
  290. }