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.

DateRevQueue.java 5.5KB

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