Skip to content

Commit 4436100

Browse files
authored
use actual length, not optimized serialized length (#57)
1 parent 1ffaeed commit 4436100

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/main/java/com/arpnetworking/tsdcore/model/AggregationMessage.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,20 @@ public static Optional<AggregationMessage> deserialize(final ByteString data) {
9393
try {
9494
switch (type) {
9595
case 0x01:
96-
return Optional.of(new AggregationMessage(Messages.HostIdentification.parseFrom(payloadBytes)));
96+
return Optional.of(new AggregationMessage(Messages.HostIdentification.parseFrom(payloadBytes), length));
9797
case 0x03:
98-
return Optional.of(new AggregationMessage(Messages.HeartbeatRecord.parseFrom(payloadBytes)));
98+
return Optional.of(new AggregationMessage(Messages.HeartbeatRecord.parseFrom(payloadBytes), length));
9999
case 0x04:
100-
return Optional.of(new AggregationMessage(Messages.StatisticSetRecord.parseFrom(payloadBytes)));
100+
return Optional.of(new AggregationMessage(Messages.StatisticSetRecord.parseFrom(payloadBytes), length));
101101
case 0x05:
102102
// 0x05 is the message type for all supporting data
103103
switch (subType) {
104104
case 0x01:
105-
return Optional.of(new AggregationMessage(Messages.SamplesSupportingData.parseFrom(payloadBytes)));
105+
return Optional.of(new AggregationMessage(Messages.SamplesSupportingData.parseFrom(payloadBytes), length));
106106
case 0x02:
107-
return Optional.of(new AggregationMessage(Messages.SparseHistogramSupportingData.parseFrom(payloadBytes)));
107+
return Optional.of(new AggregationMessage(
108+
Messages.SparseHistogramSupportingData.parseFrom(payloadBytes),
109+
length));
108110
default:
109111
LOGGER.warn(
110112
String.format("Invalid protocol buffer, unknown subtype; type=%s, subtype=%s, bytes=%s",
@@ -134,24 +136,28 @@ private static boolean typeHasSubtype(final byte type) {
134136
* @return <code>Buffer</code> containing serialized message.
135137
*/
136138
public ByteString serialize() {
139+
return AggregationMessage.serialize(_message);
140+
}
141+
142+
private static ByteString serialize(final GeneratedMessageV3 message) {
137143
final ByteStringBuilder b = ByteString.createBuilder();
138-
if (_message instanceof Messages.HostIdentification) {
144+
if (message instanceof Messages.HostIdentification) {
139145
b.putByte((byte) 0x01);
140-
} else if (_message instanceof Messages.HeartbeatRecord) {
146+
} else if (message instanceof Messages.HeartbeatRecord) {
141147
b.putByte((byte) 0x03);
142-
} else if (_message instanceof Messages.StatisticSetRecord) {
148+
} else if (message instanceof Messages.StatisticSetRecord) {
143149
b.putByte((byte) 0x04);
144-
} else if (_message instanceof Messages.SamplesSupportingData) {
150+
} else if (message instanceof Messages.SamplesSupportingData) {
145151
b.putByte((byte) 0x05);
146152
b.putByte((byte) 0x01);
147-
} else if (_message instanceof Messages.SparseHistogramSupportingData) {
153+
} else if (message instanceof Messages.SparseHistogramSupportingData) {
148154
b.putByte((byte) 0x05);
149155
b.putByte((byte) 0x02);
150156
} else {
151-
throw new IllegalArgumentException(String.format("Unsupported message; message=%s", _message));
157+
throw new IllegalArgumentException(String.format("Unsupported message; message=%s", message));
152158
}
153159
try {
154-
_message.writeTo(b.asOutputStream());
160+
message.writeTo(b.asOutputStream());
155161
} catch (final IOException e) {
156162
throw new RuntimeException(e);
157163
}
@@ -165,14 +171,20 @@ public GeneratedMessageV3 getMessage() {
165171
}
166172

167173
public int getLength() {
168-
return _message.getSerializedSize() + HEADER_SIZE_IN_BYTES;
174+
return _length;
169175
}
170176

171177
private AggregationMessage(final GeneratedMessageV3 message) {
178+
this(message, serialize(message).length());
179+
}
180+
181+
private AggregationMessage(final GeneratedMessageV3 message, final int length) {
182+
_length = length;
172183
_message = message;
173184
}
174185

175186
private final GeneratedMessageV3 _message;
187+
private final int _length;
176188

177189
private static final int BYTE_SIZE_IN_BYTES = 1;
178190
private static final int INTEGER_SIZE_IN_BYTES = Integer.SIZE / 8;

0 commit comments

Comments
 (0)