| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [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 static com.google.common.base.Preconditions.checkArgument; |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | |
| 21 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 22 | import org.eclipse.jgit.errors.MissingObjectException; |
| 23 | import org.eclipse.jgit.errors.RevWalkException; |
| 24 | import org.eclipse.jgit.lib.ObjectId; |
| 25 | import org.eclipse.jgit.revwalk.RevCommit; |
| 26 | import org.eclipse.jgit.revwalk.RevWalk; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.util.ArrayDeque; |
| 30 | import java.util.Deque; |
| 31 | import java.util.Iterator; |
| 32 | |
| 33 | import javax.annotation.Nullable; |
| 34 | |
| 35 | /** |
| 36 | * Wrapper around {@link RevWalk} that paginates for Gitiles. |
| 37 | * |
| 38 | * A single page of a shortlog is defined by a revision range, such as "master" |
| 39 | * or "master..next", a page size, and a start commit, such as "c0ffee". The |
| 40 | * distance between the first commit in the walk ("next") and the first commit |
| 41 | * in the page may be arbitrarily long, but in order to present the commit list |
| 42 | * in a stable way, we must always start from the first commit in the walk. This |
| 43 | * is because there may be arbitrary merge commits between "c0ffee" and "next" |
| 44 | * that effectively insert arbitrary commits into the history starting from |
| 45 | * "c0ffee". |
| 46 | */ |
| 47 | class Paginator implements Iterable<RevCommit> { |
| 48 | private final RevWalk walk; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | private final int limit; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 50 | private final ObjectId prevStart; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 52 | private RevCommit first; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 53 | private boolean done; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | private int n; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 55 | private ObjectId nextStart; |
| 56 | |
| 57 | /** |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 58 | * Construct a paginator and walk eagerly to the first returned commit. |
| 59 | * |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 60 | * @param walk revision walk. |
| 61 | * @param limit page size. |
| 62 | * @param start commit at which to start the walk, or null to start at the |
| 63 | * beginning. |
| 64 | */ |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 65 | Paginator(RevWalk walk, int limit, @Nullable ObjectId start) throws MissingObjectException, |
| 66 | IncorrectObjectTypeException, IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 67 | this.walk = checkNotNull(walk, "walk"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 68 | checkArgument(limit > 0, "limit must be positive: %s", limit); |
| 69 | this.limit = limit; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | Deque<ObjectId> prevBuffer = new ArrayDeque<ObjectId>(start != null ? limit : 0); |
| 73 | while (true) { |
| 74 | RevCommit commit = walk.next(); |
| 75 | if (commit == null) { |
| 76 | done = true; |
| 77 | break; |
| 78 | } |
| 79 | if (start == null || start.equals(commit)) { |
| 80 | first = commit; |
| 81 | break; |
| 82 | } |
| 83 | if (prevBuffer.size() == limit) { |
| 84 | prevBuffer.remove(); |
| 85 | } |
| 86 | prevBuffer.add(commit); |
| 87 | } |
| 88 | prevStart = prevBuffer.pollFirst(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the next element in this page of the walk. |
| 93 | * |
| 94 | * @return the next element, or null if the walk is finished. |
| 95 | * |
| 96 | * @throws MissingObjectException See {@link RevWalk#next()}. |
| 97 | * @throws IncorrectObjectTypeException See {@link RevWalk#next()}. |
| 98 | * @throws IOException See {@link RevWalk#next()}. |
| 99 | */ |
| 100 | public RevCommit next() throws MissingObjectException, IncorrectObjectTypeException, |
| 101 | IOException { |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 102 | if (done) { |
| 103 | return null; |
| 104 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 105 | RevCommit commit; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 106 | if (first != null) { |
| 107 | commit = first; |
| 108 | first = null; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 109 | } else { |
| 110 | commit = walk.next(); |
| 111 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 112 | if (++n == limit) { |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 113 | nextStart = walk.next(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 114 | done = true; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 115 | } else if (commit == null) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 116 | done = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 117 | } |
| 118 | return commit; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return the ID at the start of the page of results preceding this one, or |
| 123 | * null if this is the first page. |
| 124 | */ |
| 125 | public ObjectId getPreviousStart() { |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 126 | return prevStart; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return the ID at the start of the page of results after this one, or null |
| 131 | * if this is the last page. |
| 132 | */ |
| 133 | public ObjectId getNextStart() { |
| 134 | checkState(done, "getNextStart() invalid before walk done"); |
| 135 | return nextStart; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return an iterator over the commits in this walk. |
| 140 | * @throws RevWalkException if an error occurred, wrapping the checked |
| 141 | * exception from {@link #next()}. |
| 142 | */ |
| 143 | @Override |
| 144 | public Iterator<RevCommit> iterator() { |
| 145 | return new Iterator<RevCommit>() { |
| 146 | RevCommit next = nextUnchecked(); |
| 147 | |
| 148 | @Override |
| 149 | public boolean hasNext() { |
| 150 | return next != null; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public RevCommit next() { |
| 155 | RevCommit r = next; |
| 156 | next = nextUnchecked(); |
| 157 | return r; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void remove() { |
| 162 | throw new UnsupportedOperationException(); |
| 163 | } |
| 164 | }; |
| 165 | } |
| 166 | |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 167 | public int getLimit() { |
| 168 | return limit; |
| 169 | } |
| 170 | |
| 171 | public RevWalk getWalk() { |
| 172 | return walk; |
| 173 | } |
| 174 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 175 | private RevCommit nextUnchecked() { |
| 176 | try { |
| 177 | return next(); |
| 178 | } catch (MissingObjectException e) { |
| 179 | throw new RevWalkException(e); |
| 180 | } catch (IncorrectObjectTypeException e) { |
| 181 | throw new RevWalkException(e); |
| 182 | } catch (IOException e) { |
| 183 | throw new RevWalkException(e); |
| 184 | } |
| 185 | } |
| 186 | } |