Search in sources :

Example 66 with Function

use of com.google.common.base.Function in project crate by crate.

the class ManyTableConsumer method buildTwoTableJoinTree.

/**
     * build a TwoTableJoin tree.
     * E.g. given a MSS with 3 tables:
     * <code>
     * select t1.a, t2.b, t3.c from t1, t2, t3
     * </code>
     * <p>
     * a TwoTableJoin tree is built:
     * <p>
     * </code>
     * join(
     * join(t1, t2),
     * t3
     * )
     * </code>
     * <p>
     * Where:
     * <code>
     * join(t1, t2)
     * has:
     * QS: [ RC(t1, 0), RC(t2, 0) ]
     * t1: select a from t1
     * t2: select b from t2
     * </code>
     * <p>
     * and
     * <code>
     * join(join(t1, t2), t3)
     * has:
     * QS: [ RC(join(t1, t2), 0), RC(join(t1, t2), 1),  RC(t3, 0) ]
     * join(t1, t2) -
     * t3: select c from t3
     * <p>
     * </code>
     */
static TwoTableJoin buildTwoTableJoinTree(MultiSourceSelect mss) {
    Map<Set<QualifiedName>, Symbol> splitQuery = ImmutableMap.of();
    if (mss.querySpec().where().hasQuery()) {
        splitQuery = QuerySplitter.split(mss.querySpec().where().query());
        mss.querySpec().where(WhereClause.MATCH_ALL);
    }
    Collection<QualifiedName> orderedRelationNames = getOrderedRelationNames(mss, splitQuery.keySet());
    Iterator<QualifiedName> it = orderedRelationNames.iterator();
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("relations={} orderedRelations={}", mss.sources().keySet(), orderedRelationNames);
    }
    QualifiedName leftName = it.next();
    QuerySpec rootQuerySpec = mss.querySpec();
    RelationSource leftSource = mss.sources().get(leftName);
    AnalyzedRelation leftRelation = leftSource.relation();
    QuerySpec leftQuerySpec = leftSource.querySpec();
    Optional<RemainingOrderBy> remainingOrderBy = mss.remainingOrderBy();
    List<JoinPair> joinPairs = mss.joinPairs();
    List<TwoTableJoin> twoTableJoinList = new ArrayList<>(orderedRelationNames.size());
    QualifiedName rightName;
    RelationSource rightSource;
    while (it.hasNext()) {
        rightName = it.next();
        rightSource = mss.sources().get(rightName);
        // process where clause
        Set<QualifiedName> names = Sets.newHashSet(leftName, rightName);
        Predicate<Symbol> predicate = new SubSetOfQualifiedNamesPredicate(names);
        QuerySpec newQuerySpec = rootQuerySpec.subset(predicate, it.hasNext());
        if (splitQuery.containsKey(names)) {
            Symbol symbol = splitQuery.remove(names);
            newQuerySpec.where(new WhereClause(symbol));
        }
        Optional<OrderBy> remainingOrderByToApply = Optional.empty();
        if (remainingOrderBy.isPresent() && remainingOrderBy.get().validForRelations(names)) {
            remainingOrderByToApply = Optional.of(remainingOrderBy.get().orderBy());
            remainingOrderBy = Optional.empty();
        }
        // get explicit join definition
        JoinPair joinPair = JoinPairs.ofRelationsWithMergedConditions(leftName, rightName, joinPairs, true);
        JoinPairs.removeOrderByOnOuterRelation(leftName, rightName, leftQuerySpec, rightSource.querySpec(), joinPair);
        // NestedLoop will add NULL rows - so order by needs to be applied after the NestedLoop
        TwoTableJoin join = new TwoTableJoin(newQuerySpec, new RelationSource(leftName, leftRelation, leftQuerySpec), rightSource, remainingOrderByToApply, joinPair);
        assert leftQuerySpec != null : "leftQuerySpec must not be null";
        final RelationColumnReWriteCtx reWriteCtx = new RelationColumnReWriteCtx(join);
        Function<? super Symbol, Symbol> replaceFunction = new Function<Symbol, Symbol>() {

            @Nullable
            @Override
            public Symbol apply(@Nullable Symbol input) {
                if (input == null) {
                    return null;
                }
                return RelationColumnReWriter.INSTANCE.process(input, reWriteCtx);
            }
        };
        /**
             * Rewrite where, join and order by clauses and create a new query spec, where all RelationColumn symbols
             * with a QualifiedName of {@link RelationColumnReWriteCtx#left} or {@link RelationColumnReWriteCtx#right}
             * are replaced with a RelationColumn with QualifiedName of {@link RelationColumnReWriteCtx#newName}
             */
        splitQuery = rewriteSplitQueryNames(splitQuery, leftName, rightName, join.name(), replaceFunction);
        JoinPairs.rewriteNames(leftName, rightName, join.name(), replaceFunction, joinPairs);
        rewriteOrderByNames(remainingOrderBy, leftName, rightName, join.name(), replaceFunction);
        rootQuerySpec = rootQuerySpec.copyAndReplace(replaceFunction);
        leftQuerySpec = newQuerySpec.copyAndReplace(replaceFunction);
        leftRelation = join;
        leftName = join.name();
        twoTableJoinList.add(join);
    }
    TwoTableJoin join = (TwoTableJoin) leftRelation;
    if (!splitQuery.isEmpty()) {
        join.querySpec().where(new WhereClause(AndOperator.join(splitQuery.values())));
    }
    // Find the last join pair that contains a filtering
    int index = 0;
    for (int i = twoTableJoinList.size() - 1; i >= 0; i--) {
        index = i;
        WhereClause where = twoTableJoinList.get(i).querySpec().where();
        if (where.hasQuery() && !(where.query() instanceof Literal)) {
            break;
        }
    }
    // Remove limit from all join pairs before the last filtered one
    for (int i = 0; i < index; i++) {
        twoTableJoinList.get(i).querySpec().limit(Optional.empty());
    }
    return join;
}
Also used : Function(com.google.common.base.Function) QualifiedName(io.crate.sql.tree.QualifiedName) Nullable(javax.annotation.Nullable)

