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

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