14
14
import org .elasticsearch .common .io .stream .StreamInput ;
15
15
import org .elasticsearch .common .io .stream .StreamOutput ;
16
16
import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
17
- import org .elasticsearch .common .xcontent .ToXContentObject ;
17
+ import org .elasticsearch .common .xcontent .ToXContentFragment ;
18
18
import org .elasticsearch .common .xcontent .XContentBuilder ;
19
19
import org .elasticsearch .common .xcontent .XContentParser ;
20
20
21
21
import java .io .IOException ;
22
22
import java .util .ArrayList ;
23
23
import java .util .Collection ;
24
24
import java .util .Collections ;
25
+ import java .util .HashSet ;
25
26
import java .util .List ;
26
27
import java .util .Objects ;
28
+ import java .util .Set ;
29
+ import java .util .function .Predicate ;
30
+ import java .util .stream .Collectors ;
27
31
28
- public class DataStreamAlias extends AbstractDiffable <DataStreamAlias > implements ToXContentObject {
32
+ public class DataStreamAlias extends AbstractDiffable <DataStreamAlias > implements ToXContentFragment {
29
33
30
34
public static final ParseField DATA_STREAMS_FIELD = new ParseField ("data_streams" );
31
35
public static final ParseField WRITE_DATA_STREAM_FIELD = new ParseField ("write_data_stream" );
@@ -50,6 +54,7 @@ public DataStreamAlias(String name, Collection<String> dataStreams, String write
50
54
this .name = Objects .requireNonNull (name );
51
55
this .dataStreams = Collections .unmodifiableList (new ArrayList <>(dataStreams ));
52
56
this .writeDataStream = writeDataStream ;
57
+ assert writeDataStream == null || dataStreams .contains (writeDataStream );
53
58
}
54
59
55
60
public DataStreamAlias (StreamInput in ) throws IOException {
@@ -58,18 +63,136 @@ public DataStreamAlias(StreamInput in) throws IOException {
58
63
this .writeDataStream = in .readOptionalString ();
59
64
}
60
65
66
+ /**
67
+ * Returns the name of this data stream alias.
68
+ */
61
69
public String getName () {
62
70
return name ;
63
71
}
64
72
73
+ /**
74
+ * Returns the data streams that are referenced
75
+ */
65
76
public List <String > getDataStreams () {
66
77
return dataStreams ;
67
78
}
68
79
80
+ /**
81
+ * Returns the write data stream this data stream alias is referring to.
82
+ * Write requests targeting this instance will resolve the write index
83
+ * of the write data stream this alias is referring to.
84
+ *
85
+ * Note that the write data stream is also included in {@link #getDataStreams()}.
86
+ */
69
87
public String getWriteDataStream () {
70
88
return writeDataStream ;
71
89
}
72
90
91
+ /**
92
+ * Returns a new {@link DataStreamAlias} instance with the provided data stream name added to it as a new member.
93
+ * If the provided isWriteDataStream is set to <code>true</code> then the provided data stream is also set as write data stream.
94
+ * If the provided isWriteDataStream is set to <code>false</code> and the provided data stream is also the write data stream of
95
+ * this instance then the returned data stream alias instance's write data stream is unset.
96
+ *
97
+ * The same instance is returned if the attempted addition of the provided data stream didn't change this instance.
98
+ */
99
+ public DataStreamAlias addDataStream (String dataStream , Boolean isWriteDataStream ) {
100
+ String writeDataStream = this .writeDataStream ;
101
+ if (isWriteDataStream != null ) {
102
+ if (isWriteDataStream ) {
103
+ writeDataStream = dataStream ;
104
+ } else {
105
+ if (dataStream .equals (writeDataStream )) {
106
+ writeDataStream = null ;
107
+ }
108
+ }
109
+ }
110
+
111
+ Set <String > dataStreams = new HashSet <>(this .dataStreams );
112
+ boolean added = dataStreams .add (dataStream );
113
+ if (added || Objects .equals (this .writeDataStream , writeDataStream ) == false ) {
114
+ return new DataStreamAlias (name , dataStreams , writeDataStream );
115
+ } else {
116
+ return this ;
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Returns a {@link DataStreamAlias} instance based on this instance but with the specified data stream no longer referenced.
122
+ * Returns <code>null</code> if because of the removal of the provided data stream name a new instance wouldn't reference to
123
+ * any data stream. The same instance is returned if the attempted removal of the provided data stream didn't change this instance.
124
+ */
125
+ public DataStreamAlias removeDataStream (String dataStream ) {
126
+ Set <String > dataStreams = new HashSet <>(this .dataStreams );
127
+ boolean removed = dataStreams .remove (dataStream );
128
+ if (removed == false ) {
129
+ return this ;
130
+ }
131
+
132
+ if (dataStreams .isEmpty ()) {
133
+ return null ;
134
+ } else {
135
+ String writeDataStream = this .writeDataStream ;
136
+ if (dataStream .equals (writeDataStream )) {
137
+ writeDataStream = null ;
138
+ }
139
+ return new DataStreamAlias (name , dataStreams , writeDataStream );
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Returns a new {@link DataStreamAlias} instance that contains a new intersection
145
+ * of data streams from this instance and the provided filter.
146
+ *
147
+ * The write data stream gets set to null in the returned instance if the write
148
+ * data stream no longer appears in the intersection.
149
+ */
150
+ public DataStreamAlias intersect (Predicate <String > filter ) {
151
+ List <String > intersectingDataStreams = this .dataStreams .stream ()
152
+ .filter (filter )
153
+ .collect (Collectors .toList ());
154
+ String writeDataStream = this .writeDataStream ;
155
+ if (intersectingDataStreams .contains (writeDataStream ) == false ) {
156
+ writeDataStream = null ;
157
+ }
158
+ return new DataStreamAlias (this .name , intersectingDataStreams , writeDataStream );
159
+ }
160
+
161
+ /**
162
+ * Returns a new {@link DataStreamAlias} instance containing data streams referenced in this instance
163
+ * and the other instance. If this instance doesn't have a write data stream then the write index of
164
+ * the other data stream becomes the write data stream of the returned instance.
165
+ */
166
+ public DataStreamAlias merge (DataStreamAlias other ) {
167
+ Set <String > mergedDataStreams = new HashSet <>(other .getDataStreams ());
168
+ mergedDataStreams .addAll (this .getDataStreams ());
169
+
170
+ String writeDataStream = this .writeDataStream ;
171
+ if (writeDataStream == null ) {
172
+ if (other .getWriteDataStream () != null && mergedDataStreams .contains (other .getWriteDataStream ())) {
173
+ writeDataStream = other .getWriteDataStream ();
174
+ }
175
+ }
176
+
177
+ return new DataStreamAlias (this .name , mergedDataStreams , writeDataStream );
178
+ }
179
+
180
+ /**
181
+ * Returns a new instance with potentially renamed data stream names and write data stream name.
182
+ * If a data stream name matches with the provided rename pattern then it is renamed according
183
+ * to the provided rename replacement.
184
+ */
185
+ public DataStreamAlias renameDataStreams (String renamePattern , String renameReplacement ) {
186
+ List <String > renamedDataStreams = this .dataStreams .stream ()
187
+ .map (s -> s .replaceAll (renamePattern , renameReplacement ))
188
+ .collect (Collectors .toList ());
189
+ String writeDataStream = this .writeDataStream ;
190
+ if (writeDataStream != null ) {
191
+ writeDataStream = writeDataStream .replaceAll (renamePattern , renameReplacement );
192
+ }
193
+ return new DataStreamAlias (this .name , renamedDataStreams , writeDataStream );
194
+ }
195
+
73
196
public static Diff <DataStreamAlias > readDiffFrom (StreamInput in ) throws IOException {
74
197
return readDiffFrom (DataStreamAlias ::new , in );
75
198
}
0 commit comments