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.Parameter.arg;
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.constantInt;
 import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.getStatic;
 import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.invokeStatic;
 import static com.facebook.presto.sql.gen.CompilerUtils.defineClass;
 import static com.facebook.presto.sql.gen.CompilerUtils.makeClassName;
 import static com.facebook.presto.sql.gen.SqlTypeByteCodeExpression.constantType;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class OrderingCompiler
 {
     private static final Logger log = Logger.get(OrderingCompiler.class);
 
             {
                 @Override
                 public PagesIndexOrdering load(PagesIndexComparatorCacheKey key)
                         throws Exception
                 {
                     return internalCompilePagesIndexOrdering(key.getSortTypes(), key.getSortChannels(), key.getSortOrders());
                 }
             });
 
     public PagesIndexOrdering compilePagesIndexOrdering(List<TypesortTypesList<IntegersortChannelsList<SortOrdersortOrders)
     {
         checkNotNull(sortTypes"sortTypes is null");
         checkNotNull(sortChannels"sortChannels is null");
         checkNotNull(sortOrders"sortOrders is null");
 
         try {
             return .get(new PagesIndexComparatorCacheKey(sortTypessortChannelssortOrders));
         }
         catch (ExecutionException | UncheckedExecutionException | ExecutionError e) {
             throw Throwables.propagate(e.getCause());
         }
     }
 
     public PagesIndexOrdering internalCompilePagesIndexOrdering(List<TypesortTypesList<IntegersortChannelsList<SortOrdersortOrders)
             throws Exception
     {
         checkNotNull(sortChannels"sortChannels is null");
         checkNotNull(sortOrders"sortOrders is null");
 
         PagesIndexComparator comparator;
         try {
             Class<? extends PagesIndexComparatorpagesHashStrategyClass = compilePagesIndexComparator(sortTypessortChannelssortOrders);
             comparator = pagesHashStrategyClass.newInstance();
        }
        catch (Throwable e) {
            .error(e"Error compiling comparator for channels %s with order %s"sortChannelssortChannels);
            comparator = new SimplePagesIndexComparator(sortTypessortChannelssortOrders);
        }
        // we may want to load a separate PagesIndexOrdering for each comparator
        return new PagesIndexOrdering(comparator);
    }
            List<TypesortTypes,
            List<IntegersortChannels,
            List<SortOrdersortOrders)
    {
        CallSiteBinder callSiteBinder = new CallSiteBinder();
        ClassDefinition classDefinition = new ClassDefinition(
                a(),
                makeClassName("PagesIndexComparator"),
                type(Object.class),
                type(PagesIndexComparator.class));
        classDefinition.declareDefaultConstructor(a());
        generateCompareTo(classDefinitioncallSiteBindersortTypessortChannelssortOrders);
        return defineClass(classDefinitionPagesIndexComparator.classcallSiteBinder.getBindings(), getClass().getClassLoader());
    }
    private void generateCompareTo(ClassDefinition classDefinitionCallSiteBinder callSiteBinderList<TypesortTypesList<IntegersortChannelsList<SortOrdersortOrders)
    {
        Parameter pagesIndex = arg("pagesIndex"PagesIndex.class);
        Parameter leftPosition = arg("leftPosition"int.class);
        Parameter rightPosition = arg("rightPosition"int.class);
        MethodDefinition compareToMethod = classDefinition.declareMethod(a(), "compareTo"type(int.class), pagesIndexleftPositionrightPosition);
        Scope scope = compareToMethod.getScope();
        Variable valueAddresses = scope.declareVariable(LongArrayList.class"valueAddresses");
        compareToMethod
                .getBody()
                .comment("LongArrayList valueAddresses = pagesIndex.valueAddresses")
                .append(valueAddresses.set(pagesIndex.invoke("getValueAddresses"LongArrayList.class)));
        Variable leftPageAddress = scope.declareVariable(long.class"leftPageAddress");
        compareToMethod
                .getBody()
                .comment("long leftPageAddress = valueAddresses.getLong(leftPosition)")
                .append(leftPageAddress.set(valueAddresses.invoke("getLong"long.classleftPosition)));
        Variable leftBlockIndex = scope.declareVariable(int.class"leftBlockIndex");
        compareToMethod
                .getBody()
                .comment("int leftBlockIndex = decodeSliceIndex(leftPageAddress)")
                .append(leftBlockIndex.set(invokeStatic(SyntheticAddress.class"decodeSliceIndex"int.classleftPageAddress)));
        Variable leftBlockPosition = scope.declareVariable(int.class"leftBlockPosition");
        compareToMethod
                .getBody()
                .comment("int leftBlockPosition = decodePosition(leftPageAddress)")
                .append(leftBlockPosition.set(invokeStatic(SyntheticAddress.class"decodePosition"int.classleftPageAddress)));
        Variable rightPageAddress = scope.declareVariable(long.class"rightPageAddress");
        compareToMethod
                .getBody()
                .comment("long rightPageAddress = valueAddresses.getLong(rightPosition);")
                .append(rightPageAddress.set(valueAddresses.invoke("getLong"long.classrightPosition)));
        Variable rightBlockIndex = scope.declareVariable(int.class"rightBlockIndex");
        compareToMethod
                .getBody()
                .comment("int rightBlockIndex = decodeSliceIndex(rightPageAddress)")
                .append(rightBlockIndex.set(invokeStatic(SyntheticAddress.class"decodeSliceIndex"int.classrightPageAddress)));
        Variable rightBlockPosition = scope.declareVariable(int.class"rightBlockPosition");
        compareToMethod
                .getBody()
                .comment("int rightBlockPosition = decodePosition(rightPageAddress)")
                .append(rightBlockPosition.set(invokeStatic(SyntheticAddress.class"decodePosition"int.classrightPageAddress)));
        for (int i = 0; i < sortChannels.size(); i++) {
            int sortChannel = sortChannels.get(i);
            SortOrder sortOrder = sortOrders.get(i);
            Block block = new Block()
                    .setDescription("compare channel " + sortChannel + " " + sortOrder);
            Type sortType = sortTypes.get(i);
            ByteCodeExpression leftBlock = pagesIndex
                    .invoke("getChannel"ObjectArrayList.classconstantInt(sortChannel))
                    .invoke("get"Object.classleftBlockIndex)
                    .cast(com.facebook.presto.spi.block.Block.class);
            ByteCodeExpression rightBlock = pagesIndex
                    .invoke("getChannel"ObjectArrayList.classconstantInt(sortChannel))
                    .invoke("get"Object.classrightBlockIndex)
                    .cast(com.facebook.presto.spi.block.Block.class);
            block.append(getStatic(SortOrder.classsortOrder.name())
                    .invoke("compareBlockValue",
                            int.class,
                            ImmutableList.of(Type.classcom.facebook.presto.spi.block.Block.classint.classcom.facebook.presto.spi.block.Block.classint.class),
                            constantType(callSiteBindersortType),
                            leftBlock,
                            leftBlockPosition,
                            rightBlock,
                            rightBlockPosition));
            LabelNode equal = new LabelNode("equal");
            block.comment("if (compare != 0) return compare")
                    .dup()
                    .ifZeroGoto(equal)
                    .retInt()
                    .visitLabel(equal)
                    .pop(int.class);
            compareToMethod.getBody().append(block);
        }
        // values are equal
        compareToMethod.getBody()
                .push(0)
                .retInt();
    }
    private static final class PagesIndexComparatorCacheKey
    {
        private final List<TypesortTypes;
        private final List<IntegersortChannels;
        private final List<SortOrdersortOrders;
        private PagesIndexComparatorCacheKey(List<TypesortTypesList<IntegersortChannelsList<SortOrdersortOrders)
        {
            this. = ImmutableList.copyOf(sortTypes);
            this. = ImmutableList.copyOf(sortChannels);
            this. = ImmutableList.copyOf(sortOrders);
        }
        public List<TypegetSortTypes()
        {
            return ;
        }
        public List<IntegergetSortChannels()
        {
            return ;
        }
        public List<SortOrdergetSortOrders()
        {
            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;
            }
            PagesIndexComparatorCacheKey other = (PagesIndexComparatorCacheKeyobj;
            return Objects.equals(this.other.sortTypes) &&
                    Objects.equals(this.other.sortChannels) &&
                    Objects.equals(this.other.sortOrders);
        }
    }
New to GrepCode? Check out our FAQ X