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.

ObjectReader.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.lib;
  44. import java.io.IOException;
  45. import java.util.Collection;
  46. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  47. import org.eclipse.jgit.errors.MissingObjectException;
  48. import org.eclipse.jgit.revwalk.ObjectWalk;
  49. import org.eclipse.jgit.revwalk.RevCommit;
  50. import org.eclipse.jgit.revwalk.RevWalk;
  51. import org.eclipse.jgit.storage.pack.ObjectReuseAsIs;
  52. /**
  53. * Reads an {@link ObjectDatabase} for a single thread.
  54. * <p>
  55. * Readers that can support efficient reuse of pack encoded objects should also
  56. * implement the companion interface {@link ObjectReuseAsIs}.
  57. */
  58. public abstract class ObjectReader {
  59. /** Type hint indicating the caller doesn't know the type. */
  60. public static final int OBJ_ANY = -1;
  61. /**
  62. * Construct a new reader from the same data.
  63. * <p>
  64. * Applications can use this method to build a new reader from the same data
  65. * source, but for an different thread.
  66. *
  67. * @return a brand new reader, using the same data source.
  68. */
  69. public abstract ObjectReader newReader();
  70. /**
  71. * Does the requested object exist in this database?
  72. *
  73. * @param objectId
  74. * identity of the object to test for existence of.
  75. * @return true if the specified object is stored in this database.
  76. * @throws IOException
  77. * the object store cannot be accessed.
  78. */
  79. public boolean has(AnyObjectId objectId) throws IOException {
  80. return has(objectId, OBJ_ANY);
  81. }
  82. /**
  83. * Does the requested object exist in this database?
  84. *
  85. * @param objectId
  86. * identity of the object to test for existence of.
  87. * @param typeHint
  88. * hint about the type of object being requested;
  89. * {@link #OBJ_ANY} if the object type is not known, or does not
  90. * matter to the caller.
  91. * @return true if the specified object is stored in this database.
  92. * @throws IncorrectObjectTypeException
  93. * typeHint was not OBJ_ANY, and the object's actual type does
  94. * not match typeHint.
  95. * @throws IOException
  96. * the object store cannot be accessed.
  97. */
  98. public boolean has(AnyObjectId objectId, int typeHint) throws IOException {
  99. try {
  100. open(objectId, typeHint);
  101. return true;
  102. } catch (MissingObjectException notFound) {
  103. return false;
  104. }
  105. }
  106. /**
  107. * Open an object from this database.
  108. *
  109. * @param objectId
  110. * identity of the object to open.
  111. * @return a {@link ObjectLoader} for accessing the object.
  112. * @throws MissingObjectException
  113. * the object does not exist.
  114. * @throws IOException
  115. * the object store cannot be accessed.
  116. */
  117. public ObjectLoader open(AnyObjectId objectId)
  118. throws MissingObjectException, IOException {
  119. return open(objectId, OBJ_ANY);
  120. }
  121. /**
  122. * Open an object from this database.
  123. *
  124. * @param objectId
  125. * identity of the object to open.
  126. * @param typeHint
  127. * hint about the type of object being requested;
  128. * {@link #OBJ_ANY} if the object type is not known, or does not
  129. * matter to the caller.
  130. * @return a {@link ObjectLoader} for accessing the object.
  131. * @throws MissingObjectException
  132. * the object does not exist.
  133. * @throws IncorrectObjectTypeException
  134. * typeHint was not OBJ_ANY, and the object's actual type does
  135. * not match typeHint.
  136. * @throws IOException
  137. * the object store cannot be accessed.
  138. */
  139. public abstract ObjectLoader open(AnyObjectId objectId, int typeHint)
  140. throws MissingObjectException, IncorrectObjectTypeException,
  141. IOException;
  142. /**
  143. * Get only the size of an object.
  144. * <p>
  145. * The default implementation of this method opens an ObjectLoader.
  146. * Databases are encouraged to override this if a faster access method is
  147. * available to them.
  148. *
  149. * @param objectId
  150. * identity of the object to open.
  151. * @param typeHint
  152. * hint about the type of object being requested;
  153. * {@link #OBJ_ANY} if the object type is not known, or does not
  154. * matter to the caller.
  155. * @return size of object in bytes.
  156. * @throws MissingObjectException
  157. * the object does not exist.
  158. * @throws IncorrectObjectTypeException
  159. * typeHint was not OBJ_ANY, and the object's actual type does
  160. * not match typeHint.
  161. * @throws IOException
  162. * the object store cannot be accessed.
  163. */
  164. public long getObjectSize(AnyObjectId objectId, int typeHint)
  165. throws MissingObjectException, IncorrectObjectTypeException,
  166. IOException {
  167. return open(objectId, typeHint).getSize();
  168. }
  169. /**
  170. * Advice from a {@link RevWalk} that a walk is starting from these roots.
  171. *
  172. * @param walk
  173. * the revision pool that is using this reader.
  174. * @param roots
  175. * starting points of the revision walk. The starting points have
  176. * their headers parsed, but might be missing bodies.
  177. * @throws IOException
  178. * the reader cannot initialize itself to support the walk.
  179. */
  180. public void walkAdviceBeginCommits(RevWalk walk, Collection<RevCommit> roots)
  181. throws IOException {
  182. // Do nothing by default, most readers don't want or need advice.
  183. }
  184. /**
  185. * Advice from an {@link ObjectWalk} that trees will be traversed.
  186. *
  187. * @param ow
  188. * the object pool that is using this reader.
  189. * @param min
  190. * the first commit whose root tree will be read.
  191. * @param max
  192. * the last commit whose root tree will be read.
  193. * @throws IOException
  194. * the reader cannot initialize itself to support the walk.
  195. */
  196. public void walkAdviceBeginTrees(ObjectWalk ow, RevCommit min, RevCommit max)
  197. throws IOException {
  198. // Do nothing by default, most readers don't want or need advice.
  199. }
  200. /** Advice from that a walk is over. */
  201. public void walkAdviceEnd() {
  202. // Do nothing by default, most readers don't want or need advice.
  203. }
  204. /**
  205. * Release any resources used by this reader.
  206. * <p>
  207. * A reader that has been released can be used again, but may need to be
  208. * released after the subsequent usage.
  209. */
  210. public void release() {
  211. // Do nothing.
  212. }
  213. }