18
18
*/
19
19
package org .elasticsearch .cluster .metadata ;
20
20
21
+ import org .elasticsearch .Version ;
21
22
import org .elasticsearch .cluster .AbstractDiffable ;
22
23
import org .elasticsearch .cluster .Diff ;
24
+ import org .elasticsearch .common .Nullable ;
23
25
import org .elasticsearch .common .ParseField ;
24
26
import org .elasticsearch .common .io .stream .StreamInput ;
25
27
import org .elasticsearch .common .io .stream .StreamOutput ;
35
37
import java .util .Collections ;
36
38
import java .util .List ;
37
39
import java .util .Locale ;
40
+ import java .util .Map ;
38
41
import java .util .Objects ;
39
42
40
43
public final class DataStream extends AbstractDiffable <DataStream > implements ToXContentObject {
@@ -45,18 +48,20 @@ public final class DataStream extends AbstractDiffable<DataStream> implements To
45
48
private final TimestampField timeStampField ;
46
49
private final List <Index > indices ;
47
50
private final long generation ;
51
+ private final Map <String , Object > metadata ;
48
52
49
- public DataStream (String name , TimestampField timeStampField , List <Index > indices , long generation ) {
53
+ public DataStream (String name , TimestampField timeStampField , List <Index > indices , long generation , Map < String , Object > metadata ) {
50
54
this .name = name ;
51
55
this .timeStampField = timeStampField ;
52
56
this .indices = Collections .unmodifiableList (indices );
53
57
this .generation = generation ;
58
+ this .metadata = metadata ;
54
59
assert indices .size () > 0 ;
55
60
assert indices .get (indices .size () - 1 ).getName ().equals (getDefaultBackingIndexName (name , generation ));
56
61
}
57
62
58
63
public DataStream (String name , TimestampField timeStampField , List <Index > indices ) {
59
- this (name , timeStampField , indices , indices .size ());
64
+ this (name , timeStampField , indices , indices .size (), null );
60
65
}
61
66
62
67
public String getName () {
@@ -75,6 +80,11 @@ public long getGeneration() {
75
80
return generation ;
76
81
}
77
82
83
+ @ Nullable
84
+ public Map <String , Object > getMetadata () {
85
+ return metadata ;
86
+ }
87
+
78
88
/**
79
89
* Performs a rollover on a {@code DataStream} instance and returns a new instance containing
80
90
* the updated list of backing indices and incremented generation.
@@ -87,7 +97,7 @@ public DataStream rollover(Index newWriteIndex) {
87
97
assert newWriteIndex .getName ().equals (getDefaultBackingIndexName (name , generation + 1 ));
88
98
List <Index > backingIndices = new ArrayList <>(indices );
89
99
backingIndices .add (newWriteIndex );
90
- return new DataStream (name , timeStampField , backingIndices , generation + 1 );
100
+ return new DataStream (name , timeStampField , backingIndices , generation + 1 , metadata );
91
101
}
92
102
93
103
/**
@@ -101,7 +111,7 @@ public DataStream removeBackingIndex(Index index) {
101
111
List <Index > backingIndices = new ArrayList <>(indices );
102
112
backingIndices .remove (index );
103
113
assert backingIndices .size () == indices .size () - 1 ;
104
- return new DataStream (name , timeStampField , backingIndices , generation );
114
+ return new DataStream (name , timeStampField , backingIndices , generation , metadata );
105
115
}
106
116
107
117
/**
@@ -126,7 +136,7 @@ public DataStream replaceBackingIndex(Index existingBackingIndex, Index newBacki
126
136
"it is the write index" , existingBackingIndex .getName (), name ));
127
137
}
128
138
backingIndices .set (backingIndexPosition , newBackingIndex );
129
- return new DataStream (name , timeStampField , backingIndices , generation );
139
+ return new DataStream (name , timeStampField , backingIndices , generation , metadata );
130
140
}
131
141
132
142
/**
@@ -142,7 +152,8 @@ public static String getDefaultBackingIndexName(String dataStreamName, long gene
142
152
}
143
153
144
154
public DataStream (StreamInput in ) throws IOException {
145
- this (in .readString (), new TimestampField (in ), in .readList (Index ::new ), in .readVLong ());
155
+ this (in .readString (), new TimestampField (in ), in .readList (Index ::new ), in .readVLong (),
156
+ in .getVersion ().onOrAfter (Version .V_8_0_0 ) ? in .readMap (): null );
146
157
}
147
158
148
159
public static Diff <DataStream > readDiffFrom (StreamInput in ) throws IOException {
@@ -155,22 +166,28 @@ public void writeTo(StreamOutput out) throws IOException {
155
166
timeStampField .writeTo (out );
156
167
out .writeList (indices );
157
168
out .writeVLong (generation );
169
+ if (out .getVersion ().onOrAfter (Version .V_8_0_0 )) {
170
+ out .writeMap (metadata );
171
+ }
158
172
}
159
173
160
174
public static final ParseField NAME_FIELD = new ParseField ("name" );
161
175
public static final ParseField TIMESTAMP_FIELD_FIELD = new ParseField ("timestamp_field" );
162
176
public static final ParseField INDICES_FIELD = new ParseField ("indices" );
163
177
public static final ParseField GENERATION_FIELD = new ParseField ("generation" );
178
+ public static final ParseField METADATA_FIELD = new ParseField ("_meta" );
164
179
165
180
@ SuppressWarnings ("unchecked" )
166
181
private static final ConstructingObjectParser <DataStream , Void > PARSER = new ConstructingObjectParser <>("data_stream" ,
167
- args -> new DataStream ((String ) args [0 ], (TimestampField ) args [1 ], (List <Index >) args [2 ], (Long ) args [3 ]));
182
+ args -> new DataStream ((String ) args [0 ], (TimestampField ) args [1 ], (List <Index >) args [2 ], (Long ) args [3 ],
183
+ (Map <String , Object >) args [4 ]));
168
184
169
185
static {
170
186
PARSER .declareString (ConstructingObjectParser .constructorArg (), NAME_FIELD );
171
187
PARSER .declareObject (ConstructingObjectParser .constructorArg (), TimestampField .PARSER , TIMESTAMP_FIELD_FIELD );
172
188
PARSER .declareObjectArray (ConstructingObjectParser .constructorArg (), (p , c ) -> Index .fromXContent (p ), INDICES_FIELD );
173
189
PARSER .declareLong (ConstructingObjectParser .constructorArg (), GENERATION_FIELD );
190
+ PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .map (), METADATA_FIELD );
174
191
}
175
192
176
193
public static DataStream fromXContent (XContentParser parser ) throws IOException {
@@ -184,6 +201,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
184
201
builder .field (TIMESTAMP_FIELD_FIELD .getPreferredName (), timeStampField );
185
202
builder .field (INDICES_FIELD .getPreferredName (), indices );
186
203
builder .field (GENERATION_FIELD .getPreferredName (), generation );
204
+ if (metadata != null ) {
205
+ builder .field (METADATA_FIELD .getPreferredName (), metadata );
206
+ }
187
207
builder .endObject ();
188
208
return builder ;
189
209
}
@@ -196,12 +216,13 @@ public boolean equals(Object o) {
196
216
return name .equals (that .name ) &&
197
217
timeStampField .equals (that .timeStampField ) &&
198
218
indices .equals (that .indices ) &&
199
- generation == that .generation ;
219
+ generation == that .generation &&
220
+ Objects .equals (metadata , that .metadata );
200
221
}
201
222
202
223
@ Override
203
224
public int hashCode () {
204
- return Objects .hash (name , timeStampField , indices , generation );
225
+ return Objects .hash (name , timeStampField , indices , generation , metadata );
205
226
}
206
227
207
228
public static final class TimestampField implements Writeable , ToXContentObject {
0 commit comments