001/*
002 * Copyright (c) 2016-2017 Chris K Wensel. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.nested.core;
022
023import cascading.flow.FlowProcess;
024import cascading.operation.Aggregator;
025import cascading.operation.AggregatorCall;
026import cascading.operation.OperationCall;
027import cascading.tuple.Fields;
028import cascading.tuple.Tuple;
029import cascading.tuple.TupleEntry;
030import heretical.pointer.operation.Builder;
031
032/**
033 * Class NestedBaseBuildAggregator is the base class for {@link Aggregator} implementations that rely on the
034 * {@link BuildSpec} class when declaring transformations on nested object types.
035 * <p>
036 * Specifically, {@code *BuildAsAggregator} and {@code *BuildIntoAggregator} classes create or update (respectively)
037 * nested object types from Aggregator argument {@link Fields} where the field values can be primitive types, objects
038 * (with a corresponding {@link cascading.tuple.type.CoercibleType}), or themselves nest object types.
039 * <p>
040 * In the case of a {@code *BuildIntoAggregator} the last argument in the {@code arguments} {@link TupleEntry} will be
041 * the object the BuildSpec copies values into.
042 * <p>
043 * In the case of a {@code *BuildAsAggregator} a new root object will be created for the BuildSpec to copy values into.
044 * <p>
045 * In the case of JSON objects, multiple JSON objects selected as arguments can be combined into a new JSON object
046 * by mapping the field names into locations on the new object.
047 *
048 * @see BuildSpec
049 */
050public abstract class NestedBaseBuildAggregator<Node, Result> extends NestedSpecBaseOperation<Node, Result, NestedBaseBuildAggregator<Node, Result>.Context> implements Aggregator<NestedBaseBuildAggregator<Node, Result>.Context>
051  {
052  public class Context
053    {
054    Node rootNode = null;
055
056    public void reset()
057      {
058      rootNode = getResultNode( null );
059      }
060    }
061
062  protected Builder<Node, Result> builder;
063
064  public NestedBaseBuildAggregator( NestedCoercibleType<Node, Result> nestedCoercibleType, Fields fieldDeclaration, BuildSpec... buildSpecs )
065    {
066    super( nestedCoercibleType, fieldDeclaration );
067    this.builder = new Builder<>( nestedCoercibleType.getNestedPointerCompiler(), buildSpecs );
068    }
069
070  @Override
071  public void prepare( FlowProcess flowProcess, OperationCall<Context> operationCall )
072    {
073    operationCall.setContext( new Context() );
074    }
075
076  @Override
077  public void start( FlowProcess flowProcess, AggregatorCall<Context> aggregatorCall )
078    {
079    aggregatorCall.getContext().reset();
080
081    Node rootNode = aggregatorCall.getContext().rootNode;
082
083    builder.buildLiterals( rootNode );
084    }
085
086  @Override
087  public void aggregate( FlowProcess flowProcess, AggregatorCall<Context> aggregatorCall )
088    {
089    TupleEntry arguments = aggregatorCall.getArguments();
090
091    Node rootNode = aggregatorCall.getContext().rootNode;
092
093    builder.build( arguments::getObject, rootNode );
094    }
095
096  @Override
097  public void complete( FlowProcess flowProcess, AggregatorCall<Context> aggregatorCall )
098    {
099    aggregatorCall.getOutputCollector().add( new Tuple( aggregatorCall.getContext().rootNode ) );
100    }
101  }