|
|
|
@ -13,11 +13,24 @@
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* Copyright 2012 Sam Pullara
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
package org.redisson.client.handler;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import org.redisson.client.protocol.CommandData;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@ -34,8 +47,6 @@ import io.netty.util.CharsetUtil;
|
|
|
|
|
/**
|
|
|
|
|
* Redis protocol command encoder
|
|
|
|
|
*
|
|
|
|
|
* Code parts from Sam Pullara
|
|
|
|
|
*
|
|
|
|
|
* @author Nikita Koksharov
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
@ -50,8 +61,6 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
|
|
|
|
|
private static final char BYTES_PREFIX = '$';
|
|
|
|
|
private static final byte[] CRLF = "\r\n".getBytes();
|
|
|
|
|
|
|
|
|
|
private static final Map<Long, byte[]> longCache = new HashMap<Long, byte[]>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
|
|
|
|
if (acceptOutboundMessage(msg)) {
|
|
|
|
@ -76,7 +85,7 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
|
|
|
|
|
if (msg.getCommand().getSubName() != null) {
|
|
|
|
|
len++;
|
|
|
|
|
}
|
|
|
|
|
out.writeBytes(convert(len));
|
|
|
|
|
out.writeCharSequence(Long.toString(len), CharsetUtil.US_ASCII);
|
|
|
|
|
out.writeBytes(CRLF);
|
|
|
|
|
|
|
|
|
|
writeArgument(out, msg.getCommand().getName().getBytes(CharsetUtil.UTF_8));
|
|
|
|
@ -120,7 +129,7 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
|
|
|
|
|
|
|
|
|
|
private void writeArgument(ByteBuf out, byte[] arg) {
|
|
|
|
|
out.writeByte(BYTES_PREFIX);
|
|
|
|
|
out.writeBytes(convert(arg.length));
|
|
|
|
|
out.writeCharSequence(Long.toString(arg.length), CharsetUtil.US_ASCII);
|
|
|
|
|
out.writeBytes(CRLF);
|
|
|
|
|
out.writeBytes(arg);
|
|
|
|
|
out.writeBytes(CRLF);
|
|
|
|
@ -128,92 +137,10 @@ public class CommandEncoder extends MessageToByteEncoder<CommandData<?, ?>> {
|
|
|
|
|
|
|
|
|
|
private void writeArgument(ByteBuf out, ByteBuf arg) {
|
|
|
|
|
out.writeByte(BYTES_PREFIX);
|
|
|
|
|
out.writeBytes(convert(arg.readableBytes()));
|
|
|
|
|
out.writeCharSequence(Long.toString(arg.readableBytes()), CharsetUtil.US_ASCII);
|
|
|
|
|
out.writeBytes(CRLF);
|
|
|
|
|
out.writeBytes(arg, arg.readerIndex(), arg.readableBytes());
|
|
|
|
|
out.writeBytes(CRLF);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static final char[] DIGITTENS = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1',
|
|
|
|
|
'1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3',
|
|
|
|
|
'3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5',
|
|
|
|
|
'5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7',
|
|
|
|
|
'7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', };
|
|
|
|
|
|
|
|
|
|
static final char[] DIGITONES = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5',
|
|
|
|
|
'6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6',
|
|
|
|
|
'7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
|
'8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
|
|
|
|
'9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', };
|
|
|
|
|
|
|
|
|
|
static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
|
|
|
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
|
|
|
|
|
|
|
|
|
|
static final int[] SIZETABLE = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };
|
|
|
|
|
|
|
|
|
|
// Requires positive x
|
|
|
|
|
static int stringSize(long x) {
|
|
|
|
|
for (int i = 0;; i++)
|
|
|
|
|
if (x <= SIZETABLE[i])
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void getChars(long i, int index, byte[] buf) {
|
|
|
|
|
long q, r;
|
|
|
|
|
int charPos = index;
|
|
|
|
|
byte sign = 0;
|
|
|
|
|
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
sign = '-';
|
|
|
|
|
i = -i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate two digits per iteration
|
|
|
|
|
while (i >= 65536) {
|
|
|
|
|
q = i / 100;
|
|
|
|
|
// really: r = i - (q * 100);
|
|
|
|
|
r = i - ((q << 6) + (q << 5) + (q << 2));
|
|
|
|
|
i = q;
|
|
|
|
|
buf[--charPos] = (byte) DIGITONES[(int) r];
|
|
|
|
|
buf[--charPos] = (byte) DIGITTENS[(int) r];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall thru to fast mode for smaller numbers
|
|
|
|
|
// assert(i <= 65536, i);
|
|
|
|
|
for (;;) {
|
|
|
|
|
q = (i * 52429) >>> (16 + 3);
|
|
|
|
|
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
|
|
|
|
|
buf[--charPos] = (byte) DIGITS[(int) r];
|
|
|
|
|
i = q;
|
|
|
|
|
if (i == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (sign != 0) {
|
|
|
|
|
buf[--charPos] = sign;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] convert(long i) {
|
|
|
|
|
if (i >= 0 && i <= 255) {
|
|
|
|
|
return longCache.get(i);
|
|
|
|
|
}
|
|
|
|
|
return toChars(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] toChars(long i) {
|
|
|
|
|
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
|
|
|
|
|
byte[] buf = new byte[size];
|
|
|
|
|
getChars(i, size, buf);
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
for (long i = 0; i < 256; i++) {
|
|
|
|
|
byte[] value = toChars(i);
|
|
|
|
|
longCache.put(i, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|