You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/10/12 09:23:31 UTC
[GitHub] [ignite-3] sashapolo commented on a diff in pull request #1189: IGNITE-17874 Added List and ByteBuffer support to message serialization
sashapolo commented on code in PR #1189:
URL: https://github.com/apache/ignite-3/pull/1189#discussion_r993212122
##########
modules/network/src/main/java/org/apache/ignite/internal/network/direct/stream/DirectByteBufferStreamImplV1.java:
##########
@@ -496,6 +517,89 @@ public void writeBitSet(BitSet val) {
writeLongArray(val != null ? val.toLongArray() : null);
}
+ /** {@inheritDoc} */
+ @Override
+ public void writeByteBuffer(ByteBuffer val) {
+ switch (byteBufferState) {
+ case 0:
+ byte flag = 0;
+
+ if (val != null) {
+ flag |= BYTE_BYFFER_NOT_NULL_FLAG;
+
+ if (val.order() == ByteOrder.BIG_ENDIAN) {
+ flag |= BYTE_BYFFER_BIG_ENDIAN_FLAG;
+ }
+
+ if (val.position() != 0) {
+ flag |= BYTE_BYFFER_HAS_POSITION_FLAG;
+ }
+
+ if (val.limit() != val.capacity()) {
+ flag |= BYTE_BYFFER_HAS_LIMIT_FLAG;
+ }
+ }
+
+ writeByte(flag);
+
+ if (!lastFinished || val == null) {
+ return;
+ }
+
+ byteBufferState++;
+
+ //noinspection fallthrough
+ case 1:
+ if (val.position() != 0) {
+ writeInt(val.position());
+ }
+
+ if (!lastFinished) {
+ return;
+ }
+
+ byteBufferState++;
+
+ //noinspection fallthrough
+ case 2:
+ if (val.limit() != val.capacity()) {
+ writeInt(val.limit());
+ }
+
+ if (!lastFinished) {
+ return;
+ }
+
+ byteBufferState++;
+
+ //noinspection fallthrough
+ case 3:
+ assert !val.isReadOnly();
+
+ int length = val.capacity();
+
+ if (val.isDirect()) {
+ long address = GridUnsafe.bufferAddress(val);
+
+ lastFinished = writeArray(null, address, length, length);
Review Comment:
I don't understand, why do we bother with all this GridUnsafe shenanigans when writing an array or a ByteBuffer into another ByteBuffer. What's wrong with simply calling `put(ByteBuffer)`?
##########
modules/network/src/main/java/org/apache/ignite/internal/network/direct/stream/DirectByteBufferStreamImplV1.java:
##########
@@ -65,6 +66,18 @@ public class DirectByteBufferStreamImplV1 implements DirectByteBufferStream {
/** Poison object. */
private static final Object NULL = new Object();
+ /** Flag that indicates that byte buffer is not null. */
+ private static final byte BYTE_BYFFER_NOT_NULL_FLAG = 1;
Review Comment:
There's a typo in all these constants, its `BYTE_BUFFER`, not `BYTE_BYFFER`
##########
modules/network/src/main/java/org/apache/ignite/internal/network/direct/stream/DirectByteBufferStreamImplV1.java:
##########
@@ -1049,6 +1153,80 @@ public BitSet readBitSet() {
return arr != null ? BitSet.valueOf(arr) : null;
}
+ @Override
+ public ByteBuffer readByteBuffer() {
+ byte[] bytes;
+
+ switch (byteBufferState) {
+ case 0:
+ byteBufferFlag = readByte();
+
+ boolean isNull = (byteBufferFlag & BYTE_BYFFER_NOT_NULL_FLAG) == 0;
+
+ if (!lastFinished || isNull) {
+ return null;
+ }
+
+ byteBufferState++;
+
+ //noinspection fallthrough
+ case 1:
+ if ((byteBufferFlag & BYTE_BYFFER_HAS_POSITION_FLAG) == 0) {
+ byteBufferPosition = 0;
+ } else {
+ byteBufferPosition = readInt();
+ }
+
+ if (!lastFinished) {
Review Comment:
maybe this `if` should be moved to closer to the `readInt` call
##########
modules/network/src/main/java/org/apache/ignite/internal/network/direct/stream/DirectByteBufferStreamImplV1.java:
##########
@@ -1363,8 +1541,7 @@ public <C extends Collection<?>> C readCollection(MessageCollectionItemType item
* @return Whether array was fully written.
*/
boolean writeArray(Object arr, long off, int len, int bytes) {
- assert arr != null;
- assert arr.getClass().isArray() && arr.getClass().getComponentType().isPrimitive();
+ assert arr == null || arr.getClass().isArray() && arr.getClass().getComponentType().isPrimitive();
Review Comment:
I don't think this change was necessary
##########
modules/network/src/test/java/org/apache/ignite/internal/network/messages/AllTypesMessage.java:
##########
@@ -78,4 +80,8 @@ public interface AllTypesMessage extends NetworkMessage, Serializable {
Collection<NetworkMessage> netMsgCollW();
Map<String, NetworkMessage> newMsgMapX();
+
+ List<NetworkMessage> netMsgListY();
Review Comment:
Not related to this PR, but theres' a typo above: `newMsgMapX`
##########
modules/network/src/main/java/org/apache/ignite/internal/network/direct/stream/DirectByteBufferStreamImplV1.java:
##########
@@ -496,6 +517,89 @@ public void writeBitSet(BitSet val) {
writeLongArray(val != null ? val.toLongArray() : null);
}
+ /** {@inheritDoc} */
+ @Override
+ public void writeByteBuffer(ByteBuffer val) {
+ switch (byteBufferState) {
+ case 0:
+ byte flag = 0;
+
+ if (val != null) {
+ flag |= BYTE_BYFFER_NOT_NULL_FLAG;
+
+ if (val.order() == ByteOrder.BIG_ENDIAN) {
+ flag |= BYTE_BYFFER_BIG_ENDIAN_FLAG;
+ }
+
+ if (val.position() != 0) {
+ flag |= BYTE_BYFFER_HAS_POSITION_FLAG;
+ }
+
+ if (val.limit() != val.capacity()) {
+ flag |= BYTE_BYFFER_HAS_LIMIT_FLAG;
+ }
+ }
+
+ writeByte(flag);
+
+ if (!lastFinished || val == null) {
+ return;
+ }
+
+ byteBufferState++;
+
+ //noinspection fallthrough
+ case 1:
+ if (val.position() != 0) {
+ writeInt(val.position());
+ }
+
+ if (!lastFinished) {
+ return;
+ }
+
+ byteBufferState++;
+
+ //noinspection fallthrough
+ case 2:
+ if (val.limit() != val.capacity()) {
+ writeInt(val.limit());
+ }
+
+ if (!lastFinished) {
+ return;
+ }
+
+ byteBufferState++;
+
+ //noinspection fallthrough
+ case 3:
+ assert !val.isReadOnly();
+
+ int length = val.capacity();
+
+ if (val.isDirect()) {
+ long address = GridUnsafe.bufferAddress(val);
+
+ lastFinished = writeArray(null, address, length, length);
+ } else {
+ byte[] array = val.array();
Review Comment:
hm, didn't we agree that we should transfer only (limit - position) bytes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org