/** Initializes an index, to be restored from snapshot */
 private Builder initializeAsRestore(
     IndexMetaData indexMetaData, RestoreSource restoreSource, boolean asNew) {
   if (!shards.isEmpty()) {
     throw new ElasticSearchIllegalStateException(
         "trying to initialize an index with fresh shards, but already has shards created");
   }
   for (int shardId = 0; shardId < indexMetaData.numberOfShards(); shardId++) {
     IndexShardRoutingTable.Builder indexShardRoutingBuilder =
         new IndexShardRoutingTable.Builder(
             new ShardId(indexMetaData.index(), shardId), asNew ? false : true);
     for (int i = 0; i <= indexMetaData.numberOfReplicas(); i++) {
       indexShardRoutingBuilder.addShard(
           new ImmutableShardRouting(
               index,
               shardId,
               null,
               null,
               i == 0 ? restoreSource : null,
               i == 0,
               ShardRoutingState.UNASSIGNED,
               0));
     }
     shards.put(shardId, indexShardRoutingBuilder.build());
   }
   return this;
 }
 /** Clears the post allocation flag for the specified shard */
 public Builder clearPostAllocationFlag(ShardId shardId) {
   assert this.index.equals(shardId.index().name());
   IndexShardRoutingTable indexShard = shards.get(shardId.id());
   shards.put(
       indexShard.shardId().id(),
       new IndexShardRoutingTable(indexShard.shardId(), indexShard.shards(), false));
   return this;
 }
 public Builder addReplica() {
   for (IntCursor cursor : shards.keys()) {
     int shardId = cursor.value;
     // version 0, will get updated when reroute will happen
     ImmutableShardRouting shard =
         new ImmutableShardRouting(index, shardId, null, false, ShardRoutingState.UNASSIGNED, 0);
     shards.put(
         shardId,
         new IndexShardRoutingTable.Builder(shards.get(shard.id())).addShard(shard).build());
   }
   return this;
 }
 /**
  * Adds a new shard routing (makes a copy of it), with reference data used from the index shard
  * routing table if it needs to be created.
  */
 public Builder addShard(IndexShardRoutingTable refData, ShardRouting shard) {
   IndexShardRoutingTable indexShard = shards.get(shard.id());
   if (indexShard == null) {
     indexShard =
         new IndexShardRoutingTable.Builder(refData.shardId(), refData.primaryAllocatedPostApi())
             .addShard(new ImmutableShardRouting(shard))
             .build();
   } else {
     indexShard =
         new IndexShardRoutingTable.Builder(indexShard)
             .addShard(new ImmutableShardRouting(shard))
             .build();
   }
   shards.put(indexShard.shardId().id(), indexShard);
   return this;
 }
 public Builder removeReplica() {
   for (IntCursor cursor : shards.keys()) {
     int shardId = cursor.value;
     IndexShardRoutingTable indexShard = shards.get(shardId);
     if (indexShard.replicaShards().isEmpty()) {
       // nothing to do here!
       return this;
     }
     // re-add all the current ones
     IndexShardRoutingTable.Builder builder =
         new IndexShardRoutingTable.Builder(
             indexShard.shardId(), indexShard.primaryAllocatedPostApi());
     for (ShardRouting shardRouting : indexShard) {
       builder.addShard(new ImmutableShardRouting(shardRouting));
     }
     // first check if there is one that is not assigned to a node, and remove it
     boolean removed = false;
     for (ShardRouting shardRouting : indexShard) {
       if (!shardRouting.primary() && !shardRouting.assignedToNode()) {
         builder.removeShard(shardRouting);
         removed = true;
         break;
       }
     }
     if (!removed) {
       for (ShardRouting shardRouting : indexShard) {
         if (!shardRouting.primary()) {
           builder.removeShard(shardRouting);
           removed = true;
           break;
         }
       }
     }
     shards.put(shardId, builder.build());
   }
   return this;
 }
 public IndexRoutingTable build() throws RoutingValidationException {
   IndexRoutingTable indexRoutingTable = new IndexRoutingTable(index, shards.build());
   indexRoutingTable.validate();
   return indexRoutingTable;
 }
 public Builder addIndexShard(IndexShardRoutingTable indexShard) {
   shards.put(indexShard.shardId().id(), indexShard);
   return this;
 }