Example 67 with Function

use of com.google.common.base.Function in project canal by alibaba.

the class MixedMetaManager method start.

public void start() {
    super.start();
    Assert.notNull(zooKeeperMetaManager);
    if (!zooKeeperMetaManager.isStart()) {
        zooKeeperMetaManager.start();
    }
    executor = Executors.newFixedThreadPool(1);
    destinations = MigrateMap.makeComputingMap(new Function<String, List<ClientIdentity>>() {

        public List<ClientIdentity> apply(String destination) {
            return zooKeeperMetaManager.listAllSubscribeInfo(destination);
        }
    });
    cursors = MigrateMap.makeComputingMap(new Function<ClientIdentity, Position>() {

        public Position apply(ClientIdentity clientIdentity) {
            Position position = zooKeeperMetaManager.getCursor(clientIdentity);
            if (position == null) {
                // 返回一个空对象标识,避免出现异常
                return nullCursor;
            } else {
                return position;
            }
        }
    });
    batches = MigrateMap.makeComputingMap(new Function<ClientIdentity, MemoryClientIdentityBatch>() {

        public MemoryClientIdentityBatch apply(ClientIdentity clientIdentity) {
            // 读取一下zookeeper信息,初始化一次
            MemoryClientIdentityBatch batches = MemoryClientIdentityBatch.create(clientIdentity);
            Map<Long, PositionRange> positionRanges = zooKeeperMetaManager.listAllBatchs(clientIdentity);
            for (Map.Entry<Long, PositionRange> entry : positionRanges.entrySet()) {
                // 添加记录到指定batchId
                batches.addPositionRange(entry.getValue(), entry.getKey());
            }
            return batches;
        }
    });
}
Also used : Function(com.google.common.base.Function) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) Position(com.alibaba.otter.canal.protocol.position.Position) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange) Map(java.util.Map) MigrateMap(com.google.common.collect.MigrateMap)

Example 68 with Function

use of com.google.common.base.Function in project android by JetBrains.

the class NavigationComponent method updateText.

