-
Notifications
You must be signed in to change notification settings - Fork 2
/
AfloatDBClientCliRunner.java
267 lines (208 loc) · 7.59 KB
/
AfloatDBClientCliRunner.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package io.microraft.afloatdb.client;
import static java.nio.file.Files.readAllLines;
import static java.util.Objects.requireNonNull;
import com.typesafe.config.ConfigFactory;
import io.microraft.afloatdb.client.config.AfloatDBClientConfig;
import io.microraft.afloatdb.client.kv.KV;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.function.Function;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
/**
* Main for the command line interface application
*/
@Command
public class AfloatDBClientCliRunner implements Runnable {
@Option(names = {"-s", "--server"}, required = true)
public String serverAddress;
@Option(names = {"-cfg", "--config-file-path"}, required = false)
public String configFilePath;
@Option(names = {"-id", "--client-id"}, required = false)
public String clientId;
@Option(names = {"-sc", "--single-connection"}, required = false)
public boolean singleConnection;
@Option(names = {"-tsec", "--rpc-timeout-secs"}, required = false)
public Integer rpcTimeoutSecs;
@Option(names = {"-prcfg", "--print-config"}, required = false)
public boolean printConfig;
@Option(names = {"-k", "--key"}, required = false)
public String key;
@Option(names = {"-v", "--value"}, required = false)
public String value;
@Option(names = {"-nv", "--new-value"}, required = false)
public String newValue;
@Option(names = {"-cix", "--min-commit-index"}, required = false)
public long minCommitIndex = -1;
private AfloatDBClientConfig initConfig() {
AfloatDBClientConfig.AfloatDBClientConfigBuilder configBuilder = new AfloatDBClientConfig.AfloatDBClientConfigBuilder();
if (configFilePath != null) {
try {
configBuilder.setConfig(ConfigFactory.parseString(
String.join("\n", readAllLines(Paths.get(configFilePath), StandardCharsets.UTF_8))));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Cannot read config path: " + configFilePath);
System.exit(-1);
}
}
configBuilder.setServerAddress(serverAddress);
configBuilder.setSingleConnection(singleConnection);
if (clientId != null) {
configBuilder.setClientId(clientId);
}
if (rpcTimeoutSecs != null) {
configBuilder.setRpcTimeoutSecs(rpcTimeoutSecs);
}
AfloatDBClientConfig config = configBuilder.build();
if (printConfig) {
System.out.println(config);
}
org.apache.logging.log4j.core.config.Configurator.setRootLevel(org.apache.logging.log4j.Level.OFF);
return config;
}
private AfloatDBClient initClient() {
AfloatDBClientConfig config = initConfig();
return AfloatDBClient.newInstance(config);
}
private Long parseValueLong() {
try {
return value != null ? Long.parseLong(value) : null;
} catch (Throwable ignored) {
return null;
}
}
private Long parseNewValueLong() {
try {
return value != null ? Long.parseLong(newValue) : null;
} catch (Throwable ignored) {
return null;
}
}
private void failIfKeyMissing() {
requireNonNull(key, "key is missing");
}
private void failIfValueMissing() {
requireNonNull(value, "value is missing");
}
private void failIfNewValueMissing() {
requireNonNull(newValue, "new value is missing");
}
private <T> T call(Function<KV, T> func) {
try (AfloatDBClient client = initClient()) {
return func.apply(client.getKV());
}
}
@Command(name = "put")
public void put() {
failIfKeyMissing();
failIfValueMissing();
Object result = call(kv -> {
Long longVal = parseValueLong();
return longVal != null ? kv.put(key, longVal) : kv.put(key, value);
});
System.out.println(result);
}
@Command(name = "put-if-absent")
public void putIfAbsent() {
failIfKeyMissing();
failIfValueMissing();
Object result = call(kv -> {
Long longVal = parseValueLong();
return longVal != null ? kv.putIfAbsent(key, longVal) : kv.putIfAbsent(key, value);
});
System.out.println(result);
}
@Command(name = "set")
public void set() {
failIfKeyMissing();
failIfValueMissing();
Object result = call(kv -> {
Long longVal = parseValueLong();
return longVal != null ? kv.set(key, longVal) : kv.set(key, value);
});
System.out.println(result);
}
@Command(name = "replace")
public void replace() {
failIfKeyMissing();
failIfValueMissing();
failIfNewValueMissing();
Object result = call(kv -> {
Long longVal = parseValueLong();
Long longNewVal = parseNewValueLong();
return (longVal != null && longNewVal != null)
? kv.replace(key, longVal, longNewVal)
: kv.replace(key, value, newValue);
});
System.out.println(result);
}
@Command(name = "remove")
public void remove() {
failIfKeyMissing();
Object result = call(kv -> {
if (value == null) {
return kv.remove(key);
}
Long longVal = parseValueLong();
return longVal != null ? kv.remove(key, longVal) : kv.remove(key, value);
});
System.out.println(result);
}
@Command(name = "delete")
public void delete() {
failIfKeyMissing();
Object result = call(kv -> kv.delete(key));
System.out.println(result);
}
@Command(name = "get")
public void get() {
failIfKeyMissing();
Object result = call(kv -> minCommitIndex != -1 ? kv.get(key, minCommitIndex) : kv.get(key));
System.out.println(result);
}
@Command(name = "contains")
public void contains() {
failIfKeyMissing();
failIfValueMissing();
Object result = call(kv -> {
Long longVal = parseValueLong();
if (longVal != null) {
return minCommitIndex != -1 ? kv.contains(key, longVal, minCommitIndex) : kv.contains(key, longVal);
} else {
return minCommitIndex != -1 ? kv.contains(key, value, minCommitIndex) : kv.contains(key, value);
}
});
System.out.println(result);
}
@Command(name = "contains-key")
public void containsKey() {
failIfKeyMissing();
Object result = call(kv -> minCommitIndex != -1 ? kv.containsKey(key, minCommitIndex) : kv.containsKey(key));
System.out.println(result);
}
@Command(name = "is-empty")
public void isEmpty() {
Object result = call(kv -> minCommitIndex != -1 ? kv.isEmpty(minCommitIndex) : kv.isEmpty());
System.out.println(result);
}
@Command(name = "size")
public void size() {
Object result = call(kv -> minCommitIndex != -1 ? kv.size(minCommitIndex) : kv.size());
System.out.println(result);
}
@Command(name = "clear")
public void clear() {
Object result = call(kv -> kv.clear());
System.out.println(result);
}
@Override
public void run() {
System.out.println("Command not provided. --help for how to use.");
}
public static void main(String[] args) {
CommandLine.run(new AfloatDBClientCliRunner(), args);
}
}