WriteReftable.java

  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. package org.eclipse.jgit.pgm.debug;

  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.eclipse.jgit.lib.Constants.HEAD;
  13. import static org.eclipse.jgit.lib.Constants.MASTER;
  14. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  15. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  16. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;

  17. import java.io.BufferedReader;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.io.OutputStream;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;

  29. import org.eclipse.jgit.internal.storage.reftable.ReftableConfig;
  30. import org.eclipse.jgit.internal.storage.reftable.ReftableWriter;
  31. import org.eclipse.jgit.lib.ObjectId;
  32. import org.eclipse.jgit.lib.ObjectIdRef;
  33. import org.eclipse.jgit.lib.PersonIdent;
  34. import org.eclipse.jgit.lib.Ref;
  35. import org.eclipse.jgit.lib.SymbolicRef;
  36. import org.eclipse.jgit.pgm.Command;
  37. import org.eclipse.jgit.pgm.TextBuiltin;
  38. import org.kohsuke.args4j.Argument;
  39. import org.kohsuke.args4j.Option;

  40. @Command
  41. class WriteReftable extends TextBuiltin {
  42.     private static final int KIB = 1 << 10;
  43.     private static final int MIB = 1 << 20;

  44.     @Option(name = "--block-size")
  45.     private int refBlockSize;

  46.     @Option(name = "--log-block-size")
  47.     private int logBlockSize;

  48.     @Option(name = "--restart-interval")
  49.     private int restartInterval;

  50.     @Option(name = "--index-levels")
  51.     private int indexLevels;

  52.     @Option(name = "--reflog-in")
  53.     private String reflogIn;

  54.     @Option(name = "--no-index-objects")
  55.     private boolean noIndexObjects;

  56.     @Argument(index = 0)
  57.     private String in;

  58.     @Argument(index = 1)
  59.     private String out;

  60.     /** {@inheritDoc} */
  61.     @SuppressWarnings({ "nls", "boxing" })
  62.     @Override
  63.     protected void run() throws Exception {
  64.         List<Ref> refs = readRefs(in);
  65.         List<LogEntry> logs = readLog(reflogIn);

  66.         ReftableWriter.Stats stats;
  67.         try (OutputStream os = new FileOutputStream(out)) {
  68.             ReftableConfig cfg = new ReftableConfig();
  69.             cfg.setIndexObjects(!noIndexObjects);
  70.             if (refBlockSize > 0) {
  71.                 cfg.setRefBlockSize(refBlockSize);
  72.             }
  73.             if (logBlockSize > 0) {
  74.                 cfg.setLogBlockSize(logBlockSize);
  75.             }
  76.             if (restartInterval > 0) {
  77.                 cfg.setRestartInterval(restartInterval);
  78.             }
  79.             if (indexLevels > 0) {
  80.                 cfg.setMaxIndexLevels(indexLevels);
  81.             }

  82.             ReftableWriter w = new ReftableWriter(cfg, os);
  83.             w.setMinUpdateIndex(min(logs)).setMaxUpdateIndex(max(logs));
  84.             w.begin();
  85.             w.sortAndWriteRefs(refs);
  86.             for (LogEntry e : logs) {
  87.                 w.writeLog(e.ref, e.updateIndex, e.who,
  88.                         e.oldId, e.newId, e.message);
  89.             }
  90.             stats = w.finish().getStats();
  91.         }

  92.         double fileMiB = ((double) stats.totalBytes()) / MIB;
  93.         printf("Summary:");
  94.         printf("  file sz : %.1f MiB (%d bytes)", fileMiB, stats.totalBytes());
  95.         printf("  padding : %d KiB", stats.paddingBytes() / KIB);
  96.         errw.println();

  97.         printf("Refs:");
  98.         printf("  ref blk : %d", stats.refBlockSize());
  99.         printf("  restarts: %d", stats.restartInterval());
  100.         printf("  refs    : %d", stats.refCount());
  101.         if (stats.refIndexLevels() > 0) {
  102.             int idxSize = (int) Math.round(((double) stats.refIndexSize()) / KIB);
  103.             printf("  idx sz  : %d KiB", idxSize);
  104.             printf("  idx lvl : %d", stats.refIndexLevels());
  105.         }
  106.         printf("  avg ref : %d bytes", stats.refBytes() / refs.size());
  107.         errw.println();

  108.         if (stats.objCount() > 0) {
  109.             int objMiB = (int) Math.round(((double) stats.objBytes()) / MIB);
  110.             int idLen = stats.objIdLength();
  111.             printf("Objects:");
  112.             printf("  obj blk : %d", stats.refBlockSize());
  113.             printf("  restarts: %d", stats.restartInterval());
  114.             printf("  objects : %d", stats.objCount());
  115.             printf("  obj sz  : %d MiB (%d bytes)", objMiB, stats.objBytes());
  116.             if (stats.objIndexSize() > 0) {
  117.                 int s = (int) Math.round(((double) stats.objIndexSize()) / KIB);
  118.                 printf("  idx sz  : %d KiB", s);
  119.                 printf("  idx lvl : %d", stats.objIndexLevels());
  120.             }
  121.             printf("  id len  : %d bytes (%d hex digits)", idLen, 2 * idLen);
  122.             printf("  avg obj : %d bytes", stats.objBytes() / stats.objCount());
  123.             errw.println();
  124.         }
  125.         if (stats.logCount() > 0) {
  126.             int logMiB = (int) Math.round(((double) stats.logBytes()) / MIB);
  127.             printf("Log:");
  128.             printf("  log blk : %d", stats.logBlockSize());
  129.             printf("  logs    : %d", stats.logCount());
  130.             printf("  log sz  : %d MiB (%d bytes)", logMiB, stats.logBytes());
  131.             printf("  avg log : %d bytes", stats.logBytes() / logs.size());
  132.             errw.println();
  133.         }
  134.     }

  135.     private void printf(String fmt, Object... args) throws IOException {
  136.         errw.println(String.format(fmt, args));
  137.     }

  138.     static List<Ref> readRefs(String inputFile) throws IOException {
  139.         List<Ref> refs = new ArrayList<>();
  140.         try (BufferedReader br = new BufferedReader(
  141.                 new InputStreamReader(new FileInputStream(inputFile), UTF_8))) {
  142.             String line;
  143.             while ((line = br.readLine()) != null) {
  144.                 ObjectId id = ObjectId.fromString(line.substring(0, 40));
  145.                 String name = line.substring(41, line.length());
  146.                 if (name.endsWith("^{}")) { //$NON-NLS-1$
  147.                     int lastIdx = refs.size() - 1;
  148.                     Ref last = refs.get(lastIdx);
  149.                     refs.set(lastIdx, new ObjectIdRef.PeeledTag(PACKED,
  150.                             last.getName(), last.getObjectId(), id));
  151.                     continue;
  152.                 }

  153.                 Ref ref;
  154.                 if (name.equals(HEAD)) {
  155.                     ref = new SymbolicRef(name, new ObjectIdRef.Unpeeled(NEW,
  156.                             R_HEADS + MASTER, null));
  157.                 } else {
  158.                     ref = new ObjectIdRef.PeeledNonTag(PACKED, name, id);
  159.                 }
  160.                 refs.add(ref);
  161.             }
  162.         }
  163.         Collections.sort(refs, (a, b) -> a.getName().compareTo(b.getName()));
  164.         return refs;
  165.     }

  166.     private static List<LogEntry> readLog(String logPath)
  167.             throws FileNotFoundException, IOException {
  168.         if (logPath == null) {
  169.             return Collections.emptyList();
  170.         }

  171.         List<LogEntry> log = new ArrayList<>();
  172.         try (BufferedReader br = new BufferedReader(
  173.                 new InputStreamReader(new FileInputStream(logPath), UTF_8))) {
  174.             @SuppressWarnings("nls")
  175.             Pattern pattern = Pattern.compile("([^,]+)" // 1: ref
  176.                     + ",([0-9]+(?:[.][0-9]+)?)" // 2: time
  177.                     + ",([^,]+)" // 3: who
  178.                     + ",([^,]+)" // 4: old
  179.                     + ",([^,]+)" // 5: new
  180.                     + ",(.*)"); // 6: msg
  181.             String line;
  182.             while ((line = br.readLine()) != null) {
  183.                 Matcher m = pattern.matcher(line);
  184.                 if (!m.matches()) {
  185.                     throw new IOException("unparsed line: " + line); //$NON-NLS-1$
  186.                 }
  187.                 String ref = m.group(1);
  188.                 double t = Double.parseDouble(m.group(2));
  189.                 long time = ((long) t) * 1000L;
  190.                 long index = (long) (t * 1e6);
  191.                 String user = m.group(3);
  192.                 ObjectId oldId = parseId(m.group(4));
  193.                 ObjectId newId = parseId(m.group(5));
  194.                 String msg = m.group(6);
  195.                 String email = user + "@gerrit"; //$NON-NLS-1$
  196.                 PersonIdent who = new PersonIdent(user, email, time, -480);
  197.                 log.add(new LogEntry(ref, index, who, oldId, newId, msg));
  198.             }
  199.         }
  200.         Collections.sort(log, LogEntry::compare);
  201.         return log;
  202.     }

  203.     private static long min(List<LogEntry> log) {
  204.         return log.stream().mapToLong(e -> e.updateIndex).min().orElse(0);
  205.     }

  206.     private static long max(List<LogEntry> log) {
  207.         return log.stream().mapToLong(e -> e.updateIndex).max().orElse(0);
  208.     }

  209.     private static ObjectId parseId(String s) {
  210.         if ("NULL".equals(s)) { //$NON-NLS-1$
  211.             return ObjectId.zeroId();
  212.         }
  213.         return ObjectId.fromString(s);
  214.     }

  215.     private static class LogEntry {
  216.         static int compare(LogEntry a, LogEntry b) {
  217.             int cmp = a.ref.compareTo(b.ref);
  218.             if (cmp == 0) {
  219.                 cmp = Long.signum(b.updateIndex - a.updateIndex);
  220.             }
  221.             return cmp;
  222.         }

  223.         final String ref;
  224.         final long updateIndex;
  225.         final PersonIdent who;
  226.         final ObjectId oldId;
  227.         final ObjectId newId;
  228.         final String message;

  229.         LogEntry(String ref, long updateIndex, PersonIdent who,
  230.                 ObjectId oldId, ObjectId newId, String message) {
  231.             this.ref = ref;
  232.             this.updateIndex = updateIndex;
  233.             this.who = who;
  234.             this.oldId = oldId;
  235.             this.newId = newId;
  236.             this.message = message;
  237.         }
  238.     }
  239. }