Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

FileObjectDatabase.java 8.4KB

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