This commit is contained in:
@@ -44,8 +44,9 @@ public final class HashedMap<K extends Type, V> {
|
||||
Entry<K, V> entry = data[index];
|
||||
while (entry != null) {
|
||||
if (k == entry.key) {
|
||||
V old = entry.value;
|
||||
entry.value = value;
|
||||
return entry.value;
|
||||
return old;
|
||||
}
|
||||
entry = entry.next;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ package org.redkale.convert.ext;
|
||||
import org.redkale.convert.Reader;
|
||||
import org.redkale.convert.Writer;
|
||||
import org.redkale.convert.SimpledCoder;
|
||||
import org.redkale.util.DLong;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -18,6 +18,8 @@ import org.redkale.util.DLong;
|
||||
*/
|
||||
public final class DLongSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, DLong> {
|
||||
|
||||
private static final ByteArraySimpledCoder bsSimpledCoder = ByteArraySimpledCoder.instance;
|
||||
|
||||
public static final DLongSimpledCoder instance = new DLongSimpledCoder();
|
||||
|
||||
@Override
|
||||
@@ -25,16 +27,15 @@ public final class DLongSimpledCoder<R extends Reader, W extends Writer> extends
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
out.writeSmallString(value.getFirst() + "_" + value.getSecond());
|
||||
bsSimpledCoder.convertTo(out, value.directBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DLong convertFrom(R in) {
|
||||
String str = in.readString();
|
||||
if (str == null) return null;
|
||||
int pos = str.indexOf('_');
|
||||
return new DLong(Long.parseLong(str.substring(0, pos)), Long.parseLong(str.substring(pos + 1)));
|
||||
byte[] bs = bsSimpledCoder.convertFrom(in);
|
||||
if (bs == null) return null;
|
||||
return new DLong(bs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
33
src/org/redkale/convert/json/DLongJsonSimpledCoder.java
Normal file
33
src/org/redkale/convert/json/DLongJsonSimpledCoder.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert.json;
|
||||
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class DLongJsonSimpledCoder extends JsonSimpledCoder<DLong> {
|
||||
|
||||
public static final DLongJsonSimpledCoder instance = new DLongJsonSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(final JsonWriter out, final DLong value) {
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
out.writeSmallString(value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DLong convertFrom(JsonReader in) {
|
||||
final String str = in.readString();
|
||||
if (str == null) return null;
|
||||
return new DLong(Utility.hexToBin(str));
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.redkale.convert.ConvertType;
|
||||
import org.redkale.convert.Factory;
|
||||
import java.io.Serializable;
|
||||
import java.net.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -21,6 +22,7 @@ public final class JsonFactory extends Factory<JsonReader, JsonWriter> {
|
||||
static {
|
||||
instance.register(InetAddress.class, InetAddressJsonSimpledCoder.instance);
|
||||
instance.register(InetSocketAddress.class, InetAddressJsonSimpledCoder.InetSocketAddressJsonSimpledCoder.instance);
|
||||
instance.register(DLong.class, DLongJsonSimpledCoder.instance);
|
||||
instance.register(Serializable.class, instance.loadEncoder(Object.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ public abstract class Sncp {
|
||||
}
|
||||
|
||||
public static DLong hash(final java.lang.reflect.Method method) {
|
||||
if (method == null) return new DLong(-1L, -1L);
|
||||
if (method == null) return new DLong(new byte[16]);
|
||||
String n = method.getName();
|
||||
if (n.length() > 11) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -375,9 +375,9 @@ public final class SncpClient {
|
||||
if (rserviceid != serviceid) throw new RuntimeException("sncp(" + action.method + ") response.serviceid = " + serviceid + ", but request.serviceid =" + rserviceid);
|
||||
long rnameid = buffer.getLong();
|
||||
if (rnameid != nameid) throw new RuntimeException("sncp(" + action.method + ") response.nameid = " + nameid + ", but receive nameid =" + rnameid);
|
||||
long ractionid1 = buffer.getLong();
|
||||
long ractionid2 = buffer.getLong();
|
||||
if (!action.actionid.equals(ractionid1, ractionid2)) throw new RuntimeException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + ractionid1 + "_" + ractionid2 + ")");
|
||||
byte[] bs = new byte[16];
|
||||
buffer.get(bs);
|
||||
if (!action.actionid.equals(bs)) throw new RuntimeException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + Utility.binToHexString(bs) + ")");
|
||||
buffer.getInt(); //地址
|
||||
buffer.getChar(); //端口
|
||||
}
|
||||
@@ -390,8 +390,7 @@ public final class SncpClient {
|
||||
buffer.putChar((char) HEADER_SIZE); //header长度
|
||||
buffer.putLong(this.serviceid);
|
||||
buffer.putLong(this.nameid);
|
||||
buffer.putLong(actionid.getFirst());
|
||||
buffer.putLong(actionid.getSecond());
|
||||
actionid.putTo(buffer);
|
||||
buffer.put(addrBytes[0]);
|
||||
buffer.put(addrBytes[1]);
|
||||
buffer.put(addrBytes[2]);
|
||||
|
||||
@@ -62,7 +62,9 @@ public final class SncpRequest extends Request {
|
||||
}
|
||||
this.serviceid = buffer.getLong();
|
||||
this.nameid = buffer.getLong();
|
||||
this.actionid = new DLong(buffer.getLong(), buffer.getLong());
|
||||
byte[] bs = new byte[16];
|
||||
buffer.get(bs);
|
||||
this.actionid = new DLong(bs);
|
||||
buffer.get(bufferbytes);
|
||||
this.bodylength = buffer.getInt();
|
||||
this.bodyoffset = buffer.getInt();
|
||||
|
||||
@@ -70,8 +70,7 @@ public final class SncpResponse extends Response<SncpRequest> {
|
||||
buffer.putLong(request.getServiceid());
|
||||
buffer.putLong(request.getNameid());
|
||||
DLong actionid = request.getActionid();
|
||||
buffer.putLong(actionid.getFirst());
|
||||
buffer.putLong(actionid.getSecond());
|
||||
actionid.putTo(buffer);
|
||||
buffer.put(addrBytes);
|
||||
buffer.putChar((char) this.addrPort);
|
||||
buffer.putInt(bodyLength);
|
||||
|
||||
@@ -5,32 +5,45 @@
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.nio.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 双long数据结构
|
||||
* 16bytes数据结构
|
||||
* 注意: 为了提高性能, DLong中的bytes是直接返回, 不得对bytes的内容进行修改。
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public final class DLong extends Number implements Comparable<DLong> {
|
||||
|
||||
private final long first;
|
||||
private final byte[] bytes;
|
||||
|
||||
private final long second;
|
||||
|
||||
public DLong(long one, long two) {
|
||||
this.first = one;
|
||||
this.second = two;
|
||||
public DLong(long v1, long v2) {
|
||||
this.bytes = new byte[]{(byte) (v1 >> 56), (byte) (v1 >> 48), (byte) (v1 >> 40), (byte) (v1 >> 32),
|
||||
(byte) (v1 >> 24), (byte) (v1 >> 16), (byte) (v1 >> 8), (byte) v1, (byte) (v2 >> 56), (byte) (v2 >> 48), (byte) (v2 >> 40), (byte) (v2 >> 32),
|
||||
(byte) (v2 >> 24), (byte) (v2 >> 16), (byte) (v2 >> 8), (byte) v2};
|
||||
}
|
||||
|
||||
public long getFirst() {
|
||||
return first;
|
||||
public DLong(byte[] bytes) {
|
||||
if (bytes == null || bytes.length != 16) throw new NumberFormatException("Not 16 length bytes");
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
public long getSecond() {
|
||||
return second;
|
||||
public byte[] getBytes() {
|
||||
return Arrays.copyOf(bytes, bytes.length);
|
||||
}
|
||||
|
||||
public boolean equals(long one, long two) {
|
||||
return this.first == one && this.second == two;
|
||||
public byte[] directBytes() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public ByteBuffer putTo(ByteBuffer buffer) {
|
||||
buffer.put(bytes);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public boolean equals(byte[] bytes) {
|
||||
return Arrays.equals(this.bytes, bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -38,30 +51,34 @@ public final class DLong extends Number implements Comparable<DLong> {
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
final DLong other = (DLong) obj;
|
||||
return (this.first == other.first && this.second == other.second);
|
||||
return Arrays.equals(this.bytes, other.bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 7;
|
||||
hash = 89 * hash + (int) (this.first ^ (this.first >>> 32));
|
||||
hash = 89 * hash + (int) (this.second ^ (this.second >>> 32));
|
||||
return hash;
|
||||
return Arrays.hashCode(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.first + "_" + this.second;
|
||||
return new String(Utility.binToHex(bytes));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return (int) longValue();
|
||||
return ((bytes[12] & 0xff) << 24) | ((bytes[113] & 0xff) << 16) | ((bytes[14] & 0xff) << 8) | (bytes[15] & 0xff);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long longValue() {
|
||||
return first ^ second;
|
||||
return ((((long) bytes[8] & 0xff) << 56)
|
||||
| (((long) bytes[9] & 0xff) << 48)
|
||||
| (((long) bytes[10] & 0xff) << 40)
|
||||
| (((long) bytes[11] & 0xff) << 32)
|
||||
| (((long) bytes[12] & 0xff) << 24)
|
||||
| (((long) bytes[13] & 0xff) << 16)
|
||||
| (((long) bytes[14] & 0xff) << 8)
|
||||
| (((long) bytes[15] & 0xff)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -76,7 +93,11 @@ public final class DLong extends Number implements Comparable<DLong> {
|
||||
|
||||
@Override
|
||||
public int compareTo(DLong o) {
|
||||
return (int) (first == o.first ? (second - o.second) : (first - o.first));
|
||||
if (o == null) return 1;
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
if (this.bytes[i] != o.bytes[i]) return this.bytes[i] - o.bytes[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -248,6 +248,10 @@ public final class Utility {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static byte[] hexToBin(String str) {
|
||||
return hexToBin(charArray(str));
|
||||
}
|
||||
|
||||
public static byte[] hexToBin(char[] src) {
|
||||
return hexToBin(src, 0, src.length);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user