@@ -42,7 +42,7 @@ public final class ShardGenerations {
42
42
private final Map <IndexId , List <String >> shardGenerations ;
43
43
44
44
private ShardGenerations (Map <IndexId , List <String >> shardGenerations ) {
45
- this .shardGenerations = shardGenerations ;
45
+ this .shardGenerations = Map . copyOf ( shardGenerations ) ;
46
46
}
47
47
48
48
private static final Pattern IS_NUMBER = Pattern .compile ("^\\ d+$" );
@@ -76,7 +76,7 @@ public int totalShards() {
76
76
* @return indices for which shard generations are tracked
77
77
*/
78
78
public Collection <IndexId > indices () {
79
- return Collections . unmodifiableSet ( shardGenerations .keySet () );
79
+ return shardGenerations .keySet ();
80
80
}
81
81
82
82
/**
@@ -135,8 +135,7 @@ public String getShardGen(IndexId indexId, int shardId) {
135
135
}
136
136
137
137
public List <String > getGens (IndexId indexId ) {
138
- final List <String > existing = shardGenerations .get (indexId );
139
- return existing == null ? Collections .emptyList () : Collections .unmodifiableList (existing );
138
+ return shardGenerations .getOrDefault (indexId , Collections .emptyList ());
140
139
}
141
140
142
141
@ Override
@@ -200,15 +199,17 @@ public Builder putAll(ShardGenerations shardGenerations) {
200
199
for (int i = 0 ; i < gens .size (); i ++) {
201
200
final String gen = gens .get (i );
202
201
if (gen != null ) {
203
- put (indexId , i , gens . get ( i ) );
202
+ put (indexId , i , gen );
204
203
}
205
204
}
206
205
});
207
206
return this ;
208
207
}
209
208
210
209
public Builder put (IndexId indexId , int shardId , String generation ) {
211
- generations .computeIfAbsent (indexId , i -> new HashMap <>()).put (shardId , generation );
210
+ String existingGeneration = generations .computeIfAbsent (indexId , i -> new HashMap <>()).put (shardId , generation );
211
+ assert generation != null || existingGeneration == null :
212
+ "must not overwrite existing generation with null generation [" + existingGeneration + "]" ;
212
213
return this ;
213
214
}
214
215
@@ -223,7 +224,7 @@ public ShardGenerations build() {
223
224
// a map entry.
224
225
final String [] gens = new String [size ];
225
226
entry .getValue ().forEach ((shardId , generation ) -> gens [shardId ] = generation );
226
- return Arrays .asList (gens );
227
+ return Collections . unmodifiableList ( Arrays .asList (gens ) );
227
228
}
228
229
)));
229
230
}
0 commit comments