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.     /**
  30.      * Create a default reader configuration.
  31.      */
  32.     public DfsReaderOptions() {
  33.         setDeltaBaseCacheLimit(10 * MiB);
  34.         setStreamFileThreshold(PackConfig.DEFAULT_BIG_FILE_THRESHOLD);
  35.     }

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

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

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

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

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

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

  100.     /**
  101.      * Update properties by setting fields from the configuration.
  102.      * <p>
  103.      * If a property is not defined in the configuration, then it is left
  104.      * unmodified.
  105.      *
  106.      * @param rc
  107.      *            configuration to read properties from.
  108.      * @return {@code this}
  109.      */
  110.     public DfsReaderOptions fromConfig(Config rc) {
  111.         setDeltaBaseCacheLimit(rc.getInt(
  112.                 CONFIG_CORE_SECTION,
  113.                 CONFIG_DFS_SECTION,
  114.                 CONFIG_KEY_DELTA_BASE_CACHE_LIMIT,
  115.                 getDeltaBaseCacheLimit()));

  116.         long maxMem = Runtime.getRuntime().maxMemory();
  117.         long sft = rc.getLong(
  118.                 CONFIG_CORE_SECTION,
  119.                 CONFIG_DFS_SECTION,
  120.                 CONFIG_KEY_STREAM_FILE_TRESHOLD,
  121.                 getStreamFileThreshold());
  122.         sft = Math.min(sft, maxMem / 4); // don't use more than 1/4 of the heap
  123.         sft = Math.min(sft, Integer.MAX_VALUE); // cannot exceed array length
  124.         setStreamFileThreshold((int) sft);

  125.         setStreamPackBufferSize(rc.getInt(
  126.                 CONFIG_CORE_SECTION,
  127.                 CONFIG_DFS_SECTION,
  128.                 CONFIG_KEY_STREAM_BUFFER,
  129.                 getStreamPackBufferSize()));
  130.         return this;
  131.     }
  132. }