1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.dfs;
12
13 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
14 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
15 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_LIMIT;
16 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_SIZE;
17 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CONCURRENCY_LEVEL;
18 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
19
20 import java.text.MessageFormat;
21 import java.util.Collections;
22 import java.util.Map;
23 import java.util.function.Consumer;
24
25 import org.eclipse.jgit.internal.JGitText;
26 import org.eclipse.jgit.internal.storage.pack.PackExt;
27 import org.eclipse.jgit.lib.Config;
28
29
30
31
32
33 public class DfsBlockCacheConfig {
34
35 public static final int KB = 1024;
36
37
38 public static final int MB = 1024 * KB;
39
40
41 public static final int DEFAULT_CACHE_HOT_MAX = 1;
42
43 private long blockLimit;
44 private int blockSize;
45 private double streamRatio;
46 private int concurrencyLevel;
47
48 private Consumer<Long> refLock;
49
50 private Map<PackExt, Integer> cacheHotMap;
51
52
53
54
55 public DfsBlockCacheConfig() {
56 setBlockLimit(32 * MB);
57 setBlockSize(64 * KB);
58 setStreamRatio(0.30);
59 setConcurrencyLevel(32);
60 cacheHotMap = Collections.emptyMap();
61 }
62
63
64
65
66
67
68
69
70 public long getBlockLimit() {
71 return blockLimit;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public DfsBlockCacheConfig setBlockLimit(long newLimit) {
89 if (newLimit <= 0) {
90 throw new IllegalArgumentException(MessageFormat.format(
91 JGitText.get().blockLimitNotPositive,
92 Long.valueOf(newLimit)));
93 }
94 blockLimit = newLimit;
95 return this;
96 }
97
98
99
100
101
102
103
104
105 public int getBlockSize() {
106 return blockSize;
107 }
108
109
110
111
112
113
114
115
116
117 public DfsBlockCacheConfig setBlockSize(int newSize) {
118 int size = Math.max(512, newSize);
119 if ((size & (size - 1)) != 0) {
120 throw new IllegalArgumentException(
121 JGitText.get().blockSizeNotPowerOf2);
122 }
123 blockSize = size;
124 return this;
125 }
126
127
128
129
130
131
132
133 public int getConcurrencyLevel() {
134 return concurrencyLevel;
135 }
136
137
138
139
140
141
142
143
144
145 public DfsBlockCacheConfig setConcurrencyLevel(
146 final int newConcurrencyLevel) {
147 concurrencyLevel = newConcurrencyLevel;
148 return this;
149 }
150
151
152
153
154
155
156
157
158
159 public double getStreamRatio() {
160 return streamRatio;
161 }
162
163
164
165
166
167
168
169
170 public DfsBlockCacheConfig setStreamRatio(double ratio) {
171 streamRatio = Math.max(0, Math.min(ratio, 1.0));
172 return this;
173 }
174
175
176
177
178
179
180 public Consumer<Long> getRefLockWaitTimeConsumer() {
181 return refLock;
182 }
183
184
185
186
187
188
189
190
191 public DfsBlockCacheConfig setRefLockWaitTimeConsumer(Consumer<Long> c) {
192 refLock = c;
193 return this;
194 }
195
196
197
198
199
200
201 public Map<PackExt, Integer> getCacheHotMap() {
202 return cacheHotMap;
203 }
204
205
206
207
208
209
210
211
212 public DfsBlockCacheConfig setCacheHotMap(
213 Map<PackExt, Integer> cacheHotMap) {
214 this.cacheHotMap = Collections.unmodifiableMap(cacheHotMap);
215 return this;
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 public DfsBlockCacheConfig fromConfig(Config rc) {
232 long cfgBlockLimit = rc.getLong(
233 CONFIG_CORE_SECTION,
234 CONFIG_DFS_SECTION,
235 CONFIG_KEY_BLOCK_LIMIT,
236 getBlockLimit());
237 int cfgBlockSize = rc.getInt(
238 CONFIG_CORE_SECTION,
239 CONFIG_DFS_SECTION,
240 CONFIG_KEY_BLOCK_SIZE,
241 getBlockSize());
242 if (cfgBlockLimit % cfgBlockSize != 0) {
243 throw new IllegalArgumentException(MessageFormat.format(
244 JGitText.get().blockLimitNotMultipleOfBlockSize,
245 Long.valueOf(cfgBlockLimit),
246 Long.valueOf(cfgBlockSize)));
247 }
248
249 setBlockLimit(cfgBlockLimit);
250 setBlockSize(cfgBlockSize);
251
252 setConcurrencyLevel(rc.getInt(
253 CONFIG_CORE_SECTION,
254 CONFIG_DFS_SECTION,
255 CONFIG_KEY_CONCURRENCY_LEVEL,
256 getConcurrencyLevel()));
257
258 String v = rc.getString(
259 CONFIG_CORE_SECTION,
260 CONFIG_DFS_SECTION,
261 CONFIG_KEY_STREAM_RATIO);
262 if (v != null) {
263 try {
264 setStreamRatio(Double.parseDouble(v));
265 } catch (NumberFormatException e) {
266 throw new IllegalArgumentException(MessageFormat.format(
267 JGitText.get().enumValueNotSupported3,
268 CONFIG_CORE_SECTION,
269 CONFIG_DFS_SECTION,
270 CONFIG_KEY_STREAM_RATIO, v), e);
271 }
272 }
273 return this;
274 }
275 }