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.

MergedReftable.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (C) 2017, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.reftable;
  44. import java.io.IOException;
  45. import java.util.List;
  46. import java.util.PriorityQueue;
  47. import org.eclipse.jgit.lib.AnyObjectId;
  48. import org.eclipse.jgit.lib.Ref;
  49. import org.eclipse.jgit.lib.ReflogEntry;
  50. /**
  51. * Merges multiple reference tables together.
  52. * <p>
  53. * A {@link MergedReftable} merge-joins multiple {@link ReftableReader} on the
  54. * fly. Tables higher/later in the stack shadow lower/earlier tables, hiding
  55. * references that been updated/replaced.
  56. * <p>
  57. * By default deleted references are skipped and not returned to the caller.
  58. * {@link #setIncludeDeletes(boolean)} can be used to modify this behavior if
  59. * the caller needs to preserve deletions during partial compaction.
  60. * <p>
  61. * A {@code MergedReftable} is not thread-safe.
  62. */
  63. public class MergedReftable extends Reftable {
  64. private final Reftable[] tables;
  65. /**
  66. * Initialize a merged table reader.
  67. * <p>
  68. * The tables in {@code tableStack} will be closed when this
  69. * {@code MergedReftable} is closed.
  70. *
  71. * @param tableStack
  72. * stack of tables to read from. The base of the stack is at
  73. * index 0, the most recent should be at the top of the stack at
  74. * {@code tableStack.size() - 1}. The top of the stack (higher
  75. * index) shadows the base of the stack (lower index).
  76. */
  77. public MergedReftable(List<Reftable> tableStack) {
  78. tables = tableStack.toArray(new Reftable[0]);
  79. // Tables must expose deletes to this instance to correctly
  80. // shadow references from lower tables.
  81. for (Reftable t : tables) {
  82. t.setIncludeDeletes(true);
  83. }
  84. }
  85. @Override
  86. public RefCursor allRefs() throws IOException {
  87. MergedRefCursor m = new MergedRefCursor();
  88. for (int i = 0; i < tables.length; i++) {
  89. m.add(new RefQueueEntry(tables[i].allRefs(), i));
  90. }
  91. return m;
  92. }
  93. @Override
  94. public RefCursor seekRef(String name) throws IOException {
  95. MergedRefCursor m = new MergedRefCursor();
  96. for (int i = 0; i < tables.length; i++) {
  97. m.add(new RefQueueEntry(tables[i].seekRef(name), i));
  98. }
  99. return m;
  100. }
  101. @Override
  102. public RefCursor byObjectId(AnyObjectId name) throws IOException {
  103. MergedRefCursor m = new MergedRefCursor();
  104. for (int i = 0; i < tables.length; i++) {
  105. m.add(new RefQueueEntry(tables[i].byObjectId(name), i));
  106. }
  107. return m;
  108. }
  109. @Override
  110. public LogCursor allLogs() throws IOException {
  111. MergedLogCursor m = new MergedLogCursor();
  112. for (int i = 0; i < tables.length; i++) {
  113. m.add(new LogQueueEntry(tables[i].allLogs(), i));
  114. }
  115. return m;
  116. }
  117. @Override
  118. public LogCursor seekLog(String refName, long updateIdx)
  119. throws IOException {
  120. MergedLogCursor m = new MergedLogCursor();
  121. for (int i = 0; i < tables.length; i++) {
  122. m.add(new LogQueueEntry(tables[i].seekLog(refName, updateIdx), i));
  123. }
  124. return m;
  125. }
  126. @Override
  127. public void close() throws IOException {
  128. for (Reftable t : tables) {
  129. t.close();
  130. }
  131. }
  132. int queueSize() {
  133. return Math.max(1, tables.length);
  134. }
  135. private class MergedRefCursor extends RefCursor {
  136. private final PriorityQueue<RefQueueEntry> queue;
  137. private RefQueueEntry head;
  138. private Ref ref;
  139. private long updateIndex;
  140. MergedRefCursor() {
  141. queue = new PriorityQueue<>(queueSize(), RefQueueEntry::compare);
  142. }
  143. void add(RefQueueEntry t) throws IOException {
  144. // Common case is many iterations over the same RefQueueEntry
  145. // for the bottom of the stack (scanning all refs). Its almost
  146. // always less than the top of the queue. Avoid the queue's
  147. // O(log N) insertion and removal costs for this common case.
  148. if (!t.rc.next()) {
  149. t.rc.close();
  150. } else if (head == null) {
  151. RefQueueEntry p = queue.peek();
  152. if (p == null || RefQueueEntry.compare(t, p) < 0) {
  153. head = t;
  154. } else {
  155. head = queue.poll();
  156. queue.add(t);
  157. }
  158. } else if (RefQueueEntry.compare(t, head) > 0) {
  159. queue.add(t);
  160. } else {
  161. queue.add(head);
  162. head = t;
  163. }
  164. }
  165. @Override
  166. public boolean next() throws IOException {
  167. for (;;) {
  168. RefQueueEntry t = poll();
  169. if (t == null) {
  170. return false;
  171. }
  172. ref = t.rc.getRef();
  173. updateIndex = t.rc.getUpdateIndex();
  174. boolean include = includeDeletes || !t.rc.wasDeleted();
  175. skipShadowedRefs(ref.getName());
  176. add(t);
  177. if (include) {
  178. return true;
  179. }
  180. }
  181. }
  182. private RefQueueEntry poll() {
  183. RefQueueEntry e = head;
  184. if (e != null) {
  185. head = null;
  186. return e;
  187. }
  188. return queue.poll();
  189. }
  190. private void skipShadowedRefs(String name) throws IOException {
  191. for (;;) {
  192. RefQueueEntry t = head != null ? head : queue.peek();
  193. if (t != null && name.equals(t.name())) {
  194. add(poll());
  195. } else {
  196. break;
  197. }
  198. }
  199. }
  200. @Override
  201. public Ref getRef() {
  202. return ref;
  203. }
  204. @Override
  205. public long getUpdateIndex() {
  206. return updateIndex;
  207. }
  208. @Override
  209. public void close() {
  210. if (head != null) {
  211. head.rc.close();
  212. head = null;
  213. }
  214. while (!queue.isEmpty()) {
  215. queue.remove().rc.close();
  216. }
  217. }
  218. }
  219. private static class RefQueueEntry {
  220. static int compare(RefQueueEntry a, RefQueueEntry b) {
  221. int cmp = a.name().compareTo(b.name());
  222. if (cmp == 0) {
  223. // higher updateIndex shadows lower updateIndex.
  224. cmp = Long.signum(b.updateIndex() - a.updateIndex());
  225. }
  226. if (cmp == 0) {
  227. // higher index shadows lower index, so higher index first.
  228. cmp = b.stackIdx - a.stackIdx;
  229. }
  230. return cmp;
  231. }
  232. final RefCursor rc;
  233. final int stackIdx;
  234. RefQueueEntry(RefCursor rc, int stackIdx) {
  235. this.rc = rc;
  236. this.stackIdx = stackIdx;
  237. }
  238. String name() {
  239. return rc.getRef().getName();
  240. }
  241. long updateIndex() {
  242. return rc.getUpdateIndex();
  243. }
  244. }
  245. private class MergedLogCursor extends LogCursor {
  246. private final PriorityQueue<LogQueueEntry> queue;
  247. private String refName;
  248. private long updateIndex;
  249. private ReflogEntry entry;
  250. MergedLogCursor() {
  251. queue = new PriorityQueue<>(queueSize(), LogQueueEntry::compare);
  252. }
  253. void add(LogQueueEntry t) throws IOException {
  254. if (t.lc.next()) {
  255. queue.add(t);
  256. } else {
  257. t.lc.close();
  258. }
  259. }
  260. @Override
  261. public boolean next() throws IOException {
  262. for (;;) {
  263. LogQueueEntry t = queue.poll();
  264. if (t == null) {
  265. return false;
  266. }
  267. refName = t.lc.getRefName();
  268. updateIndex = t.lc.getUpdateIndex();
  269. entry = t.lc.getReflogEntry();
  270. boolean include = includeDeletes || entry != null;
  271. skipShadowed(refName, updateIndex);
  272. add(t);
  273. if (include) {
  274. return true;
  275. }
  276. }
  277. }
  278. private void skipShadowed(String name, long index) throws IOException {
  279. for (;;) {
  280. LogQueueEntry t = queue.peek();
  281. if (t != null && name.equals(t.name()) && index == t.index()) {
  282. add(queue.remove());
  283. } else {
  284. break;
  285. }
  286. }
  287. }
  288. @Override
  289. public String getRefName() {
  290. return refName;
  291. }
  292. @Override
  293. public long getUpdateIndex() {
  294. return updateIndex;
  295. }
  296. @Override
  297. public ReflogEntry getReflogEntry() {
  298. return entry;
  299. }
  300. @Override
  301. public void close() {
  302. while (!queue.isEmpty()) {
  303. queue.remove().lc.close();
  304. }
  305. }
  306. }
  307. private static class LogQueueEntry {
  308. static int compare(LogQueueEntry a, LogQueueEntry b) {
  309. int cmp = a.name().compareTo(b.name());
  310. if (cmp == 0) {
  311. // higher update index sorts first.
  312. cmp = Long.signum(b.index() - a.index());
  313. }
  314. if (cmp == 0) {
  315. // higher index comes first.
  316. cmp = b.stackIdx - a.stackIdx;
  317. }
  318. return cmp;
  319. }
  320. final LogCursor lc;
  321. final int stackIdx;
  322. LogQueueEntry(LogCursor lc, int stackIdx) {
  323. this.lc = lc;
  324. this.stackIdx = stackIdx;
  325. }
  326. String name() {
  327. return lc.getRefName();
  328. }
  329. long index() {
  330. return lc.getUpdateIndex();
  331. }
  332. }
  333. }