Skip to content

Commit 9c83d7d

Browse files
author
Peter Alfonsi
committed
Fix serialization part 2
Signed-off-by: Peter Alfonsi <[email protected]>
1 parent 1bcb5a0 commit 9c83d7d

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

server/src/main/java/org/opensearch/search/aggregations/metrics/TDigestState.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,24 @@ public static TDigestState read(StreamInput in) throws IOException {
8383
double compression = in.readDouble();
8484
AVLTreeDigest treeDigest = new AVLTreeDigest(compression);
8585
int n = in.readVInt();
86-
for (int i = 0; i < n; i++) {
87-
treeDigest.add(in.readDouble(), in.readVInt());
86+
if (n > 0) {
87+
for (int i = 0; i < n; i++) {
88+
treeDigest.add(in.readDouble(), in.readVInt());
89+
}
90+
MergingDigest mergingDigest = new MergingDigest(compression);
91+
mergingDigest.add(List.of(treeDigest));
92+
return new TDigestState(compression, mergingDigest);
8893
}
89-
MergingDigest mergingDigest = new MergingDigest(compression);
90-
mergingDigest.add(List.of(treeDigest));
91-
return new TDigestState(compression, mergingDigest);
94+
return new TDigestState(compression);
9295
} else {
9396
// For MergingDigest, adding the original centroids in ascending order to a new, empty MergingDigest isn't guaranteed
9497
// to produce a MergingDigest whose centroids are exactly equal to the originals.
9598
// So, use the library's serialization code to ensure we get the exact same centroids, allowing us to compare with equals().
9699
// The AVLTreeDigest had the same limitation for equals() where it was only guaranteed to return true if the other object was
97100
// produced by de/serializing the object, so this should be fine.
98101
int byteSize = in.readVInt();
99-
byte[] bytes = in.readNBytes(byteSize);
102+
byte[] bytes = new byte[byteSize];
103+
in.readBytes(bytes, 0, byteSize);
100104
MergingDigest mergingDigest = MergingDigest.fromBytes(ByteBuffer.wrap(bytes));
101105
if (mergingDigest.centroids().isEmpty()) {
102106
return new TDigestState(mergingDigest.compression());

server/src/test/java/org/opensearch/search/aggregations/metrics/InternalMedianAbsoluteDeviationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Map;
4242

4343
public class InternalMedianAbsoluteDeviationTests extends InternalAggregationTestCase<InternalMedianAbsoluteDeviation> {
44+
4445
@Override
4546
protected InternalMedianAbsoluteDeviation createTestInstance(String name, Map<String, Object> metadata) {
4647
final TDigestState valuesSketch = new TDigestState(randomDoubleBetween(20, 1000, true));

0 commit comments

Comments
 (0)