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