mirror of
https://github.com/wassname/ray.git
synced 2026-07-13 04:26:46 +08:00
[java] Improve UniqueID code. (#2723)
This commit is contained in:
committed by
Robert Nishihara
parent
4f4bea086a
commit
26d3c0655c
@@ -10,14 +10,14 @@ import org.ray.util.Sha1Digestor;
|
||||
* Ray actor abstraction.
|
||||
*/
|
||||
public class RayActor<T> extends RayObject<T> implements Externalizable {
|
||||
public static final RayActor<?> nil = new RayActor<>(UniqueID.nil, UniqueID.nil);
|
||||
public static final RayActor<?> NIL = new RayActor<>(UniqueID.NIL, UniqueID.NIL);
|
||||
private static final long serialVersionUID = 1877485807405645036L;
|
||||
|
||||
private int taskCounter = 0;
|
||||
|
||||
private UniqueID taskCursor = null;
|
||||
|
||||
private UniqueID actorHandleId = UniqueID.nil;
|
||||
private UniqueID actorHandleId = UniqueID.NIL;
|
||||
|
||||
private int forksNum = 0;
|
||||
|
||||
@@ -73,7 +73,7 @@ public class RayActor<T> extends RayObject<T> implements Externalizable {
|
||||
}
|
||||
|
||||
public UniqueID computeNextActorHandleId() {
|
||||
byte[] bytes = Sha1Digestor.digest(actorHandleId.id, ++forksNum);
|
||||
byte[] bytes = Sha1Digestor.digest(actorHandleId.getBytes(), ++forksNum);
|
||||
return new UniqueID(bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ package org.ray.api;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
/**
|
||||
* Unique ID for task, worker, function...
|
||||
@@ -12,40 +12,25 @@ import java.util.Random;
|
||||
public class UniqueID implements Serializable {
|
||||
|
||||
public static final int LENGTH = 20;
|
||||
public static final UniqueID nil = genNil();
|
||||
public static final UniqueID NIL = genNil();
|
||||
private static final long serialVersionUID = 8588849129675565761L;
|
||||
byte[] id;
|
||||
private final byte[] id;
|
||||
|
||||
public UniqueID(byte[] id) {
|
||||
this.id = id;
|
||||
public static UniqueID fromHexString(String hex) {
|
||||
byte[] bytes = DatatypeConverter.parseHexBinary(hex);
|
||||
return new UniqueID(bytes);
|
||||
}
|
||||
|
||||
public UniqueID(ByteBuffer bb) {
|
||||
assert (bb.remaining() == LENGTH);
|
||||
id = new byte[bb.remaining()];
|
||||
public static UniqueID fromByteBuffer(ByteBuffer bb) {
|
||||
byte[] id = new byte[bb.remaining()];
|
||||
bb.get(id);
|
||||
}
|
||||
|
||||
public UniqueID(String optionValue) {
|
||||
assert (optionValue.length() == 2 * LENGTH);
|
||||
int j = 0;
|
||||
|
||||
id = new byte[LENGTH];
|
||||
for (int i = 0; i < LENGTH; i++) {
|
||||
char c1 = optionValue.charAt(j++);
|
||||
char c2 = optionValue.charAt(j++);
|
||||
int first = c1 <= '9' ? (c1 - '0') : (c1 - 'a' + 0xa);
|
||||
int second = c2 <= '9' ? (c2 - '0') : (c2 - 'a' + 0xa);
|
||||
id[i] = (byte) (first * 16 + second);
|
||||
}
|
||||
return new UniqueID(id);
|
||||
}
|
||||
|
||||
public static UniqueID genNil() {
|
||||
byte[] b = new byte[LENGTH];
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
b[i] = (byte) 0xFF;
|
||||
}
|
||||
|
||||
Arrays.fill(b, (byte) 0xFF);
|
||||
return new UniqueID(b);
|
||||
}
|
||||
|
||||
@@ -55,6 +40,14 @@ public class UniqueID implements Serializable {
|
||||
return new UniqueID(b);
|
||||
}
|
||||
|
||||
public UniqueID(byte[] id) {
|
||||
if (id.length != LENGTH) {
|
||||
throw new IllegalArgumentException("Illegal argument: " + id.toString());
|
||||
}
|
||||
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return id;
|
||||
}
|
||||
@@ -70,12 +63,7 @@ public class UniqueID implements Serializable {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 0xdeadbeef;
|
||||
IntBuffer bb = ByteBuffer.wrap(id).asIntBuffer();
|
||||
while (bb.hasRemaining()) {
|
||||
hash ^= bb.get();
|
||||
}
|
||||
return hash;
|
||||
return Arrays.hashCode(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,14 +82,7 @@ public class UniqueID implements Serializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
String hex = "0123456789abcdef";
|
||||
for (int i = 0; i < LENGTH; i++) {
|
||||
int val = id[i] & 0xff;
|
||||
s += hex.charAt(val >> 4);
|
||||
s += hex.charAt(val & 0xf);
|
||||
}
|
||||
return s;
|
||||
return DatatypeConverter.printHexBinary(id);
|
||||
}
|
||||
|
||||
public boolean isNil() {
|
||||
|
||||
Reference in New Issue
Block a user