DfsReaderOptions.java

  1. /*
  2.  * Copyright (C) 2011, 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.dfs;

  11. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
  12. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
  13. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_DELTA_BASE_CACHE_LIMIT;
  14. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_BUFFER;
  15. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_FILE_TRESHOLD;

  16. import org.eclipse.jgit.lib.Config;
  17. import org.eclipse.jgit.storage.pack.PackConfig;

  18. /**
  19.  * Options controlling how objects are read from a DFS stored repository.
  20.  */
  21. public class DfsReaderOptions {
  22.     /** 1024 (number of bytes in one kibibyte/kilobyte) */
  23.     public static final int KiB = 1024;

  24.     /** 1024 {@link #KiB} (number of bytes in one mebibyte/megabyte) */
  25.     public static final int MiB = 1024 * KiB;

  26.     private int deltaBaseCacheLimit;
  27.     private int streamFileThreshold;

  28.     private int streamPackBufferSize;

  29.     private boolean loadRevIndexInParallel;

  30.     /**
  31.      * Create a default reader configuration.
  32.      */
  33.     public DfsReaderOptions() {
  34.         setDeltaBaseCacheLimit(10 * MiB);
  35.         setStreamFileThreshold(PackConfig.DEFAULT_BIG_FILE_THRESHOLD);
  36.     }

  37.     /**
  38.      * Get maximum number of bytes to hold in per-reader DeltaBaseCache.
  39.      *
  40.      * @return maximum number of bytes to hold in per-reader DeltaBaseCache.
  41.      */
  42.     public int getDeltaBaseCacheLimit() {
  43.         return deltaBaseCacheLimit;
  44.     }

  45.     /**
  46.      * Set the maximum number of bytes in the DeltaBaseCache.
  47.      *
  48.      * @param maxBytes
  49.      *            the new limit.
  50.      * @return {@code this}
  51.      */
  52.     public DfsReaderOptions setDeltaBaseCacheLimit(int maxBytes) {
  53.         deltaBaseCacheLimit = Math.max(0, maxBytes);
  54.         return this;
  55.     }

  56.     /**
  57.      * Get the size threshold beyond which objects must be streamed.
  58.      *
  59.      * @return the size threshold beyond which objects must be streamed.
  60.      */
  61.     public int getStreamFileThreshold() {
  62.         return streamFileThreshold;
  63.     }

  64.     /**
  65.      * Set new byte limit for objects that must be streamed.
  66.      *
  67.      * @param newLimit
  68.      *            new byte limit for objects that must be streamed. Objects
  69.      *            smaller than this size can be obtained as a contiguous byte
  70.      *            array, while objects bigger than this size require using an
  71.      *            {@link org.eclipse.jgit.lib.ObjectStream}.
  72.      * @return {@code this}
  73.      */
  74.     public DfsReaderOptions setStreamFileThreshold(int newLimit) {
  75.         streamFileThreshold = Math.max(0, newLimit);
  76.         return this;
  77.     }

  78.     /**
  79.      * Get number of bytes to use for buffering when streaming a pack file
  80.      * during copying.
  81.      *
  82.      * @return number of bytes to use for buffering when streaming a pack file
  83.      *         during copying. If 0 the block size of the pack is used.
  84.      */
  85.     public int getStreamPackBufferSize() {
  86.         return streamPackBufferSize;
  87.     }

  88.     /**
  89.      * Set new buffer size in bytes for buffers used when streaming pack files
  90.      * during copying.
  91.      *
  92.      * @param bufsz
  93.      *            new buffer size in bytes for buffers used when streaming pack
  94.      *            files during copying.
  95.      * @return {@code this}
  96.      */
  97.     public DfsReaderOptions setStreamPackBufferSize(int bufsz) {
  98.         streamPackBufferSize = Math.max(0, bufsz);
  99.         return this;
  100.     }

  101.     /**
  102.      * Check if reverse index should be loaded in parallel.
  103.      *
  104.      * @return true if reverse index is loaded in parallel for bitmap index.
  105.      */
  106.     public boolean shouldLoadRevIndexInParallel() {
  107.         return loadRevIndexInParallel;
  108.     }

  109.     /**
  110.      * Enable (or disable) parallel loading of reverse index.
  111.      *
  112.      * @param loadRevIndexInParallel
  113.      *            whether to load reverse index in parallel.
  114.      * @return {@code this}
  115.      */
  116.     public DfsReaderOptions setLoadRevIndexInParallel(
  117.             boolean loadRevIndexInParallel) {
  118.         this.loadRevIndexInParallel = loadRevIndexInParallel;
  119.         return this;
  120.     }

  121.     /**
  122.      * Update properties by setting fields from the configuration.
  123.      * <p>
  124.      * If a property is not defined in the configuration, then it is left
  125.      * unmodified.
  126.      *
  127.      * @param rc
  128.      *            configuration to read properties from.
  129.      * @return {@code this}
  130.      */
  131.     public DfsReaderOptions fromConfig(Config rc) {
  132.         setDeltaBaseCacheLimit(rc.getInt(
  133.                 CONFIG_CORE_SECTION,
  134.                 CONFIG_DFS_SECTION,
  135.                 CONFIG_KEY_DELTA_BASE_CACHE_LIMIT,
  136.                 getDeltaBaseCacheLimit()));

  137.         long maxMem = Runtime.getRuntime().maxMemory();
  138.         long sft = rc.getLong(
  139.                 CONFIG_CORE_SECTION,
  140.                 CONFIG_DFS_SECTION,
  141.                 CONFIG_KEY_STREAM_FILE_TRESHOLD,
  142.                 getStreamFileThreshold());
  143.         sft = Math.min(sft, maxMem / 4); // don't use more than 1/4 of the heap
  144.         sft = Math.min(sft, Integer.MAX_VALUE); // cannot exceed array length
  145.         setStreamFileThreshold((int) sft);

  146.         setStreamPackBufferSize(rc.getInt(
  147.                 CONFIG_CORE_SECTION,
  148.                 CONFIG_DFS_SECTION,
  149.                 CONFIG_KEY_STREAM_BUFFER,
  150.                 getStreamPackBufferSize()));
  151.         return this;
  152.     }
  153. }