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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. import org.eclipse.jgit.storage.pack.ObjectReuseAsIs;
  48. /**
  49. * Reads an {@link ObjectDatabase} for a single thread.
  50. * <p>
  51. * Readers that can support efficient reuse of pack encoded objects should also
  52. * implement the companion interface {@link ObjectReuseAsIs}.
  53. */
  54. public abstract class ObjectReader {
  55. /** Type hint indicating the caller doesn't know the type. */
  56. protected static final int OBJ_ANY = -1;
  57. /**
  58. * Construct a new reader from the same data.
  59. * <p>
  60. * Applications can use this method to build a new reader from the same data
  61. * source, but for an different thread.
  62. *
  63. * @return a brand new reader, using the same data source.
  64. */
  65. public abstract ObjectReader newReader();
  66. /**
  67. * Does the requested object exist in this database?
  68. *
  69. * @param objectId
  70. * identity of the object to test for existence of.
  71. * @return true if the specified object is stored in this database.
  72. * @throws IOException
  73. * the object store cannot be accessed.
  74. */
  75. public boolean has(AnyObjectId objectId) throws IOException {
  76. return has(objectId, OBJ_ANY);
  77. }
  78. /**
  79. * Does the requested object exist in this database?
  80. *
  81. * @param objectId
  82. * identity of the object to test for existence of.
  83. * @param typeHint
  84. * hint about the type of object being requested;
  85. * {@link #OBJ_ANY} if the object type is not known, or does not
  86. * matter to the caller.
  87. * @return true if the specified object is stored in this database.
  88. * @throws IncorrectObjectTypeException
  89. * typeHint was not OBJ_ANY, and the object's actual type does
  90. * not match typeHint.
  91. * @throws IOException
  92. * the object store cannot be accessed.
  93. */
  94. public boolean has(AnyObjectId objectId, int typeHint) throws IOException {
  95. try {
  96. open(objectId, typeHint);
  97. return true;
  98. } catch (MissingObjectException notFound) {
  99. return false;
  100. }
  101. }
  102. /**
  103. * Open an object from this database.
  104. *
  105. * @param objectId
  106. * identity of the object to open.
  107. * @return a {@link ObjectLoader} for accessing the object.
  108. * @throws MissingObjectException
  109. * the object does not exist.
  110. * @throws IOException
  111. * the object store cannot be accessed.
  112. */
  113. public ObjectLoader open(AnyObjectId objectId)
  114. throws MissingObjectException, IOException {
  115. return open(objectId, OBJ_ANY);
  116. }
  117. /**
  118. * Open an object from this database.
  119. *
  120. * @param objectId
  121. * identity of the object to open.
  122. * @param typeHint
  123. * hint about the type of object being requested;
  124. * {@link #OBJ_ANY} if the object type is not known, or does not
  125. * matter to the caller.
  126. * @return a {@link ObjectLoader} for accessing the object.
  127. * @throws MissingObjectException
  128. * the object does not exist.
  129. * @throws IncorrectObjectTypeException
  130. * typeHint was not OBJ_ANY, and the object's actual type does
  131. * not match typeHint.
  132. * @throws IOException
  133. * the object store cannot be accessed.
  134. */
  135. public abstract ObjectLoader open(AnyObjectId objectId, int typeHint)
  136. throws MissingObjectException, IncorrectObjectTypeException,
  137. IOException;
  138. /**
  139. * Get only the size of an object.
  140. * <p>
  141. * The default implementation of this method opens an ObjectLoader.
  142. * Databases are encouraged to override this if a faster access method is
  143. * available to them.
  144. *
  145. * @param objectId
  146. * identity of the object to open.
  147. * @param typeHint
  148. * hint about the type of object being requested;
  149. * {@link #OBJ_ANY} if the object type is not known, or does not
  150. * matter to the caller.
  151. * @return size of object in bytes.
  152. * @throws MissingObjectException
  153. * the object does not exist.
  154. * @throws IncorrectObjectTypeException
  155. * typeHint was not OBJ_ANY, and the object's actual type does
  156. * not match typeHint.
  157. * @throws IOException
  158. * the object store cannot be accessed.
  159. */
  160. public long getObjectSize(AnyObjectId objectId, int typeHint)
  161. throws MissingObjectException, IncorrectObjectTypeException,
  162. IOException {
  163. return open(objectId, typeHint).getSize();
  164. }
  165. /**
  166. * Release any resources used by this reader.
  167. * <p>
  168. * A reader that has been released can be used again, but may need to be
  169. * released after the subsequent usage.
  170. */
  171. public void release() {
  172. // Do nothing.
  173. }
  174. }