ReftableConfig.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.internal.storage.reftable;

  11. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.MAX_BLOCK_SIZE;

  12. import org.eclipse.jgit.lib.Config;
  13. import org.eclipse.jgit.lib.Repository;

  14. /**
  15.  * Configuration used by a reftable writer when constructing the stream.
  16.  */
  17. public class ReftableConfig {
  18.     private int refBlockSize = 4 << 10;
  19.     private int logBlockSize;
  20.     private int restartInterval;
  21.     private int maxIndexLevels;
  22.     private boolean alignBlocks = true;
  23.     private boolean indexObjects = true;

  24.     /**
  25.      * Create a default configuration.
  26.      */
  27.     public ReftableConfig() {
  28.     }

  29.     /**
  30.      * Create a configuration honoring the repository's settings.
  31.      *
  32.      * @param db
  33.      *            the repository to read settings from. The repository is not
  34.      *            retained by the new configuration, instead its settings are
  35.      *            copied during the constructor.
  36.      */
  37.     public ReftableConfig(Repository db) {
  38.         fromConfig(db.getConfig());
  39.     }

  40.     /**
  41.      * Create a configuration honoring settings in a
  42.      * {@link org.eclipse.jgit.lib.Config}.
  43.      *
  44.      * @param cfg
  45.      *            the source to read settings from. The source is not retained
  46.      *            by the new configuration, instead its settings are copied
  47.      *            during the constructor.
  48.      */
  49.     public ReftableConfig(Config cfg) {
  50.         fromConfig(cfg);
  51.     }

  52.     /**
  53.      * Copy an existing configuration to a new instance.
  54.      *
  55.      * @param cfg
  56.      *            the source configuration to copy from.
  57.      */
  58.     public ReftableConfig(ReftableConfig cfg) {
  59.         this.refBlockSize = cfg.refBlockSize;
  60.         this.logBlockSize = cfg.logBlockSize;
  61.         this.restartInterval = cfg.restartInterval;
  62.         this.maxIndexLevels = cfg.maxIndexLevels;
  63.         this.alignBlocks = cfg.alignBlocks;
  64.         this.indexObjects = cfg.indexObjects;
  65.     }

  66.     /**
  67.      * Get desired output block size for references, in bytes.
  68.      *
  69.      * @return desired output block size for references, in bytes.
  70.      */
  71.     public int getRefBlockSize() {
  72.         return refBlockSize;
  73.     }

  74.     /**
  75.      * Set desired output block size for references, in bytes.
  76.      *
  77.      * @param szBytes
  78.      *            desired output block size for references, in bytes.
  79.      */
  80.     public void setRefBlockSize(int szBytes) {
  81.         if (szBytes > MAX_BLOCK_SIZE) {
  82.             throw new IllegalArgumentException();
  83.         }
  84.         refBlockSize = Math.max(0, szBytes);
  85.     }

  86.     /**
  87.      * Get desired output block size for log entries, in bytes.
  88.      *
  89.      * @return desired output block size for log entries, in bytes. If 0 the
  90.      *         writer will default to {@code 2 * getRefBlockSize()}.
  91.      */
  92.     public int getLogBlockSize() {
  93.         return logBlockSize;
  94.     }

  95.     /**
  96.      * Set desired output block size for log entries, in bytes.
  97.      *
  98.      * @param szBytes
  99.      *            desired output block size for log entries, in bytes. If 0 will
  100.      *            default to {@code 2 * getRefBlockSize()}.
  101.      */
  102.     public void setLogBlockSize(int szBytes) {
  103.         if (szBytes > MAX_BLOCK_SIZE) {
  104.             throw new IllegalArgumentException();
  105.         }
  106.         logBlockSize = Math.max(0, szBytes);
  107.     }

  108.     /**
  109.      * Get number of references between binary search markers.
  110.      *
  111.      * @return number of references between binary search markers.
  112.      */
  113.     public int getRestartInterval() {
  114.         return restartInterval;
  115.     }

  116.     /**
  117.      * <p>Setter for the field <code>restartInterval</code>.</p>
  118.      *
  119.      * @param interval
  120.      *            number of references between binary search markers. If
  121.      *            {@code interval} is 0 (default), the writer will select a
  122.      *            default value based on the block size.
  123.      */
  124.     public void setRestartInterval(int interval) {
  125.         restartInterval = Math.max(0, interval);
  126.     }

  127.     /**
  128.      * Get maximum depth of the index; 0 for unlimited.
  129.      *
  130.      * @return maximum depth of the index; 0 for unlimited.
  131.      */
  132.     public int getMaxIndexLevels() {
  133.         return maxIndexLevels;
  134.     }

  135.     /**
  136.      * Set maximum number of levels to use in indexes.
  137.      *
  138.      * @param levels
  139.      *            maximum number of levels to use in indexes. Lower levels of
  140.      *            the index respect {@link #getRefBlockSize()}, and the highest
  141.      *            level may exceed that if the number of levels is limited.
  142.      */
  143.     public void setMaxIndexLevels(int levels) {
  144.         maxIndexLevels = Math.max(0, levels);
  145.     }

  146.     /**
  147.      * Whether the writer should align blocks.
  148.      *
  149.      * @return {@code true} if the writer should align blocks.
  150.      */
  151.     public boolean isAlignBlocks() {
  152.         return alignBlocks;
  153.     }

  154.     /**
  155.      * Whether blocks are written aligned to multiples of
  156.      * {@link #getRefBlockSize()}.
  157.      *
  158.      * @param align
  159.      *            if {@code true} blocks are written aligned to multiples of
  160.      *            {@link #getRefBlockSize()}. May increase file size due to NUL
  161.      *            padding bytes added between blocks. Default is {@code true}.
  162.      */
  163.     public void setAlignBlocks(boolean align) {
  164.         alignBlocks = align;
  165.     }

  166.     /**
  167.      * Whether the writer should index object to ref.
  168.      *
  169.      * @return {@code true} if the writer should index object to ref.
  170.      */
  171.     public boolean isIndexObjects() {
  172.         return indexObjects;
  173.     }

  174.     /**
  175.      * Whether the reftable may include additional storage to efficiently map
  176.      * from {@code ObjectId} to reference names.
  177.      *
  178.      * @param index
  179.      *            if {@code true} the reftable may include additional storage to
  180.      *            efficiently map from {@code ObjectId} to reference names. By
  181.      *            default, {@code true}.
  182.      */
  183.     public void setIndexObjects(boolean index) {
  184.         indexObjects = index;
  185.     }

  186.     /**
  187.      * Update properties by setting fields from the configuration.
  188.      *
  189.      * If a property's corresponding variable is not defined in the supplied
  190.      * configuration, then it is left unmodified.
  191.      *
  192.      * @param rc
  193.      *            configuration to read properties from.
  194.      */
  195.     public void fromConfig(Config rc) {
  196.         refBlockSize = rc.getInt("reftable", "blockSize", refBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
  197.         logBlockSize = rc.getInt("reftable", "logBlockSize", logBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
  198.         restartInterval = rc.getInt("reftable", "restartInterval", restartInterval); //$NON-NLS-1$ //$NON-NLS-2$
  199.         maxIndexLevels = rc.getInt("reftable", "indexLevels", maxIndexLevels); //$NON-NLS-1$ //$NON-NLS-2$
  200.         alignBlocks = rc.getBoolean("reftable", "alignBlocks", alignBlocks); //$NON-NLS-1$ //$NON-NLS-2$
  201.         indexObjects = rc.getBoolean("reftable", "indexObjects", indexObjects); //$NON-NLS-1$ //$NON-NLS-2$
  202.     }
  203. }