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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Constants;
  51. import org.eclipse.jgit.lib.ObjectDatabase;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  54. import org.eclipse.jgit.lib.ObjectLoader;
  55. import org.eclipse.jgit.storage.pack.ObjectToPack;
  56. import org.eclipse.jgit.storage.pack.PackWriter;
  57. /**
  58. * The cached instance of an {@link ObjectDirectory}.
  59. * <p>
  60. * This class caches the list of loose objects in memory, so the file system is
  61. * not queried with stat calls.
  62. */
  63. class CachedObjectDirectory extends FileObjectDatabase {
  64. /**
  65. * The set that contains unpacked objects identifiers, it is created when
  66. * the cached instance is created.
  67. */
  68. private final ObjectIdSubclassMap<ObjectId> unpackedObjects = new ObjectIdSubclassMap<ObjectId>();
  69. private final ObjectDirectory wrapped;
  70. private AlternateHandle[] alts;
  71. /**
  72. * The constructor
  73. *
  74. * @param wrapped
  75. * the wrapped database
  76. */
  77. CachedObjectDirectory(ObjectDirectory wrapped) {
  78. this.wrapped = wrapped;
  79. File objects = wrapped.getDirectory();
  80. String[] fanout = objects.list();
  81. if (fanout == null)
  82. fanout = new String[0];
  83. for (String d : fanout) {
  84. if (d.length() != 2)
  85. continue;
  86. String[] entries = new File(objects, d).list();
  87. if (entries == null)
  88. continue;
  89. for (String e : entries) {
  90. if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  91. continue;
  92. try {
  93. unpackedObjects.add(ObjectId.fromString(d + e));
  94. } catch (IllegalArgumentException notAnObject) {
  95. // ignoring the file that does not represent loose object
  96. }
  97. }
  98. }
  99. }
  100. @Override
  101. public void close() {
  102. // Don't close anything.
  103. }
  104. @Override
  105. public ObjectDirectoryInserter newInserter() {
  106. return wrapped.newInserter();
  107. }
  108. @Override
  109. public ObjectDatabase newCachedDatabase() {
  110. return this;
  111. }
  112. @Override
  113. FileObjectDatabase newCachedFileObjectDatabase() {
  114. return this;
  115. }
  116. @Override
  117. File getDirectory() {
  118. return wrapped.getDirectory();
  119. }
  120. @Override
  121. AlternateHandle[] myAlternates() {
  122. if (alts == null) {
  123. AlternateHandle[] src = wrapped.myAlternates();
  124. alts = new AlternateHandle[src.length];
  125. for (int i = 0; i < alts.length; i++) {
  126. FileObjectDatabase s = src[i].db;
  127. alts[i] = new AlternateHandle(s.newCachedFileObjectDatabase());
  128. }
  129. }
  130. return alts;
  131. }
  132. @Override
  133. void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
  134. throws IOException {
  135. // In theory we could accelerate the loose object scan using our
  136. // unpackedObjects map, but its not worth the huge code complexity.
  137. // Scanning a single loose directory is fast enough, and this is
  138. // unlikely to be called anyway.
  139. //
  140. wrapped.resolve(matches, id);
  141. }
  142. @Override
  143. boolean tryAgain1() {
  144. return wrapped.tryAgain1();
  145. }
  146. @Override
  147. public boolean has(final AnyObjectId objectId) {
  148. return hasObjectImpl1(objectId);
  149. }
  150. @Override
  151. boolean hasObject1(AnyObjectId objectId) {
  152. return unpackedObjects.contains(objectId)
  153. || wrapped.hasObject1(objectId);
  154. }
  155. @Override
  156. ObjectLoader openObject(final WindowCursor curs,
  157. final AnyObjectId objectId) throws IOException {
  158. return openObjectImpl1(curs, objectId);
  159. }
  160. @Override
  161. ObjectLoader openObject1(WindowCursor curs, AnyObjectId objectId)
  162. throws IOException {
  163. if (unpackedObjects.contains(objectId))
  164. return wrapped.openObject2(curs, objectId.name(), objectId);
  165. return wrapped.openObject1(curs, objectId);
  166. }
  167. @Override
  168. boolean hasObject2(String objectId) {
  169. // This method should never be invoked.
  170. throw new UnsupportedOperationException();
  171. }
  172. @Override
  173. ObjectLoader openObject2(WindowCursor curs, String objectName,
  174. AnyObjectId objectId) throws IOException {
  175. // This method should never be invoked.
  176. throw new UnsupportedOperationException();
  177. }
  178. @Override
  179. long getObjectSize1(WindowCursor curs, AnyObjectId objectId) throws IOException {
  180. if (unpackedObjects.contains(objectId))
  181. return wrapped.getObjectSize2(curs, objectId.name(), objectId);
  182. return wrapped.getObjectSize1(curs, objectId);
  183. }
  184. @Override
  185. long getObjectSize2(WindowCursor curs, String objectName, AnyObjectId objectId)
  186. throws IOException {
  187. // This method should never be invoked.
  188. throw new UnsupportedOperationException();
  189. }
  190. @Override
  191. InsertLooseObjectResult insertUnpackedObject(File tmp, ObjectId objectId,
  192. boolean createDuplicate) {
  193. InsertLooseObjectResult result = wrapped.insertUnpackedObject(tmp,
  194. objectId, createDuplicate);
  195. switch (result) {
  196. case INSERTED:
  197. case EXISTS_LOOSE:
  198. if (!unpackedObjects.contains(objectId))
  199. unpackedObjects.add(objectId);
  200. break;
  201. case EXISTS_PACKED:
  202. case FAILURE:
  203. break;
  204. }
  205. return result;
  206. }
  207. @Override
  208. void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
  209. WindowCursor curs) throws IOException {
  210. wrapped.selectObjectRepresentation(packer, otp, curs);
  211. }
  212. }