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.

CommitCache.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.text.MessageFormat;
  18. import java.util.ArrayList;
  19. import java.util.Calendar;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.concurrent.TimeUnit;
  25. import org.eclipse.jgit.lib.ObjectId;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.revwalk.RevCommit;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import com.gitblit.models.RefModel;
  31. import com.gitblit.models.RepositoryCommit;
  32. /**
  33. * Caches repository commits for re-use in the dashboard and activity pages.
  34. *
  35. * @author James Moger
  36. *
  37. */
  38. public class CommitCache {
  39. private static final CommitCache instance;
  40. protected final Logger logger = LoggerFactory.getLogger(getClass());
  41. protected final Map<String, ObjectCache<List<RepositoryCommit>>> cache;
  42. protected int cacheDays = -1;
  43. public static CommitCache instance() {
  44. return instance;
  45. }
  46. static {
  47. instance = new CommitCache();
  48. }
  49. protected CommitCache() {
  50. cache = new ConcurrentHashMap<String, ObjectCache<List<RepositoryCommit>>>();
  51. }
  52. /**
  53. * Returns the cutoff date for the cache. Commits after this date are cached.
  54. * Commits before this date are not cached.
  55. *
  56. * @return
  57. */
  58. public Date getCutoffDate() {
  59. final Calendar cal = Calendar.getInstance();
  60. cal.setTimeInMillis(System.currentTimeMillis());
  61. cal.set(Calendar.HOUR_OF_DAY, 0);
  62. cal.set(Calendar.MINUTE, 0);
  63. cal.set(Calendar.SECOND, 0);
  64. cal.set(Calendar.MILLISECOND, 0);
  65. cal.add(Calendar.DATE, -1*cacheDays);
  66. return cal.getTime();
  67. }
  68. /**
  69. * Sets the number of days to cache.
  70. *
  71. * @param days
  72. */
  73. public synchronized void setCacheDays(int days) {
  74. this.cacheDays = days;
  75. clear();
  76. }
  77. /**
  78. * Clears the entire commit cache.
  79. *
  80. */
  81. public void clear() {
  82. cache.clear();
  83. }
  84. /**
  85. * Clears the commit cache for a specific repository.
  86. *
  87. * @param repositoryName
  88. */
  89. public void clear(String repositoryName) {
  90. String repoKey = repositoryName.toLowerCase();
  91. ObjectCache<List<RepositoryCommit>> repoCache = cache.remove(repoKey);
  92. if (repoCache != null) {
  93. logger.info(MessageFormat.format("{0} commit cache cleared", repositoryName));
  94. }
  95. }
  96. /**
  97. * Clears the commit cache for a specific branch of a specific repository.
  98. *
  99. * @param repositoryName
  100. * @param branch
  101. */
  102. public void clear(String repositoryName, String branch) {
  103. String repoKey = repositoryName.toLowerCase();
  104. ObjectCache<List<RepositoryCommit>> repoCache = cache.get(repoKey);
  105. if (repoCache != null) {
  106. List<RepositoryCommit> commits = repoCache.remove(branch.toLowerCase());
  107. if (!ArrayUtils.isEmpty(commits)) {
  108. logger.info(MessageFormat.format("{0}:{1} commit cache cleared", repositoryName, branch));
  109. }
  110. }
  111. }
  112. /**
  113. * Get all commits for the specified repository:branch that are in the cache.
  114. *
  115. * @param repositoryName
  116. * @param repository
  117. * @param branch
  118. * @return a list of commits
  119. */
  120. public List<RepositoryCommit> getCommits(String repositoryName, Repository repository, String branch) {
  121. return getCommits(repositoryName, repository, branch, getCutoffDate());
  122. }
  123. /**
  124. * Get all commits for the specified repository:branch since a specific date.
  125. * These commits may be retrieved from the cache if the sinceDate is after
  126. * the cacheCutoffDate.
  127. *
  128. * @param repositoryName
  129. * @param repository
  130. * @param branch
  131. * @param sinceDate
  132. * @return a list of commits
  133. */
  134. public List<RepositoryCommit> getCommits(String repositoryName, Repository repository, String branch, Date sinceDate) {
  135. long start = System.nanoTime();
  136. Date cacheCutoffDate = getCutoffDate();
  137. List<RepositoryCommit> list;
  138. if (cacheDays > 0 && (sinceDate.getTime() >= cacheCutoffDate.getTime())) {
  139. // request fits within the cache window
  140. String repoKey = repositoryName.toLowerCase();
  141. if (!cache.containsKey(repoKey)) {
  142. cache.put(repoKey, new ObjectCache<List<RepositoryCommit>>());
  143. }
  144. ObjectCache<List<RepositoryCommit>> repoCache = cache.get(repoKey);
  145. String branchKey = branch.toLowerCase();
  146. RevCommit tip = JGitUtils.getCommit(repository, branch);
  147. Date tipDate = JGitUtils.getCommitDate(tip);
  148. List<RepositoryCommit> commits;
  149. if (!repoCache.hasCurrent(branchKey, tipDate)) {
  150. commits = repoCache.getObject(branchKey);
  151. if (ArrayUtils.isEmpty(commits)) {
  152. // we don't have any cached commits for this branch, reload
  153. commits = get(repositoryName, repository, branch, cacheCutoffDate);
  154. repoCache.updateObject(branchKey, tipDate, commits);
  155. logger.debug(MessageFormat.format("parsed {0} commits from {1}:{2} since {3,date,yyyy-MM-dd} in {4} msecs",
  156. commits.size(), repositoryName, branch, cacheCutoffDate, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
  157. } else {
  158. // incrementally update cache since the last cached commit
  159. ObjectId sinceCommit = commits.get(0).getId();
  160. List<RepositoryCommit> incremental = get(repositoryName, repository, branch, sinceCommit);
  161. logger.info(MessageFormat.format("incrementally added {0} commits to cache for {1}:{2} in {3} msecs",
  162. incremental.size(), repositoryName, branch, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
  163. incremental.addAll(commits);
  164. repoCache.updateObject(branchKey, tipDate, incremental);
  165. commits = incremental;
  166. }
  167. } else {
  168. // cache is current
  169. commits = repoCache.getObject(branchKey);
  170. // evict older commits outside the cache window
  171. commits = reduce(commits, cacheCutoffDate);
  172. // update cache
  173. repoCache.updateObject(branchKey, tipDate, commits);
  174. }
  175. if (sinceDate.equals(cacheCutoffDate)) {
  176. list = commits;
  177. } else {
  178. // reduce the commits to those since the specified date
  179. list = reduce(commits, sinceDate);
  180. }
  181. logger.debug(MessageFormat.format("retrieved {0} commits from cache of {1}:{2} since {3,date,yyyy-MM-dd} in {4} msecs",
  182. list.size(), repositoryName, branch, sinceDate, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
  183. } else {
  184. // not caching or request outside cache window
  185. list = get(repositoryName, repository, branch, sinceDate);
  186. logger.debug(MessageFormat.format("parsed {0} commits from {1}:{2} since {3,date,yyyy-MM-dd} in {4} msecs",
  187. list.size(), repositoryName, branch, sinceDate, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
  188. }
  189. return list;
  190. }
  191. /**
  192. * Returns a list of commits for the specified repository branch.
  193. *
  194. * @param repositoryName
  195. * @param repository
  196. * @param branch
  197. * @param sinceDate
  198. * @return a list of commits
  199. */
  200. protected List<RepositoryCommit> get(String repositoryName, Repository repository, String branch, Date sinceDate) {
  201. Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository, false);
  202. List<RepositoryCommit> commits = new ArrayList<RepositoryCommit>();
  203. for (RevCommit commit : JGitUtils.getRevLog(repository, branch, sinceDate)) {
  204. RepositoryCommit commitModel = new RepositoryCommit(repositoryName, branch, commit);
  205. commitModel.setRefs(allRefs.get(commitModel.getName()));
  206. commits.add(commitModel);
  207. }
  208. return commits;
  209. }
  210. /**
  211. * Returns a list of commits for the specified repository branch since the specified commit.
  212. *
  213. * @param repositoryName
  214. * @param repository
  215. * @param branch
  216. * @param sinceCommit
  217. * @return a list of commits
  218. */
  219. protected List<RepositoryCommit> get(String repositoryName, Repository repository, String branch, ObjectId sinceCommit) {
  220. Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository, false);
  221. List<RepositoryCommit> commits = new ArrayList<RepositoryCommit>();
  222. for (RevCommit commit : JGitUtils.getRevLog(repository, sinceCommit.getName(), branch)) {
  223. RepositoryCommit commitModel = new RepositoryCommit(repositoryName, branch, commit);
  224. commitModel.setRefs(allRefs.get(commitModel.getName()));
  225. commits.add(commitModel);
  226. }
  227. return commits;
  228. }
  229. /**
  230. * Reduces the list of commits to those since the specified date.
  231. *
  232. * @param commits
  233. * @param sinceDate
  234. * @return a list of commits
  235. */
  236. protected List<RepositoryCommit> reduce(List<RepositoryCommit> commits, Date sinceDate) {
  237. List<RepositoryCommit> filtered = new ArrayList<RepositoryCommit>();
  238. for (RepositoryCommit commit : commits) {
  239. if (commit.getCommitDate().compareTo(sinceDate) >= 0) {
  240. filtered.add(commit);
  241. }
  242. }
  243. return filtered;
  244. }
  245. }