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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.lib.PersonIdent;
  50. import org.eclipse.jgit.internal.storage.reftable.ReftableWriter.Stats;
  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 #setMinUpdateIndex(long)} and {@link #setMaxUpdateIndex(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 #setOldestReflogTimeMillis(long)}, or disable the log altogether with
  64. * {@code setOldestReflogTimeMillis(Long.MAX_VALUE)}.
  65. */
  66. public class ReftableCompactor {
  67. private final ReftableWriter writer = new ReftableWriter();
  68. private final ArrayDeque<Reftable> tables = new ArrayDeque<>();
  69. private long compactBytesLimit;
  70. private long bytesToCompact;
  71. private boolean includeDeletes;
  72. private long minUpdateIndex;
  73. private long maxUpdateIndex;
  74. private long oldestReflogTimeMillis;
  75. private Stats stats;
  76. /**
  77. * @param cfg
  78. * configuration for the reftable.
  79. * @return {@code this}
  80. */
  81. public ReftableCompactor setConfig(ReftableConfig cfg) {
  82. writer.setConfig(cfg);
  83. return this;
  84. }
  85. /**
  86. * @param bytes
  87. * limit on number of bytes from source tables to compact.
  88. * @return {@code this}
  89. */
  90. public ReftableCompactor setCompactBytesLimit(long bytes) {
  91. compactBytesLimit = bytes;
  92. return this;
  93. }
  94. /**
  95. * @param deletes
  96. * {@code true} to include deletions in the output, which may be
  97. * necessary for partial compaction.
  98. * @return {@code this}
  99. */
  100. public ReftableCompactor setIncludeDeletes(boolean deletes) {
  101. includeDeletes = deletes;
  102. return this;
  103. }
  104. /**
  105. * @param min
  106. * the minimum update index for log entries that appear in the
  107. * compacted reftable. This should be 1 higher than the prior
  108. * reftable's {@code maxUpdateIndex} if this table will be used
  109. * in a stack.
  110. * @return {@code this}
  111. */
  112. public ReftableCompactor setMinUpdateIndex(long min) {
  113. minUpdateIndex = min;
  114. return this;
  115. }
  116. /**
  117. * @param max
  118. * the maximum update index for log entries that appear in the
  119. * compacted reftable. This should be at least 1 higher than the
  120. * prior reftable's {@code maxUpdateIndex} if this table will be
  121. * used in a stack.
  122. * @return {@code this}
  123. */
  124. public ReftableCompactor setMaxUpdateIndex(long max) {
  125. maxUpdateIndex = max;
  126. return this;
  127. }
  128. /**
  129. * @param timeMillis
  130. * oldest log time to preserve. Entries whose timestamps are
  131. * {@code >= timeMillis} will be copied into the output file. Log
  132. * entries that predate {@code timeMillis} will be discarded.
  133. * Specified in Java standard milliseconds since the epoch.
  134. * @return {@code this}
  135. */
  136. public ReftableCompactor setOldestReflogTimeMillis(long timeMillis) {
  137. oldestReflogTimeMillis = timeMillis;
  138. return this;
  139. }
  140. /**
  141. * Add all of the tables, in the specified order.
  142. * <p>
  143. * Unconditionally adds all tables, ignoring the
  144. * {@link #setCompactBytesLimit(long)}.
  145. *
  146. * @param readers
  147. * tables to compact. Tables should be ordered oldest first/most
  148. * recent last so that the more recent tables can shadow the
  149. * older results. Caller is responsible for closing the readers.
  150. */
  151. public void addAll(List<? extends Reftable> readers) {
  152. tables.addAll(readers);
  153. }
  154. /**
  155. * Try to add this reader at the bottom of the stack.
  156. * <p>
  157. * A reader may be rejected by returning {@code false} if the compactor is
  158. * already rewriting its {@link #setCompactBytesLimit(long)}. When this
  159. * happens the caller should stop trying to add tables, and execute the
  160. * compaction.
  161. *
  162. * @param reader
  163. * the reader to insert at the bottom of the stack. Caller is
  164. * responsible for closing the reader.
  165. * @return {@code true} if the compactor accepted this table; {@code false}
  166. * if the compactor has reached its limit.
  167. * @throws IOException
  168. * if size of {@code reader} cannot be read.
  169. */
  170. public boolean tryAddFirst(ReftableReader reader) throws IOException {
  171. long sz = reader.size();
  172. if (compactBytesLimit > 0 && bytesToCompact + sz > compactBytesLimit) {
  173. return false;
  174. }
  175. bytesToCompact += sz;
  176. tables.addFirst(reader);
  177. return true;
  178. }
  179. /**
  180. * Write a compaction to {@code out}.
  181. *
  182. * @param out
  183. * stream to write the compacted tables to. Caller is responsible
  184. * for closing {@code out}.
  185. * @throws IOException
  186. * if tables cannot be read, or cannot be written.
  187. */
  188. public void compact(OutputStream out) throws IOException {
  189. MergedReftable mr = new MergedReftable(new ArrayList<>(tables));
  190. mr.setIncludeDeletes(includeDeletes);
  191. writer.setMinUpdateIndex(minUpdateIndex);
  192. writer.setMaxUpdateIndex(maxUpdateIndex);
  193. writer.begin(out);
  194. mergeRefs(mr);
  195. mergeLogs(mr);
  196. writer.finish();
  197. stats = writer.getStats();
  198. }
  199. /** @return statistics of the last written reftable. */
  200. public Stats getStats() {
  201. return stats;
  202. }
  203. private void mergeRefs(MergedReftable mr) throws IOException {
  204. try (RefCursor rc = mr.allRefs()) {
  205. while (rc.next()) {
  206. writer.writeRef(rc.getRef(), rc.getUpdateIndex());
  207. }
  208. }
  209. }
  210. private void mergeLogs(MergedReftable mr) throws IOException {
  211. if (oldestReflogTimeMillis == Long.MAX_VALUE) {
  212. return;
  213. }
  214. try (LogCursor lc = mr.allLogs()) {
  215. while (lc.next()) {
  216. long updateIndex = lc.getUpdateIndex();
  217. if (updateIndex < minUpdateIndex
  218. || updateIndex > maxUpdateIndex) {
  219. // Cannot merge log records outside the header's range.
  220. continue;
  221. }
  222. String refName = lc.getRefName();
  223. ReflogEntry log = lc.getReflogEntry();
  224. if (log == null) {
  225. if (includeDeletes) {
  226. writer.deleteLog(refName, updateIndex);
  227. }
  228. continue;
  229. }
  230. PersonIdent who = log.getWho();
  231. if (who.getWhen().getTime() >= oldestReflogTimeMillis) {
  232. writer.writeLog(
  233. refName,
  234. updateIndex,
  235. who,
  236. log.getOldId(),
  237. log.getNewId(),
  238. log.getComment());
  239. }
  240. }
  241. }
  242. }
  243. }