View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.pgm.debug;
12  
13  import static java.nio.charset.StandardCharsets.UTF_8;
14  import static org.eclipse.jgit.lib.Constants.HEAD;
15  import static org.eclipse.jgit.lib.Constants.MASTER;
16  import static org.eclipse.jgit.lib.Constants.R_HEADS;
17  import static org.eclipse.jgit.lib.Ref.Storage.NEW;
18  import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
19  
20  import java.io.BufferedReader;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  import java.io.OutputStream;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  import java.util.regex.Matcher;
31  import java.util.regex.Pattern;
32  
33  import org.eclipse.jgit.internal.storage.reftable.ReftableConfig;
34  import org.eclipse.jgit.internal.storage.reftable.ReftableWriter;
35  import org.eclipse.jgit.lib.ObjectId;
36  import org.eclipse.jgit.lib.ObjectIdRef;
37  import org.eclipse.jgit.lib.PersonIdent;
38  import org.eclipse.jgit.lib.Ref;
39  import org.eclipse.jgit.lib.SymbolicRef;
40  import org.eclipse.jgit.pgm.Command;
41  import org.eclipse.jgit.pgm.TextBuiltin;
42  import org.kohsuke.args4j.Argument;
43  import org.kohsuke.args4j.Option;
44  
45  @Command
46  class WriteReftable extends TextBuiltin {
47  	private static final int KIB = 1 << 10;
48  	private static final int MIB = 1 << 20;
49  
50  	@Option(name = "--block-size")
51  	private int refBlockSize;
52  
53  	@Option(name = "--log-block-size")
54  	private int logBlockSize;
55  
56  	@Option(name = "--restart-interval")
57  	private int restartInterval;
58  
59  	@Option(name = "--index-levels")
60  	private int indexLevels;
61  
62  	@Option(name = "--reflog-in")
63  	private String reflogIn;
64  
65  	@Option(name = "--no-index-objects")
66  	private boolean noIndexObjects;
67  
68  	@Argument(index = 0)
69  	private String in;
70  
71  	@Argument(index = 1)
72  	private String out;
73  
74  	/** {@inheritDoc} */
75  	@SuppressWarnings({ "nls", "boxing" })
76  	@Override
77  	protected void run() throws Exception {
78  		List<Ref> refs = readRefs(in);
79  		List<LogEntry> logs = readLog(reflogIn);
80  
81  		ReftableWriter.Stats stats;
82  		try (OutputStream os = new FileOutputStream(out)) {
83  			ReftableConfig cfg = new ReftableConfig();
84  			cfg.setIndexObjects(!noIndexObjects);
85  			if (refBlockSize > 0) {
86  				cfg.setRefBlockSize(refBlockSize);
87  			}
88  			if (logBlockSize > 0) {
89  				cfg.setLogBlockSize(logBlockSize);
90  			}
91  			if (restartInterval > 0) {
92  				cfg.setRestartInterval(restartInterval);
93  			}
94  			if (indexLevels > 0) {
95  				cfg.setMaxIndexLevels(indexLevels);
96  			}
97  
98  			ReftableWriter w = new ReftableWriter(cfg, os);
99  			w.setMinUpdateIndex(min(logs)).setMaxUpdateIndex(max(logs));
100 			w.begin();
101 			w.sortAndWriteRefs(refs);
102 			for (LogEntry e : logs) {
103 				w.writeLog(e.ref, e.updateIndex, e.who,
104 						e.oldId, e.newId, e.message);
105 			}
106 			stats = w.finish().getStats();
107 		}
108 
109 		double fileMiB = ((double) stats.totalBytes()) / MIB;
110 		printf("Summary:");
111 		printf("  file sz : %.1f MiB (%d bytes)", fileMiB, stats.totalBytes());
112 		printf("  padding : %d KiB", stats.paddingBytes() / KIB);
113 		errw.println();
114 
115 		printf("Refs:");
116 		printf("  ref blk : %d", stats.refBlockSize());
117 		printf("  restarts: %d", stats.restartInterval());
118 		printf("  refs    : %d", stats.refCount());
119 		if (stats.refIndexLevels() > 0) {
120 			int idxSize = (int) Math.round(((double) stats.refIndexSize()) / KIB);
121 			printf("  idx sz  : %d KiB", idxSize);
122 			printf("  idx lvl : %d", stats.refIndexLevels());
123 		}
124 		printf("  avg ref : %d bytes", stats.refBytes() / refs.size());
125 		errw.println();
126 
127 		if (stats.objCount() > 0) {
128 			int objMiB = (int) Math.round(((double) stats.objBytes()) / MIB);
129 			int idLen = stats.objIdLength();
130 			printf("Objects:");
131 			printf("  obj blk : %d", stats.refBlockSize());
132 			printf("  restarts: %d", stats.restartInterval());
133 			printf("  objects : %d", stats.objCount());
134 			printf("  obj sz  : %d MiB (%d bytes)", objMiB, stats.objBytes());
135 			if (stats.objIndexSize() > 0) {
136 				int s = (int) Math.round(((double) stats.objIndexSize()) / KIB);
137 				printf("  idx sz  : %d KiB", s);
138 				printf("  idx lvl : %d", stats.objIndexLevels());
139 			}
140 			printf("  id len  : %d bytes (%d hex digits)", idLen, 2 * idLen);
141 			printf("  avg obj : %d bytes", stats.objBytes() / stats.objCount());
142 			errw.println();
143 		}
144 		if (stats.logCount() > 0) {
145 			int logMiB = (int) Math.round(((double) stats.logBytes()) / MIB);
146 			printf("Log:");
147 			printf("  log blk : %d", stats.logBlockSize());
148 			printf("  logs    : %d", stats.logCount());
149 			printf("  log sz  : %d MiB (%d bytes)", logMiB, stats.logBytes());
150 			printf("  avg log : %d bytes", stats.logBytes() / logs.size());
151 			errw.println();
152 		}
153 	}
154 
155 	private void printf(String fmt, Object... args) throws IOException {
156 		errw.println(String.format(fmt, args));
157 	}
158 
159 	static List<Ref> readRefs(String inputFile) throws IOException {
160 		List<Ref> refs = new ArrayList<>();
161 		try (BufferedReader br = new BufferedReader(
162 				new InputStreamReader(new FileInputStream(inputFile), UTF_8))) {
163 			String line;
164 			while ((line = br.readLine()) != null) {
165 				ObjectId id = ObjectId.fromString(line.substring(0, 40));
166 				String name = line.substring(41, line.length());
167 				if (name.endsWith("^{}")) { //$NON-NLS-1$
168 					int lastIdx = refs.size() - 1;
169 					Ref last = refs.get(lastIdx);
170 					refs.set(lastIdx, new ObjectIdRef.PeeledTag(PACKED,
171 							last.getName(), last.getObjectId(), id));
172 					continue;
173 				}
174 
175 				Ref ref;
176 				if (name.equals(HEAD)) {
177 					ref = new SymbolicRef(name, new ObjectIdRef.Unpeeled(NEW,
178 							R_HEADS + MASTER, null));
179 				} else {
180 					ref = new ObjectIdRef.PeeledNonTag(PACKED, name, id);
181 				}
182 				refs.add(ref);
183 			}
184 		}
185 		Collections.sort(refs, (a, b) -> a.getName().compareTo(b.getName()));
186 		return refs;
187 	}
188 
189 	private static List<LogEntry> readLog(String logPath)
190 			throws FileNotFoundException, IOException {
191 		if (logPath == null) {
192 			return Collections.emptyList();
193 		}
194 
195 		List<LogEntry> log = new ArrayList<>();
196 		try (BufferedReader br = new BufferedReader(
197 				new InputStreamReader(new FileInputStream(logPath), UTF_8))) {
198 			@SuppressWarnings("nls")
199 			Pattern pattern = Pattern.compile("([^,]+)" // 1: ref
200 					+ ",([0-9]+(?:[.][0-9]+)?)" // 2: time
201 					+ ",([^,]+)" // 3: who
202 					+ ",([^,]+)" // 4: old
203 					+ ",([^,]+)" // 5: new
204 					+ ",(.*)"); // 6: msg
205 			String line;
206 			while ((line = br.readLine()) != null) {
207 				Matcher m = pattern.matcher(line);
208 				if (!m.matches()) {
209 					throw new IOException("unparsed line: " + line); //$NON-NLS-1$
210 				}
211 				String ref = m.group(1);
212 				double t = Double.parseDouble(m.group(2));
213 				long time = ((long) t) * 1000L;
214 				long index = (long) (t * 1e6);
215 				String user = m.group(3);
216 				ObjectId oldId = parseId(m.group(4));
217 				ObjectId newId = parseId(m.group(5));
218 				String msg = m.group(6);
219 				String email = user + "@gerrit"; //$NON-NLS-1$
220 				PersonIdent who = new PersonIdent(user, email, time, -480);
221 				log.add(new LogEntry(ref, index, who, oldId, newId, msg));
222 			}
223 		}
224 		Collections.sort(log, LogEntry::compare);
225 		return log;
226 	}
227 
228 	private static long min(List<LogEntry> log) {
229 		return log.stream().mapToLong(e -> e.updateIndex).min().orElse(0);
230 	}
231 
232 	private static long max(List<LogEntry> log) {
233 		return log.stream().mapToLong(e -> e.updateIndex).max().orElse(0);
234 	}
235 
236 	private static ObjectId parseId(String s) {
237 		if ("NULL".equals(s)) { //$NON-NLS-1$
238 			return ObjectId.zeroId();
239 		}
240 		return ObjectId.fromString(s);
241 	}
242 
243 	private static class LogEntry {
244 		static int compare(LogEntry a, LogEntry b) {
245 			int cmp = a.ref.compareTo(b.ref);
246 			if (cmp == 0) {
247 				cmp = Long.signum(b.updateIndex - a.updateIndex);
248 			}
249 			return cmp;
250 		}
251 
252 		final String ref;
253 		final long updateIndex;
254 		final PersonIdent who;
255 		final ObjectId oldId;
256 		final ObjectId newId;
257 		final String message;
258 
259 		LogEntry(String ref, long updateIndex, PersonIdent who,
260 				ObjectId oldId, ObjectId newId, String message) {
261 			this.ref = ref;
262 			this.updateIndex = updateIndex;
263 			this.who = who;
264 			this.oldId = oldId;
265 			this.newId = newId;
266 			this.message = message;
267 		}
268 	}
269 }