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.

ObjectReuseAsIs.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.pack;
  44. import java.io.IOException;
  45. import java.util.Collection;
  46. import java.util.List;
  47. import org.eclipse.jgit.errors.MissingObjectException;
  48. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.ObjectReader;
  51. import org.eclipse.jgit.lib.ProgressMonitor;
  52. /**
  53. * Extension of {@link ObjectReader} that supports reusing objects in packs.
  54. * <p>
  55. * {@code ObjectReader} implementations may also optionally implement this
  56. * interface to support {@link PackWriter} with a means of copying an object
  57. * that is already in pack encoding format directly into the output stream,
  58. * without incurring decompression and recompression overheads.
  59. */
  60. public interface ObjectReuseAsIs {
  61. /**
  62. * Allocate a new {@code PackWriter} state structure for an object.
  63. * <p>
  64. * {@link PackWriter} allocates these objects to keep track of the
  65. * per-object state, and how to load the objects efficiently into the
  66. * generated stream. Implementers may subclass this type with additional
  67. * object state, such as to remember what file and offset contains the
  68. * object's pack encoded data.
  69. *
  70. * @param objectId
  71. * the id of the object that will be packed.
  72. * @param type
  73. * the Git type of the object that will be packed.
  74. * @return a new instance for this object.
  75. */
  76. public ObjectToPack newObjectToPack(AnyObjectId objectId, int type);
  77. /**
  78. * Select the best object representation for a packer.
  79. * <p>
  80. * Implementations should iterate through all available representations of
  81. * an object, and pass them in turn to the PackWriter though
  82. * {@link PackWriter#select(ObjectToPack, StoredObjectRepresentation)} so
  83. * the writer can select the most suitable representation to reuse into the
  84. * output stream.
  85. * <p>
  86. * If the implementation returns CachedPack from {@link #getCachedPacks()},
  87. * it must consider the representation of any object that is stored in any
  88. * of the offered CachedPacks. PackWriter relies on this behavior to prune
  89. * duplicate objects out of the pack stream when it selects a CachedPack and
  90. * the object was also reached through the thin-pack enumeration.
  91. * <p>
  92. * The implementation may choose to consider multiple objects at once on
  93. * concurrent threads, but must evaluate all representations of an object
  94. * within the same thread.
  95. *
  96. * @param packer
  97. * the packer that will write the object in the near future.
  98. * @param monitor
  99. * progress monitor, implementation should update the monitor
  100. * once for each item in the iteration when selection is done.
  101. * @param objects
  102. * the objects that are being packed.
  103. * @throws MissingObjectException
  104. * there is no representation available for the object, as it is
  105. * no longer in the repository. Packing will abort.
  106. * @throws IOException
  107. * the repository cannot be accessed. Packing will abort.
  108. */
  109. public void selectObjectRepresentation(PackWriter packer,
  110. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  111. throws IOException, MissingObjectException;
  112. /**
  113. * Write objects to the pack stream in roughly the order given.
  114. *
  115. * {@code PackWriter} invokes this method to write out one or more objects,
  116. * in approximately the order specified by the iteration over the list. A
  117. * simple implementation of this method would just iterate the list and
  118. * output each object:
  119. *
  120. * <pre>
  121. * for (ObjectToPack obj : list)
  122. * out.writeObject(obj)
  123. * </pre>
  124. *
  125. * However more sophisticated implementors may try to perform some (small)
  126. * reordering to access objects that are stored close to each other at
  127. * roughly the same time. Implementations may choose to write objects out of
  128. * order, but this may increase pack file size due to using a larger header
  129. * format to reach a delta base that is later in the stream. It may also
  130. * reduce data locality for the reader, slowing down data access.
  131. *
  132. * Invoking {@link PackOutputStream#writeObject(ObjectToPack)} will cause
  133. * {@link #copyObjectAsIs(PackOutputStream, ObjectToPack, boolean)} to be
  134. * invoked recursively on {@code this} if the current object is scheduled
  135. * for reuse.
  136. *
  137. * @param out
  138. * the stream to write each object to.
  139. * @param list
  140. * the list of objects to write. Objects should be written in
  141. * approximately this order. Implementors may resort the list
  142. * elements in-place during writing if desired.
  143. * @throws IOException
  144. * the stream cannot be written to, or one or more required
  145. * objects cannot be accessed from the object database.
  146. */
  147. public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  148. throws IOException;
  149. /**
  150. * Output a previously selected representation.
  151. * <p>
  152. * {@code PackWriter} invokes this method only if a representation
  153. * previously given to it by {@code selectObjectRepresentation} was chosen
  154. * for reuse into the output stream. The {@code otp} argument is an instance
  155. * created by this reader's own {@code newObjectToPack}, and the
  156. * representation data saved within it also originated from this reader.
  157. * <p>
  158. * Implementors must write the object header before copying the raw data to
  159. * the output stream. The typical implementation is like:
  160. *
  161. * <pre>
  162. * MyToPack mtp = (MyToPack) otp;
  163. * byte[] raw;
  164. * if (validate)
  165. * raw = validate(mtp); // throw SORNAE here, if at all
  166. * else
  167. * raw = readFast(mtp);
  168. * out.writeHeader(mtp, mtp.inflatedSize);
  169. * out.write(raw);
  170. * </pre>
  171. *
  172. * @param out
  173. * stream the object should be written to.
  174. * @param otp
  175. * the object's saved representation information.
  176. * @param validate
  177. * if true the representation must be validated and not be
  178. * corrupt before being reused. If false, validation may be
  179. * skipped as it will be performed elsewhere in the processing
  180. * pipeline.
  181. * @throws StoredObjectRepresentationNotAvailableException
  182. * the previously selected representation is no longer
  183. * available. If thrown before {@code out.writeHeader} the pack
  184. * writer will try to find another representation, and write
  185. * that one instead. If throw after {@code out.writeHeader},
  186. * packing will abort.
  187. * @throws IOException
  188. * the stream's write method threw an exception. Packing will
  189. * abort.
  190. */
  191. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp,
  192. boolean validate) throws IOException,
  193. StoredObjectRepresentationNotAvailableException;
  194. /**
  195. * Obtain the available cached packs.
  196. * <p>
  197. * A cached pack has known starting points and may be sent entirely as-is,
  198. * with almost no effort on the sender's part.
  199. *
  200. * @return the available cached packs.
  201. * @throws IOException
  202. * the cached packs cannot be listed from the repository.
  203. * Callers may choose to ignore this and continue as-if there
  204. * were no cached packs.
  205. */
  206. public Collection<CachedPack> getCachedPacks() throws IOException;
  207. /**
  208. * Append an entire pack's contents onto the output stream.
  209. * <p>
  210. * The entire pack, excluding its header and trailing footer is sent.
  211. *
  212. * @param out
  213. * stream to append the pack onto.
  214. * @param pack
  215. * the cached pack to send.
  216. * @param validate
  217. * if true the representation must be validated and not be
  218. * corrupt before being reused. If false, validation may be
  219. * skipped as it will be performed elsewhere in the processing
  220. * pipeline.
  221. * @throws IOException
  222. * the pack cannot be read, or stream did not accept a write.
  223. */
  224. public abstract void copyPackAsIs(PackOutputStream out, CachedPack pack,
  225. boolean validate) throws IOException;
  226. }