private List<FieldDeclaration> prepareFields() {
      List<FieldDeclaration> fields = Lists.create();

      // shufles
      for (ShuffleModel.Segment segment : shuffle.getSegments()) {
        SimpleName shuffleName = names.create("shuffle");
        shuffleNames.put(segment, shuffleName);
        Name shuffleTypeName = segment.getCompiled().getCombineOutputType().getQualifiedName();
        fields.add(
            factory.newFieldDeclaration(
                null,
                new AttributeBuilder(factory).Private().toAttributes(),
                importer.toType(shuffleTypeName),
                shuffleName,
                null));
      }

      // rendezvous
      for (ReduceUnit unit : reduceUnits) {
        if (unit.canCombine() == false) {
          continue;
        }
        Fragment first = unit.getFragments().get(0);
        SimpleName rendezvousName = names.create("combine");
        rendezvousNames.put(first, rendezvousName);
        fields.add(
            factory.newFieldDeclaration(
                null,
                new AttributeBuilder(factory).Private().toAttributes(),
                importer.toType(first.getCompiled().getQualifiedName()),
                rendezvousName,
                null));
      }
      return fields;
    }
 private TypeDeclaration createType() {
   SimpleName name = factory.newSimpleName(Naming.getCombineClass());
   importer.resolvePackageMember(name);
   List<TypeBodyDeclaration> members = Lists.create();
   members.addAll(prepareFields());
   members.add(createSetup());
   members.add(createCleanup());
   members.add(createGetRendezvous());
   return factory.newClassDeclaration(
       new JavadocBuilder(factory)
           .text("ステージ{0}の処理を担当するコンバイナープログラム。", shuffle.getStageBlock().getStageNumber())
           .toJavadoc(),
       new AttributeBuilder(factory)
           .annotation(t(SuppressWarnings.class), v("deprecation"))
           .Public()
           .Final()
           .toAttributes(),
       name,
       Collections.<TypeParameterDeclaration>emptyList(),
       importer.resolve(
           factory.newParameterizedType(
               Models.toType(factory, SegmentedCombiner.class),
               Arrays.asList(
                   importer.toType(shuffle.getCompiled().getKeyTypeName()),
                   importer.toType(shuffle.getCompiled().getValueTypeName())))),
       Collections.<Type>emptyList(),
       members);
 }
 private MethodDeclaration createSlotObjects() {
   SimpleName resultName = factory.newSimpleName("results");
   List<Statement> statements = Lists.create();
   statements.add(
       new TypeBuilder(factory, importer.toType(Writable.class))
           .array(1)
           .newArray(getSlotCount())
           .toLocalVariableDeclaration(importer.toType(Writable[].class), resultName));
   for (ResolvedSlot slot : slots) {
     Expression object;
     if (slot.getSortProperties().isEmpty()
         && ParallelSortClientEmitter.legacy(environment) == false) {
       object = Models.toNullLiteral(factory);
     } else {
       DataClass slotClass = slot.getValueClass();
       object = slotClass.createNewInstance(importer.toType(slotClass.getType()));
     }
     statements.add(
         new ExpressionBuilder(factory, resultName)
             .array(slot.getSlotNumber())
             .assignFrom(object)
             .toStatement());
   }
   statements.add(new ExpressionBuilder(factory, resultName).toReturnStatement());
   return factory.newMethodDeclaration(
       null,
       new AttributeBuilder(factory)
           .annotation(importer.toType(Override.class))
           .Protected()
           .toAttributes(),
       importer.toType(Writable[].class),
       factory.newSimpleName(SlotSorter.NAME_CREATE_SLOT_OBJECTS),
       Collections.<FormalParameterDeclaration>emptyList(),
       statements);
 }
 private MethodDeclaration createSlotNames() {
   SimpleName resultName = factory.newSimpleName("results");
   List<Statement> statements = Lists.create();
   statements.add(
       new TypeBuilder(factory, importer.toType(String.class))
           .array(1)
           .newArray(getSlotCount())
           .toLocalVariableDeclaration(importer.toType(String[].class), resultName));
   for (ResolvedSlot slot : slots) {
     Expression outputName;
     if (slot.getSortProperties().isEmpty()
         && ParallelSortClientEmitter.legacy(environment) == false) {
       outputName = Models.toNullLiteral(factory);
     } else {
       outputName = Models.toLiteral(factory, slot.getSource().getOutputName());
     }
     statements.add(
         new ExpressionBuilder(factory, resultName)
             .array(slot.getSlotNumber())
             .assignFrom(outputName)
             .toStatement());
   }
   statements.add(new ExpressionBuilder(factory, resultName).toReturnStatement());
   return factory.newMethodDeclaration(
       null,
       new AttributeBuilder(factory)
           .annotation(importer.toType(Override.class))
           .Protected()
           .toAttributes(),
       importer.toType(String[].class),
       factory.newSimpleName(SlotSorter.NAME_GET_OUTPUT_NAMES),
       Collections.<FormalParameterDeclaration>emptyList(),
       statements);
 }
    private MethodDeclaration createSetup() {
      Map<FlowElementInput, Segment> segments = Maps.create();
      List<Statement> statements = Lists.create();

      // shuffle outputs
      for (Map.Entry<ShuffleModel.Segment, SimpleName> entry : shuffleNames.entrySet()) {
        ShuffleModel.Segment segment = entry.getKey();
        SimpleName name = entry.getValue();
        Name shuffleTypeName = segment.getCompiled().getCombineOutputType().getQualifiedName();
        statements.add(
            new ExpressionBuilder(factory, factory.newThis())
                .field(name)
                .assignFrom(
                    new TypeBuilder(factory, importer.toType(shuffleTypeName))
                        .newObject(context)
                        .toExpression())
                .toStatement());
        segments.put(segment.getPort(), segment);
      }

      // rendezvous
      for (Map.Entry<Fragment, SimpleName> entry : rendezvousNames.entrySet()) {
        Fragment fragment = entry.getKey();
        Type rendezvousType = importer.toType(fragment.getCompiled().getQualifiedName());
        List<Expression> arguments = Lists.create();
        for (FlowElementInput input : fragment.getInputPorts()) {
          Segment segment = segments.get(input);
          assert segment != null;
          SimpleName shuffleName = shuffleNames.get(segment);
          assert shuffleName != null;
          arguments.add(
              new ExpressionBuilder(factory, factory.newThis()).field(shuffleName).toExpression());
        }
        SimpleName name = entry.getValue();
        statements.add(
            new ExpressionBuilder(factory, factory.newThis())
                .field(name)
                .assignFrom(
                    new TypeBuilder(factory, rendezvousType).newObject(arguments).toExpression())
                .toStatement());
      }

      return factory.newMethodDeclaration(
          null,
          new AttributeBuilder(factory).annotation(t(Override.class)).Public().toAttributes(),
          Collections.<TypeParameterDeclaration>emptyList(),
          t(void.class),
          factory.newSimpleName("setup"),
          Collections.singletonList(
              factory.newFormalParameterDeclaration(
                  factory.newNamedType(factory.newSimpleName("Context")), context)),
          0,
          Arrays.asList(t(IOException.class), t(InterruptedException.class)),
          factory.newBlock(statements));
    }
    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 TypeDeclaration createType() {
   SimpleName name = factory.newSimpleName(Naming.getReduceClass());
   importer.resolvePackageMember(name);
   return factory.newClassDeclaration(
       new JavadocBuilder(factory).text("エピローグ用のレデューサー。").toJavadoc(),
       new AttributeBuilder(factory).Public().toAttributes(),
       name,
       Collections.<TypeParameterDeclaration>emptyList(),
       importer.toType(SlotSorter.class),
       Collections.<Type>emptyList(),
       Arrays.asList(createSlotNames(), createSlotObjects()));
 }
Beispiel #8
0
 /**
  * Returns the type model for the specified type.
  *
  * @param type the target type
  * @return the type model
  * @throws IllegalArgumentException if the parameter is {@code null}
  */
 public final Type t(java.lang.reflect.Type type) {
   Precondition.checkMustNotBeNull(type, "type"); // $NON-NLS-1$
   return importer.toType(type);
 }