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

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