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

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