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 public class ReplicaConfig {
70
71
72
73
74
75
76
77
78
79 public static ReplicaConfig newFromConfig(Config cfg, String name) {
80 return new ReplicaConfig().fromConfig(cfg, name);
81 }
82
83 private Participation participation = Participation.FULL;
84 private CommitMethod commitMethod = CommitMethod.ALL_REFS;
85 private CommitSpeed commitSpeed = CommitSpeed.BATCHED;
86 private long minRetry = SECONDS.toMillis(5);
87 private long maxRetry = MINUTES.toMillis(1);
88
89
90 public Participation getParticipation() {
91 return participation;
92 }
93
94
95 public CommitMethod getCommitMethod() {
96 return commitMethod;
97 }
98
99
100 public CommitSpeed getCommitSpeed() {
101 return commitSpeed;
102 }
103
104
105
106
107
108
109
110
111 public long getMinRetry(TimeUnit unit) {
112 return unit.convert(minRetry, MILLISECONDS);
113 }
114
115
116
117
118
119
120
121
122 public long getMaxRetry(TimeUnit unit) {
123 return unit.convert(maxRetry, MILLISECONDS);
124 }
125
126
127
128
129
130
131
132
133
134
135 public ReplicaConfig fromConfig(Config cfg, String name) {
136 participation = cfg.getEnum(
137 CONFIG_KEY_REMOTE, name, CONFIG_KEY_TYPE,
138 participation);
139 commitMethod = cfg.getEnum(
140 CONFIG_KEY_REMOTE, name, CONFIG_KEY_COMMIT,
141 commitMethod);
142 commitSpeed = cfg.getEnum(
143 CONFIG_KEY_REMOTE, name, CONFIG_KEY_SPEED,
144 commitSpeed);
145 minRetry = getMillis(cfg, name, "ketch-minRetry", minRetry);
146 maxRetry = getMillis(cfg, name, "ketch-maxRetry", maxRetry);
147 return this;
148 }
149
150 private static long getMillis(Config cfg, String name, String key,
151 long defaultValue) {
152 String valStr = cfg.getString(CONFIG_KEY_REMOTE, name, key);
153 if (valStr == null) {
154 return defaultValue;
155 }
156
157 valStr = valStr.trim();
158 if (valStr.isEmpty()) {
159 return defaultValue;
160 }
161
162 Matcher m = UnitMap.PATTERN.matcher(valStr);
163 if (!m.matches()) {
164 return defaultValue;
165 }
166
167 String digits = m.group(1);
168 String unitName = m.group(2).trim();
169 TimeUnit unit = UnitMap.UNITS.get(unitName);
170 if (unit == null) {
171 return defaultValue;
172 }
173
174 try {
175 if (digits.indexOf('.') == -1) {
176 return unit.toMillis(Long.parseLong(digits));
177 }
178
179 double val = Double.parseDouble(digits);
180 return (long) (val * unit.toMillis(1));
181 } catch (NumberFormatException nfe) {
182 return defaultValue;
183 }
184 }
185
186 static class UnitMap {
187 static final Pattern PATTERN = Pattern
188 .compile("^([1-9][0-9]*(?:\\.[0-9]*)?)\\s*(.*)$");
189
190 static final Map<String, TimeUnit> UNITS;
191
192 static {
193 Map<String, TimeUnit> m = new HashMap<>();
194 TimeUnit u = MILLISECONDS;
195 m.put("", u);
196 m.put("ms", u);
197 m.put("millis", u);
198 m.put("millisecond", u);
199 m.put("milliseconds", u);
200
201 u = SECONDS;
202 m.put("s", u);
203 m.put("sec", u);
204 m.put("secs", u);
205 m.put("second", u);
206 m.put("seconds", u);
207
208 u = MINUTES;
209 m.put("m", u);
210 m.put("min", u);
211 m.put("mins", u);
212 m.put("minute", u);
213 m.put("minutes", u);
214
215 u = HOURS;
216 m.put("h", u);
217 m.put("hr", u);
218 m.put("hrs", u);
219 m.put("hour", u);
220 m.put("hours", u);
221
222 u = DAYS;
223 m.put("d", u);
224 m.put("day", u);
225 m.put("days", u);
226
227 UNITS = Collections.unmodifiableMap(m);
228 }
229
230 private UnitMap() {
231 }
232 }
233 }