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.FINAL;
 import static com.facebook.presto.byteCode.Access.PUBLIC;
 import static com.facebook.presto.byteCode.Access.a;
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.facebook.presto.sql.gen.ByteCodeUtils.invoke;
 import static com.facebook.presto.sql.gen.CompilerUtils.defineClass;
 import static com.facebook.presto.sql.gen.CompilerUtils.makeClassName;
 import static com.google.common.base.MoreObjects.toStringHelper;
 
 public class ExpressionCompiler
 {
     private final Metadata metadata;
 
     private final LoadingCache<CacheKeyPageProcessorpageProcessors = CacheBuilder.newBuilder().maximumSize(1000).build(
             new CacheLoader<CacheKeyPageProcessor>()
             {
                 @Override
                 public PageProcessor load(CacheKey key)
                         throws Exception
                 {
                     return compileAndInstantiate(key.getFilter(), key.getProjections(), new PageProcessorCompiler(), PageProcessor.class);
                 }
             });
 
     private final LoadingCache<CacheKeyCursorProcessorcursorProcessors = CacheBuilder.newBuilder().maximumSize(1000).build(
             new CacheLoader<CacheKeyCursorProcessor>()
             {
                 @Override
                 public CursorProcessor load(CacheKey key)
                         throws Exception
                 {
                     return compileAndInstantiate(key.getFilter(), key.getProjections(), new CursorProcessorCompiler(), CursorProcessor.class);
                 }
             });
 
     @Inject
     public ExpressionCompiler(Metadata metadata)
     {
         this. = metadata;
     }
 
     @Managed
     public long getCacheSize()
     {
         return .size();
     }
 
     public CursorProcessor compileCursorProcessor(RowExpression filterList<RowExpressionprojectionsObject uniqueKey)
     {
         return .getUnchecked(new CacheKey(filterprojectionsuniqueKey));
     }
 
     public PageProcessor compilePageProcessor(RowExpression filterList<RowExpressionprojections)
     {
         return .getUnchecked(new CacheKey(filterprojectionsnull));
     }
 
     private <T> T compileAndInstantiate(RowExpression filterList<RowExpressionprojectionsBodyCompiler<T> bodyCompilerClass<? extends T> superType)
     {
         // create filter and project page iterator class
         Class<? extends T> clazz = compileProcessor(filterprojectionsbodyCompilersuperType);
         try {
             return clazz.newInstance();
         }
         catch (ReflectiveOperationException e) {
             throw Throwables.propagate(e);
         }
    }
    private <T> Class<? extends T> compileProcessor(
            RowExpression filter,
            List<RowExpressionprojections,
            BodyCompiler<T> bodyCompiler,
            Class<? extends T> superType)
    {
        ClassDefinition classDefinition = new ClassDefinition(
                a(),
                makeClassName(superType.getSimpleName()),
                type(Object.class),
                type(superType));
        classDefinition.declareDefaultConstructor(a());
        CallSiteBinder callSiteBinder = new CallSiteBinder();
        bodyCompiler.generateMethods(classDefinitioncallSiteBinderfilterprojections);
        //
        // toString method
        //
        generateToString(
                classDefinition,
                callSiteBinder,
                toStringHelper(classDefinition.getType().getJavaClassName())
                        .add("filter"filter)
                        .add("projections"projections)
                        .toString());
        return defineClass(classDefinitionsuperTypecallSiteBinder.getBindings(), getClass().getClassLoader());
    }
    private static void generateToString(ClassDefinition classDefinitionCallSiteBinder callSiteBinderString string)
    {
        // bind constant via invokedynamic to avoid constant pool issues due to large strings
        classDefinition.declareMethod(a(), "toString"type(String.class))
                .getBody()
                .append(invoke(callSiteBinder.bind(stringString.class), "toString"))
                .retObject();
    }
    private static final class CacheKey
    {
        private final RowExpression filter;
        private final List<RowExpressionprojections;
        private final Object uniqueKey;
        private CacheKey(RowExpression filterList<RowExpressionprojectionsObject uniqueKey)
        {
            this. = filter;
            this. = uniqueKey;
            this. = ImmutableList.copyOf(projections);
        }
        private RowExpression getFilter()
        {
            return ;
        }
        private List<RowExpressiongetProjections()
        {
            return ;
        }
        @Override
        public int hashCode()
        {
            return Objects.hash();
        }
        @Override
        public boolean equals(Object obj)
        {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            CacheKey other = (CacheKeyobj;
            return Objects.equals(this.other.filter) &&
                    Objects.equals(this.other.projections) &&
                    Objects.equals(this.other.uniqueKey);
        }
        @Override
        public String toString()
        {
            return toStringHelper(this)
                    .add("filter")
                    .add("projections")
                    .add("uniqueKey")
                    .toString();
        }
    }
New to GrepCode? Check out our FAQ X