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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.internal.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.BitmapIndex.BitmapBuilder;
  51. import org.eclipse.jgit.lib.ProgressMonitor;
  52. /**
  53. * Extension of {@link org.eclipse.jgit.lib.ObjectReader} that supports reusing
  54. * objects in packs.
  55. * <p>
  56. * {@code ObjectReader} implementations may also optionally implement this
  57. * interface to support
  58. * {@link org.eclipse.jgit.internal.storage.pack.PackWriter} with a means of
  59. * copying an object that is already in pack encoding format directly into the
  60. * output stream, without incurring decompression and recompression overheads.
  61. */
  62. public interface ObjectReuseAsIs {
  63. /**
  64. * Allocate a new {@code PackWriter} state structure for an object.
  65. * <p>
  66. * {@link org.eclipse.jgit.internal.storage.pack.PackWriter} allocates these
  67. * objects to keep track of the per-object state, and how to load the
  68. * objects efficiently into the generated stream. Implementers may subclass
  69. * this type with additional object state, such as to remember what file and
  70. * offset contains the object's pack encoded data.
  71. *
  72. * @param objectId
  73. * the id of the object that will be packed.
  74. * @param type
  75. * the Git type of the object that will be packed.
  76. * @return a new instance for this object.
  77. */
  78. ObjectToPack newObjectToPack(AnyObjectId objectId, int type);
  79. /**
  80. * Select the best object representation for a packer.
  81. * <p>
  82. * Implementations should iterate through all available representations of
  83. * an object, and pass them in turn to the PackWriter though
  84. * {@link org.eclipse.jgit.internal.storage.pack.PackWriter#select(ObjectToPack, StoredObjectRepresentation)}
  85. * so the writer can select the most suitable representation to reuse into
  86. * the output stream.
  87. * <p>
  88. * If the implementation returns CachedPack from
  89. * {@link #getCachedPacksAndUpdate(BitmapBuilder)} it must consider the
  90. * representation of any object that is stored in any of the offered
  91. * CachedPacks. PackWriter relies on this behavior to prune duplicate
  92. * objects out of the pack stream when it selects a CachedPack and the
  93. * object was also reached through the thin-pack enumeration.
  94. * <p>
  95. * The implementation may choose to consider multiple objects at once on
  96. * concurrent threads, but must evaluate all representations of an object
  97. * within the same thread.
  98. *
  99. * @param packer
  100. * the packer that will write the object in the near future.
  101. * @param monitor
  102. * progress monitor, implementation should update the monitor
  103. * once for each item in the iteration when selection is done.
  104. * @param objects
  105. * the objects that are being packed.
  106. * @throws org.eclipse.jgit.errors.MissingObjectException
  107. * there is no representation available for the object, as it is
  108. * no longer in the repository. Packing will abort.
  109. * @throws java.io.IOException
  110. * the repository cannot be accessed. Packing will abort.
  111. */
  112. void selectObjectRepresentation(PackWriter packer,
  113. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  114. throws IOException, MissingObjectException;
  115. /**
  116. * Write objects to the pack stream in roughly the order given.
  117. *
  118. * {@code PackWriter} invokes this method to write out one or more objects,
  119. * in approximately the order specified by the iteration over the list. A
  120. * simple implementation of this method would just iterate the list and
  121. * output each object:
  122. *
  123. * <pre>
  124. * for (ObjectToPack obj : list)
  125. * out.writeObject(obj)
  126. * </pre>
  127. *
  128. * However more sophisticated implementors may try to perform some (small)
  129. * reordering to access objects that are stored close to each other at
  130. * roughly the same time. Implementations may choose to write objects out of
  131. * order, but this may increase pack file size due to using a larger header
  132. * format to reach a delta base that is later in the stream. It may also
  133. * reduce data locality for the reader, slowing down data access.
  134. *
  135. * Invoking
  136. * {@link org.eclipse.jgit.internal.storage.pack.PackOutputStream#writeObject(ObjectToPack)}
  137. * will cause
  138. * {@link #copyObjectAsIs(PackOutputStream, ObjectToPack, boolean)} to be
  139. * invoked recursively on {@code this} if the current object is scheduled
  140. * for reuse.
  141. *
  142. * @param out
  143. * the stream to write each object to.
  144. * @param list
  145. * the list of objects to write. Objects should be written in
  146. * approximately this order. Implementors may resort the list
  147. * elements in-place during writing if desired.
  148. * @throws java.io.IOException
  149. * the stream cannot be written to, or one or more required
  150. * objects cannot be accessed from the object database.
  151. */
  152. void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  153. throws IOException;
  154. /**
  155. * Output a previously selected representation.
  156. * <p>
  157. * {@code PackWriter} invokes this method only if a representation
  158. * previously given to it by {@code selectObjectRepresentation} was chosen
  159. * for reuse into the output stream. The {@code otp} argument is an instance
  160. * created by this reader's own {@code newObjectToPack}, and the
  161. * representation data saved within it also originated from this reader.
  162. * <p>
  163. * Implementors must write the object header before copying the raw data to
  164. * the output stream. The typical implementation is like:
  165. *
  166. * <pre>
  167. * MyToPack mtp = (MyToPack) otp;
  168. * byte[] raw;
  169. * if (validate)
  170. * raw = validate(mtp); // throw SORNAE here, if at all
  171. * else
  172. * raw = readFast(mtp);
  173. * out.writeHeader(mtp, mtp.inflatedSize);
  174. * out.write(raw);
  175. * </pre>
  176. *
  177. * @param out
  178. * stream the object should be written to.
  179. * @param otp
  180. * the object's saved representation information.
  181. * @param validate
  182. * if true the representation must be validated and not be
  183. * corrupt before being reused. If false, validation may be
  184. * skipped as it will be performed elsewhere in the processing
  185. * pipeline.
  186. * @throws org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException
  187. * the previously selected representation is no longer
  188. * available. If thrown before {@code out.writeHeader} the pack
  189. * writer will try to find another representation, and write
  190. * that one instead. If throw after {@code out.writeHeader},
  191. * packing will abort.
  192. * @throws java.io.IOException
  193. * the stream's write method threw an exception. Packing will
  194. * abort.
  195. */
  196. void copyObjectAsIs(PackOutputStream out, ObjectToPack otp,
  197. boolean validate) throws IOException,
  198. StoredObjectRepresentationNotAvailableException;
  199. /**
  200. * Append an entire pack's contents onto the output stream.
  201. * <p>
  202. * The entire pack, excluding its header and trailing footer is sent.
  203. *
  204. * @param out
  205. * stream to append the pack onto.
  206. * @param pack
  207. * the cached pack to send.
  208. * @throws java.io.IOException
  209. * the pack cannot be read, or stream did not accept a write.
  210. */
  211. void copyPackAsIs(PackOutputStream out, CachedPack pack)
  212. throws IOException;
  213. /**
  214. * Obtain the available cached packs that match the bitmap and update
  215. * the bitmap by removing the items that are in the CachedPack.
  216. * <p>
  217. * A cached pack has known starting points and may be sent entirely as-is,
  218. * with almost no effort on the sender's part.
  219. *
  220. * @param needBitmap
  221. * the bitmap that contains all of the objects the client wants.
  222. * @return the available cached packs.
  223. * @throws java.io.IOException
  224. * the cached packs cannot be listed from the repository.
  225. * Callers may choose to ignore this and continue as-if there
  226. * were no cached packs.
  227. */
  228. Collection<CachedPack> getCachedPacksAndUpdate(
  229. BitmapBuilder needBitmap) throws IOException;
  230. }