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.

ReftableCompactor.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.io.OutputStream;
  46. import java.util.ArrayDeque;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import org.eclipse.jgit.internal.storage.reftable.ReftableWriter.Stats;
  50. import org.eclipse.jgit.lib.PersonIdent;
  51. import org.eclipse.jgit.lib.ReflogEntry;
  52. /**
  53. * Merges reftables and compacts them into a single output.
  54. * <p>
  55. * For a partial compaction callers should {@link #setIncludeDeletes(boolean)}
  56. * to {@code true} to ensure the new reftable continues to use a delete marker
  57. * to shadow any lower reftable that may have the reference present.
  58. * <p>
  59. * By default all log entries within the range defined by
  60. * {@link #setReflogExpireMinUpdateIndex(long)} and {@link #setReflogExpireMaxUpdateIndex(long)} are
  61. * copied, even if no references in the output file match the log records.
  62. * Callers may truncate the log to a more recent time horizon with
  63. * {@link #setReflogExpireOldestReflogTimeMillis(long)}, or disable the log altogether with
  64. * {@code setOldestReflogTimeMillis(Long.MAX_VALUE)}.
  65. */
  66. public class ReftableCompactor {
  67. private final ReftableWriter writer;
  68. private final ArrayDeque<ReftableReader> tables = new ArrayDeque<>();
  69. private boolean includeDeletes;
  70. private long reflogExpireMinUpdateIndex = 0;
  71. private long reflogExpireMaxUpdateIndex = Long.MAX_VALUE;
  72. private long reflogExpireOldestReflogTimeMillis;
  73. private Stats stats;
  74. /**
  75. * Creates a new compactor.
  76. *
  77. * @param out
  78. * stream to write the compacted tables to. Caller is responsible
  79. * for closing {@code out}.
  80. */
  81. public ReftableCompactor(OutputStream out) {
  82. writer = new ReftableWriter(out);
  83. }
  84. /**
  85. * Set configuration for the reftable.
  86. *
  87. * @param cfg
  88. * configuration for the reftable.
  89. * @return {@code this}
  90. */
  91. public ReftableCompactor setConfig(ReftableConfig cfg) {
  92. writer.setConfig(cfg);
  93. return this;
  94. }
  95. /**
  96. * Whether to include deletions in the output, which may be necessary for
  97. * partial compaction.
  98. *
  99. * @param deletes
  100. * {@code true} to include deletions in the output, which may be
  101. * necessary for partial compaction.
  102. * @return {@code this}
  103. */
  104. public ReftableCompactor setIncludeDeletes(boolean deletes) {
  105. includeDeletes = deletes;
  106. return this;
  107. }
  108. /**
  109. * Set the minimum update index for log entries that appear in the compacted
  110. * reftable.
  111. *
  112. * @param min
  113. * the minimum update index for log entries that appear in the
  114. * compacted reftable. This should be 1 higher than the prior
  115. * reftable's {@code maxUpdateIndex} if this table will be used
  116. * in a stack.
  117. * @return {@code this}
  118. */
  119. public ReftableCompactor setReflogExpireMinUpdateIndex(long min) {
  120. reflogExpireMinUpdateIndex = min;
  121. return this;
  122. }
  123. /**
  124. * Set the maximum update index for log entries that appear in the compacted
  125. * reftable.
  126. *
  127. * @param max
  128. * the maximum update index for log entries that appear in the
  129. * compacted reftable. This should be at least 1 higher than the
  130. * prior reftable's {@code maxUpdateIndex} if this table will be
  131. * used in a stack.
  132. * @return {@code this}
  133. */
  134. public ReftableCompactor setReflogExpireMaxUpdateIndex(long max) {
  135. reflogExpireMaxUpdateIndex = max;
  136. return this;
  137. }
  138. /**
  139. * Set oldest reflog time to preserve.
  140. *
  141. * @param timeMillis
  142. * oldest log time to preserve. Entries whose timestamps are
  143. * {@code >= timeMillis} will be copied into the output file. Log
  144. * entries that predate {@code timeMillis} will be discarded.
  145. * Specified in Java standard milliseconds since the epoch.
  146. * @return {@code this}
  147. */
  148. public ReftableCompactor setReflogExpireOldestReflogTimeMillis(long timeMillis) {
  149. reflogExpireOldestReflogTimeMillis = timeMillis;
  150. return this;
  151. }
  152. /**
  153. * Add all of the tables, in the specified order.
  154. *
  155. * @param readers
  156. * tables to compact. Tables should be ordered oldest first/most
  157. * recent last so that the more recent tables can shadow the
  158. * older results. Caller is responsible for closing the readers.
  159. * @throws java.io.IOException
  160. * update indexes of a reader cannot be accessed.
  161. */
  162. public void addAll(List<ReftableReader> readers) throws IOException {
  163. for (ReftableReader r : readers) {
  164. tables.add(r);
  165. }
  166. }
  167. /**
  168. * Write a compaction to {@code out}.
  169. *
  170. * @throws java.io.IOException
  171. * if tables cannot be read, or cannot be written.
  172. */
  173. public void compact() throws IOException {
  174. MergedReftable mr = new MergedReftable(new ArrayList<>(tables));
  175. mr.setIncludeDeletes(includeDeletes);
  176. writer.setMaxUpdateIndex(mr.maxUpdateIndex());
  177. writer.setMinUpdateIndex(mr.minUpdateIndex());
  178. writer.begin();
  179. mergeRefs(mr);
  180. mergeLogs(mr);
  181. writer.finish();
  182. stats = writer.getStats();
  183. }
  184. /**
  185. * Get statistics of the last written reftable.
  186. *
  187. * @return statistics of the last written reftable.
  188. */
  189. public Stats getStats() {
  190. return stats;
  191. }
  192. private void mergeRefs(MergedReftable mr) throws IOException {
  193. try (RefCursor rc = mr.allRefs()) {
  194. while (rc.next()) {
  195. writer.writeRef(rc.getRef(), rc.getRef().getUpdateIndex());
  196. }
  197. }
  198. }
  199. private void mergeLogs(MergedReftable mr) throws IOException {
  200. if (reflogExpireOldestReflogTimeMillis == Long.MAX_VALUE) {
  201. return;
  202. }
  203. try (LogCursor lc = mr.allLogs()) {
  204. while (lc.next()) {
  205. long updateIndex = lc.getUpdateIndex();
  206. if (updateIndex > reflogExpireMaxUpdateIndex || updateIndex < reflogExpireMinUpdateIndex) {
  207. continue;
  208. }
  209. String refName = lc.getRefName();
  210. ReflogEntry log = lc.getReflogEntry();
  211. if (log == null) {
  212. if (includeDeletes) {
  213. writer.deleteLog(refName, updateIndex);
  214. }
  215. continue;
  216. }
  217. PersonIdent who = log.getWho();
  218. if (who.getWhen().getTime() >= reflogExpireOldestReflogTimeMillis) {
  219. writer.writeLog(
  220. refName,
  221. updateIndex,
  222. who,
  223. log.getOldId(),
  224. log.getNewId(),
  225. log.getComment());
  226. }
  227. }
  228. }
  229. }
  230. }