@@ -83,20 +83,24 @@ public static TDigestState read(StreamInput in) throws IOException {
83
83
double compression = in .readDouble ();
84
84
AVLTreeDigest treeDigest = new AVLTreeDigest (compression );
85
85
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 );
88
93
}
89
- MergingDigest mergingDigest = new MergingDigest (compression );
90
- mergingDigest .add (List .of (treeDigest ));
91
- return new TDigestState (compression , mergingDigest );
94
+ return new TDigestState (compression );
92
95
} else {
93
96
// For MergingDigest, adding the original centroids in ascending order to a new, empty MergingDigest isn't guaranteed
94
97
// to produce a MergingDigest whose centroids are exactly equal to the originals.
95
98
// So, use the library's serialization code to ensure we get the exact same centroids, allowing us to compare with equals().
96
99
// The AVLTreeDigest had the same limitation for equals() where it was only guaranteed to return true if the other object was
97
100
// produced by de/serializing the object, so this should be fine.
98
101
int byteSize = in .readVInt ();
99
- byte [] bytes = in .readNBytes (byteSize );
102
+ byte [] bytes = new byte [byteSize ];
103
+ in .readBytes (bytes , 0 , byteSize );
100
104
MergingDigest mergingDigest = MergingDigest .fromBytes (ByteBuffer .wrap (bytes ));
101
105
if (mergingDigest .centroids ().isEmpty ()) {
102
106
return new TDigestState (mergingDigest .compression ());
0 commit comments