private void updateText() {
    if (myItemStack.isEmpty()) {
        setText("");
        return;
    }
    if (myItemStack.size() == 1 && hasRootItem && !myDisplaySingleRoot) {
        setText("");
        return;
    }
    final AtomicInteger id = new AtomicInteger(myItemStack.size() - 1);
    String text = Joiner.on(" &gt; ").join(Iterators.transform(myItemStack.descendingIterator(), new Function<T, String>() {

        @Override
        public String apply(T input) {
            // Do not display link for the last element.
            if (id.get() == 0) {
                return input.getDisplayText();
            }
            return String.format("<a href=\"%d\">%s</a>", id.getAndDecrement(), input.getDisplayText());
        }

        @Override
        public boolean equals(Object object) {
            return false;
        }
    }));
    setText(text);
}
Also used : Function(com.google.common.base.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 69 with Function

use of com.google.common.base.Function in project android by JetBrains.

the class ConfigureAvdOptionsStep method createUIComponents.

private void createUIComponents() {
    Function<ScreenOrientation, Image> orientationIconFunction = new Function<ScreenOrientation, Image>() {

        @Override
        public Image apply(ScreenOrientation input) {
            return IconUtil.toImage(ORIENTATIONS.get(input).myIcon);
        }
    };
    Function<ScreenOrientation, String> orientationNameFunction = new Function<ScreenOrientation, String>() {

        @Override
        public String apply(ScreenOrientation input) {
            return ORIENTATIONS.get(input).myName;
        }
    };
    myOrientationToggle = new ASGallery<ScreenOrientation>(JBList.createDefaultListModel(ScreenOrientation.PORTRAIT, ScreenOrientation.LANDSCAPE), orientationIconFunction, orientationNameFunction, JBUI.size(50, 50), null);
    myOrientationToggle.setCellMargin(JBUI.insets(5, 20, 4, 20));
    myOrientationToggle.setBackground(JBColor.background());
    myOrientationToggle.setForeground(JBColor.foreground());
    myHardwareSkinHelpLabel = new HyperlinkLabel("How do I create a custom hardware skin?");
    myHardwareSkinHelpLabel.setHyperlinkTarget(AvdWizardUtils.CREATE_SKIN_HELP_LINK);
    mySkinComboBox = new SkinChooser(myProject, true);
}
Also used : ScreenOrientation(com.android.resources.ScreenOrientation) Function(com.google.common.base.Function) SystemImage(com.android.sdklib.repository.targets.SystemImage) HyperlinkLabel(com.intellij.ui.HyperlinkLabel)

Example 70 with Function

use of com.google.common.base.Function in project android by JetBrains.

the class ModuleListModel method checkForDuplicateNames.

private void checkForDuplicateNames() {
    Collection<ModuleToImport> modules = getSelectedModules();
    ImmutableMultiset<String> names = ImmutableMultiset.copyOf(Iterables.transform(modules, new Function<ModuleToImport, String>() {

        @Override
        public String apply(@Nullable ModuleToImport input) {
            return input == null ? null : getModuleName(input);
        }
    }));
    for (ModuleToImport module : modules) {
        ModuleValidationState state = myModules.get(module);
        if (state == ModuleValidationState.OK) {
            if (names.count(getModuleName(module)) > 1) {
                myModules.put(module, ModuleValidationState.DUPLICATE_MODULE_NAME);
            }
        }
    }
}
Also used : Function(com.google.common.base.Function) ModuleToImport(com.android.tools.idea.gradle.project.ModuleToImport) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Function (com.google.common.base.Function)616 List (java.util.List)138 ArrayList (java.util.ArrayList)114 Nullable (javax.annotation.Nullable)103 Map (java.util.Map)97 IOException (java.io.IOException)89 HashMap (java.util.HashMap)78 Test (org.junit.Test)73 ImmutableList (com.google.common.collect.ImmutableList)49 File (java.io.File)46 Set (java.util.Set)46 Collection (java.util.Collection)35 ImmutableMap (com.google.common.collect.ImmutableMap)34 DateTime (org.joda.time.DateTime)33 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)29 HashSet (java.util.HashSet)27 Iterator (java.util.Iterator)27 LinkedList (java.util.LinkedList)26 ExecutionException (java.util.concurrent.ExecutionException)24 Collectors (java.util.stream.Collectors)15