Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.sql.gen;
 
 
 import java.util.List;
 
 import static com.facebook.presto.byteCode.Access.PUBLIC;
 import static com.facebook.presto.byteCode.Access.a;
 import static com.facebook.presto.byteCode.NamedParameterDefinition.arg;
 import static com.facebook.presto.byteCode.OpCode.NOP;
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.facebook.presto.byteCode.control.ForLoop.ForLoopBuilder;
 import static com.facebook.presto.byteCode.control.IfStatement.IfStatementBuilder;
 import static com.facebook.presto.sql.gen.Bootstrap.BOOTSTRAP_METHOD;
 import static com.facebook.presto.sql.gen.ByteCodeUtils.generateWrite;
 import static java.lang.String.format;
 
 public class CursorProcessorCompiler
         implements BodyCompiler<CursorProcessor>
 {
     private final Metadata metadata;
 
     public CursorProcessorCompiler(Metadata metadata)
     {
         this. = metadata;
     }
 
     @Override
     public void generateMethods(ClassDefinition classDefinitionCallSiteBinder callSiteBinderRowExpression filterList<RowExpressionprojections)
     {
         generateProcessMethod(classDefinitionprojections.size());
         generateFilterMethod(classDefinitioncallSiteBinderfilter);
 
         for (int i = 0; i < projections.size(); i++) {
             generateProjectMethod(classDefinitioncallSiteBinder"project_" + iprojections.get(i));
         }
     }
 
     private void generateProcessMethod(ClassDefinition classDefinitionint projections)
     {
         CompilerContext context = new CompilerContext();
         MethodDefinition method = classDefinition.declareMethod(context,
                 a(),
                 "process",
                 type(int.class),
                 arg("session"ConnectorSession.class),
                 arg("cursor"RecordCursor.class),
                 arg("count"int.class),
                 arg("pageBuilder"PageBuilder.class));
 
         Variable sessionVariable = context.getVariable("session");
         Variable cursorVariable = context.getVariable("cursor");
         Variable countVariable = context.getVariable("count");
         Variable pageBuilderVariable = context.getVariable("pageBuilder");
 
         Variable completedPositionsVariable = context.declareVariable(int.class"completedPositions");
 
         method.getBody()
                 .comment("int completedPositions = 0;")
                 .putVariable(completedPositionsVariable, 0);
 
         //
         // for loop loop body
         //
         LabelNode done = new LabelNode("done");
        ForLoopBuilder forLoop = ForLoop.forLoopBuilder(context)
                .initialize()
                .condition(new Block(context)
                                .comment("completedPositions < count")
                                .getVariable(completedPositionsVariable)
                                .getVariable(countVariable)
                                .invokeStatic(CompilerOperations.class"lessThan"boolean.classint.classint.class)
                )
                .update(new Block(context)
                                .comment("completedPositions++")
                                .incrementVariable(completedPositionsVariable, (byte) 1)
                );
        Block forLoopBody = new Block(context)
                .comment("if (pageBuilder.isFull()) break;")
                .append(new Block(context)
                        .getVariable(pageBuilderVariable)
                        .invokeVirtual(PageBuilder.class"isFull"boolean.class)
                        .ifTrueGoto(done))
                .comment("if (!cursor.advanceNextPosition()) break;")
                .append(new Block(context)
                        .getVariable(cursorVariable)
                        .invokeInterface(RecordCursor.class"advanceNextPosition"boolean.class)
                        .ifFalseGoto(done));
        forLoop.body(forLoopBody);
        // if (filter(cursor))
        IfStatementBuilder ifStatement = new IfStatementBuilder(context);
        ifStatement.condition(new Block(context)
                .pushThis()
                .getVariable(sessionVariable)
                .getVariable(cursorVariable)
                .invokeVirtual(classDefinition.getType(), "filter"type(boolean.class), type(ConnectorSession.class), type(RecordCursor.class)));
        Block trueBlock = new Block(context);
        ifStatement.ifTrue(trueBlock);
        // pageBuilder.declarePosition();
        trueBlock.getVariable(pageBuilderVariable)
                .invokeVirtual(PageBuilder.class"declarePosition"void.class);
        // this.project_43(session, cursor, pageBuilder.getBlockBuilder(42)));
        for (int projectionIndex = 0; projectionIndex < projectionsprojectionIndex++) {
            trueBlock.pushThis()
                    .getVariable(sessionVariable)
                    .getVariable(cursorVariable);
            // pageBuilder.getBlockBuilder(0)
            trueBlock.getVariable(pageBuilderVariable)
                    .push(projectionIndex)
                    .invokeVirtual(PageBuilder.class"getBlockBuilder"BlockBuilder.classint.class);
            // project(block..., blockBuilder)
            trueBlock.invokeVirtual(classDefinition.getType(),
                    "project_" + projectionIndex,
                    type(void.class),
                    type(ConnectorSession.class),
                    type(RecordCursor.class),
                    type(BlockBuilder.class));
        }
        forLoopBody.append(ifStatement.build());
        method.getBody()
                .append(forLoop.build())
                .visitLabel(done)
                .comment("return completedPositions;")
                .getVariable(completedPositionsVariable)
                .retInt();
    }
    private void generateFilterMethod(ClassDefinition classDefinitionCallSiteBinder callSiteBinderRowExpression filter)
    {
        CompilerContext context = new CompilerContext();
        MethodDefinition method = classDefinition.declareMethod(
                context,
                a(),
                "filter",
                type(boolean.class),
                arg("session"ConnectorSession.class),
                arg("cursor"RecordCursor.class));
        method.comment("Filter: %s"filter);
        Variable wasNullVariable = context.declareVariable(type(boolean.class), "wasNull");
        Variable cursorVariable = context.getVariable("cursor");
        ByteCodeExpressionVisitor visitor = new ByteCodeExpressionVisitor(callSiteBinderfieldReferenceCompiler(cursorVariablewasNullVariable), .getFunctionRegistry());
        LabelNode end = new LabelNode("end");
        method.getBody()
                .comment("boolean wasNull = false;")
                .putVariable(wasNullVariablefalse)
                .comment("evaluate filter: " + filter)
                .append(filter.accept(visitorcontext))
                .comment("if (wasNull) return false;")
                .getVariable(wasNullVariable)
                .ifFalseGoto(end)
                .pop(boolean.class)
                .push(false)
                .visitLabel(end)
                .retBoolean();
    }
    private void generateProjectMethod(ClassDefinition classDefinitionCallSiteBinder callSiteBinderString methodNameRowExpression projection)
    {
        CompilerContext context = new CompilerContext();
        MethodDefinition method = classDefinition.declareMethod(context,
                a(),
                methodName,
                type(void.class),
                arg("session"ConnectorSession.class),
                arg("cursor"RecordCursor.class),
                arg("output"BlockBuilder.class));
        method.comment("Projection: %s"projection.toString());
        Variable outputVariable = context.getVariable("output");
        Variable cursorVariable = context.getVariable("cursor");
        Variable wasNullVariable = context.declareVariable(type(boolean.class), "wasNull");
        Block body = method.getBody()
                .comment("boolean wasNull = false;")
                .putVariable(wasNullVariablefalse);
        ByteCodeExpressionVisitor visitor = new ByteCodeExpressionVisitor(callSiteBinderfieldReferenceCompiler(cursorVariablewasNullVariable), .getFunctionRegistry());
        body.getVariable(outputVariable)
                .comment("evaluate projection: " + projection.toString())
                .append(projection.accept(visitorcontext))
                .append(generateWrite(callSiteBindercontextwasNullVariableprojection.getType()))
                .ret();
    }
    private RowExpressionVisitor<CompilerContextByteCodeNodefieldReferenceCompiler(final Variable cursorVariablefinal Variable wasNullVariable)
    {
        return new RowExpressionVisitor<CompilerContextByteCodeNode>()
        {
            @Override
            public ByteCodeNode visitInputReference(InputReferenceExpression nodeCompilerContext context)
            {
                int field = node.getField();
                Type type = node.getType();
                Class<?> javaType = type.getJavaType();
                Block isNullCheck = new Block(context)
                        .setDescription(format("cursor.get%s(%d)"typefield))
                        .getVariable(cursorVariable)
                        .push(field)
                        .invokeInterface(RecordCursor.class"isNull"boolean.classint.class);
                Block isNull = new Block(context)
                        .putVariable(wasNullVariabletrue)
                        .pushJavaDefault(javaType);
                Block isNotNull = new Block(context)
                        .getVariable(cursorVariable)
                        .push(field);
                String methodName = "get" + Primitives.wrap(javaType).getSimpleName();
                isNotNull.invokeInterface(RecordCursor.classmethodNamejavaTypeint.class);
                return new IfStatement(contextisNullCheckisNullisNotNull);
            }
            @Override
            public ByteCodeNode visitCall(CallExpression callCompilerContext context)
            {
                throw new UnsupportedOperationException("not yet implemented");
            }
            @Override
            public ByteCodeNode visitConstant(ConstantExpression literalCompilerContext context)
            {
                throw new UnsupportedOperationException("not yet implemented");
            }
        };
    }
New to GrepCode? Check out our FAQ X