Exemplo n.º 1
0
  @Override
  public StreamExpression toExpression(StreamFactory factory) throws IOException {

    // function name
    StreamExpression expression = new StreamExpression(factory.getFunctionName(this.getClass()));

    // collection
    expression.addParameter(collection);

    // workers
    expression.addParameter(
        new StreamExpressionNamedParameter("workers", Integer.toString(workers)));

    // stream
    if (tupleStream instanceof Expressible) {
      expression.addParameter(((Expressible) tupleStream).toExpression(factory));
    } else {
      throw new IOException(
          "This ParallelStream contains a non-expressible TupleStream - it cannot be converted to an expression");
    }

    // sort
    expression.addParameter(new StreamExpressionNamedParameter("sort", comp.toExpression(factory)));

    // zkHost
    expression.addParameter(new StreamExpressionNamedParameter("zkHost", zkHost));

    return expression;
  }
Exemplo n.º 2
0
  public ParallelStream(StreamExpression expression, StreamFactory factory) throws IOException {
    // grab all parameters out
    String collectionName = factory.getValueOperand(expression, 0);
    StreamExpressionNamedParameter workersParam = factory.getNamedOperand(expression, "workers");
    List<StreamExpression> streamExpressions =
        factory.getExpressionOperandsRepresentingTypes(
            expression, Expressible.class, TupleStream.class);
    StreamExpressionNamedParameter sortExpression = factory.getNamedOperand(expression, "sort");
    StreamExpressionNamedParameter zkHostExpression = factory.getNamedOperand(expression, "zkHost");

    // validate expression contains only what we want.

    if (expression.getParameters().size()
        != streamExpressions.size() + 3 + (null != zkHostExpression ? 1 : 0)) {
      throw new IOException(
          String.format(Locale.ROOT, "Invalid expression %s - unknown operands found", expression));
    }

    // Collection Name
    if (null == collectionName) {
      throw new IOException(
          String.format(
              Locale.ROOT,
              "invalid expression %s - collectionName expected as first operand",
              expression));
    }

    // Workers
    if (null == workersParam
        || null == workersParam.getParameter()
        || !(workersParam.getParameter() instanceof StreamExpressionValue)) {
      throw new IOException(
          String.format(
              Locale.ROOT,
              "Invalid expression %s - expecting a single 'workersParam' parameter of type positive integer but didn't find one",
              expression));
    }
    String workersStr = ((StreamExpressionValue) workersParam.getParameter()).getValue();
    int workersInt = 0;
    try {
      workersInt = Integer.parseInt(workersStr);
      if (workersInt <= 0) {
        throw new IOException(
            String.format(
                Locale.ROOT,
                "invalid expression %s - workers '%s' must be greater than 0.",
                expression,
                workersStr));
      }
    } catch (NumberFormatException e) {
      throw new IOException(
          String.format(
              Locale.ROOT,
              "invalid expression %s - workers '%s' is not a valid integer.",
              expression,
              workersStr));
    }

    // Stream
    if (1 != streamExpressions.size()) {
      throw new IOException(
          String.format(
              Locale.ROOT,
              "Invalid expression %s - expecting a single stream but found %d",
              expression,
              streamExpressions.size()));
    }

    // Sort
    if (null == sortExpression
        || !(sortExpression.getParameter() instanceof StreamExpressionValue)) {
      throw new IOException(
          String.format(
              Locale.ROOT,
              "Invalid expression %s - expecting single 'sort' parameter telling us how to join the parallel streams but didn't find one",
              expression));
    }

    // zkHost, optional - if not provided then will look into factory list to get
    String zkHost = null;
    if (null == zkHostExpression) {
      zkHost = factory.getCollectionZkHost(collectionName);
      if (zkHost == null) {
        zkHost = factory.getDefaultZkHost();
      }
    } else if (zkHostExpression.getParameter() instanceof StreamExpressionValue) {
      zkHost = ((StreamExpressionValue) zkHostExpression.getParameter()).getValue();
    }
    if (null == zkHost) {
      throw new IOException(
          String.format(
              Locale.ROOT,
              "invalid expression %s - zkHost not found for collection '%s'",
              expression,
              collectionName));
    }

    // We've got all the required items
    TupleStream stream = factory.constructStream(streamExpressions.get(0));
    StreamComparator comp =
        factory.constructComparator(
            ((StreamExpressionValue) sortExpression.getParameter()).getValue(),
            FieldComparator.class);
    streamFactory = factory;
    init(zkHost, collectionName, stream, workersInt, comp);
  }