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.

CachedObjectDirectory.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2010, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  3. * Copyright (C) 2010, JetBrains s.r.o.
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.storage.file;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.util.Set;
  48. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.Config;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.ObjectDatabase;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  55. import org.eclipse.jgit.lib.ObjectLoader;
  56. import org.eclipse.jgit.storage.pack.ObjectToPack;
  57. import org.eclipse.jgit.storage.pack.PackWriter;
  58. import org.eclipse.jgit.util.FS;
  59. /**
  60. * The cached instance of an {@link ObjectDirectory}.
  61. * <p>
  62. * This class caches the list of loose objects in memory, so the file system is
  63. * not queried with stat calls.
  64. */
  65. class CachedObjectDirectory extends FileObjectDatabase {
  66. /**
  67. * The set that contains unpacked objects identifiers, it is created when
  68. * the cached instance is created.
  69. */
  70. private final ObjectIdSubclassMap<ObjectId> unpackedObjects = new ObjectIdSubclassMap<ObjectId>();
  71. private final ObjectDirectory wrapped;
  72. private AlternateHandle[] alts;
  73. /**
  74. * The constructor
  75. *
  76. * @param wrapped
  77. * the wrapped database
  78. */
  79. CachedObjectDirectory(ObjectDirectory wrapped) {
  80. this.wrapped = wrapped;
  81. File objects = wrapped.getDirectory();
  82. String[] fanout = objects.list();
  83. if (fanout == null)
  84. fanout = new String[0];
  85. for (String d : fanout) {
  86. if (d.length() != 2)
  87. continue;
  88. String[] entries = new File(objects, d).list();
  89. if (entries == null)
  90. continue;
  91. for (String e : entries) {
  92. if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  93. continue;
  94. try {
  95. unpackedObjects.add(ObjectId.fromString(d + e));
  96. } catch (IllegalArgumentException notAnObject) {
  97. // ignoring the file that does not represent loose object
  98. }
  99. }
  100. }
  101. }
  102. @Override
  103. public void close() {
  104. // Don't close anything.
  105. }
  106. @Override
  107. public ObjectDatabase newCachedDatabase() {
  108. return this;
  109. }
  110. @Override
  111. FileObjectDatabase newCachedFileObjectDatabase() {
  112. return this;
  113. }
  114. @Override
  115. File getDirectory() {
  116. return wrapped.getDirectory();
  117. }
  118. @Override
  119. Config getConfig() {
  120. return wrapped.getConfig();
  121. }
  122. @Override
  123. FS getFS() {
  124. return wrapped.getFS();
  125. }
  126. @Override
  127. AlternateHandle[] myAlternates() {
  128. if (alts == null) {
  129. AlternateHandle[] src = wrapped.myAlternates();
  130. alts = new AlternateHandle[src.length];
  131. for (int i = 0; i < alts.length; i++) {
  132. FileObjectDatabase s = src[i].db;
  133. alts[i] = new AlternateHandle(s.newCachedFileObjectDatabase());
  134. }
  135. }
  136. return alts;
  137. }
  138. @Override
  139. void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
  140. throws IOException {
  141. // In theory we could accelerate the loose object scan using our
  142. // unpackedObjects map, but its not worth the huge code complexity.
  143. // Scanning a single loose directory is fast enough, and this is
  144. // unlikely to be called anyway.
  145. //
  146. wrapped.resolve(matches, id);
  147. }
  148. @Override
  149. boolean tryAgain1() {
  150. return wrapped.tryAgain1();
  151. }
  152. @Override
  153. public boolean has(final AnyObjectId objectId) {
  154. return hasObjectImpl1(objectId);
  155. }
  156. @Override
  157. boolean hasObject1(AnyObjectId objectId) {
  158. return unpackedObjects.contains(objectId)
  159. || wrapped.hasObject1(objectId);
  160. }
  161. @Override
  162. ObjectLoader openObject(final WindowCursor curs,
  163. final AnyObjectId objectId) throws IOException {
  164. return openObjectImpl1(curs, objectId);
  165. }
  166. @Override
  167. ObjectLoader openObject1(WindowCursor curs, AnyObjectId objectId)
  168. throws IOException {
  169. if (unpackedObjects.contains(objectId))
  170. return wrapped.openObject2(curs, objectId.name(), objectId);
  171. return wrapped.openObject1(curs, objectId);
  172. }
  173. @Override
  174. boolean hasObject2(String objectId) {
  175. return unpackedObjects.contains(ObjectId.fromString(objectId));
  176. }
  177. @Override
  178. ObjectLoader openObject2(WindowCursor curs, String objectName,
  179. AnyObjectId objectId) throws IOException {
  180. if (unpackedObjects.contains(objectId))
  181. return wrapped.openObject2(curs, objectName, objectId);
  182. return null;
  183. }
  184. @Override
  185. long getObjectSize1(WindowCursor curs, AnyObjectId objectId) throws IOException {
  186. if (unpackedObjects.contains(objectId))
  187. return wrapped.getObjectSize2(curs, objectId.name(), objectId);
  188. return wrapped.getObjectSize1(curs, objectId);
  189. }
  190. @Override
  191. long getObjectSize2(WindowCursor curs, String objectName, AnyObjectId objectId)
  192. throws IOException {
  193. if (unpackedObjects.contains(objectId))
  194. return wrapped.getObjectSize2(curs, objectName, objectId);
  195. return -1;
  196. }
  197. @Override
  198. InsertLooseObjectResult insertUnpackedObject(File tmp, ObjectId objectId,
  199. boolean createDuplicate) throws IOException {
  200. InsertLooseObjectResult result = wrapped.insertUnpackedObject(tmp,
  201. objectId, createDuplicate);
  202. switch (result) {
  203. case INSERTED:
  204. case EXISTS_LOOSE:
  205. if (!unpackedObjects.contains(objectId))
  206. unpackedObjects.add(objectId);
  207. break;
  208. case EXISTS_PACKED:
  209. case FAILURE:
  210. break;
  211. }
  212. return result;
  213. }
  214. @Override
  215. PackFile openPack(File pack, File idx) throws IOException {
  216. return wrapped.openPack(pack, idx);
  217. }
  218. @Override
  219. void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
  220. WindowCursor curs) throws IOException {
  221. wrapped.selectObjectRepresentation(packer, otp, curs);
  222. }
  223. }