View Javadoc
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_BLOCK_LIMIT;
49  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_SIZE;
50  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
51  
52  import java.text.MessageFormat;
53  
54  import org.eclipse.jgit.internal.JGitText;
55  import org.eclipse.jgit.lib.Config;
56  
57  /** Configuration parameters for {@link DfsBlockCache}. */
58  public class DfsBlockCacheConfig {
59  	/** 1024 (number of bytes in one kibibyte/kilobyte) */
60  	public static final int KB = 1024;
61  
62  	/** 1024 {@link #KB} (number of bytes in one mebibyte/megabyte) */
63  	public static final int MB = 1024 * KB;
64  
65  	private long blockLimit;
66  	private int blockSize;
67  	private double streamRatio;
68  
69  	/** Create a default configuration. */
70  	public DfsBlockCacheConfig() {
71  		setBlockLimit(32 * MB);
72  		setBlockSize(64 * KB);
73  		setStreamRatio(0.30);
74  	}
75  
76  	/**
77  	 * @return maximum number bytes of heap memory to dedicate to caching pack
78  	 *         file data. <b>Default is 32 MB.</b>
79  	 */
80  	public long getBlockLimit() {
81  		return blockLimit;
82  	}
83  
84  	/**
85  	 * @param newLimit
86  	 *            maximum number bytes of heap memory to dedicate to caching
87  	 *            pack file data.
88  	 * @return {@code this}
89  	 */
90  	public DfsBlockCacheConfig setBlockLimit(final long newLimit) {
91  		blockLimit = newLimit;
92  		return this;
93  	}
94  
95  	/**
96  	 * @return size in bytes of a single window mapped or read in from the pack
97  	 *         file. <b>Default is 64 KB.</b>
98  	 */
99  	public int getBlockSize() {
100 		return blockSize;
101 	}
102 
103 	/**
104 	 * @param newSize
105 	 *            size in bytes of a single window read in from the pack file.
106 	 * @return {@code this}
107 	 */
108 	public DfsBlockCacheConfig setBlockSize(final int newSize) {
109 		blockSize = Math.max(512, newSize);
110 		return this;
111 	}
112 
113 	/**
114 	 * @return highest percentage of {@link #getBlockLimit()} a single pack can
115 	 *         occupy while being copied by the pack reuse strategy. <b>Default
116 	 *         is 0.30, or 30%</b>.
117 	 * @since 4.0
118 	 */
119 	public double getStreamRatio() {
120 		return streamRatio;
121 	}
122 
123 	/**
124 	 * @param ratio
125 	 *            percentage of cache to occupy with a copied pack.
126 	 * @return {@code this}
127 	 * @since 4.0
128 	 */
129 	public DfsBlockCacheConfig setStreamRatio(double ratio) {
130 		streamRatio = Math.max(0, Math.min(ratio, 1.0));
131 		return this;
132 	}
133 
134 	/**
135 	 * Update properties by setting fields from the configuration.
136 	 * <p>
137 	 * If a property is not defined in the configuration, then it is left
138 	 * unmodified.
139 	 *
140 	 * @param rc
141 	 *            configuration to read properties from.
142 	 * @return {@code this}
143 	 */
144 	public DfsBlockCacheConfig fromConfig(final Config rc) {
145 		setBlockLimit(rc.getLong(
146 				CONFIG_CORE_SECTION,
147 				CONFIG_DFS_SECTION,
148 				CONFIG_KEY_BLOCK_LIMIT,
149 				getBlockLimit()));
150 
151 		setBlockSize(rc.getInt(
152 				CONFIG_CORE_SECTION,
153 				CONFIG_DFS_SECTION,
154 				CONFIG_KEY_BLOCK_SIZE,
155 				getBlockSize()));
156 
157 		String v = rc.getString(
158 				CONFIG_CORE_SECTION,
159 				CONFIG_DFS_SECTION,
160 				CONFIG_KEY_STREAM_RATIO);
161 		if (v != null) {
162 			try {
163 				setStreamRatio(Double.parseDouble(v));
164 			} catch (NumberFormatException e) {
165 				throw new IllegalArgumentException(MessageFormat.format(
166 						JGitText.get().enumValueNotSupported3,
167 						CONFIG_CORE_SECTION,
168 						CONFIG_DFS_SECTION,
169 						CONFIG_KEY_STREAM_RATIO, v));
170 			}
171 		}
172 		return this;
173 	}
174 }