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.

FileObjectDatabase.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.storage.file;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.util.Set;
  47. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  48. import org.eclipse.jgit.lib.AnyObjectId;
  49. import org.eclipse.jgit.lib.ObjectDatabase;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.lib.ObjectLoader;
  52. import org.eclipse.jgit.lib.ObjectReader;
  53. import org.eclipse.jgit.storage.pack.ObjectToPack;
  54. import org.eclipse.jgit.storage.pack.PackWriter;
  55. abstract class FileObjectDatabase extends ObjectDatabase {
  56. static enum InsertLooseObjectResult {
  57. INSERTED, EXISTS_PACKED, EXISTS_LOOSE, FAILURE;
  58. }
  59. @Override
  60. public ObjectReader newReader() {
  61. return new WindowCursor(this);
  62. }
  63. @Override
  64. public abstract ObjectDirectoryInserter newInserter();
  65. /**
  66. * Does the requested object exist in this database?
  67. * <p>
  68. * Alternates (if present) are searched automatically.
  69. *
  70. * @param objectId
  71. * identity of the object to test for existence of.
  72. * @return true if the specified object is stored in this database, or any
  73. * of the alternate databases.
  74. */
  75. public boolean has(final AnyObjectId objectId) {
  76. return hasObjectImpl1(objectId) || hasObjectImpl2(objectId.name());
  77. }
  78. final boolean hasObjectImpl1(final AnyObjectId objectId) {
  79. if (hasObject1(objectId))
  80. return true;
  81. for (final AlternateHandle alt : myAlternates()) {
  82. if (alt.db.hasObjectImpl1(objectId))
  83. return true;
  84. }
  85. return tryAgain1() && hasObject1(objectId);
  86. }
  87. final boolean hasObjectImpl2(final String objectId) {
  88. if (hasObject2(objectId))
  89. return true;
  90. for (final AlternateHandle alt : myAlternates()) {
  91. if (alt.db.hasObjectImpl2(objectId))
  92. return true;
  93. }
  94. return false;
  95. }
  96. abstract void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
  97. throws IOException;
  98. /**
  99. * Open an object from this database.
  100. * <p>
  101. * Alternates (if present) are searched automatically.
  102. *
  103. * @param curs
  104. * temporary working space associated with the calling thread.
  105. * @param objectId
  106. * identity of the object to open.
  107. * @return a {@link ObjectLoader} for accessing the data of the named
  108. * object, or null if the object does not exist.
  109. * @throws IOException
  110. */
  111. ObjectLoader openObject(final WindowCursor curs, final AnyObjectId objectId)
  112. throws IOException {
  113. ObjectLoader ldr;
  114. ldr = openObjectImpl1(curs, objectId);
  115. if (ldr != null)
  116. return ldr;
  117. ldr = openObjectImpl2(curs, objectId.name(), objectId);
  118. if (ldr != null)
  119. return ldr;
  120. return null;
  121. }
  122. final ObjectLoader openObjectImpl1(final WindowCursor curs,
  123. final AnyObjectId objectId) throws IOException {
  124. ObjectLoader ldr;
  125. ldr = openObject1(curs, objectId);
  126. if (ldr != null)
  127. return ldr;
  128. for (final AlternateHandle alt : myAlternates()) {
  129. ldr = alt.db.openObjectImpl1(curs, objectId);
  130. if (ldr != null)
  131. return ldr;
  132. }
  133. if (tryAgain1()) {
  134. ldr = openObject1(curs, objectId);
  135. if (ldr != null)
  136. return ldr;
  137. }
  138. return null;
  139. }
  140. final ObjectLoader openObjectImpl2(final WindowCursor curs,
  141. final String objectName, final AnyObjectId objectId)
  142. throws IOException {
  143. ObjectLoader ldr;
  144. ldr = openObject2(curs, objectName, objectId);
  145. if (ldr != null)
  146. return ldr;
  147. for (final AlternateHandle alt : myAlternates()) {
  148. ldr = alt.db.openObjectImpl2(curs, objectName, objectId);
  149. if (ldr != null)
  150. return ldr;
  151. }
  152. return null;
  153. }
  154. long getObjectSize(WindowCursor curs, AnyObjectId objectId)
  155. throws IOException {
  156. long sz = getObjectSizeImpl1(curs, objectId);
  157. if (0 <= sz)
  158. return sz;
  159. return getObjectSizeImpl2(curs, objectId.name(), objectId);
  160. }
  161. final long getObjectSizeImpl1(final WindowCursor curs,
  162. final AnyObjectId objectId) throws IOException {
  163. long sz;
  164. sz = getObjectSize1(curs, objectId);
  165. if (0 <= sz)
  166. return sz;
  167. for (final AlternateHandle alt : myAlternates()) {
  168. sz = alt.db.getObjectSizeImpl1(curs, objectId);
  169. if (0 <= sz)
  170. return sz;
  171. }
  172. if (tryAgain1()) {
  173. sz = getObjectSize1(curs, objectId);
  174. if (0 <= sz)
  175. return sz;
  176. }
  177. return -1;
  178. }
  179. final long getObjectSizeImpl2(final WindowCursor curs,
  180. final String objectName, final AnyObjectId objectId)
  181. throws IOException {
  182. long sz;
  183. sz = getObjectSize2(curs, objectName, objectId);
  184. if (0 <= sz)
  185. return sz;
  186. for (final AlternateHandle alt : myAlternates()) {
  187. sz = alt.db.getObjectSizeImpl2(curs, objectName, objectId);
  188. if (0 <= sz)
  189. return sz;
  190. }
  191. return -1;
  192. }
  193. abstract void selectObjectRepresentation(PackWriter packer,
  194. ObjectToPack otp, WindowCursor curs) throws IOException;
  195. abstract File getDirectory();
  196. abstract AlternateHandle[] myAlternates();
  197. abstract boolean tryAgain1();
  198. abstract boolean hasObject1(AnyObjectId objectId);
  199. abstract boolean hasObject2(String objectId);
  200. abstract ObjectLoader openObject1(WindowCursor curs, AnyObjectId objectId)
  201. throws IOException;
  202. abstract ObjectLoader openObject2(WindowCursor curs, String objectName,
  203. AnyObjectId objectId) throws IOException;
  204. abstract long getObjectSize1(WindowCursor curs, AnyObjectId objectId)
  205. throws IOException;
  206. abstract long getObjectSize2(WindowCursor curs, String objectName,
  207. AnyObjectId objectId) throws IOException;
  208. abstract InsertLooseObjectResult insertUnpackedObject(File tmp,
  209. ObjectId id, boolean createDuplicate);
  210. abstract FileObjectDatabase newCachedFileObjectDatabase();
  211. static class AlternateHandle {
  212. final FileObjectDatabase db;
  213. AlternateHandle(FileObjectDatabase db) {
  214. this.db = db;
  215. }
  216. void close() {
  217. db.close();
  218. }
  219. }
  220. static class AlternateRepository extends AlternateHandle {
  221. final FileRepository repository;
  222. AlternateRepository(FileRepository r) {
  223. super(r.getObjectDatabase());
  224. repository = r;
  225. }
  226. void close() {
  227. repository.close();
  228. }
  229. }
  230. }