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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. try (final ObjectReader or = newReader()) {
  114. return or.has(objectId);
  115. }
  116. }
  117. /**
  118. * Open an object from this database.
  119. * <p>
  120. * This is a one-shot call interface which may be faster than allocating a
  121. * {@link #newReader()} to perform the lookup.
  122. *
  123. * @param objectId
  124. * identity of the object to open.
  125. * @return a {@link ObjectLoader} for accessing the object.
  126. * @throws MissingObjectException
  127. * the object does not exist.
  128. * @throws IOException
  129. * the object store cannot be accessed.
  130. */
  131. public ObjectLoader open(final AnyObjectId objectId)
  132. throws IOException {
  133. return open(objectId, ObjectReader.OBJ_ANY);
  134. }
  135. /**
  136. * Open an object from this database.
  137. * <p>
  138. * This is a one-shot call interface which may be faster than allocating a
  139. * {@link #newReader()} to perform the lookup.
  140. *
  141. * @param objectId
  142. * identity of the object to open.
  143. * @param typeHint
  144. * hint about the type of object being requested, e.g.
  145. * {@link Constants#OBJ_BLOB}; {@link ObjectReader#OBJ_ANY} if
  146. * the object type is not known, or does not matter to the
  147. * caller.
  148. * @return a {@link ObjectLoader} for accessing the object.
  149. * @throws MissingObjectException
  150. * the object does not exist.
  151. * @throws IncorrectObjectTypeException
  152. * typeHint was not OBJ_ANY, and the object's actual type does
  153. * not match typeHint.
  154. * @throws IOException
  155. * the object store cannot be accessed.
  156. */
  157. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  158. throws MissingObjectException, IncorrectObjectTypeException,
  159. IOException {
  160. try (final ObjectReader or = newReader()) {
  161. return or.open(objectId, typeHint);
  162. }
  163. }
  164. /**
  165. * Create a new cached database instance over this database. This instance might
  166. * optimize queries by caching some information about database. So some modifications
  167. * done after instance creation might fail to be noticed.
  168. *
  169. * @return new cached database instance
  170. */
  171. public ObjectDatabase newCachedDatabase() {
  172. return this;
  173. }
  174. }