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

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