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.

PackIndexWriter.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.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.BufferedOutputStream;
  46. import java.io.IOException;
  47. import java.io.OutputStream;
  48. import java.security.DigestOutputStream;
  49. import java.text.MessageFormat;
  50. import java.util.List;
  51. import org.eclipse.jgit.JGitText;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.transport.PackedObjectInfo;
  55. import org.eclipse.jgit.util.NB;
  56. /**
  57. * Creates a table of contents to support random access by {@link PackFile}.
  58. * <p>
  59. * Pack index files (the <code>.idx</code> suffix in a pack file pair)
  60. * provides random access to any object in the pack by associating an ObjectId
  61. * to the byte offset within the pack where the object's data can be read.
  62. */
  63. public abstract class PackIndexWriter {
  64. /** Magic constant indicating post-version 1 format. */
  65. protected static final byte[] TOC = { -1, 't', 'O', 'c' };
  66. /**
  67. * Create a new writer for the oldest (most widely understood) format.
  68. * <p>
  69. * This method selects an index format that can accurate describe the
  70. * supplied objects and that will be the most compatible format with older
  71. * Git implementations.
  72. * <p>
  73. * Index version 1 is widely recognized by all Git implementations, but
  74. * index version 2 (and later) is not as well recognized as it was
  75. * introduced more than a year later. Index version 1 can only be used if
  76. * the resulting pack file is under 4 gigabytes in size; packs larger than
  77. * that limit must use index version 2.
  78. *
  79. * @param dst
  80. * the stream the index data will be written to. If not already
  81. * buffered it will be automatically wrapped in a buffered
  82. * stream. Callers are always responsible for closing the stream.
  83. * @param objs
  84. * the objects the caller needs to store in the index. Entries
  85. * will be examined until a format can be conclusively selected.
  86. * @return a new writer to output an index file of the requested format to
  87. * the supplied stream.
  88. * @throws IllegalArgumentException
  89. * no recognized pack index version can support the supplied
  90. * objects. This is likely a bug in the implementation.
  91. */
  92. @SuppressWarnings("fallthrough")
  93. public static PackIndexWriter createOldestPossible(final OutputStream dst,
  94. final List<? extends PackedObjectInfo> objs) {
  95. int version = 1;
  96. LOOP: for (final PackedObjectInfo oe : objs) {
  97. switch (version) {
  98. case 1:
  99. if (PackIndexWriterV1.canStore(oe))
  100. continue;
  101. version = 2;
  102. case 2:
  103. break LOOP;
  104. }
  105. }
  106. return createVersion(dst, version);
  107. }
  108. /**
  109. * Create a new writer instance for a specific index format version.
  110. *
  111. * @param dst
  112. * the stream the index data will be written to. If not already
  113. * buffered it will be automatically wrapped in a buffered
  114. * stream. Callers are always responsible for closing the stream.
  115. * @param version
  116. * index format version number required by the caller. Exactly
  117. * this formatted version will be written.
  118. * @return a new writer to output an index file of the requested format to
  119. * the supplied stream.
  120. * @throws IllegalArgumentException
  121. * the version requested is not supported by this
  122. * implementation.
  123. */
  124. public static PackIndexWriter createVersion(final OutputStream dst,
  125. final int version) {
  126. switch (version) {
  127. case 1:
  128. return new PackIndexWriterV1(dst);
  129. case 2:
  130. return new PackIndexWriterV2(dst);
  131. default:
  132. throw new IllegalArgumentException(MessageFormat.format(
  133. JGitText.get().unsupportedPackIndexVersion, version));
  134. }
  135. }
  136. /** The index data stream we are responsible for creating. */
  137. protected final DigestOutputStream out;
  138. /** A temporary buffer for use during IO to {link #out}. */
  139. protected final byte[] tmp;
  140. /** The entries this writer must pack. */
  141. protected List<? extends PackedObjectInfo> entries;
  142. /** SHA-1 checksum for the entire pack data. */
  143. protected byte[] packChecksum;
  144. /**
  145. * Create a new writer instance.
  146. *
  147. * @param dst
  148. * the stream this instance outputs to. If not already buffered
  149. * it will be automatically wrapped in a buffered stream.
  150. */
  151. protected PackIndexWriter(final OutputStream dst) {
  152. out = new DigestOutputStream(dst instanceof BufferedOutputStream ? dst
  153. : new BufferedOutputStream(dst), Constants.newMessageDigest());
  154. tmp = new byte[4 + Constants.OBJECT_ID_LENGTH];
  155. }
  156. /**
  157. * Write all object entries to the index stream.
  158. * <p>
  159. * After writing the stream passed to the factory is flushed but remains
  160. * open. Callers are always responsible for closing the output stream.
  161. *
  162. * @param toStore
  163. * sorted list of objects to store in the index. The caller must
  164. * have previously sorted the list using {@link PackedObjectInfo}'s
  165. * native {@link Comparable} implementation.
  166. * @param packDataChecksum
  167. * checksum signature of the entire pack data content. This is
  168. * traditionally the last 20 bytes of the pack file's own stream.
  169. * @throws IOException
  170. * an error occurred while writing to the output stream, or this
  171. * index format cannot store the object data supplied.
  172. */
  173. public void write(final List<? extends PackedObjectInfo> toStore,
  174. final byte[] packDataChecksum) throws IOException {
  175. entries = toStore;
  176. packChecksum = packDataChecksum;
  177. writeImpl();
  178. out.flush();
  179. }
  180. /**
  181. * Writes the index file to {@link #out}.
  182. * <p>
  183. * Implementations should go something like:
  184. *
  185. * <pre>
  186. * writeFanOutTable();
  187. * for (final PackedObjectInfo po : entries)
  188. * writeOneEntry(po);
  189. * writeChecksumFooter();
  190. * </pre>
  191. *
  192. * <p>
  193. * Where the logic for <code>writeOneEntry</code> is specific to the index
  194. * format in use. Additional headers/footers may be used if necessary and
  195. * the {@link #entries} collection may be iterated over more than once if
  196. * necessary. Implementors therefore have complete control over the data.
  197. *
  198. * @throws IOException
  199. * an error occurred while writing to the output stream, or this
  200. * index format cannot store the object data supplied.
  201. */
  202. protected abstract void writeImpl() throws IOException;
  203. /**
  204. * Output the version 2 (and later) TOC header, with version number.
  205. * <p>
  206. * Post version 1 all index files start with a TOC header that makes the
  207. * file an invalid version 1 file, and then includes the version number.
  208. * This header is necessary to recognize a version 1 from a version 2
  209. * formatted index.
  210. *
  211. * @param version
  212. * version number of this index format being written.
  213. * @throws IOException
  214. * an error occurred while writing to the output stream.
  215. */
  216. protected void writeTOC(final int version) throws IOException {
  217. out.write(TOC);
  218. NB.encodeInt32(tmp, 0, version);
  219. out.write(tmp, 0, 4);
  220. }
  221. /**
  222. * Output the standard 256 entry first-level fan-out table.
  223. * <p>
  224. * The fan-out table is 4 KB in size, holding 256 32-bit unsigned integer
  225. * counts. Each count represents the number of objects within this index
  226. * whose {@link ObjectId#getFirstByte()} matches the count's position in the
  227. * fan-out table.
  228. *
  229. * @throws IOException
  230. * an error occurred while writing to the output stream.
  231. */
  232. protected void writeFanOutTable() throws IOException {
  233. final int[] fanout = new int[256];
  234. for (final PackedObjectInfo po : entries)
  235. fanout[po.getFirstByte() & 0xff]++;
  236. for (int i = 1; i < 256; i++)
  237. fanout[i] += fanout[i - 1];
  238. for (final int n : fanout) {
  239. NB.encodeInt32(tmp, 0, n);
  240. out.write(tmp, 0, 4);
  241. }
  242. }
  243. /**
  244. * Output the standard two-checksum index footer.
  245. * <p>
  246. * The standard footer contains two checksums (20 byte SHA-1 values):
  247. * <ol>
  248. * <li>Pack data checksum - taken from the last 20 bytes of the pack file.</li>
  249. * <li>Index data checksum - checksum of all index bytes written, including
  250. * the pack data checksum above.</li>
  251. * </ol>
  252. *
  253. * @throws IOException
  254. * an error occurred while writing to the output stream.
  255. */
  256. protected void writeChecksumFooter() throws IOException {
  257. out.write(packChecksum);
  258. out.on(false);
  259. out.write(out.getMessageDigest().digest());
  260. }
  261. }