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.3KB

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