private TypeDeclaration createType() {
   SimpleName name = factory.newSimpleName(Naming.getShuffleGroupingComparatorClass());
   importer.resolvePackageMember(name);
   List<TypeBodyDeclaration> members = new ArrayList<TypeBodyDeclaration>();
   members.add(createCompareBytes());
   members.add(createCompareObjects());
   members.add(ShuffleEmiterUtil.createCompareInts(factory));
   members.add(ShuffleEmiterUtil.createPortToElement(factory, model));
   return factory.newClassDeclaration(
       createJavadoc(),
       new AttributeBuilder(factory)
           .annotation(t(SuppressWarnings.class), v("rawtypes"))
           .Public()
           .toAttributes(),
       name,
       Collections.<TypeParameterDeclaration>emptyList(),
       null,
       Collections.singletonList(
           importer.resolve(
               factory.newParameterizedType(
                   t(RawComparator.class), Collections.singletonList(keyType)))),
       members);
 }
Пример #2
0
    private MethodDeclaration createGetRendezvous() {
      Map<FlowElement, SimpleName> fragments = Maps.create();
      for (Map.Entry<Fragment, SimpleName> entry : rendezvousNames.entrySet()) {
        fragments.put(entry.getKey().getFactors().get(0).getElement(), entry.getValue());
      }

      List<Statement> cases = Lists.create();
      for (List<ShuffleModel.Segment> group : ShuffleEmiterUtil.groupByElement(shuffle)) {
        for (ShuffleModel.Segment segment : group) {
          cases.add(factory.newSwitchCaseLabel(v(segment.getPortId())));
        }
        FlowElement element = group.get(0).getPort().getOwner();
        SimpleName rendezvousName = fragments.get(element);
        if (rendezvousName == null) {
          cases.add(
              new ExpressionBuilder(factory, Models.toNullLiteral(factory)).toReturnStatement());
        } else {
          cases.add(
              new ExpressionBuilder(factory, factory.newThis())
                  .field(rendezvousName)
                  .toReturnStatement());
        }
      }
      cases.add(factory.newSwitchDefaultLabel());
      cases.add(new TypeBuilder(factory, t(AssertionError.class)).newObject().toThrowStatement());

      SimpleName argument = names.create("nextKey");
      List<Statement> statements = Lists.create();
      statements.add(
          factory.newSwitchStatement(
              new ExpressionBuilder(factory, argument)
                  .method(SegmentedWritable.ID_GETTER)
                  .toExpression(),
              cases));

      return factory.newMethodDeclaration(
          null,
          new AttributeBuilder(factory).annotation(t(Override.class)).Protected().toAttributes(),
          importer.resolve(
              factory.newParameterizedType(
                  Models.toType(factory, Rendezvous.class),
                  importer.toType(shuffle.getCompiled().getValueTypeName()))),
          factory.newSimpleName(SegmentedReducer.GET_RENDEZVOUS),
          Collections.singletonList(
              factory.newFormalParameterDeclaration(
                  importer.toType(shuffle.getCompiled().getKeyTypeName()), argument)),
          statements);
    }
    private TypeBodyDeclaration createCompareObjects() {
      SimpleName o1 = factory.newSimpleName("o1");
      SimpleName o2 = factory.newSimpleName("o2");

      List<Statement> statements = new ArrayList<Statement>();
      SimpleName segmentId1 = factory.newSimpleName("segmentId1");
      SimpleName segmentId2 = factory.newSimpleName("segmentId2");
      statements.add(
          new ExpressionBuilder(factory, o1)
              .method(SegmentedWritable.ID_GETTER)
              .toLocalVariableDeclaration(t(int.class), segmentId1));
      statements.add(
          new ExpressionBuilder(factory, o2)
              .method(SegmentedWritable.ID_GETTER)
              .toLocalVariableDeclaration(t(int.class), segmentId2));

      SimpleName diff = factory.newSimpleName("diff");
      statements.add(
          new ExpressionBuilder(factory, factory.newThis())
              .method(
                  ShuffleEmiterUtil.COMPARE_INT,
                  new ExpressionBuilder(factory, factory.newThis())
                      .method(ShuffleEmiterUtil.PORT_TO_ELEMENT, segmentId1)
                      .toExpression(),
                  new ExpressionBuilder(factory, factory.newThis())
                      .method(ShuffleEmiterUtil.PORT_TO_ELEMENT, segmentId2)
                      .toExpression())
              .toLocalVariableDeclaration(t(int.class), diff));
      statements.add(createDiffBranch(diff));

      List<Statement> cases = new ArrayList<Statement>();
      for (List<Segment> segments : ShuffleEmiterUtil.groupByElement(model)) {
        for (Segment segment : segments) {
          cases.add(factory.newSwitchCaseLabel(v(segment.getPortId())));
        }
        Segment segment = segments.get(0);
        for (Term term : segment.getTerms()) {
          if (term.getArrangement() != Arrangement.GROUPING) {
            continue;
          }
          String name = ShuffleEmiterUtil.getPropertyName(segment, term);
          Expression rhs =
              term.getSource()
                  .createValueDiff(
                      new ExpressionBuilder(factory, o1).field(name).toExpression(),
                      new ExpressionBuilder(factory, o2).field(name).toExpression());
          cases.add(new ExpressionBuilder(factory, diff).assignFrom(rhs).toStatement());
          cases.add(createDiffBranch(diff));
        }
        cases.add(factory.newBreakStatement());
      }
      cases.add(factory.newSwitchDefaultLabel());
      cases.add(new TypeBuilder(factory, t(AssertionError.class)).newObject().toThrowStatement());

      statements.add(factory.newSwitchStatement(segmentId1, cases));
      statements.add(new ExpressionBuilder(factory, v(0)).toReturnStatement());

      return factory.newMethodDeclaration(
          null,
          new AttributeBuilder(factory).annotation(t(Override.class)).Public().toAttributes(),
          t(int.class),
          factory.newSimpleName("compare"),
          Arrays.asList(
              new FormalParameterDeclaration[] {
                factory.newFormalParameterDeclaration(keyType, o1),
                factory.newFormalParameterDeclaration(keyType, o2),
              }),
          statements);
    }
    private MethodDeclaration createCompareBytes() {
      SimpleName b1 = factory.newSimpleName("b1");
      SimpleName s1 = factory.newSimpleName("s1");
      SimpleName l1 = factory.newSimpleName("l1");
      SimpleName b2 = factory.newSimpleName("b2");
      SimpleName s2 = factory.newSimpleName("s2");
      SimpleName l2 = factory.newSimpleName("l2");

      List<Statement> statements = new ArrayList<Statement>();
      SimpleName segmentId1 = factory.newSimpleName("segmentId1");
      SimpleName segmentId2 = factory.newSimpleName("segmentId2");
      statements.add(
          new TypeBuilder(factory, t(WritableComparator.class))
              .method("readInt", b1, s1)
              .toLocalVariableDeclaration(t(int.class), segmentId1));
      statements.add(
          new TypeBuilder(factory, t(WritableComparator.class))
              .method("readInt", b2, s2)
              .toLocalVariableDeclaration(t(int.class), segmentId2));

      SimpleName diff = factory.newSimpleName("diff");
      statements.add(
          new ExpressionBuilder(factory, factory.newThis())
              .method(
                  ShuffleEmiterUtil.COMPARE_INT,
                  new ExpressionBuilder(factory, factory.newThis())
                      .method(ShuffleEmiterUtil.PORT_TO_ELEMENT, segmentId1)
                      .toExpression(),
                  new ExpressionBuilder(factory, factory.newThis())
                      .method(ShuffleEmiterUtil.PORT_TO_ELEMENT, segmentId2)
                      .toExpression())
              .toLocalVariableDeclaration(t(int.class), diff));
      statements.add(createDiffBranch(diff));

      SimpleName o1 = factory.newSimpleName("o1");
      SimpleName o2 = factory.newSimpleName("o2");
      SimpleName size1 = factory.newSimpleName("size1");
      SimpleName size2 = factory.newSimpleName("size2");
      statements.add(
          new ExpressionBuilder(factory, v(4)).toLocalVariableDeclaration(t(int.class), o1));
      statements.add(
          new ExpressionBuilder(factory, v(4)).toLocalVariableDeclaration(t(int.class), o2));
      statements.add(
          new ExpressionBuilder(factory, v(-1)).toLocalVariableDeclaration(t(int.class), size1));
      statements.add(
          new ExpressionBuilder(factory, v(-1)).toLocalVariableDeclaration(t(int.class), size2));

      List<Statement> cases = new ArrayList<Statement>();
      for (List<Segment> segments : ShuffleEmiterUtil.groupByElement(model)) {
        for (Segment segment : segments) {
          cases.add(factory.newSwitchCaseLabel(v(segment.getPortId())));
        }
        for (Term term : segments.get(0).getTerms()) {
          if (term.getArrangement() != Arrangement.GROUPING) {
            continue;
          }
          cases.add(
              new ExpressionBuilder(factory, size1)
                  .assignFrom(
                      term.getSource()
                          .createBytesSize(
                              b1,
                              factory.newInfixExpression(s1, InfixOperator.PLUS, o1),
                              factory.newInfixExpression(l1, InfixOperator.MINUS, o1)))
                  .toStatement());
          cases.add(
              new ExpressionBuilder(factory, size2)
                  .assignFrom(
                      term.getSource()
                          .createBytesSize(
                              b2,
                              factory.newInfixExpression(s2, InfixOperator.PLUS, o2),
                              factory.newInfixExpression(l2, InfixOperator.MINUS, o2)))
                  .toStatement());
          cases.add(
              new ExpressionBuilder(factory, diff)
                  .assignFrom(
                      term.getSource()
                          .createBytesDiff(
                              b1,
                              factory.newInfixExpression(s1, InfixOperator.PLUS, o1),
                              size1,
                              b2,
                              factory.newInfixExpression(s2, InfixOperator.PLUS, o2),
                              size2))
                  .toStatement());
          cases.add(createDiffBranch(diff));
          cases.add(
              new ExpressionBuilder(factory, o1)
                  .assignFrom(InfixOperator.PLUS, size1)
                  .toStatement());
          cases.add(
              new ExpressionBuilder(factory, o2)
                  .assignFrom(InfixOperator.PLUS, size2)
                  .toStatement());
        }
        cases.add(factory.newBreakStatement());
      }
      cases.add(factory.newSwitchDefaultLabel());
      cases.add(new TypeBuilder(factory, t(AssertionError.class)).newObject().toThrowStatement());

      statements.add(factory.newSwitchStatement(segmentId1, cases));
      statements.add(new ExpressionBuilder(factory, v(0)).toReturnStatement());

      return factory.newMethodDeclaration(
          null,
          new AttributeBuilder(factory).annotation(t(Override.class)).Public().toAttributes(),
          t(int.class),
          factory.newSimpleName("compare"),
          Arrays.asList(
              new FormalParameterDeclaration[] {
                factory.newFormalParameterDeclaration(t(byte[].class), b1),
                factory.newFormalParameterDeclaration(t(int.class), s1),
                factory.newFormalParameterDeclaration(t(int.class), l1),
                factory.newFormalParameterDeclaration(t(byte[].class), b2),
                factory.newFormalParameterDeclaration(t(int.class), s2),
                factory.newFormalParameterDeclaration(t(int.class), l2),
              }),
          statements);
    }