forked from voldemort/voldemort
/
VoldemortClientShell.java
218 lines (204 loc) · 8.93 KB
/
VoldemortClientShell.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
package voldemort;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import voldemort.client.DefaultStoreClient;
import voldemort.client.SocketStoreClientFactory;
import voldemort.client.StoreClientFactory;
import voldemort.cluster.Node;
import voldemort.serialization.SerializationException;
import voldemort.serialization.json.JsonReader;
import voldemort.utils.Utils;
import voldemort.versioning.Versioned;
/**
* A toy shell to interact with the server via the command line
*
* @author jay
*
*/
public class VoldemortClientShell {
private static final String PROMPT = "> ";
private static final Logger logger = Logger.getLogger(VoldemortClientShell.class.getName());
public static void main(String[] args) throws Exception {
if(args.length < 2 || args.length > 3)
Utils.croak("USAGE: java VoldemortClientShell store_name bootstrap_url [command_file]");
String storeName = args[0];
String bootstrapUrl = args[1];
BufferedReader reader = null;
try {
if(args.length == 3)
reader = new BufferedReader(new FileReader(args[2]));
else
reader = new BufferedReader(new InputStreamReader(System.in));
} catch(IOException e) {
Utils.croak("Failure to open input stream: " + e.getMessage());
}
StoreClientFactory factory = new SocketStoreClientFactory(Executors.newFixedThreadPool(5),
3,
10,
2000,
2000,
2000,
bootstrapUrl);
DefaultStoreClient<Object, Object> client = null;
try {
client = (DefaultStoreClient<Object, Object>) factory.getStoreClient(storeName);
} catch(Exception e) {
Utils.croak("Could not connect to server: " + e.getMessage());
}
System.out.println("Established connection to " + storeName + " via " + bootstrapUrl);
System.out.print(PROMPT);
for(String line = reader.readLine(); line != null; line = reader.readLine()) {
if(line.trim().equals(""))
continue;
try {
if(line.toLowerCase().startsWith("put")) {
JsonReader jsonReader = new JsonReader(new StringReader(line.substring("put".length())));
client.put(tightenNumericTypes(jsonReader.read()),
tightenNumericTypes(jsonReader.read()));
} else if(line.toLowerCase().startsWith("get")) {
JsonReader jsonReader = new JsonReader(new StringReader(line.substring("get".length())));
printVersioned(client.get(tightenNumericTypes(jsonReader.read())));
} else if(line.toLowerCase().startsWith("delete")) {
JsonReader jsonReader = new JsonReader(new StringReader(line.substring("delete".length())));
client.delete(tightenNumericTypes(jsonReader.read()));
} else if(line.startsWith("locate")) {
JsonReader jsonReader = new JsonReader(new StringReader(line.substring("locate".length())));
Object key = tightenNumericTypes(jsonReader.read());
printNodeList(client.getResponsibleNodes(key));
} else if(line.startsWith("help")) {
System.out.println("Commands:");
System.out.println("put key value -- Associate the given value with the key.");
System.out.println("get key -- Retrieve the value associated with the key.");
System.out.println("delete key -- Remove all values associated with the key.");
System.out.println("locate key -- Determine which servers host the give key.");
System.out.println("help -- Print this message.");
System.out.println("exit -- Exit from this shell.");
System.out.println();
} else if(line.startsWith("quit") || line.startsWith("exit")) {
System.out.println("k k thx bye.");
System.exit(0);
} else {
System.err.println("Invalid command.");
}
} catch(SerializationException e) {
System.err.print("Error serializing values: ");
System.err.println(e.getMessage());
} catch(VoldemortException e) {
System.err.println("Exception thrown during operation.");
e.printStackTrace(System.err);
} catch(ArrayIndexOutOfBoundsException e) {
System.err.println("Invalid command.");
} catch(Exception e) {
System.err.println("Unexpected error:");
e.printStackTrace(System.err);
}
System.out.print(PROMPT);
}
}
private static void printNodeList(List<Node> nodes) {
if(nodes.size() > 0) {
for(int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
System.out.println("Node " + node.getId());
System.out.println("host: " + node.getHost());
System.out.println("port: " + node.getSocketPort());
System.out.println("available: " + (node.getStatus().isAvailable() ? "yes" : "no"));
System.out.println("last checked: " + node.getStatus().getMsSinceLastCheck()
+ " ms ago");
System.out.println();
}
}
}
private static void printVersioned(Versioned<Object> v) {
if(v == null) {
System.out.println("null");
} else {
System.out.print(v.getVersion());
System.out.print(": ");
printObject(v.getValue());
System.out.println();
}
}
@SuppressWarnings("unchecked")
private static void printObject(Object o) {
if(o == null) {
System.out.print("null");
} else if(o instanceof String) {
System.out.print('"');
System.out.print(o);
System.out.print('"');
} else if(o instanceof Date) {
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
System.out.print("'");
System.out.print(df.format((Date) o));
System.out.print("'");
} else if(o instanceof List) {
List<Object> l = (List<Object>) o;
System.out.print("[");
for(Object obj: l)
printObject(obj);
System.out.print("]");
} else if(o instanceof Map) {
Map<String, Object> m = (Map<String, Object>) o;
System.out.print('{');
for(String s: m.keySet()) {
printObject(s);
System.out.print(':');
printObject(m.get(s));
System.out.print(", ");
}
System.out.print('}');
} else {
System.out.print(o);
}
}
/*
* We need to coerce numbers to the tightest possible type and let the
* schema coerce them to the proper
*/
@SuppressWarnings("unchecked")
private static Object tightenNumericTypes(Object o) {
if(o == null) {
return null;
} else if(o instanceof List) {
List l = (List) o;
for(int i = 0; i < l.size(); i++)
l.set(i, tightenNumericTypes(l.get(i)));
return l;
} else if(o instanceof Map) {
Map m = (Map) o;
for(Map.Entry entry: (Set<Map.Entry>) m.entrySet())
m.put(entry.getKey(), tightenNumericTypes(entry.getValue()));
return m;
} else if(o instanceof Number) {
Number n = (Number) o;
if(o instanceof Integer) {
if(n.intValue() < Byte.MAX_VALUE)
return n.byteValue();
else if(n.intValue() < Short.MAX_VALUE)
return n.shortValue();
else
return n;
} else if(o instanceof Double) {
if(n.doubleValue() < Float.MAX_VALUE)
return n.floatValue();
else
return n;
} else {
throw new RuntimeException("Unsupported numeric type: " + o.getClass());
}
} else {
return o;
}
}
}