3636import org .elasticsearch .xcontent .XContentParser ;
3737
3838import java .io .IOException ;
39- import java .util .Arrays ;
4039import java .util .List ;
4140import java .util .Locale ;
4241import java .util .Map ;
4342import java .util .Objects ;
44- import java .util .function .Function ;
45- import java .util .stream .Collectors ;
4643
4744import static org .elasticsearch .xcontent .ConstructingObjectParser .constructorArg ;
4845
@@ -97,8 +94,8 @@ public static boolean isDataStreamsLifecycleOnlyMode(final Settings settings) {
9794 Setting .Property .NodeScope
9895 );
9996
100- public static final DataStreamLifecycle DEFAULT_DATA_LIFECYCLE = DataStreamLifecycle . createDataLifecycle ( null , null , null , null );
101- public static final DataStreamLifecycle DEFAULT_FAILURE_LIFECYCLE = DataStreamLifecycle . createFailuresLifecycle ( null , null );
97+ public static final DataStreamLifecycle DEFAULT_DATA_LIFECYCLE = dataLifecycleBuilder (). build ( );
98+ public static final DataStreamLifecycle DEFAULT_FAILURE_LIFECYCLE = failuresLifecycleBuilder (). build ( );
10299
103100 public static final String DATA_STREAM_LIFECYCLE_ORIGIN = "data_stream_lifecycle" ;
104101
@@ -163,9 +160,8 @@ public static boolean isDataStreamsLifecycleOnlyMode(final Settings settings) {
163160 private final DownsampleConfig .SamplingMethod downsamplingMethod ;
164161
165162 /**
166- * This constructor is visible for testing, please use
167- * {@link DataStreamLifecycle#createDataLifecycle(Boolean, TimeValue, List, DownsampleConfig.SamplingMethod)} or
168- * {@link DataStreamLifecycle#createFailuresLifecycle(Boolean, TimeValue)}.
163+ * This constructor is visible for testing, please use {@link DataStreamLifecycle#dataLifecycleBuilder()} or
164+ * {@link DataStreamLifecycle#failuresLifecycleBuilder()}.
169165 */
170166 DataStreamLifecycle (
171167 LifecycleType lifecycleType ,
@@ -188,27 +184,6 @@ public static boolean isDataStreamsLifecycleOnlyMode(final Settings settings) {
188184 this .downsamplingMethod = downsamplingMethod ;
189185 }
190186
191- /**
192- * This factory method creates a lifecycle applicable for the data index component of a data stream. This
193- * means it supports all configuration applicable for backing indices.
194- */
195- public static DataStreamLifecycle createDataLifecycle (
196- @ Nullable Boolean enabled ,
197- @ Nullable TimeValue dataRetention ,
198- @ Nullable List <DownsamplingRound > downsamplingRounds ,
199- @ Nullable DownsampleConfig .SamplingMethod downsamplingMethod
200- ) {
201- return new DataStreamLifecycle (LifecycleType .DATA , enabled , dataRetention , downsamplingRounds , downsamplingMethod );
202- }
203-
204- /**
205- * This factory method creates a lifecycle applicable for the failures index component of a data stream. This
206- * means it supports only enabling and retention.
207- */
208- public static DataStreamLifecycle createFailuresLifecycle (@ Nullable Boolean enabled , @ Nullable TimeValue dataRetention ) {
209- return new DataStreamLifecycle (LifecycleType .FAILURES , enabled , dataRetention , null , null );
210- }
211-
212187 /**
213188 * Returns true, if this data stream lifecycle configuration is enabled, false otherwise
214189 */
@@ -671,52 +646,6 @@ public String toString() {
671646 }
672647 }
673648
674- /**
675- * This factory method creates a lifecycle template applicable for the data index component of a data stream. This
676- * means it supports all configuration applicable for backing indices.
677- */
678- public static Template createDataLifecycleTemplate (
679- boolean enabled ,
680- TimeValue dataRetention ,
681- List <DataStreamLifecycle .DownsamplingRound > downsamplingRounds ,
682- DownsampleConfig .SamplingMethod downsamplingMethod
683- ) {
684- return new Template (
685- LifecycleType .DATA ,
686- enabled ,
687- ResettableValue .create (dataRetention ),
688- ResettableValue .create (downsamplingRounds ),
689- ResettableValue .create (downsamplingMethod )
690- );
691- }
692-
693- /**
694- * This factory method creates a lifecycle template applicable for the data index component of a data stream. This
695- * means it supports all configuration applicable for backing indices.
696- */
697- public static Template createDataLifecycleTemplate (
698- boolean enabled ,
699- ResettableValue <TimeValue > dataRetention ,
700- ResettableValue <List <DataStreamLifecycle .DownsamplingRound >> downsamplingRounds ,
701- ResettableValue <DownsampleConfig .SamplingMethod > downsamplingMethod
702- ) {
703- return new Template (LifecycleType .DATA , enabled , dataRetention , downsamplingRounds , downsamplingMethod );
704- }
705-
706- /**
707- * This factory method creates a lifecycle template applicable for the failures index component of a data stream. This
708- * means it supports only setting the enabled and the retention.
709- */
710- public static Template createFailuresLifecycleTemplate (boolean enabled , TimeValue dataRetention ) {
711- return new Template (
712- LifecycleType .FAILURES ,
713- enabled ,
714- ResettableValue .create (dataRetention ),
715- ResettableValue .undefined (),
716- ResettableValue .undefined ()
717- );
718- }
719-
720649 /**
721650 * Represents the template configuration of a lifecycle. It supports explicitly resettable values
722651 * to allow value reset during template composition.
@@ -729,22 +658,6 @@ public record Template(
729658 ResettableValue <DownsampleConfig .SamplingMethod > downsamplingMethod
730659 ) implements ToXContentObject , Writeable {
731660
732- Template (
733- LifecycleType lifecycleType ,
734- boolean enabled ,
735- TimeValue dataRetention ,
736- List <DataStreamLifecycle .DownsamplingRound > downsamplingRounds ,
737- DownsampleConfig .SamplingMethod downsamplingMethod
738- ) {
739- this (
740- lifecycleType ,
741- enabled ,
742- ResettableValue .create (dataRetention ),
743- ResettableValue .create (downsamplingRounds ),
744- ResettableValue .create (downsamplingMethod )
745- );
746- }
747-
748661 public Template {
749662 if (lifecycleType == LifecycleType .FAILURES && downsamplingRounds .get () != null ) {
750663 throw new IllegalArgumentException (DOWNSAMPLING_NOT_SUPPORTED_ERROR_MESSAGE );
@@ -756,13 +669,7 @@ public record Template(
756669 }
757670 }
758671
759- public static final DataStreamLifecycle .Template DATA_DEFAULT = new DataStreamLifecycle .Template (
760- LifecycleType .DATA ,
761- true ,
762- ResettableValue .undefined (),
763- ResettableValue .undefined (),
764- ResettableValue .undefined ()
765- );
672+ public static final DataStreamLifecycle .Template DATA_DEFAULT = dataLifecycleBuilder ().enabled (true ).buildTemplate ();
766673
767674 @ SuppressWarnings ("unchecked" )
768675 public static final ConstructingObjectParser <DataStreamLifecycle .Template , LifecycleType > PARSER = new ConstructingObjectParser <>(
@@ -974,12 +881,9 @@ public static Builder failuresLifecycleBuilder() {
974881 public static class Builder {
975882 private final LifecycleType lifecycleType ;
976883 private boolean enabled = true ;
977- @ Nullable
978- private TimeValue dataRetention = null ;
979- @ Nullable
980- private List <DownsamplingRound > downsamplingRounds = null ;
981- @ Nullable
982- private DownsampleConfig .SamplingMethod downsamplingMethod = null ;
884+ private ResettableValue <TimeValue > dataRetention = ResettableValue .undefined ();
885+ private ResettableValue <List <DownsamplingRound >> downsamplingRounds = ResettableValue .undefined ();
886+ private ResettableValue <DownsampleConfig .SamplingMethod > downsamplingMethod = ResettableValue .undefined ();
983887
984888 private Builder (LifecycleType lifecycleType ) {
985889 this .lifecycleType = lifecycleType ;
@@ -988,17 +892,17 @@ private Builder(LifecycleType lifecycleType) {
988892 private Builder (DataStreamLifecycle .Template template ) {
989893 lifecycleType = template .lifecycleType ();
990894 enabled = template .enabled ();
991- dataRetention = template .dataRetention (). get () ;
992- downsamplingRounds = template .downsamplingRounds (). get () ;
993- downsamplingMethod = template .downsamplingMethod (). get () ;
895+ dataRetention = template .dataRetention ();
896+ downsamplingRounds = template .downsamplingRounds ();
897+ downsamplingMethod = template .downsamplingMethod ();
994898 }
995899
996900 private Builder (DataStreamLifecycle lifecycle ) {
997901 lifecycleType = lifecycle .lifecycleType ;
998902 enabled = lifecycle .enabled ();
999- dataRetention = lifecycle .dataRetention ();
1000- downsamplingRounds = lifecycle .downsamplingRounds ();
1001- downsamplingMethod = lifecycle .downsamplingMethod ();
903+ dataRetention = ResettableValue . create ( lifecycle .dataRetention () );
904+ downsamplingRounds = ResettableValue . create ( lifecycle .downsamplingRounds () );
905+ downsamplingMethod = ResettableValue . create ( lifecycle .downsamplingMethod () );
1002906 }
1003907
1004908 public Builder composeTemplate (DataStreamLifecycle .Template template ) {
@@ -1017,42 +921,42 @@ public Builder enabled(boolean enabled) {
1017921
1018922 public Builder dataRetention (ResettableValue <TimeValue > dataRetention ) {
1019923 if (dataRetention .isDefined ()) {
1020- this .dataRetention = dataRetention . get () ;
924+ this .dataRetention = dataRetention ;
1021925 }
1022926 return this ;
1023927 }
1024928
1025929 public Builder dataRetention (@ Nullable TimeValue dataRetention ) {
1026- this .dataRetention = dataRetention ;
930+ this .dataRetention = ResettableValue . create ( dataRetention ) ;
1027931 return this ;
1028932 }
1029933
1030934 public Builder downsamplingRounds (ResettableValue <List <DownsamplingRound >> downsampling ) {
1031935 if (downsampling .isDefined ()) {
1032- this .downsamplingRounds = downsampling . get () ;
936+ this .downsamplingRounds = downsampling ;
1033937 }
1034938 return this ;
1035939 }
1036940
1037941 public Builder downsamplingRounds (@ Nullable List <DownsamplingRound > downsampling ) {
1038- this .downsamplingRounds = downsampling ;
942+ this .downsamplingRounds = ResettableValue . create ( downsampling ) ;
1039943 return this ;
1040944 }
1041945
1042946 public Builder downsamplingMethod (ResettableValue <DownsampleConfig .SamplingMethod > downsamplingMethod ) {
1043947 if (downsamplingMethod .isDefined ()) {
1044- this .downsamplingMethod = downsamplingMethod . get () ;
948+ this .downsamplingMethod = downsamplingMethod ;
1045949 }
1046950 return this ;
1047951 }
1048952
1049953 public Builder downsamplingMethod (@ Nullable DownsampleConfig .SamplingMethod downsamplingMethod ) {
1050- this .downsamplingMethod = downsamplingMethod ;
954+ this .downsamplingMethod = ResettableValue . create ( downsamplingMethod ) ;
1051955 return this ;
1052956 }
1053957
1054958 public DataStreamLifecycle build () {
1055- return new DataStreamLifecycle (lifecycleType , enabled , dataRetention , downsamplingRounds , downsamplingMethod );
959+ return new DataStreamLifecycle (lifecycleType , enabled , dataRetention . get () , downsamplingRounds . get () , downsamplingMethod . get () );
1056960 }
1057961
1058962 public Template buildTemplate () {
@@ -1065,26 +969,22 @@ public Template buildTemplate() {
1065969 * Visible for testing
1066970 */
1067971 enum LifecycleType implements Writeable {
1068- DATA ("data" , ( byte ) 0 ),
1069- FAILURES ("failures" , ( byte ) 1 );
972+ DATA ("data" ),
973+ FAILURES ("failures" );
1070974
1071975 private final String label ;
1072- private final byte id ;
1073- private static final Map <Byte , LifecycleType > REGISTRY = Arrays .stream (LifecycleType .values ())
1074- .collect (Collectors .toMap (l -> l .id , Function .identity ()));
1075976
1076- LifecycleType (String label , byte id ) {
977+ LifecycleType (String label ) {
1077978 this .label = label ;
1078- this .id = id ;
1079979 }
1080980
1081981 @ Override
1082982 public void writeTo (StreamOutput out ) throws IOException {
1083- out .write ( id );
983+ out .writeEnum ( this );
1084984 }
1085985
1086986 public static LifecycleType read (StreamInput in ) throws IOException {
1087- return REGISTRY . get ( in .readByte () );
987+ return in .readEnum ( LifecycleType . class );
1088988 }
1089989 }
1090990}
0 commit comments