2020import org .elasticsearch .compute .data .LongBlock ;
2121import org .elasticsearch .compute .operator .DriverContext ;
2222import org .elasticsearch .core .Releasable ;
23+ import org .elasticsearch .core .Releasables ;
2324import org .elasticsearch .xcontent .ToXContent ;
2425import org .elasticsearch .xcontent .XContentBuilder ;
2526import org .elasticsearch .xcontent .XContentFactory ;
3738
3839public class ChangePointStates {
3940
40- public static class SingleState implements Releasable {
41+ record TimeAndValue (long timestamp , double value ) implements Comparable <TimeAndValue > {
42+ @ Override
43+ public int compareTo (TimeAndValue other ) {
44+ return Long .compare (timestamp , other .timestamp );
45+ }
46+ }
47+
48+ static class SingleState implements Releasable {
4149 private final BigArrays bigArrays ;
4250 private int count ;
4351 private LongArray timestamps ;
@@ -67,38 +75,38 @@ void add(LongBlock timestamps, DoubleBlock values) {
6775 }
6876
6977 void toIntermediate (Block [] blocks , int offset , DriverContext driverContext ) {
70- blocks [offset ] = toTimestampsBlock ( timestamps , driverContext .blockFactory ());
71- blocks [offset + 1 ] = toValuesBlock ( values , driverContext .blockFactory ());
78+ blocks [offset ] = buildTimestampsBlock ( driverContext .blockFactory ());
79+ blocks [offset + 1 ] = buildValuesBlock ( driverContext .blockFactory ());
7280 }
7381
74- Block toTimestampsBlock ( LongArray arr , BlockFactory blockFactory ) {
75- if (arr .size () == 0 ) {
82+ private Block buildTimestampsBlock ( BlockFactory blockFactory ) {
83+ if (timestamps .size () == 0 ) {
7684 return blockFactory .newConstantNullBlock (1 );
7785 }
7886 if (values .size () == 1 ) {
79- return blockFactory .newConstantLongBlockWith (arr .get (0 ), 1 );
87+ return blockFactory .newConstantLongBlockWith (timestamps .get (0 ), 1 );
8088 }
81- try (LongBlock .Builder builder = blockFactory .newLongBlockBuilder ((int ) arr .size ())) {
89+ try (LongBlock .Builder builder = blockFactory .newLongBlockBuilder ((int ) timestamps .size ())) {
8290 builder .beginPositionEntry ();
8391 for (int id = 0 ; id < count ; id ++) {
84- builder .appendLong (arr .get (id ));
92+ builder .appendLong (timestamps .get (id ));
8593 }
8694 builder .endPositionEntry ();
8795 return builder .build ();
8896 }
8997 }
9098
91- Block toValuesBlock ( DoubleArray arr , BlockFactory blockFactory ) {
92- if (arr .size () == 0 ) {
99+ private Block buildValuesBlock ( BlockFactory blockFactory ) {
100+ if (values .size () == 0 ) {
93101 return blockFactory .newConstantNullBlock (1 );
94102 }
95103 if (values .size () == 1 ) {
96- return blockFactory .newConstantDoubleBlockWith (arr .get (0 ), 1 );
104+ return blockFactory .newConstantDoubleBlockWith (values .get (0 ), 1 );
97105 }
98- try (DoubleBlock .Builder builder = blockFactory .newDoubleBlockBuilder ((int ) arr .size ())) {
106+ try (DoubleBlock .Builder builder = blockFactory .newDoubleBlockBuilder ((int ) values .size ())) {
99107 builder .beginPositionEntry ();
100108 for (int id = 0 ; id < count ; id ++) {
101- builder .appendDouble (arr .get (id ));
109+ builder .appendDouble (values .get (id ));
102110 }
103111 builder .endPositionEntry ();
104112 return builder .build ();
@@ -142,21 +150,13 @@ Block toBlock(BlockFactory blockFactory) {
142150 return blockFactory .newConstantBytesRefBlockWith (toBytesRef (), 1 );
143151 }
144152
145- record TimeAndValue (long timestamp , double value ) implements Comparable <TimeAndValue > {
146- @ Override
147- public int compareTo (TimeAndValue other ) {
148- return Long .compare (timestamp , other .timestamp );
149- }
150- }
151-
152153 @ Override
153154 public void close () {
154- timestamps .close ();
155- values .close ();
155+ Releasables .close (timestamps , values );
156156 }
157157 }
158158
159- public static class GroupingState implements Releasable {
159+ static class GroupingState implements Releasable {
160160 private final BigArrays bigArrays ;
161161 private final Map <Integer , SingleState > states ;
162162
@@ -190,8 +190,8 @@ void combineState(int groupId, GroupingState otherState, int otherGroupId) {
190190 }
191191
192192 void toIntermediate (Block [] blocks , int offset , IntVector selected , DriverContext driverContext ) {
193- blocks [offset ] = toTimestampsBlock (driverContext .blockFactory (), selected );
194- blocks [offset + 1 ] = toValuesBlock (driverContext .blockFactory (), selected );
193+ blocks [offset ] = buildTimestampsBlock (driverContext .blockFactory (), selected );
194+ blocks [offset + 1 ] = buildValuesBlock (driverContext .blockFactory (), selected );
195195 }
196196
197197 Block evaluateFinal (IntVector selected , BlockFactory blockFactory ) {
@@ -204,7 +204,7 @@ Block evaluateFinal(IntVector selected, BlockFactory blockFactory) {
204204 }
205205 }
206206
207- Block toTimestampsBlock (BlockFactory blockFactory , IntVector selected ) {
207+ private Block buildTimestampsBlock (BlockFactory blockFactory , IntVector selected ) {
208208 if (states .isEmpty ()) {
209209 return blockFactory .newConstantNullBlock (selected .getPositionCount ());
210210 }
@@ -237,7 +237,7 @@ Block toTimestampsBlock(BlockFactory blockFactory, IntVector selected) {
237237 }
238238 }
239239
240- Block toValuesBlock (BlockFactory blockFactory , IntVector selected ) {
240+ private Block buildValuesBlock (BlockFactory blockFactory , IntVector selected ) {
241241 if (states .isEmpty ()) {
242242 return blockFactory .newConstantNullBlock (selected .getPositionCount ());
243243 }
@@ -274,9 +274,7 @@ void enableGroupIdTracking(SeenGroupIds seenGroupIds) {}
274274
275275 @ Override
276276 public void close () {
277- for (SingleState state : states .values ()) {
278- state .close ();
279- }
277+ Releasables .close (states .values ());
280278 }
281279 }
282280}
0 commit comments