WriteReftable.java

  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.pgm.debug;

  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.eclipse.jgit.lib.Constants.HEAD;
  46. import static org.eclipse.jgit.lib.Constants.MASTER;
  47. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  48. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  49. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;

  50. import java.io.BufferedReader;
  51. import java.io.FileInputStream;
  52. import java.io.FileNotFoundException;
  53. import java.io.FileOutputStream;
  54. import java.io.IOException;
  55. import java.io.InputStreamReader;
  56. import java.io.OutputStream;
  57. import java.util.ArrayList;
  58. import java.util.Collections;
  59. import java.util.List;
  60. import java.util.regex.Matcher;
  61. import java.util.regex.Pattern;

  62. import org.eclipse.jgit.internal.storage.reftable.ReftableConfig;
  63. import org.eclipse.jgit.internal.storage.reftable.ReftableWriter;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectIdRef;
  66. import org.eclipse.jgit.lib.PersonIdent;
  67. import org.eclipse.jgit.lib.Ref;
  68. import org.eclipse.jgit.lib.SymbolicRef;
  69. import org.eclipse.jgit.pgm.Command;
  70. import org.eclipse.jgit.pgm.TextBuiltin;
  71. import org.kohsuke.args4j.Argument;
  72. import org.kohsuke.args4j.Option;

  73. @Command
  74. class WriteReftable extends TextBuiltin {
  75.     private static final int KIB = 1 << 10;
  76.     private static final int MIB = 1 << 20;

  77.     @Option(name = "--block-size")
  78.     private int refBlockSize;

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

  81.     @Option(name = "--restart-interval")
  82.     private int restartInterval;

  83.     @Option(name = "--index-levels")
  84.     private int indexLevels;

  85.     @Option(name = "--reflog-in")
  86.     private String reflogIn;

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

  89.     @Argument(index = 0)
  90.     private String in;

  91.     @Argument(index = 1)
  92.     private String out;

  93.     /** {@inheritDoc} */
  94.     @SuppressWarnings({ "nls", "boxing" })
  95.     @Override
  96.     protected void run() throws Exception {
  97.         List<Ref> refs = readRefs(in);
  98.         List<LogEntry> logs = readLog(reflogIn);

  99.         ReftableWriter.Stats stats;
  100.         try (OutputStream os = new FileOutputStream(out)) {
  101.             ReftableConfig cfg = new ReftableConfig();
  102.             cfg.setIndexObjects(!noIndexObjects);
  103.             if (refBlockSize > 0) {
  104.                 cfg.setRefBlockSize(refBlockSize);
  105.             }
  106.             if (logBlockSize > 0) {
  107.                 cfg.setLogBlockSize(logBlockSize);
  108.             }
  109.             if (restartInterval > 0) {
  110.                 cfg.setRestartInterval(restartInterval);
  111.             }
  112.             if (indexLevels > 0) {
  113.                 cfg.setMaxIndexLevels(indexLevels);
  114.             }

  115.             ReftableWriter w = new ReftableWriter(cfg);
  116.             w.setMinUpdateIndex(min(logs)).setMaxUpdateIndex(max(logs));
  117.             w.begin(os);
  118.             w.sortAndWriteRefs(refs);
  119.             for (LogEntry e : logs) {
  120.                 w.writeLog(e.ref, e.updateIndex, e.who,
  121.                         e.oldId, e.newId, e.message);
  122.             }
  123.             stats = w.finish().getStats();
  124.         }

  125.         double fileMiB = ((double) stats.totalBytes()) / MIB;
  126.         printf("Summary:");
  127.         printf("  file sz : %.1f MiB (%d bytes)", fileMiB, stats.totalBytes());
  128.         printf("  padding : %d KiB", stats.paddingBytes() / KIB);
  129.         errw.println();

  130.         printf("Refs:");
  131.         printf("  ref blk : %d", stats.refBlockSize());
  132.         printf("  restarts: %d", stats.restartInterval());
  133.         printf("  refs    : %d", stats.refCount());
  134.         if (stats.refIndexLevels() > 0) {
  135.             int idxSize = (int) Math.round(((double) stats.refIndexSize()) / KIB);
  136.             printf("  idx sz  : %d KiB", idxSize);
  137.             printf("  idx lvl : %d", stats.refIndexLevels());
  138.         }
  139.         printf("  avg ref : %d bytes", stats.refBytes() / refs.size());
  140.         errw.println();

  141.         if (stats.objCount() > 0) {
  142.             int objMiB = (int) Math.round(((double) stats.objBytes()) / MIB);
  143.             int idLen = stats.objIdLength();
  144.             printf("Objects:");
  145.             printf("  obj blk : %d", stats.refBlockSize());
  146.             printf("  restarts: %d", stats.restartInterval());
  147.             printf("  objects : %d", stats.objCount());
  148.             printf("  obj sz  : %d MiB (%d bytes)", objMiB, stats.objBytes());
  149.             if (stats.objIndexSize() > 0) {
  150.                 int s = (int) Math.round(((double) stats.objIndexSize()) / KIB);
  151.                 printf("  idx sz  : %d KiB", s);
  152.                 printf("  idx lvl : %d", stats.objIndexLevels());
  153.             }
  154.             printf("  id len  : %d bytes (%d hex digits)", idLen, 2 * idLen);
  155.             printf("  avg obj : %d bytes", stats.objBytes() / stats.objCount());
  156.             errw.println();
  157.         }
  158.         if (stats.logCount() > 0) {
  159.             int logMiB = (int) Math.round(((double) stats.logBytes()) / MIB);
  160.             printf("Log:");
  161.             printf("  log blk : %d", stats.logBlockSize());
  162.             printf("  logs    : %d", stats.logCount());
  163.             printf("  log sz  : %d MiB (%d bytes)", logMiB, stats.logBytes());
  164.             printf("  avg log : %d bytes", stats.logBytes() / logs.size());
  165.             errw.println();
  166.         }
  167.     }

  168.     private void printf(String fmt, Object... args) throws IOException {
  169.         errw.println(String.format(fmt, args));
  170.     }

  171.     static List<Ref> readRefs(String inputFile) throws IOException {
  172.         List<Ref> refs = new ArrayList<>();
  173.         try (BufferedReader br = new BufferedReader(
  174.                 new InputStreamReader(new FileInputStream(inputFile), UTF_8))) {
  175.             String line;
  176.             while ((line = br.readLine()) != null) {
  177.                 ObjectId id = ObjectId.fromString(line.substring(0, 40));
  178.                 String name = line.substring(41, line.length());
  179.                 if (name.endsWith("^{}")) { //$NON-NLS-1$
  180.                     int lastIdx = refs.size() - 1;
  181.                     Ref last = refs.get(lastIdx);
  182.                     refs.set(lastIdx, new ObjectIdRef.PeeledTag(PACKED,
  183.                             last.getName(), last.getObjectId(), id));
  184.                     continue;
  185.                 }

  186.                 Ref ref;
  187.                 if (name.equals(HEAD)) {
  188.                     ref = new SymbolicRef(name, new ObjectIdRef.Unpeeled(NEW,
  189.                             R_HEADS + MASTER, null));
  190.                 } else {
  191.                     ref = new ObjectIdRef.PeeledNonTag(PACKED, name, id);
  192.                 }
  193.                 refs.add(ref);
  194.             }
  195.         }
  196.         Collections.sort(refs, (a, b) -> a.getName().compareTo(b.getName()));
  197.         return refs;
  198.     }

  199.     private static List<LogEntry> readLog(String logPath)
  200.             throws FileNotFoundException, IOException {
  201.         if (logPath == null) {
  202.             return Collections.emptyList();
  203.         }

  204.         List<LogEntry> log = new ArrayList<>();
  205.         try (BufferedReader br = new BufferedReader(
  206.                 new InputStreamReader(new FileInputStream(logPath), UTF_8))) {
  207.             @SuppressWarnings("nls")
  208.             Pattern pattern = Pattern.compile("([^,]+)" // 1: ref
  209.                     + ",([0-9]+(?:[.][0-9]+)?)" // 2: time
  210.                     + ",([^,]+)" // 3: who
  211.                     + ",([^,]+)" // 4: old
  212.                     + ",([^,]+)" // 5: new
  213.                     + ",(.*)"); // 6: msg
  214.             String line;
  215.             while ((line = br.readLine()) != null) {
  216.                 Matcher m = pattern.matcher(line);
  217.                 if (!m.matches()) {
  218.                     throw new IOException("unparsed line: " + line); //$NON-NLS-1$
  219.                 }
  220.                 String ref = m.group(1);
  221.                 double t = Double.parseDouble(m.group(2));
  222.                 long time = ((long) t) * 1000L;
  223.                 long index = (long) (t * 1e6);
  224.                 String user = m.group(3);
  225.                 ObjectId oldId = parseId(m.group(4));
  226.                 ObjectId newId = parseId(m.group(5));
  227.                 String msg = m.group(6);
  228.                 String email = user + "@gerrit"; //$NON-NLS-1$
  229.                 PersonIdent who = new PersonIdent(user, email, time, -480);
  230.                 log.add(new LogEntry(ref, index, who, oldId, newId, msg));
  231.             }
  232.         }
  233.         Collections.sort(log, LogEntry::compare);
  234.         return log;
  235.     }

  236.     private static long min(List<LogEntry> log) {
  237.         return log.stream().mapToLong(e -> e.updateIndex).min().orElse(0);
  238.     }

  239.     private static long max(List<LogEntry> log) {
  240.         return log.stream().mapToLong(e -> e.updateIndex).max().orElse(0);
  241.     }

  242.     private static ObjectId parseId(String s) {
  243.         if ("NULL".equals(s)) { //$NON-NLS-1$
  244.             return ObjectId.zeroId();
  245.         }
  246.         return ObjectId.fromString(s);
  247.     }

  248.     private static class LogEntry {
  249.         static int compare(LogEntry a, LogEntry b) {
  250.             int cmp = a.ref.compareTo(b.ref);
  251.             if (cmp == 0) {
  252.                 cmp = Long.signum(b.updateIndex - a.updateIndex);
  253.             }
  254.             return cmp;
  255.         }

  256.         final String ref;
  257.         final long updateIndex;
  258.         final PersonIdent who;
  259.         final ObjectId oldId;
  260.         final ObjectId newId;
  261.         final String message;

  262.         LogEntry(String ref, long updateIndex, PersonIdent who,
  263.                 ObjectId oldId, ObjectId newId, String message) {
  264.             this.ref = ref;
  265.             this.updateIndex = updateIndex;
  266.             this.who = who;
  267.             this.oldId = oldId;
  268.             this.newId = newId;
  269.             this.message = message;
  270.         }
  271.     }
  272. }