1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.ketch;
45
46 import static java.util.concurrent.TimeUnit.DAYS;
47 import static java.util.concurrent.TimeUnit.HOURS;
48 import static java.util.concurrent.TimeUnit.MILLISECONDS;
49 import static java.util.concurrent.TimeUnit.MINUTES;
50 import static java.util.concurrent.TimeUnit.SECONDS;
51 import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_COMMIT;
52 import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_SPEED;
53 import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_TYPE;
54 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REMOTE;
55
56 import java.util.Collections;
57 import java.util.HashMap;
58 import java.util.Map;
59 import java.util.concurrent.TimeUnit;
60 import java.util.regex.Matcher;
61 import java.util.regex.Pattern;
62
63 import org.eclipse.jgit.internal.ketch.KetchReplica.CommitMethod;
64 import org.eclipse.jgit.internal.ketch.KetchReplica.CommitSpeed;
65 import org.eclipse.jgit.internal.ketch.KetchReplica.Participation;
66 import org.eclipse.jgit.lib.Config;
67
68
69
70
71 public class ReplicaConfig {
72
73
74
75
76
77
78
79
80
81 public static ReplicaConfig newFromConfig(Config cfg, String name) {
82 return new ReplicaConfig().fromConfig(cfg, name);
83 }
84
85 private Participation participation = Participation.FULL;
86 private CommitMethod commitMethod = CommitMethod.ALL_REFS;
87 private CommitSpeed commitSpeed = CommitSpeed.BATCHED;
88 private long minRetry = SECONDS.toMillis(5);
89 private long maxRetry = MINUTES.toMillis(1);
90
91
92
93
94
95
96 public Participation getParticipation() {
97 return participation;
98 }
99
100
101
102
103
104
105 public CommitMethod getCommitMethod() {
106 return commitMethod;
107 }
108
109
110
111
112
113
114 public CommitSpeed getCommitSpeed() {
115 return commitSpeed;
116 }
117
118
119
120
121
122
123
124
125 public long getMinRetry(TimeUnit unit) {
126 return unit.convert(minRetry, MILLISECONDS);
127 }
128
129
130
131
132
133
134
135
136 public long getMaxRetry(TimeUnit unit) {
137 return unit.convert(maxRetry, MILLISECONDS);
138 }
139
140
141
142
143
144
145
146
147
148
149 public ReplicaConfig fromConfig(Config cfg, String name) {
150 participation = cfg.getEnum(
151 CONFIG_KEY_REMOTE, name, CONFIG_KEY_TYPE,
152 participation);
153 commitMethod = cfg.getEnum(
154 CONFIG_KEY_REMOTE, name, CONFIG_KEY_COMMIT,
155 commitMethod);
156 commitSpeed = cfg.getEnum(
157 CONFIG_KEY_REMOTE, name, CONFIG_KEY_SPEED,
158 commitSpeed);
159 minRetry = getMillis(cfg, name, "ketch-minRetry", minRetry);
160 maxRetry = getMillis(cfg, name, "ketch-maxRetry", maxRetry);
161 return this;
162 }
163
164 private static long getMillis(Config cfg, String name, String key,
165 long defaultValue) {
166 String valStr = cfg.getString(CONFIG_KEY_REMOTE, name, key);
167 if (valStr == null) {
168 return defaultValue;
169 }
170
171 valStr = valStr.trim();
172 if (valStr.isEmpty()) {
173 return defaultValue;
174 }
175
176 Matcher m = UnitMap.PATTERN.matcher(valStr);
177 if (!m.matches()) {
178 return defaultValue;
179 }
180
181 String digits = m.group(1);
182 String unitName = m.group(2).trim();
183 TimeUnit unit = UnitMap.UNITS.get(unitName);
184 if (unit == null) {
185 return defaultValue;
186 }
187
188 try {
189 if (digits.indexOf('.') == -1) {
190 return unit.toMillis(Long.parseLong(digits));
191 }
192
193 double val = Double.parseDouble(digits);
194 return (long) (val * unit.toMillis(1));
195 } catch (NumberFormatException nfe) {
196 return defaultValue;
197 }
198 }
199
200 static class UnitMap {
201 static final Pattern PATTERN = Pattern
202 .compile("^([1-9][0-9]*(?:\\.[0-9]*)?)\\s*(.*)$");
203
204 static final Map<String, TimeUnit> UNITS;
205
206 static {
207 Map<String, TimeUnit> m = new HashMap<>();
208 TimeUnit u = MILLISECONDS;
209 m.put("", u);
210 m.put("ms", u);
211 m.put("millis", u);
212 m.put("millisecond", u);
213 m.put("milliseconds", u);
214
215 u = SECONDS;
216 m.put("s", u);
217 m.put("sec", u);
218 m.put("secs", u);
219 m.put("second", u);
220 m.put("seconds", u);
221
222 u = MINUTES;
223 m.put("m", u);
224 m.put("min", u);
225 m.put("mins", u);
226 m.put("minute", u);
227 m.put("minutes", u);
228
229 u = HOURS;
230 m.put("h", u);
231 m.put("hr", u);
232 m.put("hrs", u);
233 m.put("hour", u);
234 m.put("hours", u);
235
236 u = DAYS;
237 m.put("d", u);
238 m.put("day", u);
239 m.put("days", u);
240
241 UNITS = Collections.unmodifiableMap(m);
242 }
243
244 private UnitMap() {
245 }
246 }
247 }