View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>,
3    * Copyright (C) 2013, Gustaf Lundh <gustaf.lundh@sonymobile.com>
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  
45  package org.eclipse.jgit.revwalk;
46  
47  import java.io.IOException;
48  
49  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
50  import org.eclipse.jgit.errors.MissingObjectException;
51  
52  /** A queue of commits sorted by commit time order. */
53  public class DateRevQueue extends AbstractRevQueue {
54  	private static final int REBUILD_INDEX_COUNT = 1000;
55  
56  	private Entry head;
57  
58  	private Entry free;
59  
60  	private int inQueue;
61  
62  	private int sinceLastIndex;
63  
64  	private Entry[] index;
65  
66  	private int first;
67  
68  	private int last = -1;
69  
70  	/** Create an empty date queue. */
71  	public DateRevQueue() {
72  		super();
73  	}
74  
75  	DateRevQueue(final Generator s) throws MissingObjectException,
76  			IncorrectObjectTypeException, IOException {
77  		for (;;) {
78  			final RevCommit c = s.next();
79  			if (c == null)
80  				break;
81  			add(c);
82  		}
83  	}
84  
85  	@Override
86  	public void add(final RevCommit c) {
87  		sinceLastIndex++;
88  		if (++inQueue > REBUILD_INDEX_COUNT
89  				&& sinceLastIndex > REBUILD_INDEX_COUNT)
90  			buildIndex();
91  
92  		Entry q = head;
93  		final long when = c.commitTime;
94  
95  		if (first <= last && index[first].commit.commitTime > when) {
96  			int low = first, high = last;
97  			while (low <= high) {
98  				int mid = (low + high) >>> 1;
99  				int t = index[mid].commit.commitTime;
100 				if (t < when)
101 					high = mid - 1;
102 				else if (t > when)
103 					low = mid + 1;
104 				else {
105 					low = mid - 1;
106 					break;
107 				}
108 			}
109 			low = Math.min(low, high);
110 			while (low > first && when == index[low].commit.commitTime)
111 				--low;
112 			q = index[low];
113 		}
114 
115 		final Entry n = newEntry(c);
116 		if (q == null || (q == head && when > q.commit.commitTime)) {
117 			n.next = q;
118 			head = n;
119 		} else {
120 			Entry p = q.next;
121 			while (p != null && p.commit.commitTime > when) {
122 				q = p;
123 				p = q.next;
124 			}
125 			n.next = q.next;
126 			q.next = n;
127 		}
128 	}
129 
130 	@Override
131 	public RevCommit next() {
132 		final Entry q = head;
133 		if (q == null)
134 			return null;
135 
136 		if (index != null && q == index[first])
137 			index[first++] = null;
138 		inQueue--;
139 
140 		head = q.next;
141 		freeEntry(q);
142 		return q.commit;
143 	}
144 
145 	private void buildIndex() {
146 		sinceLastIndex = 0;
147 		first = 0;
148 		index = new Entry[inQueue / 100 + 1];
149 		int qi = 0, ii = 0;
150 		for (Entry q = head; q != null; q = q.next) {
151 			if (++qi % 100 == 0)
152 				index[ii++] = q;
153 		}
154 		last = ii - 1;
155 	}
156 
157 	/**
158 	 * Peek at the next commit, without removing it.
159 	 *
160 	 * @return the next available commit; null if there are no commits left.
161 	 */
162 	public RevCommit peek() {
163 		return head != null ? head.commit : null;
164 	}
165 
166 	@Override
167 	public void clear() {
168 		head = null;
169 		free = null;
170 		index = null;
171 		inQueue = 0;
172 		sinceLastIndex = 0;
173 		last = -1;
174 	}
175 
176 	@Override
177 	boolean everbodyHasFlag(final int f) {
178 		for (Entry q = head; q != null; q = q.next) {
179 			if ((q.commit.flags & f) == 0)
180 				return false;
181 		}
182 		return true;
183 	}
184 
185 	@Override
186 	boolean anybodyHasFlag(final int f) {
187 		for (Entry q = head; q != null; q = q.next) {
188 			if ((q.commit.flags & f) != 0)
189 				return true;
190 		}
191 		return false;
192 	}
193 
194 	@Override
195 	int outputType() {
196 		return outputType | SORT_COMMIT_TIME_DESC;
197 	}
198 
199 	@Override
200 	public String toString() {
201 		final StringBuilder s = new StringBuilder();
202 		for (Entry q = head; q != null; q = q.next)
203 			describe(s, q.commit);
204 		return s.toString();
205 	}
206 
207 	private Entry newEntry(final RevCommit c) {
208 		Entry r = free;
209 		if (r == null)
210 			r = new Entry();
211 		else
212 			free = r.next;
213 		r.commit = c;
214 		return r;
215 	}
216 
217 	private void freeEntry(final Entry e) {
218 		e.next = free;
219 		free = e;
220 	}
221 
222 	static class Entry {
223 		Entry next;
224 
225 		RevCommit commit;
226 	}
227 }