private void copyPartialsInResults(
     Integer shard, CollectionResult result, CollectionResult partial) {
   for (Registration r : partial.getRegistrations()) {
     ShardedNHRegistrationId shardedId = new ShardedNHRegistrationId(shard, r.getRegistrationId());
     r.setRegistrationId(shardedId.toString());
     result.getRegistrations().add(r);
   }
 }
  private CollectionResult executeShardedQuery(
      int top, String continuationToken, IExecuteQueryOnHub ex) {
    CollectionResult result = new CollectionResult();
    ShardedContinuationToken shardedContinuation =
        continuationToken == null
            ? getDefaultContinuationToken()
            : new ShardedContinuationToken(continuationToken);

    int toFill = top > 0 && top <= 100 ? top : MAX_TOP;
    for (Integer shard : hubs.keySet()) {
      // skips already processed shards
      if (shard < shardedContinuation.getShard()) continue;

      if (toFill <= 0) {
        // In this case, finished filling the result exactly when a shard is depleted.
        // resume from this shard with continuation token = null
        result.setContinuationToken(new ShardedContinuationToken(shard, null).toString());
        return result;
      }

      // if this is the first hub in the sharded continuation token, continue from where left
      String intraHubContinuationToken = null;
      if (shard == shardedContinuation.getShard())
        intraHubContinuationToken = shardedContinuation.getContinuationToken();

      CollectionResult partial =
          ex.executeQueryOnHub(hubs.get(shard), toFill, intraHubContinuationToken);

      copyPartialsInResults(shard, result, partial);

      String continuation = partial.getContinuationToken();
      if (continuation != null) {
        result.setContinuationToken(new ShardedContinuationToken(shard, continuation).toString());
        return result;
      }

      toFill -= partial.getRegistrations().size();
    }

    // no more hubs, no continuation token, and still space in return registration
    return result;
  }