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 44 package org.eclipse.jgit.internal.storage.reftable; 45 46 import java.io.IOException; 47 import java.io.OutputStream; 48 import java.util.ArrayDeque; 49 import java.util.ArrayList; 50 import java.util.List; 51 52 import org.eclipse.jgit.internal.storage.reftable.ReftableWriter.Stats; 53 import org.eclipse.jgit.lib.PersonIdent; 54 import org.eclipse.jgit.lib.ReflogEntry; 55 56 /** 57 * Merges reftables and compacts them into a single output. 58 * <p> 59 * For a partial compaction callers should {@link #setIncludeDeletes(boolean)} 60 * to {@code true} to ensure the new reftable continues to use a delete marker 61 * to shadow any lower reftable that may have the reference present. 62 * <p> 63 * By default all log entries within the range defined by 64 * {@link #setMinUpdateIndex(long)} and {@link #setMaxUpdateIndex(long)} are 65 * copied, even if no references in the output file match the log records. 66 * Callers may truncate the log to a more recent time horizon with 67 * {@link #setOldestReflogTimeMillis(long)}, or disable the log altogether with 68 * {@code setOldestReflogTimeMillis(Long.MAX_VALUE)}. 69 */ 70 public class ReftableCompactor { 71 private final ReftableWriter writer = new ReftableWriter(); 72 private final ArrayDeque<Reftable> tables = new ArrayDeque<>(); 73 74 private long compactBytesLimit; 75 private long bytesToCompact; 76 private boolean includeDeletes; 77 private long minUpdateIndex; 78 private long maxUpdateIndex; 79 private long oldestReflogTimeMillis; 80 private Stats stats; 81 82 /** 83 * @param cfg 84 * configuration for the reftable. 85 * @return {@code this} 86 */ 87 public ReftableCompactor setConfig(ReftableConfig cfg) { 88 writer.setConfig(cfg); 89 return this; 90 } 91 92 /** 93 * @param bytes 94 * limit on number of bytes from source tables to compact. 95 * @return {@code this} 96 */ 97 public ReftableCompactor setCompactBytesLimit(long bytes) { 98 compactBytesLimit = bytes; 99 return this; 100 } 101 102 /** 103 * @param deletes 104 * {@code true} to include deletions in the output, which may be 105 * necessary for partial compaction. 106 * @return {@code this} 107 */ 108 public ReftableCompactor setIncludeDeletes(boolean deletes) { 109 includeDeletes = deletes; 110 return this; 111 } 112 113 /** 114 * @param min 115 * the minimum update index for log entries that appear in the 116 * compacted reftable. This should be 1 higher than the prior 117 * reftable's {@code maxUpdateIndex} if this table will be used 118 * in a stack. 119 * @return {@code this} 120 */ 121 public ReftableCompactor setMinUpdateIndex(long min) { 122 minUpdateIndex = min; 123 return this; 124 } 125 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 setMaxUpdateIndex(long max) { 135 maxUpdateIndex = max; 136 return this; 137 } 138 139 /** 140 * @param timeMillis 141 * oldest log time to preserve. Entries whose timestamps are 142 * {@code >= timeMillis} will be copied into the output file. Log 143 * entries that predate {@code timeMillis} will be discarded. 144 * Specified in Java standard milliseconds since the epoch. 145 * @return {@code this} 146 */ 147 public ReftableCompactor setOldestReflogTimeMillis(long timeMillis) { 148 oldestReflogTimeMillis = timeMillis; 149 return this; 150 } 151 152 /** 153 * Add all of the tables, in the specified order. 154 * <p> 155 * Unconditionally adds all tables, ignoring the 156 * {@link #setCompactBytesLimit(long)}. 157 * 158 * @param readers 159 * tables to compact. Tables should be ordered oldest first/most 160 * recent last so that the more recent tables can shadow the 161 * older results. Caller is responsible for closing the readers. 162 * @throws IOException 163 * update indexes of a reader cannot be accessed. 164 */ 165 public void addAll(List<? extends Reftable> readers) throws IOException { 166 tables.addAll(readers); 167 for (Reftable r : readers) { 168 if (r instanceof ReftableReader) { 169 adjustUpdateIndexes((ReftableReader) r); 170 } 171 } 172 } 173 174 /** 175 * Try to add this reader at the bottom of the stack. 176 * <p> 177 * A reader may be rejected by returning {@code false} if the compactor is 178 * already rewriting its {@link #setCompactBytesLimit(long)}. When this 179 * happens the caller should stop trying to add tables, and execute the 180 * compaction. 181 * 182 * @param reader 183 * the reader to insert at the bottom of the stack. Caller is 184 * responsible for closing the reader. 185 * @return {@code true} if the compactor accepted this table; {@code false} 186 * if the compactor has reached its limit. 187 * @throws IOException 188 * if size of {@code reader}, or its update indexes cannot be read. 189 */ 190 public boolean tryAddFirst(ReftableReader reader) throws IOException { 191 long sz = reader.size(); 192 if (compactBytesLimit > 0 && bytesToCompact + sz > compactBytesLimit) { 193 return false; 194 } 195 bytesToCompact += sz; 196 adjustUpdateIndexes(reader); 197 tables.addFirst(reader); 198 return true; 199 } 200 201 private void adjustUpdateIndexes(ReftableReader reader) throws IOException { 202 if (minUpdateIndex == 0) { 203 minUpdateIndex = reader.minUpdateIndex(); 204 } else { 205 minUpdateIndex = Math.min(minUpdateIndex, reader.minUpdateIndex()); 206 } 207 maxUpdateIndex = Math.max(maxUpdateIndex, reader.maxUpdateIndex()); 208 } 209 210 /** 211 * Write a compaction to {@code out}. 212 * 213 * @param out 214 * stream to write the compacted tables to. Caller is responsible 215 * for closing {@code out}. 216 * @throws IOException 217 * if tables cannot be read, or cannot be written. 218 */ 219 public void compact(OutputStream out) throws IOException { 220 MergedReftable mr = new MergedReftable(new ArrayList<>(tables)); 221 mr.setIncludeDeletes(includeDeletes); 222 223 writer.setMinUpdateIndex(minUpdateIndex); 224 writer.setMaxUpdateIndex(maxUpdateIndex); 225 writer.begin(out); 226 mergeRefs(mr); 227 mergeLogs(mr); 228 writer.finish(); 229 stats = writer.getStats(); 230 } 231 232 /** @return statistics of the last written reftable. */ 233 public Stats getStats() { 234 return stats; 235 } 236 237 private void mergeRefs(MergedReftable mr) throws IOException { 238 try (RefCursor rc = mr.allRefs()) { 239 while (rc.next()) { 240 writer.writeRef(rc.getRef(), rc.getUpdateIndex()); 241 } 242 } 243 } 244 245 private void mergeLogs(MergedReftable mr) throws IOException { 246 if (oldestReflogTimeMillis == Long.MAX_VALUE) { 247 return; 248 } 249 250 try (LogCursor lc = mr.allLogs()) { 251 while (lc.next()) { 252 long updateIndex = lc.getUpdateIndex(); 253 if (updateIndex < minUpdateIndex 254 || updateIndex > maxUpdateIndex) { 255 // Cannot merge log records outside the header's range. 256 continue; 257 } 258 259 String refName = lc.getRefName(); 260 ReflogEntry log = lc.getReflogEntry(); 261 if (log == null) { 262 if (includeDeletes) { 263 writer.deleteLog(refName, updateIndex); 264 } 265 continue; 266 } 267 268 PersonIdent who = log.getWho(); 269 if (who.getWhen().getTime() >= oldestReflogTimeMillis) { 270 writer.writeLog( 271 refName, 272 updateIndex, 273 who, 274 log.getOldId(), 275 log.getNewId(), 276 log.getComment()); 277 } 278 } 279 } 280 } 281 }