| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import com.google.common.base.Throwables; |
| 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | |
| 21 | import org.eclipse.jgit.lib.Constants; |
| 22 | import org.eclipse.jgit.lib.ObjectId; |
| 23 | import org.eclipse.jgit.lib.PersonIdent; |
| 24 | import org.eclipse.jgit.revwalk.RevCommit; |
| 25 | import org.eclipse.jgit.revwalk.RevObject; |
| 26 | import org.eclipse.jgit.revwalk.RevTag; |
| 27 | import org.eclipse.jgit.revwalk.RevWalk; |
| 28 | |
| 29 | import java.io.IOException; |
| 30 | import java.util.concurrent.Callable; |
| 31 | import java.util.concurrent.ExecutionException; |
| 32 | |
| 33 | /** |
| 34 | * Cache of the time associated with Git objects. |
| 35 | * <p> |
| 36 | * Uses the time as stored in annotated tags if available, or else the commit |
| 37 | * time of the tagged commit. Non-commits are given {@link Long#MIN_VALUE}, |
| 38 | * rather than searching for occurrences in the entire repository. |
| 39 | */ |
| 40 | public class TimeCache { |
| 41 | public static CacheBuilder<Object, Object> newBuilder() { |
| 42 | return CacheBuilder.newBuilder().maximumSize(10 << 10); |
| 43 | } |
| 44 | |
| 45 | private final Cache<ObjectId, Long> cache; |
| 46 | |
| 47 | public TimeCache() { |
| 48 | this(newBuilder()); |
| 49 | } |
| 50 | |
| 51 | public TimeCache(CacheBuilder<Object, Object> builder) { |
| 52 | this.cache = builder.build(); |
| 53 | } |
| 54 | |
| 55 | public Cache<?, ?> getCache() { |
| 56 | return cache; |
| 57 | } |
| 58 | |
| 59 | Long getTime(final RevWalk walk, final ObjectId id) throws IOException { |
| 60 | try { |
| 61 | return cache.get(id, new Callable<Long>() { |
| 62 | @Override |
| 63 | public Long call() throws IOException { |
| 64 | RevObject o = walk.parseAny(id); |
| 65 | while (o instanceof RevTag) { |
| 66 | RevTag tag = (RevTag) o; |
| 67 | PersonIdent ident = tag.getTaggerIdent(); |
| 68 | if (ident != null) { |
| 69 | return ident.getWhen().getTime() / 1000; |
| 70 | } |
| 71 | o = tag.getObject(); |
| 72 | walk.parseHeaders(o); |
| 73 | } |
| 74 | if (o.getType() == Constants.OBJ_COMMIT) { |
| 75 | return Long.valueOf(((RevCommit) o).getCommitTime()); |
| 76 | } |
| 77 | return Long.MIN_VALUE; |
| 78 | } |
| 79 | }); |
| 80 | } catch (ExecutionException e) { |
| 81 | Throwables.propagateIfInstanceOf(e.getCause(), IOException.class); |
| 82 | throw new IOException(e); |
| 83 | } |
| 84 | } |
| 85 | } |