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