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

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