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