This is an automated email from the ASF dual-hosted git repository.
av pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new dc2432ca8e4 IGNITE-26454 Use MessageSerializer for GridByteArrayList
(#12352)
dc2432ca8e4 is described below
commit dc2432ca8e4173ffd489e9bd3f9edc8f4c6b976a
Author: Dmitry Werner <[email protected]>
AuthorDate: Thu Feb 19 22:32:19 2026 +0500
IGNITE-26454 Use MessageSerializer for GridByteArrayList (#12352)
---
.../communication/GridIoMessageFactory.java | 3 +-
.../ignite/internal/util/GridByteArrayList.java | 245 ++-------------------
.../ignite/lang/GridByteArrayListSelfTest.java | 84 +------
.../ignite/p2p/P2PUnsupportedClassVersionTest.java | 16 +-
4 files changed, 35 insertions(+), 313 deletions(-)
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
b/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
index b50aad31c9d..1dc8af2d28d 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
@@ -53,6 +53,7 @@ import
org.apache.ignite.internal.codegen.ErrorMessageSerializer;
import org.apache.ignite.internal.codegen.ExchangeInfoSerializer;
import
org.apache.ignite.internal.codegen.GenerateEncryptionKeyRequestSerializer;
import
org.apache.ignite.internal.codegen.GenerateEncryptionKeyResponseSerializer;
+import org.apache.ignite.internal.codegen.GridByteArrayListSerializer;
import org.apache.ignite.internal.codegen.GridCacheEntryInfoSerializer;
import org.apache.ignite.internal.codegen.GridCacheQueryRequestSerializer;
import org.apache.ignite.internal.codegen.GridCacheQueryResponseSerializer;
@@ -430,7 +431,7 @@ public class GridIoMessageFactory implements
MessageFactoryProvider {
factory.register((short)80, MetadataRequestMessage::new, new
MetadataRequestMessageSerializer());
factory.register((short)81, MetadataResponseMessage::new, new
MetadataResponseMessageSerializer());
factory.register((short)82, JobStealingRequest::new, new
JobStealingRequestSerializer());
- factory.register((short)84, GridByteArrayList::new);
+ factory.register((short)84, GridByteArrayList::new, new
GridByteArrayListSerializer());
factory.register((short)86, GridCacheVersion::new, new
GridCacheVersionSerializer());
factory.register((short)87, GridDhtPartitionExchangeId::new, new
GridDhtPartitionExchangeIdSerializer());
factory.register((short)88, GridCacheReturn::new, new
GridCacheReturnSerializer());
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/util/GridByteArrayList.java
b/modules/core/src/main/java/org/apache/ignite/internal/util/GridByteArrayList.java
index 00fcba242ca..3e31658599b 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/util/GridByteArrayList.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/util/GridByteArrayList.java
@@ -22,17 +22,12 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;
-import java.io.OutputStream;
-import java.nio.ByteBuffer;
import java.util.Arrays;
-import org.apache.ignite.internal.util.io.GridUnsafeDataInput;
-import org.apache.ignite.internal.util.io.GridUnsafeDataOutput;
+import org.apache.ignite.internal.Order;
import org.apache.ignite.internal.util.tostring.GridToStringExclude;
import org.apache.ignite.internal.util.typedef.internal.S;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.plugin.extensions.communication.Message;
-import org.apache.ignite.plugin.extensions.communication.MessageReader;
-import org.apache.ignite.plugin.extensions.communication.MessageWriter;
/**
* Re-sizable array implementation of the byte list (eliminating auto-boxing
of primitive byte type).
@@ -42,10 +37,12 @@ public class GridByteArrayList implements Message,
Externalizable {
private static final long serialVersionUID = 0L;
/** List byte data. */
+ @Order(value = 0, method = "internalArray")
@GridToStringExclude
private byte[] data;
/** List's size. */
+ @Order(1)
private int size;
/**
@@ -67,41 +64,6 @@ public class GridByteArrayList implements Message,
Externalizable {
data = new byte[cap];
}
- /**
- * Wraps existing array into byte array list.
- *
- * @param data Array to wrap.
- * @param size Size of data inside of array.
- */
- public GridByteArrayList(byte[] data, int size) {
- assert data != null;
- assert size > 0;
-
- this.data = data;
- this.size = size;
- }
-
- /**
- * Wraps existing array into byte array list.
- *
- * @param data Array to wrap.
- */
- public GridByteArrayList(byte[] data) {
- assert data != null;
-
- this.data = data;
-
- size = data.length;
- }
-
- /**
- * Resets byte array to empty. Note that this method simply resets the size
- * as there is no need to reset every byte in the array.
- */
- public void reset() {
- size = 0;
- }
-
/**
* Returns the underlying array. This method exists as performance
* optimization to avoid extra copying of the arrays. Data inside
@@ -113,6 +75,13 @@ public class GridByteArrayList implements Message,
Externalizable {
return data;
}
+ /**
+ * @param data The underlying array.
+ */
+ public void internalArray(byte[] data) {
+ this.data = data;
+ }
+
/**
* Gets copy of internal array.
*
@@ -136,15 +105,6 @@ public class GridByteArrayList implements Message,
Externalizable {
return size == data.length ? internalArray() : array();
}
- /**
- * Gets initial capacity of the list.
- *
- * @return Initial capacity.
- */
- public int capacity() {
- return data.length;
- }
-
/**
* Sets initial capacity of the list.
*
@@ -153,7 +113,7 @@ public class GridByteArrayList implements Message,
Externalizable {
private void capacity(int cap) {
assert cap > 0;
- if (cap != capacity()) {
+ if (cap != data.length) {
if (cap < size) {
size = cap;
@@ -174,14 +134,10 @@ public class GridByteArrayList implements Message,
Externalizable {
}
/**
- * Pre-allocates internal array for specified byte number only
- * if it currently is smaller than desired number.
- *
- * @param cnt Byte number to preallocate.
+ * @param size Number of bytes in the list.
*/
- public void allocate(int cnt) {
- if (size + cnt > capacity())
- capacity(size + cnt);
+ public void size(int size) {
+ this.size = size;
}
/**
@@ -190,7 +146,7 @@ public class GridByteArrayList implements Message,
Externalizable {
* @param cnt Number of bytes to request.
*/
private void requestFreeSize(int cnt) {
- if (size + cnt > capacity())
+ if (size + cnt > data.length)
capacity((size + cnt) << 1);
}
@@ -205,19 +161,6 @@ public class GridByteArrayList implements Message,
Externalizable {
data[size++] = b;
}
- /**
- * Sets a byte at specified position.
- *
- * @param pos Specified position.
- * @param b Byte to set.
- */
- public void set(int pos, byte b) {
- assert pos >= 0;
- assert pos < size;
-
- data[pos] = b;
- }
-
/**
* Appends integer to the next 4 bytes of list.
*
@@ -244,32 +187,6 @@ public class GridByteArrayList implements Message,
Externalizable {
size += 2;
}
- /**
- * Sets short at specified position.
- *
- * @param pos Specified position.
- * @param i Short to set.
- */
- public void set(int pos, short i) {
- assert pos >= 0;
- assert pos + 2 <= size;
-
- U.shortToBytes(i, data, pos);
- }
-
- /**
- * Sets integer at specified position.
- *
- * @param pos Specified position.
- * @param i Integer to set.
- */
- public void set(int pos, int i) {
- assert pos >= 0;
- assert pos + 4 <= size;
-
- U.intToBytes(i, data, pos);
- }
-
/**
* Appends long to the next 8 bytes of list.
*
@@ -283,19 +200,6 @@ public class GridByteArrayList implements Message,
Externalizable {
size += 8;
}
- /**
- * Sets long at specified position.
- *
- * @param pos Specified position.
- * @param l Long to set.
- */
- public void set(int pos, long l) {
- assert pos >= 0;
- assert pos + 8 <= size;
-
- U.longToBytes(l, data, pos);
- }
-
/**
* @param bytes Byte to add.
* @param off Offset at which to add.
@@ -309,44 +213,6 @@ public class GridByteArrayList implements Message,
Externalizable {
size += len;
}
- /**
- * Adds data from byte buffer into array.
- *
- * @param buf Buffer to read bytes from.
- * @param len Number of bytes to add.
- */
- public void add(ByteBuffer buf, int len) {
- requestFreeSize(len);
-
- buf.get(data, size, len);
-
- size += len;
- }
-
- /**
- * Gets the element (byte) at the specified position in the list.
- *
- * @param i Index of element to return.
- * @return The element at the specified position in the list.
- */
- public byte get(int i) {
- assert i < size;
-
- return data[i];
- }
-
- /**
- * Gets 4 bytes from byte list as an integer.
- *
- * @param i Index into the byte list.
- * @return Integer starting at index location.
- */
- public int getInt(int i) {
- assert i + 4 <= size;
-
- return U.bytesToInt(data, i);
- }
-
/**
* Reads all data from input stream until the end into this byte list.
*
@@ -359,12 +225,12 @@ public class GridByteArrayList implements Message,
Externalizable {
int read = 0;
while (read >= 0) {
- int free = capacity() - size;
+ int free = data.length - size;
if (free == 0) {
requestFreeSize(1);
- free = capacity() - size;
+ free = data.length - size;
assert free > 0;
}
@@ -376,28 +242,6 @@ public class GridByteArrayList implements Message,
Externalizable {
}
}
- /**
- * @return Output stream based on this byte array list.
- */
- public OutputStream outputStream() {
- GridUnsafeDataOutput out = new GridUnsafeDataOutput();
-
- out.bytes(data, size);
-
- return out;
- }
-
- /**
- * @return Input stream based on this byte array list.
- */
- public InputStream inputStream() {
- GridUnsafeDataInput in = new GridUnsafeDataInput();
-
- in.bytes(data, size);
-
- return in;
- }
-
/** {@inheritDoc} */
@Override public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(size);
@@ -414,61 +258,6 @@ public class GridByteArrayList implements Message,
Externalizable {
in.readFully(data, 0, size);
}
- /** {@inheritDoc} */
- @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
- writer.setBuffer(buf);
-
- if (!writer.isHeaderWritten()) {
- if (!writer.writeHeader(directType()))
- return false;
-
- writer.onHeaderWritten();
- }
-
- switch (writer.state()) {
- case 0:
- if (!writer.writeByteArray(data))
- return false;
-
- writer.incrementState();
-
- case 1:
- if (!writer.writeInt(size))
- return false;
-
- writer.incrementState();
-
- }
-
- return true;
- }
-
- /** {@inheritDoc} */
- @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
- reader.setBuffer(buf);
-
- switch (reader.state()) {
- case 0:
- data = reader.readByteArray();
-
- if (!reader.isLastRead())
- return false;
-
- reader.incrementState();
-
- case 1:
- size = reader.readInt();
-
- if (!reader.isLastRead())
- return false;
-
- reader.incrementState();
-
- }
-
- return true;
- }
-
/** {@inheritDoc} */
@Override public short directType() {
return 84;
diff --git
a/modules/core/src/test/java/org/apache/ignite/lang/GridByteArrayListSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/lang/GridByteArrayListSelfTest.java
index 3cd615b0160..65e12b05276 100644
---
a/modules/core/src/test/java/org/apache/ignite/lang/GridByteArrayListSelfTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/lang/GridByteArrayListSelfTest.java
@@ -19,7 +19,6 @@ package org.apache.ignite.lang;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
-import java.nio.ByteBuffer;
import java.util.Arrays;
import org.apache.ignite.internal.util.GridByteArrayList;
import org.apache.ignite.internal.util.GridUnsafe;
@@ -54,13 +53,9 @@ public class GridByteArrayListSelfTest extends
GridCommonAbstractTest {
list.add((byte)0x01);
- list.allocate(cap);
-
arr = list.internalArray();
assert arr != null;
- assert arr.length == (cap + 1);
-
assert list.size() == 1;
}
@@ -75,13 +70,9 @@ public class GridByteArrayListSelfTest extends
GridCommonAbstractTest {
list.add(a);
- assert list.get(0) == a;
-
- byte b = 0x02;
+ byte[] data = U.field(list, "data");
- list.set(0, b);
-
- assert list.get(0) == b;
+ assert data[0] == a;
}
/**
@@ -105,7 +96,9 @@ public class GridByteArrayListSelfTest extends
GridCommonAbstractTest {
int num3 = 3;
- list.set(4, num3);
+ byte[] data = U.field(list, "data");
+
+ U.intToBytes(num3, data, 4);
assert list.size() == 8;
@@ -116,8 +109,8 @@ public class GridByteArrayListSelfTest extends
GridCommonAbstractTest {
assert Arrays.equals(list.array(), arr2);
- assert list.getInt(0) == num;
- assert list.getInt(4) == num3;
+ assert U.bytesToInt(data, 0) == num;
+ assert U.bytesToInt(data, 4) == num3;
}
/**
@@ -143,69 +136,6 @@ public class GridByteArrayListSelfTest extends
GridCommonAbstractTest {
assert list.internalArray().length == arr2.length * 2;
}
- /**
- *
- */
- @Test
- public void testAddByteBuffer() {
- GridByteArrayList list = new GridByteArrayList(3);
-
- list.add((byte)0x01);
-
- ByteBuffer buf = ByteBuffer.allocateDirect(5);
-
- buf.put(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 });
-
- buf.rewind();
-
- list.add(buf, 5);
-
- assert list.size() == 6;
-
- byte[] arr2 = new byte[] { 0x01, 0x01, 0x02, 0x03, 0x04, 0x05 };
-
- assert Arrays.equals(list.array(), arr2);
-
- // Capacity should grow.
- assert list.internalArray().length == arr2.length * 2;
- }
-
- /**
- *
- */
- @SuppressWarnings({"ErrorNotRethrown"})
- @Test
- public void testBounds() {
- GridByteArrayList list = new GridByteArrayList(3);
-
- try {
- list.get(0);
-
- assert false;
- }
- catch (AssertionError e) {
- info("Caught expected error: " + e);
- }
-
- try {
- list.set(0, (byte)0x01);
-
- assert false;
- }
- catch (AssertionError e) {
- info("Caught expected error: " + e);
- }
-
- try {
- list.getInt(0);
-
- assert false;
- }
- catch (AssertionError e) {
- info("Caught expected error: " + e);
- }
- }
-
/**
* @throws Exception If failed.
*/
diff --git
a/modules/core/src/test/java/org/apache/ignite/p2p/P2PUnsupportedClassVersionTest.java
b/modules/core/src/test/java/org/apache/ignite/p2p/P2PUnsupportedClassVersionTest.java
index a6452d63e04..a9f43bb73e9 100644
---
a/modules/core/src/test/java/org/apache/ignite/p2p/P2PUnsupportedClassVersionTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/p2p/P2PUnsupportedClassVersionTest.java
@@ -175,19 +175,21 @@ public class P2PUnsupportedClassVersionTest extends
GridCommonAbstractTest {
private void incComputeClassVersion(GridDeploymentResponse resp) {
GridByteArrayList byteSrc = U.field(resp, "byteSrc");
+ byte[] data = U.field(byteSrc, "data");
+
// Assert byte array contains class file.
- assertEquals(0xCAFEBABE, byteSrc.getInt(0));
+ assertEquals(0xCAFEBABE, U.bytesToInt(data, 0));
// Assert minor version and first byte of major class version is
zero.
- assertEquals(0, byteSrc.get(4));
- assertEquals(0, byteSrc.get(5));
- assertEquals(0, byteSrc.get(6));
+ assertEquals(0, data[4]);
+ assertEquals(0, data[5]);
+ assertEquals(0, data[6]);
- byte majorClsVer = byteSrc.get(7);
+ byte majorClsVer = data[7];
- assertTrue(byteSrc.get(7) > 0);
+ assertTrue(data[7] > 0);
- byteSrc.set(7, (byte)(majorClsVer + 1));
+ data[7] = (byte)(majorClsVer + 1);
}
}