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.0KB

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