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.

ObjectDatabase.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2009, 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. /**
  48. * Abstraction of arbitrary object storage.
  49. * <p>
  50. * An object database stores one or more Git objects, indexed by their unique
  51. * {@link ObjectId}.
  52. */
  53. public abstract class ObjectDatabase {
  54. /** Initialize a new database instance for access. */
  55. protected ObjectDatabase() {
  56. // Protected to force extension.
  57. }
  58. /**
  59. * Does this database exist yet?
  60. *
  61. * @return true if this database is already created; false if the caller
  62. * should invoke {@link #create()} to create this database location.
  63. */
  64. public boolean exists() {
  65. return true;
  66. }
  67. /**
  68. * Initialize a new object database at this location.
  69. *
  70. * @throws IOException
  71. * the database could not be created.
  72. */
  73. public void create() throws IOException {
  74. // Assume no action is required.
  75. }
  76. /**
  77. * Create a new {@code ObjectInserter} to insert new objects.
  78. * <p>
  79. * The returned inserter is not itself thread-safe, but multiple concurrent
  80. * inserter instances created from the same {@code ObjectDatabase} must be
  81. * thread-safe.
  82. *
  83. * @return writer the caller can use to create objects in this database.
  84. */
  85. public abstract ObjectInserter newInserter();
  86. /**
  87. * Create a new {@code ObjectReader} to read existing objects.
  88. * <p>
  89. * The returned reader is not itself thread-safe, but multiple concurrent
  90. * reader instances created from the same {@code ObjectDatabase} must be
  91. * thread-safe.
  92. *
  93. * @return reader the caller can use to load objects from this database.
  94. */
  95. public abstract ObjectReader newReader();
  96. /**
  97. * Close any resources held by this database.
  98. */
  99. public abstract void close();
  100. /**
  101. * Does the requested object exist in this database?
  102. * <p>
  103. * This is a one-shot call interface which may be faster than allocating a
  104. * {@link #newReader()} to perform the lookup.
  105. *
  106. * @param objectId
  107. * identity of the object to test for existence of.
  108. * @return true if the specified object is stored in this database.
  109. * @throws IOException
  110. * the object store cannot be accessed.
  111. */
  112. public boolean has(final AnyObjectId objectId) throws IOException {
  113. final ObjectReader or = newReader();
  114. try {
  115. return or.has(objectId);
  116. } finally {
  117. or.release();
  118. }
  119. }
  120. /**
  121. * Open an object from this database.
  122. * <p>
  123. * This is a one-shot call interface which may be faster than allocating a
  124. * {@link #newReader()} to perform the lookup.
  125. *
  126. * @param objectId
  127. * identity of the object to open.
  128. * @return a {@link ObjectLoader} for accessing the object.
  129. * @throws MissingObjectException
  130. * the object does not exist.
  131. * @throws IOException
  132. * the object store cannot be accessed.
  133. */
  134. public ObjectLoader open(final AnyObjectId objectId)
  135. throws IOException {
  136. return open(objectId, ObjectReader.OBJ_ANY);
  137. }
  138. /**
  139. * Open an object from this database.
  140. * <p>
  141. * This is a one-shot call interface which may be faster than allocating a
  142. * {@link #newReader()} to perform the lookup.
  143. *
  144. * @param objectId
  145. * identity of the object to open.
  146. * @param typeHint
  147. * hint about the type of object being requested, e.g.
  148. * {@link Constants#OBJ_BLOB}; {@link ObjectReader#OBJ_ANY} if
  149. * the object type is not known, or does not matter to the
  150. * caller.
  151. * @return a {@link ObjectLoader} for accessing the object.
  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 ObjectLoader open(AnyObjectId objectId, int typeHint)
  161. throws MissingObjectException, IncorrectObjectTypeException,
  162. IOException {
  163. final ObjectReader or = newReader();
  164. try {
  165. return or.open(objectId, typeHint);
  166. } finally {
  167. or.release();
  168. }
  169. }
  170. /**
  171. * Create a new cached database instance over this database. This instance might
  172. * optimize queries by caching some information about database. So some modifications
  173. * done after instance creation might fail to be noticed.
  174. *
  175. * @return new cached database instance
  176. */
  177. public ObjectDatabase newCachedDatabase() {
  178. return this;
  179. }
  180. }