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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.internal.storage.file;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.util.Collection;
  48. import java.util.Set;
  49. import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
  50. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  51. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  52. import org.eclipse.jgit.lib.AnyObjectId;
  53. import org.eclipse.jgit.lib.Config;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.ObjectDatabase;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  58. import org.eclipse.jgit.lib.ObjectLoader;
  59. import org.eclipse.jgit.util.FS;
  60. /**
  61. * The cached instance of an {@link ObjectDirectory}.
  62. * <p>
  63. * This class caches the list of loose objects in memory, so the file system is
  64. * not queried with stat calls.
  65. */
  66. class CachedObjectDirectory extends FileObjectDatabase {
  67. /**
  68. * The set that contains unpacked objects identifiers, it is created when
  69. * the cached instance is created.
  70. */
  71. private ObjectIdOwnerMap<UnpackedObjectId> unpackedObjects;
  72. private final ObjectDirectory wrapped;
  73. private CachedObjectDirectory[] alts;
  74. /**
  75. * The constructor
  76. *
  77. * @param wrapped
  78. * the wrapped database
  79. */
  80. CachedObjectDirectory(ObjectDirectory wrapped) {
  81. this.wrapped = wrapped;
  82. this.unpackedObjects = scanLoose();
  83. }
  84. private ObjectIdOwnerMap<UnpackedObjectId> scanLoose() {
  85. ObjectIdOwnerMap<UnpackedObjectId> m = new ObjectIdOwnerMap<UnpackedObjectId>();
  86. File objects = wrapped.getDirectory();
  87. String[] fanout = objects.list();
  88. if (fanout == null)
  89. return m;
  90. for (String d : fanout) {
  91. if (d.length() != 2)
  92. continue;
  93. String[] entries = new File(objects, d).list();
  94. if (entries == null)
  95. continue;
  96. for (String e : entries) {
  97. if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  98. continue;
  99. try {
  100. ObjectId id = ObjectId.fromString(d + e);
  101. m.add(new UnpackedObjectId(id));
  102. } catch (IllegalArgumentException notAnObject) {
  103. // ignoring the file that does not represent loose object
  104. }
  105. }
  106. }
  107. return m;
  108. }
  109. @Override
  110. public void close() {
  111. // Don't close anything.
  112. }
  113. @Override
  114. public ObjectDatabase newCachedDatabase() {
  115. return this;
  116. }
  117. @Override
  118. File getDirectory() {
  119. return wrapped.getDirectory();
  120. }
  121. @Override
  122. File fileFor(AnyObjectId id) {
  123. return wrapped.fileFor(id);
  124. }
  125. @Override
  126. Config getConfig() {
  127. return wrapped.getConfig();
  128. }
  129. @Override
  130. FS getFS() {
  131. return wrapped.getFS();
  132. }
  133. @Override
  134. Set<ObjectId> getShallowCommits() throws IOException {
  135. return wrapped.getShallowCommits();
  136. }
  137. private CachedObjectDirectory[] myAlternates() {
  138. if (alts == null) {
  139. ObjectDirectory.AlternateHandle[] src = wrapped.myAlternates();
  140. alts = new CachedObjectDirectory[src.length];
  141. for (int i = 0; i < alts.length; i++)
  142. alts[i] = src[i].db.newCachedFileObjectDatabase();
  143. }
  144. return alts;
  145. }
  146. @Override
  147. void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
  148. throws IOException {
  149. // In theory we could accelerate the loose object scan using our
  150. // unpackedObjects map, but its not worth the huge code complexity.
  151. // Scanning a single loose directory is fast enough, and this is
  152. // unlikely to be called anyway.
  153. //
  154. wrapped.resolve(matches, id);
  155. }
  156. @Override
  157. public boolean has(final AnyObjectId objectId) throws IOException {
  158. if (unpackedObjects.contains(objectId))
  159. return true;
  160. if (wrapped.hasPackedObject(objectId))
  161. return true;
  162. for (CachedObjectDirectory alt : myAlternates()) {
  163. if (alt.has(objectId))
  164. return true;
  165. }
  166. return false;
  167. }
  168. @Override
  169. ObjectLoader openObject(final WindowCursor curs,
  170. final AnyObjectId objectId) throws IOException {
  171. ObjectLoader ldr = openLooseObject(curs, objectId);
  172. if (ldr != null)
  173. return ldr;
  174. ldr = wrapped.openPackedObject(curs, objectId);
  175. if (ldr != null)
  176. return ldr;
  177. for (CachedObjectDirectory alt : myAlternates()) {
  178. ldr = alt.openObject(curs, objectId);
  179. if (ldr != null)
  180. return ldr;
  181. }
  182. return null;
  183. }
  184. @Override
  185. long getObjectSize(WindowCursor curs, AnyObjectId objectId)
  186. throws IOException {
  187. // Object size is unlikely to be requested from contexts using
  188. // this type. Don't bother trying to accelerate the lookup.
  189. return wrapped.getObjectSize(curs, objectId);
  190. }
  191. @Override
  192. ObjectLoader openLooseObject(WindowCursor curs, AnyObjectId id)
  193. throws IOException {
  194. if (unpackedObjects.contains(id)) {
  195. ObjectLoader ldr = wrapped.openLooseObject(curs, id);
  196. if (ldr != null)
  197. return ldr;
  198. unpackedObjects = scanLoose();
  199. }
  200. return null;
  201. }
  202. @Override
  203. InsertLooseObjectResult insertUnpackedObject(File tmp, ObjectId objectId,
  204. boolean createDuplicate) throws IOException {
  205. InsertLooseObjectResult result = wrapped.insertUnpackedObject(tmp,
  206. objectId, createDuplicate);
  207. switch (result) {
  208. case INSERTED:
  209. case EXISTS_LOOSE:
  210. unpackedObjects.addIfAbsent(new UnpackedObjectId(objectId));
  211. break;
  212. case EXISTS_PACKED:
  213. case FAILURE:
  214. break;
  215. }
  216. return result;
  217. }
  218. @Override
  219. PackFile openPack(File pack) throws IOException {
  220. return wrapped.openPack(pack);
  221. }
  222. @Override
  223. void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
  224. WindowCursor curs) throws IOException {
  225. wrapped.selectObjectRepresentation(packer, otp, curs);
  226. }
  227. @Override
  228. Collection<PackFile> getPacks() {
  229. return wrapped.getPacks();
  230. }
  231. private static class UnpackedObjectId extends ObjectIdOwnerMap.Entry {
  232. UnpackedObjectId(AnyObjectId id) {
  233. super(id);
  234. }
  235. }
  236. }