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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.ObjectReader;
  50. import org.eclipse.jgit.lib.ProgressMonitor;
  51. import org.eclipse.jgit.revwalk.RevObject;
  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 obj
  71. * identity of the object that will be packed. The object's
  72. * parsed status is undefined here. Implementers must not rely on
  73. * the object being parsed.
  74. * @return a new instance for this object.
  75. */
  76. public ObjectToPack newObjectToPack(RevObject obj);
  77. /**
  78. * Select the best object representation for a packer.
  79. *
  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. *
  86. * The implementation may choose to consider multiple objects at once on
  87. * concurrent threads, but must evaluate all representations of an object
  88. * within the same thread.
  89. *
  90. * @param packer
  91. * the packer that will write the object in the near future.
  92. * @param monitor
  93. * progress monitor, implementation should update the monitor
  94. * once for each item in the iteration when selection is done.
  95. * @param objects
  96. * the objects that are being packed.
  97. * @throws MissingObjectException
  98. * there is no representation available for the object, as it is
  99. * no longer in the repository. Packing will abort.
  100. * @throws IOException
  101. * the repository cannot be accessed. Packing will abort.
  102. */
  103. public void selectObjectRepresentation(PackWriter packer,
  104. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  105. throws IOException, MissingObjectException;
  106. /**
  107. * Write objects to the pack stream in roughly the order given.
  108. *
  109. * {@code PackWriter} invokes this method to write out one or more objects,
  110. * in approximately the order specified by the iteration over the list. A
  111. * simple implementation of this method would just iterate the list and
  112. * output each object:
  113. *
  114. * <pre>
  115. * for (ObjectToPack obj : list)
  116. * out.writeObject(obj)
  117. * </pre>
  118. *
  119. * However more sophisticated implementors may try to perform some (small)
  120. * reordering to access objects that are stored close to each other at
  121. * roughly the same time. Implementations may choose to write objects out of
  122. * order, but this may increase pack file size due to using a larger header
  123. * format to reach a delta base that is later in the stream. It may also
  124. * reduce data locality for the reader, slowing down data access.
  125. *
  126. * Invoking {@link PackOutputStream#writeObject(ObjectToPack)} will cause
  127. * {@link #copyObjectAsIs(PackOutputStream, ObjectToPack)} to be invoked
  128. * recursively on {@code this} if the current object is scheduled for reuse.
  129. *
  130. * @param out
  131. * the stream to write each object to.
  132. * @param list
  133. * the list of objects to write. Objects should be written in
  134. * approximately this order. Implementors may resort the list
  135. * elements in-place during writing if desired.
  136. * @throws IOException
  137. * the stream cannot be written to, or one or more required
  138. * objects cannot be accessed from the object database.
  139. */
  140. public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  141. throws IOException;
  142. /**
  143. * Output a previously selected representation.
  144. * <p>
  145. * {@code PackWriter} invokes this method only if a representation
  146. * previously given to it by {@code selectObjectRepresentation} was chosen
  147. * for reuse into the output stream. The {@code otp} argument is an instance
  148. * created by this reader's own {@code newObjectToPack}, and the
  149. * representation data saved within it also originated from this reader.
  150. * <p>
  151. * Implementors must write the object header before copying the raw data to
  152. * the output stream. The typical implementation is like:
  153. *
  154. * <pre>
  155. * MyToPack mtp = (MyToPack) otp;
  156. * byte[] raw = validate(mtp); // throw SORNAE here, if at all
  157. * out.writeHeader(mtp, mtp.inflatedSize);
  158. * out.write(raw);
  159. * </pre>
  160. *
  161. * @param out
  162. * stream the object should be written to.
  163. * @param otp
  164. * the object's saved representation information.
  165. * @throws StoredObjectRepresentationNotAvailableException
  166. * the previously selected representation is no longer
  167. * available. If thrown before {@code out.writeHeader} the pack
  168. * writer will try to find another representation, and write
  169. * that one instead. If throw after {@code out.writeHeader},
  170. * packing will abort.
  171. * @throws IOException
  172. * the stream's write method threw an exception. Packing will
  173. * abort.
  174. */
  175. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp)
  176. throws IOException, StoredObjectRepresentationNotAvailableException;
  177. /**
  178. * Obtain the available cached packs.
  179. * <p>
  180. * A cached pack has known starting points and may be sent entirely as-is,
  181. * with almost no effort on the sender's part.
  182. *
  183. * @return the available cached packs.
  184. * @throws IOException
  185. * the cached packs cannot be listed from the repository.
  186. * Callers may choose to ignore this and continue as-if there
  187. * were no cached packs.
  188. */
  189. public Collection<CachedPack> getCachedPacks() throws IOException;
  190. /**
  191. * Append an entire pack's contents onto the output stream.
  192. * <p>
  193. * The entire pack, excluding its header and trailing footer is sent.
  194. *
  195. * @param out
  196. * stream to append the pack onto.
  197. * @param pack
  198. * the cached pack to send.
  199. * @throws IOException
  200. * the pack cannot be read, or stream did not accept a write.
  201. */
  202. public abstract void copyPackAsIs(PackOutputStream out, CachedPack pack)
  203. throws IOException;
  204. }