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