forked from vert-x/mod-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.java
More file actions
206 lines (172 loc) · 6.46 KB
/
Command.java
File metadata and controls
206 lines (172 loc) · 6.46 KB
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
package io.vertx.redis;
import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.net.NetSocket;
import org.vertx.java.core.streams.WriteStream;
import java.nio.charset.Charset;
public class Command {
private static final byte ARGS_PREFIX = '*';
private static final byte[] CRLF = "\r\n".getBytes();
private static final byte BYTES_PREFIX = '$';
private static final byte[] NEG_ONE = convert(-1);
// Cache 256 number conversions. That should cover a huge
// percentage of numbers passed over the wire.
private static final int NUM_MAP_LENGTH = 256;
private static final byte[][] numMap = new byte[NUM_MAP_LENGTH][];
static {
for (int i = 0; i < NUM_MAP_LENGTH; i++) {
numMap[i] = convert(i);
}
}
// Optimized for the direct to ASCII bytes case
// About 5x faster than using Long.toString.getBytes
private static byte[] numToBytes(long value) {
if (value >= 0 && value < NUM_MAP_LENGTH) {
int index = (int) value;
return numMap[index];
} else if (value == -1) {
return NEG_ONE;
}
return convert(value);
}
private static byte[] convert(long value) {
boolean negative = value < 0;
// Checked javadoc: If the argument is equal to 10^n for integer n, then the result is n.
// Also, if negative, leave another slot for the sign.
long abs = Math.abs(value);
int index = (value == 0 ? 0 : (int) Math.log10(abs)) + (negative ? 2 : 1);
byte[] bytes = new byte[index];
// Put the sign in the slot we saved
if (negative) bytes[0] = '-';
long next = abs;
while ((next /= 10) > 0) {
bytes[--index] = (byte) ('0' + (abs % 10));
abs = next;
}
bytes[--index] = (byte) ('0' + abs);
return bytes;
}
private void appendToBuffer(final Object value, final Charset encoding, final Buffer buffer) {
buffer.appendByte(BYTES_PREFIX);
if (value == null) {
buffer.appendByte((byte) '0');
buffer.appendBytes(CRLF);
buffer.appendBytes(CRLF);
} else {
byte[] bytes;
// Possible types are: String, JsonObject, JsonArray, JsonElement, Number, Boolean, byte[]
if (value instanceof byte[]) {
bytes = (byte[]) value;
} else if (value instanceof Buffer) {
bytes = ((Buffer) value).getBytes();
} else if (value instanceof String) {
bytes = ((String) value).getBytes(encoding);
} else if (value instanceof Byte) {
bytes = numToBytes((Byte) value);
} else if (value instanceof Short) {
bytes = numToBytes((Short) value);
} else if (value instanceof Integer) {
bytes = numToBytes((Integer) value);
} else if (value instanceof Long) {
bytes = numToBytes((Long) value);
} else {
bytes = value.toString().getBytes(encoding);
}
buffer.appendBytes(numToBytes(bytes.length));
buffer.appendBytes(CRLF);
buffer.appendBytes(bytes);
buffer.appendBytes(CRLF);
}
}
private final Buffer buffer;
private int expectedReplies = 1;
private Handler<Reply> handler;
public Command(JsonObject json, Charset encoding) {
String command = json.getString("command");
JsonArray args = json.getArray("args");
int totalArgs;
if (args == null) {
totalArgs = 0;
} else {
totalArgs = args.size();
}
int spc = command.indexOf(' '); // there are commands which are multi word
String extraCommand = null;
if (spc != -1) {
extraCommand = command.substring(spc + 1);
command = command.substring(0, spc);
}
// serialize the request
buffer = new Buffer();
buffer.appendByte(ARGS_PREFIX);
if (extraCommand == null) {
buffer.appendBytes(numToBytes(totalArgs + 1));
} else {
buffer.appendBytes(numToBytes(totalArgs + 2));
}
buffer.appendBytes(CRLF);
// serialize the command
appendToBuffer(command.getBytes(encoding), encoding, buffer);
if (extraCommand != null) {
appendToBuffer(extraCommand.getBytes(encoding), encoding, buffer);
}
// serialize arguments
for (int i = 0; i < totalArgs; i++) {
appendToBuffer(args.get(i), encoding, buffer);
}
}
public Command(String command, Object... args) {
this.expectedReplies = 1;
this.handler = null;
int totalArgs;
if (args == null) {
totalArgs = 0;
} else {
totalArgs = args.length;
}
int spc = command.indexOf(' '); // there are commands which are multi word
String extraCommand = null;
if (spc != -1) {
extraCommand = command.substring(spc + 1);
command = command.substring(0, spc);
}
// serialize the request
buffer = new Buffer();
buffer.appendByte(ARGS_PREFIX);
if (extraCommand == null) {
buffer.appendBytes(numToBytes(totalArgs + 1));
} else {
buffer.appendBytes(numToBytes(totalArgs + 2));
}
buffer.appendBytes(CRLF);
// serialize the command
Charset defaultCharset = Charset.defaultCharset();
appendToBuffer(command.getBytes(defaultCharset), defaultCharset, buffer);
if (extraCommand != null) {
appendToBuffer(extraCommand.getBytes(defaultCharset), defaultCharset, buffer);
}
// serialize arguments
for (int i = 0; i < totalArgs; i++) {
appendToBuffer(args[i], defaultCharset, buffer);
}
}
public Command setExpectedReplies(int expectedReplies) {
this.expectedReplies = expectedReplies;
return this;
}
public Command setHandler(Handler<Reply> handler) {
this.handler = handler;
return this;
}
public void writeTo(WriteStream<NetSocket> writeStream) {
writeStream.write(buffer);
}
public int getExpectedReplies() {
return expectedReplies;
}
public Handler<Reply> getHandler() {
return handler;
}
}