- Notifications
You must be signed in to change notification settings - Fork 44
add UnsafeByteArrayGraphInput and UnsafeByteArrayGraphOutput #13
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
Conversation
| ||
private static final sun.misc.Unsafe UNSAFE; | ||
| ||
private byte[] buffer; |
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.
set final
private static final sun.misc.Unsafe UNSAFE; | ||
| ||
private byte[] buffer; | ||
private int limit; |
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.
set final
char c; | ||
int count = 0; | ||
| ||
/* use charAt instead of copying String to char array */ |
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.
//
...uter-core/src/main/java/com/baidu/hugegraph/computer/core/io/UnsafeByteArrayGraphOutput.java Outdated Show resolved Hide resolved
} | ||
} | ||
this.writeShort(utfLen); | ||
this.require(utfLen); |
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.
assert utfLen == bytes.length
| ||
public void skipBytes(int bytesToSkip) { | ||
this.require(bytesToSkip); | ||
this.position += bytesToSkip; |
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.
comments used in what scenes
/** | ||
* @return the internal byte array, can't modify the returned byte array | ||
*/ | ||
public byte[] getByteArray() { |
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.
buffer()
@Test | ||
public void testShort() throws IOException { | ||
UnsafeByteArrayGraphOutput output = new UnsafeByteArrayGraphOutput(); | ||
for (short i = -128; i <= 127; i++) { |
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.
add more cases
@Test | ||
public void testShortWithPosition() throws IOException { | ||
UnsafeByteArrayGraphOutput output = new UnsafeByteArrayGraphOutput(); | ||
output.skipBytes(Constants.SHORT_LEN); |
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.
return position
} | ||
| ||
@Test | ||
public void testGetByteArray() throws IOException { |
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.
rename it
6c0b9ba
to 18da03a
Compare public int skipBytes(int bytesToSkip) { | ||
this.require(bytesToSkip); | ||
int positionBeforeSkip = this.position; | ||
this.position += bytesToSkip; |
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.
2 spaces
output.writeInt(Integer.MAX_VALUE); | ||
UnsafeByteArrayGraphInput input = new UnsafeByteArrayGraphInput( | ||
output.getByteArray(), | ||
output.buffer(), |
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.
also update testGetByteArray
| ||
import sun.misc.Unsafe; | ||
| ||
public class UnsafeByteArrayGraphInput implements GraphInput { |
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.
just implements DataInput
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.
set final
private static final int DEFAULT_SIZE = 32; | ||
| ||
private byte[] buffer; | ||
private int capacity; |
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.
set final
| ||
import sun.misc.Unsafe; | ||
| ||
public class UnsafeByteArrayGraphOutput implements GraphOutput { |
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.
set final
private static final sun.misc.Unsafe UNSAFE; | ||
| ||
private final byte[] buffer; | ||
private final int limit; |
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.
set final
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.
already set final
public void writeShort(int v) { | ||
this.require(Constants.SHORT_LEN); | ||
UNSAFE.putShort(buffer, | ||
Unsafe.ARRAY_BYTE_BASE_OFFSET + this.position, |
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.
align
return encoder.encode(CharBuffer.wrap(str.toCharArray())); | ||
} catch (CharacterCodingException e) { | ||
throw new ComputerException("Can't encode %s with UTF-8", e, str); | ||
for (;i < strLen; i++) { |
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.
add space
output.write(i); | ||
} | ||
UnsafeByteArrayGraphInput input = new UnsafeByteArrayGraphInput( | ||
output.toByteArray()); |
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.
align with UnsafeByteArrayGraphInput
values[i] = ValueFactory.createValue(types[i].code()); | ||
output.writeValue(values[i]); | ||
} else { | ||
// Omit ValueType.UNKNOWN |
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.
assert i == 0
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.
testWriteValue is removed
Value value = input.readValue(); | ||
Assert.assertEquals(values[i], value); | ||
} else { | ||
// Omitted ValueType.UNKNOWN |
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.
ditto
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.
testWriteValue is removed
| ||
@Override | ||
public Value readValue() throws IOException { | ||
byte typeCode = this.readByte(); |
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.
typeCode => type
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.
method readValue is removed
} | ||
| ||
for (;i < strLen; i++) { | ||
for ( ;i < strLen; i++) { |
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.
";" first
UnsafeByteArrayOutput output = new UnsafeByteArrayOutput(); | ||
output.writeChars(chars); | ||
UnsafeByteArrayGraphInput input = new UnsafeByteArrayGraphInput( | ||
UnsafeByteArrayInput input = new UnsafeByteArrayInput( |
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.
update align
| ||
@Override | ||
public int skipBytes(int n) { | ||
int remaining = remaining(); |
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.
this.remaining()
* byte size and calls writeInt(int position, int v) to write the int at | ||
* skipped position. The serialized byte size can be get by | ||
* the difference of {@link #position()} before and after write the content. | ||
* @returns the position before skip. |
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.
@Override | ||
public void writeBoolean(boolean v) { | ||
this.require(Constants.BOOLEAN_LEN); | ||
UNSAFE.putBoolean(buffer, this.offset(), v); |
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.
buffer => this.buffer, ditto as followers
} | ||
| ||
public void writeShort(int position, int v) { | ||
UNSAFE.putShort(buffer, this.offset(position), (short) v); |
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.
need require firstly?
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.
no, because it write at specified position
} | ||
| ||
public void writeInt(int position, int v) { | ||
this.require(Constants.INT_LEN); |
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.
check position + Constants.INT_LEN < capacity
this.position += Constants.SHORT_LEN; | ||
} | ||
| ||
public void writeShort(int position, int v) { |
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.
check position + Constants.SHORT_LEN < capacity
No description provided.