MergedReftable.java

  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 org.eclipse.jgit.internal.storage.reftable.MergedReftable}
  54.  * merge-joins multiple
  55.  * {@link org.eclipse.jgit.internal.storage.reftable.ReftableReader} on the fly.
  56.  * Tables higher/later in the stack shadow lower/earlier tables, hiding
  57.  * references that been updated/replaced.
  58.  * <p>
  59.  * By default deleted references are skipped and not returned to the caller.
  60.  * {@link #setIncludeDeletes(boolean)} can be used to modify this behavior if
  61.  * the caller needs to preserve deletions during partial compaction.
  62.  * <p>
  63.  * A {@code MergedReftable} is not thread-safe.
  64.  */
  65. public class MergedReftable extends Reftable {
  66.     private final Reftable[] tables;

  67.     /**
  68.      * Initialize a merged table reader.
  69.      * <p>
  70.      * The tables in {@code tableStack} will be closed when this
  71.      * {@code MergedReftable} is closed.
  72.      *
  73.      * @param tableStack
  74.      *            stack of tables to read from. The base of the stack is at
  75.      *            index 0, the most recent should be at the top of the stack at
  76.      *            {@code tableStack.size() - 1}. The top of the stack (higher
  77.      *            index) shadows the base of the stack (lower index).
  78.      */
  79.     public MergedReftable(List<Reftable> tableStack) {
  80.         tables = tableStack.toArray(new Reftable[0]);

  81.         // Tables must expose deletes to this instance to correctly
  82.         // shadow references from lower tables.
  83.         for (Reftable t : tables) {
  84.             t.setIncludeDeletes(true);
  85.         }
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public RefCursor allRefs() throws IOException {
  90.         MergedRefCursor m = new MergedRefCursor();
  91.         for (int i = 0; i < tables.length; i++) {
  92.             m.add(new RefQueueEntry(tables[i].allRefs(), i));
  93.         }
  94.         return m;
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public RefCursor seekRef(String name) throws IOException {
  99.         MergedRefCursor m = new MergedRefCursor();
  100.         for (int i = 0; i < tables.length; i++) {
  101.             m.add(new RefQueueEntry(tables[i].seekRef(name), i));
  102.         }
  103.         return m;
  104.     }

  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public RefCursor seekRefsWithPrefix(String prefix) throws IOException {
  108.         MergedRefCursor m = new MergedRefCursor();
  109.         for (int i = 0; i < tables.length; i++) {
  110.             m.add(new RefQueueEntry(tables[i].seekRefsWithPrefix(prefix), i));
  111.         }
  112.         return m;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public RefCursor byObjectId(AnyObjectId name) throws IOException {
  117.         MergedRefCursor m = new MergedRefCursor();
  118.         for (int i = 0; i < tables.length; i++) {
  119.             m.add(new RefQueueEntry(tables[i].byObjectId(name), i));
  120.         }
  121.         return m;
  122.     }

  123.     /** {@inheritDoc} */
  124.     @Override
  125.     public LogCursor allLogs() throws IOException {
  126.         MergedLogCursor m = new MergedLogCursor();
  127.         for (int i = 0; i < tables.length; i++) {
  128.             m.add(new LogQueueEntry(tables[i].allLogs(), i));
  129.         }
  130.         return m;
  131.     }

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     public LogCursor seekLog(String refName, long updateIdx)
  135.             throws IOException {
  136.         MergedLogCursor m = new MergedLogCursor();
  137.         for (int i = 0; i < tables.length; i++) {
  138.             m.add(new LogQueueEntry(tables[i].seekLog(refName, updateIdx), i));
  139.         }
  140.         return m;
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public void close() throws IOException {
  145.         for (Reftable t : tables) {
  146.             t.close();
  147.         }
  148.     }

  149.     int queueSize() {
  150.         return Math.max(1, tables.length);
  151.     }

  152.     private class MergedRefCursor extends RefCursor {
  153.         private final PriorityQueue<RefQueueEntry> queue;
  154.         private RefQueueEntry head;
  155.         private Ref ref;

  156.         MergedRefCursor() {
  157.             queue = new PriorityQueue<>(queueSize(), RefQueueEntry::compare);
  158.         }

  159.         void add(RefQueueEntry t) throws IOException {
  160.             // Common case is many iterations over the same RefQueueEntry
  161.             // for the bottom of the stack (scanning all refs). Its almost
  162.             // always less than the top of the queue. Avoid the queue's
  163.             // O(log N) insertion and removal costs for this common case.
  164.             if (!t.rc.next()) {
  165.                 t.rc.close();
  166.             } else if (head == null) {
  167.                 RefQueueEntry p = queue.peek();
  168.                 if (p == null || RefQueueEntry.compare(t, p) < 0) {
  169.                     head = t;
  170.                 } else {
  171.                     head = queue.poll();
  172.                     queue.add(t);
  173.                 }
  174.             } else if (RefQueueEntry.compare(t, head) > 0) {
  175.                 queue.add(t);
  176.             } else {
  177.                 queue.add(head);
  178.                 head = t;
  179.             }
  180.         }

  181.         @Override
  182.         public boolean next() throws IOException {
  183.             for (;;) {
  184.                 RefQueueEntry t = poll();
  185.                 if (t == null) {
  186.                     return false;
  187.                 }

  188.                 ref = t.rc.getRef();
  189.                 boolean include = includeDeletes || !t.rc.wasDeleted();
  190.                 add(t);
  191.                 skipShadowedRefs(ref.getName());
  192.                 if (include) {
  193.                     return true;
  194.                 }
  195.             }
  196.         }

  197.         private RefQueueEntry poll() {
  198.             RefQueueEntry e = head;
  199.             if (e != null) {
  200.                 head = null;
  201.                 return e;
  202.             }
  203.             return queue.poll();
  204.         }

  205.         private void skipShadowedRefs(String name) throws IOException {
  206.             for (;;) {
  207.                 RefQueueEntry t = head != null ? head : queue.peek();
  208.                 if (t != null && name.equals(t.name())) {
  209.                     add(poll());
  210.                 } else {
  211.                     break;
  212.                 }
  213.             }
  214.         }

  215.         @Override
  216.         public Ref getRef() {
  217.             return ref;
  218.         }

  219.         @Override
  220.         public void close() {
  221.             if (head != null) {
  222.                 head.rc.close();
  223.                 head = null;
  224.             }
  225.             while (!queue.isEmpty()) {
  226.                 queue.remove().rc.close();
  227.             }
  228.         }
  229.     }

  230.     private static class RefQueueEntry {
  231.         static int compare(RefQueueEntry a, RefQueueEntry b) {
  232.             int cmp = a.name().compareTo(b.name());
  233.             if (cmp == 0) {
  234.                 // higher updateIndex shadows lower updateIndex.
  235.                 cmp = Long.signum(b.updateIndex() - a.updateIndex());
  236.             }
  237.             if (cmp == 0) {
  238.                 // higher index shadows lower index, so higher index first.
  239.                 cmp = b.stackIdx - a.stackIdx;
  240.             }
  241.             return cmp;
  242.         }

  243.         final RefCursor rc;
  244.         final int stackIdx;

  245.         RefQueueEntry(RefCursor rc, int stackIdx) {
  246.             this.rc = rc;
  247.             this.stackIdx = stackIdx;
  248.         }

  249.         String name() {
  250.             return rc.getRef().getName();
  251.         }

  252.         long updateIndex() {
  253.             return rc.getRef().getUpdateIndex();
  254.         }
  255.     }

  256.     private class MergedLogCursor extends LogCursor {
  257.         private final PriorityQueue<LogQueueEntry> queue;
  258.         private String refName;
  259.         private long updateIndex;
  260.         private ReflogEntry entry;

  261.         MergedLogCursor() {
  262.             queue = new PriorityQueue<>(queueSize(), LogQueueEntry::compare);
  263.         }

  264.         void add(LogQueueEntry t) throws IOException {
  265.             if (t.lc.next()) {
  266.                 queue.add(t);
  267.             } else {
  268.                 t.lc.close();
  269.             }
  270.         }

  271.         @Override
  272.         public boolean next() throws IOException {
  273.             for (;;) {
  274.                 LogQueueEntry t = queue.poll();
  275.                 if (t == null) {
  276.                     return false;
  277.                 }

  278.                 refName = t.lc.getRefName();
  279.                 updateIndex = t.lc.getUpdateIndex();
  280.                 entry = t.lc.getReflogEntry();
  281.                 boolean include = includeDeletes || entry != null;
  282.                 skipShadowed(refName, updateIndex);
  283.                 add(t);
  284.                 if (include) {
  285.                     return true;
  286.                 }
  287.             }
  288.         }

  289.         private void skipShadowed(String name, long index) throws IOException {
  290.             for (;;) {
  291.                 LogQueueEntry t = queue.peek();
  292.                 if (t != null && name.equals(t.name()) && index == t.index()) {
  293.                     add(queue.remove());
  294.                 } else {
  295.                     break;
  296.                 }
  297.             }
  298.         }

  299.         @Override
  300.         public String getRefName() {
  301.             return refName;
  302.         }

  303.         @Override
  304.         public long getUpdateIndex() {
  305.             return updateIndex;
  306.         }

  307.         @Override
  308.         public ReflogEntry getReflogEntry() {
  309.             return entry;
  310.         }

  311.         @Override
  312.         public void close() {
  313.             while (!queue.isEmpty()) {
  314.                 queue.remove().lc.close();
  315.             }
  316.         }
  317.     }

  318.     private static class LogQueueEntry {
  319.         static int compare(LogQueueEntry a, LogQueueEntry b) {
  320.             int cmp = a.name().compareTo(b.name());
  321.             if (cmp == 0) {
  322.                 // higher update index sorts first.
  323.                 cmp = Long.signum(b.index() - a.index());
  324.             }
  325.             if (cmp == 0) {
  326.                 // higher index comes first.
  327.                 cmp = b.stackIdx - a.stackIdx;
  328.             }
  329.             return cmp;
  330.         }

  331.         final LogCursor lc;
  332.         final int stackIdx;

  333.         LogQueueEntry(LogCursor lc, int stackIdx) {
  334.             this.lc = lc;
  335.             this.stackIdx = stackIdx;
  336.         }

  337.         String name() {
  338.             return lc.getRefName();
  339.         }

  340.         long index() {
  341.             return lc.getUpdateIndex();
  342.         }
  343.     }
  344. }