@Override
 public void writeTo(StreamOutput out) throws IOException {
   shardId.writeTo(out);
   out.writeVInt(shards.length);
   for (ShardStats stats : shards) {
     stats.writeTo(out);
   }
 }
 /**
  * Total.
  *
  * @return the common stats
  */
 public CommonStats total() {
   if (total != null) {
     return total;
   }
   CommonStats stats = new CommonStats();
   for (ShardStats shard : shards) {
     stats.add(shard.stats());
   }
   total = stats;
   return stats;
 }
 /**
  * Primaries.
  *
  * @return the common stats
  */
 public CommonStats primaries() {
   if (primary != null) {
     return primary;
   }
   CommonStats stats = new CommonStats();
   for (ShardStats shard : shards) {
     if (shard.shardRouting().primary()) {
       stats.add(shard.stats());
     }
   }
   primary = stats;
   return stats;
 }
  public void testCommitStats() throws Exception {
    createIndex("test");
    ensureGreen("test");

    IndicesStatsResponse rsp = client().admin().indices().prepareStats("test").get();
    for (ShardStats shardStats : rsp.getIndex("test").getShards()) {
      final CommitStats commitStats = shardStats.getCommitStats();
      assertNotNull(commitStats);
      assertThat(commitStats.getGeneration(), greaterThan(0l));
      assertThat(commitStats.getId(), notNullValue());
      assertThat(commitStats.getUserData(), hasKey(Translog.TRANSLOG_GENERATION_KEY));
      assertThat(commitStats.getUserData(), hasKey(Translog.TRANSLOG_UUID_KEY));
    }
  }
 @Override
 public void readFrom(StreamInput in) throws IOException {
   shardId = ShardId.readShardId(in);
   int shardSize = in.readVInt();
   shards = new ShardStats[shardSize];
   for (int i = 0; i < shardSize; i++) {
     shards[i] = ShardStats.readShardStats(in);
   }
 }
 /**
  * Index shards.
  *
  * @return the map
  */
 public Map<Integer, IndexShardStats> indexShards() {
   if (indexShards != null) {
     return indexShards;
   }
   Map<Integer, List<ShardStats>> tmpIndexShards = Maps.newHashMap();
   for (ShardStats shard : shards) {
     List<ShardStats> lst = tmpIndexShards.get(shard.shardRouting().id());
     if (lst == null) {
       lst = Lists.newArrayList();
       tmpIndexShards.put(shard.shardRouting().id(), lst);
     }
     lst.add(shard);
   }
   indexShards = Maps.newHashMap();
   for (Map.Entry<Integer, List<ShardStats>> entry : tmpIndexShards.entrySet()) {
     indexShards.put(
         entry.getKey(),
         new IndexShardStats(
             entry.getValue().get(0).shardRouting().shardId(),
             entry.getValue().toArray(new ShardStats[entry.getValue().size()])));
   }
   return indexShards;
 }