-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor ID Serial 1: Separate ObjectID and TaskID from UniqueID #4776
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1a9c3cf
Enable BaseId.
1ce38a5
Change TaskID and make python test pass
3609d84
Remove unnecessary functions and fix test failure and change TaskID to
8548997
Java code change draft
c2ce958
Refine
caa32e8
Lint
9dec100
Update java/api/src/main/java/org/ray/api/id/TaskId.java
guoyuhong b02f38a
Update java/api/src/main/java/org/ray/api/id/BaseId.java
guoyuhong 660459d
Update java/api/src/main/java/org/ray/api/id/BaseId.java
guoyuhong 102439a
Update java/api/src/main/java/org/ray/api/id/ObjectId.java
guoyuhong 9025ba8
Address comment
ab22be6
Lint
0d8eff9
Fix SINGLE_PROCESS
2ed0abd
Fix comments
c5dd0dd
Refine code
4dcf9f0
Refine test
340a041
Resolve conflict
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.ray.api.id; | ||
|
||
import java.io.Serializable; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import javax.xml.bind.DatatypeConverter; | ||
|
||
public abstract class BaseId implements Serializable { | ||
private static final long serialVersionUID = 8588849129675565761L; | ||
private final byte[] id; | ||
private int hashCodeCache = 0; | ||
private Boolean isNilCache = null; | ||
|
||
/** | ||
* Create a BaseId instance according to the input byte array. | ||
*/ | ||
public BaseId(byte[] id) { | ||
if (id.length != size()) { | ||
throw new IllegalArgumentException("Failed to construct BaseId, expect " + size() | ||
+ " bytes, but got " + id.length + " bytes."); | ||
} | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Get the byte data of this id. | ||
*/ | ||
public byte[] getBytes() { | ||
return id; | ||
} | ||
|
||
/** | ||
* Convert the byte data to a ByteBuffer. | ||
*/ | ||
public ByteBuffer toByteBuffer() { | ||
return ByteBuffer.wrap(id); | ||
} | ||
|
||
/** | ||
* @return True if this id is nil. | ||
*/ | ||
public boolean isNil() { | ||
if (isNilCache == null) { | ||
isNilCache = true; | ||
for (int i = 0; i < size(); ++i) { | ||
if (id[i] != (byte) 0xff) { | ||
isNilCache = false; | ||
break; | ||
} | ||
} | ||
} | ||
return isNilCache; | ||
} | ||
|
||
/** | ||
* Derived class should implement this function. | ||
* @return The length of this id in bytes. | ||
*/ | ||
public abstract int size(); | ||
|
||
@Override | ||
public int hashCode() { | ||
raulchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Lazy evaluation. | ||
if (hashCodeCache == 0) { | ||
hashCodeCache = Arrays.hashCode(id); | ||
} | ||
return hashCodeCache; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
|
||
if (!this.getClass().equals(obj.getClass())) { | ||
return false; | ||
} | ||
|
||
BaseId r = (BaseId) obj; | ||
return Arrays.equals(id, r.id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return DatatypeConverter.printHexBinary(id).toLowerCase(); | ||
} | ||
|
||
protected static byte[] hexString2Bytes(String hex) { | ||
return DatatypeConverter.parseHexBinary(hex); | ||
} | ||
|
||
protected static byte[] byteBuffer2Bytes(ByteBuffer bb) { | ||
byte[] id = new byte[bb.remaining()]; | ||
bb.get(id); | ||
return id; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.ray.api.id; | ||
|
||
import java.io.Serializable; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.Random; | ||
|
||
/** | ||
* Represents the id of a Ray object. | ||
*/ | ||
public class ObjectId extends BaseId implements Serializable { | ||
|
||
public static final int LENGTH = 20; | ||
public static final ObjectId NIL = genNil(); | ||
|
||
/** | ||
* Create an ObjectId from a hex string. | ||
*/ | ||
public static ObjectId fromHexString(String hex) { | ||
return new ObjectId(hexString2Bytes(hex)); | ||
} | ||
|
||
/** | ||
* Create an ObjectId from a ByteBuffer. | ||
*/ | ||
public static ObjectId fromByteBuffer(ByteBuffer bb) { | ||
return new ObjectId(byteBuffer2Bytes(bb)); | ||
} | ||
|
||
/** | ||
* Generate a nil ObjectId. | ||
*/ | ||
private static ObjectId genNil() { | ||
byte[] b = new byte[LENGTH]; | ||
Arrays.fill(b, (byte) 0xFF); | ||
return new ObjectId(b); | ||
} | ||
|
||
/** | ||
* Generate an ObjectId with random value. | ||
*/ | ||
public static ObjectId randomId() { | ||
byte[] b = new byte[LENGTH]; | ||
new Random().nextBytes(b); | ||
return new ObjectId(b); | ||
} | ||
|
||
public ObjectId(byte[] id) { | ||
super(id); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return LENGTH; | ||
} | ||
|
||
public TaskId getTaskId() { | ||
byte[] taskIdBytes = Arrays.copyOf(getBytes(), TaskId.LENGTH); | ||
return new TaskId(taskIdBytes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.ray.api.id; | ||
|
||
import java.io.Serializable; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.Random; | ||
|
||
/** | ||
* Represents the id of a Ray task. | ||
*/ | ||
public class TaskId extends BaseId implements Serializable { | ||
|
||
public static final int LENGTH = 16; | ||
public static final TaskId NIL = genNil(); | ||
|
||
/** | ||
* Create a TaskId from a hex string. | ||
*/ | ||
public static TaskId fromHexString(String hex) { | ||
return new TaskId(hexString2Bytes(hex)); | ||
} | ||
|
||
/** | ||
* Creates a TaskId from a ByteBuffer. | ||
*/ | ||
public static TaskId fromByteBuffer(ByteBuffer bb) { | ||
return new TaskId(byteBuffer2Bytes(bb)); | ||
} | ||
|
||
/** | ||
* Generate a nil TaskId. | ||
*/ | ||
private static TaskId genNil() { | ||
byte[] b = new byte[LENGTH]; | ||
Arrays.fill(b, (byte) 0xFF); | ||
return new TaskId(b); | ||
} | ||
|
||
/** | ||
* Generate an TaskId with random value. | ||
*/ | ||
public static TaskId randomId() { | ||
byte[] b = new byte[LENGTH]; | ||
new Random().nextBytes(b); | ||
return new TaskId(b); | ||
} | ||
|
||
public TaskId(byte[] id) { | ||
super(id); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return LENGTH; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of this abstract method?
Why not just return
id.length
instead of implementing this method in the derived classes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then in the constructor,
id.length != size()
will be always true.