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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 org.eclipse.jgit.lib.AnyObjectId;
  47. import org.eclipse.jgit.lib.ObjectDatabase;
  48. import org.eclipse.jgit.lib.ObjectLoader;
  49. import org.eclipse.jgit.lib.ObjectReader;
  50. import org.eclipse.jgit.storage.pack.ObjectToPack;
  51. import org.eclipse.jgit.storage.pack.PackWriter;
  52. abstract class FileObjectDatabase extends ObjectDatabase {
  53. @Override
  54. public ObjectReader newReader() {
  55. return new WindowCursor(this);
  56. }
  57. /**
  58. * Does the requested object exist in this database?
  59. * <p>
  60. * Alternates (if present) are searched automatically.
  61. *
  62. * @param objectId
  63. * identity of the object to test for existence of.
  64. * @return true if the specified object is stored in this database, or any
  65. * of the alternate databases.
  66. */
  67. public boolean has(final AnyObjectId objectId) {
  68. return hasObjectImpl1(objectId) || hasObjectImpl2(objectId.name());
  69. }
  70. final boolean hasObjectImpl1(final AnyObjectId objectId) {
  71. if (hasObject1(objectId))
  72. return true;
  73. for (final AlternateHandle alt : myAlternates()) {
  74. if (alt.db.hasObjectImpl1(objectId))
  75. return true;
  76. }
  77. return tryAgain1() && hasObject1(objectId);
  78. }
  79. final boolean hasObjectImpl2(final String objectId) {
  80. if (hasObject2(objectId))
  81. return true;
  82. for (final AlternateHandle alt : myAlternates()) {
  83. if (alt.db.hasObjectImpl2(objectId))
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * Open an object from this database.
  90. * <p>
  91. * Alternates (if present) are searched automatically.
  92. *
  93. * @param curs
  94. * temporary working space associated with the calling thread.
  95. * @param objectId
  96. * identity of the object to open.
  97. * @return a {@link ObjectLoader} for accessing the data of the named
  98. * object, or null if the object does not exist.
  99. * @throws IOException
  100. */
  101. ObjectLoader openObject(final WindowCursor curs, final AnyObjectId objectId)
  102. throws IOException {
  103. ObjectLoader ldr;
  104. ldr = openObjectImpl1(curs, objectId);
  105. if (ldr != null)
  106. return ldr;
  107. ldr = openObjectImpl2(curs, objectId.name(), objectId);
  108. if (ldr != null)
  109. return ldr;
  110. return null;
  111. }
  112. final ObjectLoader openObjectImpl1(final WindowCursor curs,
  113. final AnyObjectId objectId) throws IOException {
  114. ObjectLoader ldr;
  115. ldr = openObject1(curs, objectId);
  116. if (ldr != null)
  117. return ldr;
  118. for (final AlternateHandle alt : myAlternates()) {
  119. ldr = alt.db.openObjectImpl1(curs, objectId);
  120. if (ldr != null)
  121. return ldr;
  122. }
  123. if (tryAgain1()) {
  124. ldr = openObject1(curs, objectId);
  125. if (ldr != null)
  126. return ldr;
  127. }
  128. return null;
  129. }
  130. final ObjectLoader openObjectImpl2(final WindowCursor curs,
  131. final String objectName, final AnyObjectId objectId)
  132. throws IOException {
  133. ObjectLoader ldr;
  134. ldr = openObject2(curs, objectName, objectId);
  135. if (ldr != null)
  136. return ldr;
  137. for (final AlternateHandle alt : myAlternates()) {
  138. ldr = alt.db.openObjectImpl2(curs, objectName, objectId);
  139. if (ldr != null)
  140. return ldr;
  141. }
  142. return null;
  143. }
  144. abstract void selectObjectRepresentation(PackWriter packer,
  145. ObjectToPack otp, WindowCursor curs) throws IOException;
  146. abstract File getDirectory();
  147. abstract AlternateHandle[] myAlternates();
  148. abstract boolean tryAgain1();
  149. abstract boolean hasObject1(AnyObjectId objectId);
  150. abstract boolean hasObject2(String objectId);
  151. abstract ObjectLoader openObject1(WindowCursor curs, AnyObjectId objectId)
  152. throws IOException;
  153. abstract ObjectLoader openObject2(WindowCursor curs, String objectName,
  154. AnyObjectId objectId) throws IOException;
  155. abstract FileObjectDatabase newCachedFileObjectDatabase();
  156. abstract int getStreamFileThreshold();
  157. static class AlternateHandle {
  158. final FileObjectDatabase db;
  159. AlternateHandle(FileObjectDatabase db) {
  160. this.db = db;
  161. }
  162. void close() {
  163. db.close();
  164. }
  165. }
  166. static class AlternateRepository extends AlternateHandle {
  167. final FileRepository repository;
  168. AlternateRepository(FileRepository r) {
  169. super(r.getObjectDatabase());
  170. repository = r;
  171. }
  172. void close() {
  173. repository.close();
  174. }
  175. }
  176. }