public Iterator getPartitionIterator()

in odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/Table.java [2061:2152]


  public Iterator<Partition> getPartitionIterator(
      PartitionSpec spec,
      boolean reverse,
      Long batchSize,
      Long limit) {
    if (limit != null && limit <= 0) {
      throw new IllegalArgumentException("Argument 'limit' should be greater than 0");
    }
    if (batchSize != null && batchSize <= 0) {
      throw new IllegalArgumentException("Argument 'batchSize' should be greater than 0");
    }

    final long finalLimit = limit == null ? Long.MAX_VALUE : limit;
    return new ListIterator<Partition>() {
      long numPartitions = 0;
      Map<String, String> params = new HashMap<>();

      @Override
      public boolean hasNext() {
        return super.hasNext() && numPartitions < finalLimit;
      }

      @Override
      public Partition next() {
        Partition partition = super.next();
        numPartitions += 1;
        return partition;
      }

      @Override
      public String getMarker() {
        return params.get("marker");
      }

      @Override
      public List<Partition> list(String marker, long maxItems) {
        if (marker != null) {
          params.put("marker", marker);
        }
        if (maxItems >= 0) {
          params.put("maxitems", String.valueOf(maxItems));
        }
        return list();
      }

      @Override
      protected List<Partition> list() {
        ArrayList<Partition> partitions = new ArrayList<>();
        params.put("partitions", null);
        params.put("expectmarker", "true"); // since sprint-11
        if (spec != null && !spec.isEmpty()) {
          params.put("partition", spec.toString());
        }
        if (reverse) {
          params.put("reverse", null);
        }
        if (params.get("maxitems") == null && batchSize != null) {
          params.put("maxitems", batchSize.toString());
        }

        String lastMarker = params.get("marker");
        if (params.containsKey("marker") && lastMarker.length() == 0) {
          return null;
        }

        String resource = ResourceBuilder.buildTableResource(model.projectName, model.name);
        try {

          params.putAll(initParamsWithSchema());
          ListPartitionsResponse
              resp =
              client.request(ListPartitionsResponse.class, resource, "GET", params);

          for (PartitionModel partitionModel : resp.partitions) {
            Partition t = new Partition(
                partitionModel,
                model.projectName,
                model.schemaName,
                model.name,
                odps);
            partitions.add(t);
          }

          params.put("marker", resp.marker);
        } catch (OdpsException e) {
          throw new RuntimeException(e.getMessage(), e);
        }

        return partitions;
      }
    };
  }