You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DfsReaderOptions.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2011, 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.internal.storage.dfs;
  44. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
  45. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
  46. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_DELTA_BASE_CACHE_LIMIT;
  47. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_BUFFER;
  48. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_FILE_TRESHOLD;
  49. import org.eclipse.jgit.lib.Config;
  50. import org.eclipse.jgit.storage.pack.PackConfig;
  51. /**
  52. * Options controlling how objects are read from a DFS stored repository.
  53. */
  54. public class DfsReaderOptions {
  55. /** 1024 (number of bytes in one kibibyte/kilobyte) */
  56. public static final int KiB = 1024;
  57. /** 1024 {@link #KiB} (number of bytes in one mebibyte/megabyte) */
  58. public static final int MiB = 1024 * KiB;
  59. private int deltaBaseCacheLimit;
  60. private int streamFileThreshold;
  61. private int streamPackBufferSize;
  62. /**
  63. * Create a default reader configuration.
  64. */
  65. public DfsReaderOptions() {
  66. setDeltaBaseCacheLimit(10 * MiB);
  67. setStreamFileThreshold(PackConfig.DEFAULT_BIG_FILE_THRESHOLD);
  68. }
  69. /**
  70. * Get maximum number of bytes to hold in per-reader DeltaBaseCache.
  71. *
  72. * @return maximum number of bytes to hold in per-reader DeltaBaseCache.
  73. */
  74. public int getDeltaBaseCacheLimit() {
  75. return deltaBaseCacheLimit;
  76. }
  77. /**
  78. * Set the maximum number of bytes in the DeltaBaseCache.
  79. *
  80. * @param maxBytes
  81. * the new limit.
  82. * @return {@code this}
  83. */
  84. public DfsReaderOptions setDeltaBaseCacheLimit(int maxBytes) {
  85. deltaBaseCacheLimit = Math.max(0, maxBytes);
  86. return this;
  87. }
  88. /**
  89. * Get the size threshold beyond which objects must be streamed.
  90. *
  91. * @return the size threshold beyond which objects must be streamed.
  92. */
  93. public int getStreamFileThreshold() {
  94. return streamFileThreshold;
  95. }
  96. /**
  97. * Set new byte limit for objects that must be streamed.
  98. *
  99. * @param newLimit
  100. * new byte limit for objects that must be streamed. Objects
  101. * smaller than this size can be obtained as a contiguous byte
  102. * array, while objects bigger than this size require using an
  103. * {@link org.eclipse.jgit.lib.ObjectStream}.
  104. * @return {@code this}
  105. */
  106. public DfsReaderOptions setStreamFileThreshold(int newLimit) {
  107. streamFileThreshold = Math.max(0, newLimit);
  108. return this;
  109. }
  110. /**
  111. * Get number of bytes to use for buffering when streaming a pack file
  112. * during copying.
  113. *
  114. * @return number of bytes to use for buffering when streaming a pack file
  115. * during copying. If 0 the block size of the pack is used.
  116. */
  117. public int getStreamPackBufferSize() {
  118. return streamPackBufferSize;
  119. }
  120. /**
  121. * Set new buffer size in bytes for buffers used when streaming pack files
  122. * during copying.
  123. *
  124. * @param bufsz
  125. * new buffer size in bytes for buffers used when streaming pack
  126. * files during copying.
  127. * @return {@code this}
  128. */
  129. public DfsReaderOptions setStreamPackBufferSize(int bufsz) {
  130. streamPackBufferSize = Math.max(0, bufsz);
  131. return this;
  132. }
  133. /**
  134. * Update properties by setting fields from the configuration.
  135. * <p>
  136. * If a property is not defined in the configuration, then it is left
  137. * unmodified.
  138. *
  139. * @param rc
  140. * configuration to read properties from.
  141. * @return {@code this}
  142. */
  143. public DfsReaderOptions fromConfig(Config rc) {
  144. setDeltaBaseCacheLimit(rc.getInt(
  145. CONFIG_CORE_SECTION,
  146. CONFIG_DFS_SECTION,
  147. CONFIG_KEY_DELTA_BASE_CACHE_LIMIT,
  148. getDeltaBaseCacheLimit()));
  149. long maxMem = Runtime.getRuntime().maxMemory();
  150. long sft = rc.getLong(
  151. CONFIG_CORE_SECTION,
  152. CONFIG_DFS_SECTION,
  153. CONFIG_KEY_STREAM_FILE_TRESHOLD,
  154. getStreamFileThreshold());
  155. sft = Math.min(sft, maxMem / 4); // don't use more than 1/4 of the heap
  156. sft = Math.min(sft, Integer.MAX_VALUE); // cannot exceed array length
  157. setStreamFileThreshold((int) sft);
  158. setStreamPackBufferSize(rc.getInt(
  159. CONFIG_CORE_SECTION,
  160. CONFIG_DFS_SECTION,
  161. CONFIG_KEY_STREAM_BUFFER,
  162. getStreamPackBufferSize()));
  163. return this;
  164. }
  165. }