Skip to content

Commit dc2432c

Browse files
authored
IGNITE-26454 Use MessageSerializer for GridByteArrayList (#12352)
1 parent d1d5436 commit dc2432c

File tree

4 files changed

+35
-313
lines changed

4 files changed

+35
-313
lines changed

modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.apache.ignite.internal.codegen.ExchangeInfoSerializer;
5454
import org.apache.ignite.internal.codegen.GenerateEncryptionKeyRequestSerializer;
5555
import org.apache.ignite.internal.codegen.GenerateEncryptionKeyResponseSerializer;
56+
import org.apache.ignite.internal.codegen.GridByteArrayListSerializer;
5657
import org.apache.ignite.internal.codegen.GridCacheEntryInfoSerializer;
5758
import org.apache.ignite.internal.codegen.GridCacheQueryRequestSerializer;
5859
import org.apache.ignite.internal.codegen.GridCacheQueryResponseSerializer;
@@ -430,7 +431,7 @@ public class GridIoMessageFactory implements MessageFactoryProvider {
430431
factory.register((short)80, MetadataRequestMessage::new, new MetadataRequestMessageSerializer());
431432
factory.register((short)81, MetadataResponseMessage::new, new MetadataResponseMessageSerializer());
432433
factory.register((short)82, JobStealingRequest::new, new JobStealingRequestSerializer());
433-
factory.register((short)84, GridByteArrayList::new);
434+
factory.register((short)84, GridByteArrayList::new, new GridByteArrayListSerializer());
434435
factory.register((short)86, GridCacheVersion::new, new GridCacheVersionSerializer());
435436
factory.register((short)87, GridDhtPartitionExchangeId::new, new GridDhtPartitionExchangeIdSerializer());
436437
factory.register((short)88, GridCacheReturn::new, new GridCacheReturnSerializer());

modules/core/src/main/java/org/apache/ignite/internal/util/GridByteArrayList.java

Lines changed: 17 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@
2222
import java.io.InputStream;
2323
import java.io.ObjectInput;
2424
import java.io.ObjectOutput;
25-
import java.io.OutputStream;
26-
import java.nio.ByteBuffer;
2725
import java.util.Arrays;
28-
import org.apache.ignite.internal.util.io.GridUnsafeDataInput;
29-
import org.apache.ignite.internal.util.io.GridUnsafeDataOutput;
26+
import org.apache.ignite.internal.Order;
3027
import org.apache.ignite.internal.util.tostring.GridToStringExclude;
3128
import org.apache.ignite.internal.util.typedef.internal.S;
3229
import org.apache.ignite.internal.util.typedef.internal.U;
3330
import org.apache.ignite.plugin.extensions.communication.Message;
34-
import org.apache.ignite.plugin.extensions.communication.MessageReader;
35-
import org.apache.ignite.plugin.extensions.communication.MessageWriter;
3631

3732
/**
3833
* 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 {
4237
private static final long serialVersionUID = 0L;
4338

4439
/** List byte data. */
40+
@Order(value = 0, method = "internalArray")
4541
@GridToStringExclude
4642
private byte[] data;
4743

4844
/** List's size. */
45+
@Order(1)
4946
private int size;
5047

5148
/**
@@ -67,41 +64,6 @@ public GridByteArrayList(int cap) {
6764
data = new byte[cap];
6865
}
6966

70-
/**
71-
* Wraps existing array into byte array list.
72-
*
73-
* @param data Array to wrap.
74-
* @param size Size of data inside of array.
75-
*/
76-
public GridByteArrayList(byte[] data, int size) {
77-
assert data != null;
78-
assert size > 0;
79-
80-
this.data = data;
81-
this.size = size;
82-
}
83-
84-
/**
85-
* Wraps existing array into byte array list.
86-
*
87-
* @param data Array to wrap.
88-
*/
89-
public GridByteArrayList(byte[] data) {
90-
assert data != null;
91-
92-
this.data = data;
93-
94-
size = data.length;
95-
}
96-
97-
/**
98-
* Resets byte array to empty. Note that this method simply resets the size
99-
* as there is no need to reset every byte in the array.
100-
*/
101-
public void reset() {
102-
size = 0;
103-
}
104-
10567
/**
10668
* Returns the underlying array. This method exists as performance
10769
* optimization to avoid extra copying of the arrays. Data inside
@@ -113,6 +75,13 @@ public byte[] internalArray() {
11375
return data;
11476
}
11577

78+
/**
79+
* @param data The underlying array.
80+
*/
81+
public void internalArray(byte[] data) {
82+
this.data = data;
83+
}
84+
11685
/**
11786
* Gets copy of internal array.
11887
*
@@ -136,15 +105,6 @@ public byte[] entireArray() {
136105
return size == data.length ? internalArray() : array();
137106
}
138107

139-
/**
140-
* Gets initial capacity of the list.
141-
*
142-
* @return Initial capacity.
143-
*/
144-
public int capacity() {
145-
return data.length;
146-
}
147-
148108
/**
149109
* Sets initial capacity of the list.
150110
*
@@ -153,7 +113,7 @@ public int capacity() {
153113
private void capacity(int cap) {
154114
assert cap > 0;
155115

156-
if (cap != capacity()) {
116+
if (cap != data.length) {
157117
if (cap < size) {
158118
size = cap;
159119

@@ -174,14 +134,10 @@ public int size() {
174134
}
175135

176136
/**
177-
* Pre-allocates internal array for specified byte number only
178-
* if it currently is smaller than desired number.
179-
*
180-
* @param cnt Byte number to preallocate.
137+
* @param size Number of bytes in the list.
181138
*/
182-
public void allocate(int cnt) {
183-
if (size + cnt > capacity())
184-
capacity(size + cnt);
139+
public void size(int size) {
140+
this.size = size;
185141
}
186142

187143
/**
@@ -190,7 +146,7 @@ public void allocate(int cnt) {
190146
* @param cnt Number of bytes to request.
191147
*/
192148
private void requestFreeSize(int cnt) {
193-
if (size + cnt > capacity())
149+
if (size + cnt > data.length)
194150
capacity((size + cnt) << 1);
195151
}
196152

@@ -205,19 +161,6 @@ public void add(byte b) {
205161
data[size++] = b;
206162
}
207163

208-
/**
209-
* Sets a byte at specified position.
210-
*
211-
* @param pos Specified position.
212-
* @param b Byte to set.
213-
*/
214-
public void set(int pos, byte b) {
215-
assert pos >= 0;
216-
assert pos < size;
217-
218-
data[pos] = b;
219-
}
220-
221164
/**
222165
* Appends integer to the next 4 bytes of list.
223166
*
@@ -244,32 +187,6 @@ public void add(short i) {
244187
size += 2;
245188
}
246189

247-
/**
248-
* Sets short at specified position.
249-
*
250-
* @param pos Specified position.
251-
* @param i Short to set.
252-
*/
253-
public void set(int pos, short i) {
254-
assert pos >= 0;
255-
assert pos + 2 <= size;
256-
257-
U.shortToBytes(i, data, pos);
258-
}
259-
260-
/**
261-
* Sets integer at specified position.
262-
*
263-
* @param pos Specified position.
264-
* @param i Integer to set.
265-
*/
266-
public void set(int pos, int i) {
267-
assert pos >= 0;
268-
assert pos + 4 <= size;
269-
270-
U.intToBytes(i, data, pos);
271-
}
272-
273190
/**
274191
* Appends long to the next 8 bytes of list.
275192
*
@@ -283,19 +200,6 @@ public void add(long l) {
283200
size += 8;
284201
}
285202

286-
/**
287-
* Sets long at specified position.
288-
*
289-
* @param pos Specified position.
290-
* @param l Long to set.
291-
*/
292-
public void set(int pos, long l) {
293-
assert pos >= 0;
294-
assert pos + 8 <= size;
295-
296-
U.longToBytes(l, data, pos);
297-
}
298-
299203
/**
300204
* @param bytes Byte to add.
301205
* @param off Offset at which to add.
@@ -309,44 +213,6 @@ public void add(byte[] bytes, int off, int len) {
309213
size += len;
310214
}
311215

312-
/**
313-
* Adds data from byte buffer into array.
314-
*
315-
* @param buf Buffer to read bytes from.
316-
* @param len Number of bytes to add.
317-
*/
318-
public void add(ByteBuffer buf, int len) {
319-
requestFreeSize(len);
320-
321-
buf.get(data, size, len);
322-
323-
size += len;
324-
}
325-
326-
/**
327-
* Gets the element (byte) at the specified position in the list.
328-
*
329-
* @param i Index of element to return.
330-
* @return The element at the specified position in the list.
331-
*/
332-
public byte get(int i) {
333-
assert i < size;
334-
335-
return data[i];
336-
}
337-
338-
/**
339-
* Gets 4 bytes from byte list as an integer.
340-
*
341-
* @param i Index into the byte list.
342-
* @return Integer starting at index location.
343-
*/
344-
public int getInt(int i) {
345-
assert i + 4 <= size;
346-
347-
return U.bytesToInt(data, i);
348-
}
349-
350216
/**
351217
* Reads all data from input stream until the end into this byte list.
352218
*
@@ -359,12 +225,12 @@ public void readAll(InputStream in) throws IOException {
359225
int read = 0;
360226

361227
while (read >= 0) {
362-
int free = capacity() - size;
228+
int free = data.length - size;
363229

364230
if (free == 0) {
365231
requestFreeSize(1);
366232

367-
free = capacity() - size;
233+
free = data.length - size;
368234

369235
assert free > 0;
370236
}
@@ -376,28 +242,6 @@ public void readAll(InputStream in) throws IOException {
376242
}
377243
}
378244

379-
/**
380-
* @return Output stream based on this byte array list.
381-
*/
382-
public OutputStream outputStream() {
383-
GridUnsafeDataOutput out = new GridUnsafeDataOutput();
384-
385-
out.bytes(data, size);
386-
387-
return out;
388-
}
389-
390-
/**
391-
* @return Input stream based on this byte array list.
392-
*/
393-
public InputStream inputStream() {
394-
GridUnsafeDataInput in = new GridUnsafeDataInput();
395-
396-
in.bytes(data, size);
397-
398-
return in;
399-
}
400-
401245
/** {@inheritDoc} */
402246
@Override public void writeExternal(ObjectOutput out) throws IOException {
403247
out.writeInt(size);
@@ -414,61 +258,6 @@ public InputStream inputStream() {
414258
in.readFully(data, 0, size);
415259
}
416260

417-
/** {@inheritDoc} */
418-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
419-
writer.setBuffer(buf);
420-
421-
if (!writer.isHeaderWritten()) {
422-
if (!writer.writeHeader(directType()))
423-
return false;
424-
425-
writer.onHeaderWritten();
426-
}
427-
428-
switch (writer.state()) {
429-
case 0:
430-
if (!writer.writeByteArray(data))
431-
return false;
432-
433-
writer.incrementState();
434-
435-
case 1:
436-
if (!writer.writeInt(size))
437-
return false;
438-
439-
writer.incrementState();
440-
441-
}
442-
443-
return true;
444-
}
445-
446-
/** {@inheritDoc} */
447-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
448-
reader.setBuffer(buf);
449-
450-
switch (reader.state()) {
451-
case 0:
452-
data = reader.readByteArray();
453-
454-
if (!reader.isLastRead())
455-
return false;
456-
457-
reader.incrementState();
458-
459-
case 1:
460-
size = reader.readInt();
461-
462-
if (!reader.isLastRead())
463-
return false;
464-
465-
reader.incrementState();
466-
467-
}
468-
469-
return true;
470-
}
471-
472261
/** {@inheritDoc} */
473262
@Override public short directType() {
474263
return 84;

0 commit comments

Comments
 (0)