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

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