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

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