All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.org.apache.calcite.rel.metadata.ChainedRelMetadataProvider Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in com.hazelcast.com.liance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.com.hazelcast.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.hazelcast.org.apache.calcite.rel.metadata;

import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.util.Util;

import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableList;
import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableMultimap;
import com.hazelcast.com.google.com.hazelcast.com.on.collect.Multimap;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * Implementation of the {@link RelMetadataProvider}
 * interface via the
 * {@link com.hazelcast.org.apache.calcite.util.Glossary#CHAIN_OF_RESPONSIBILITY_PATTERN}.
 *
 * 

When a consumer calls the {@link #apply} method to ask for a provider * for a particular type of {@link RelNode} and {@link Metadata}, scans the list * of underlying providers.

*/ public class ChainedRelMetadataProvider implements RelMetadataProvider { //~ Instance fields -------------------------------------------------------- private final ImmutableList providers; //~ Constructors ----------------------------------------------------------- /** * Creates a chain. */ protected ChainedRelMetadataProvider( ImmutableList providers) { this.providers = providers; assert !providers.contains(this); } //~ Methods ---------------------------------------------------------------- @Override public boolean equals(Object obj) { return obj == this || obj instanceof ChainedRelMetadataProvider && providers.equals(((ChainedRelMetadataProvider) obj).providers); } @Override public int hashCode() { return providers.hashCode(); } public UnboundMetadata apply( Class relClass, final Class metadataClass) { final List> functions = new ArrayList<>(); for (RelMetadataProvider provider : providers) { final UnboundMetadata function = provider.apply(relClass, metadataClass); if (function == null) { continue; } functions.add(function); } switch (functions.size()) { case 0: return null; case 1: return functions.get(0); default: return (rel, mq) -> { final List metadataList = new ArrayList<>(); for (UnboundMetadata function : functions) { final Metadata metadata = function.bind(rel, mq); if (metadata != null) { metadataList.add(metadata); } } return metadataClass.cast( Proxy.newProxyInstance(metadataClass.getClassLoader(), new Class[]{metadataClass}, new ChainedInvocationHandler(metadataList))); }; } } public Multimap> handlers( MetadataDef def) { final ImmutableMultimap.Builder> builder = ImmutableMultimap.builder(); for (RelMetadataProvider provider : providers.reverse()) { builder.putAll(provider.handlers(def)); } return builder.build(); } /** Creates a chain. */ public static RelMetadataProvider of(List list) { return new ChainedRelMetadataProvider(ImmutableList.copyOf(list)); } /** Invocation handler that calls a list of {@link Metadata} objects, * returning the first non-null value. */ private static class ChainedInvocationHandler implements InvocationHandler { private final List metadataList; ChainedInvocationHandler(List metadataList) { this.metadataList = ImmutableList.copyOf(metadataList); } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { for (Metadata metadata : metadataList) { try { final Object o = method.invoke(metadata, args); if (o != null) { return o; } } catch (InvocationTargetException e) { if (e.getCause() instanceof CyclicMetadataException) { continue; } Util.throwIfUnchecked(e.getCause()); throw new RuntimeException(e.getCause()); } } return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy