com.hazelcast.org.apache.calcite.rel.metadata.ChainedRelMetadataProvider Maven / Gradle / Ivy
/*
* 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 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.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.common.collect.ImmutableList;
import com.hazelcast.com.google.common.collect.ImmutableMultimap;
import com.hazelcast.com.google.common.collect.Multimap;
import com.hazelcast.org.checkerframework.checker.nullness.qual.Nullable;
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.
*/
@SuppressWarnings("argument.type.incompatible")
protected ChainedRelMetadataProvider(
ImmutableList providers) {
this.providers = providers;
assert !providers.contains(this);
}
//~ Methods ----------------------------------------------------------------
@Override public boolean equals(@Nullable Object obj) {
return obj == this
|| obj instanceof ChainedRelMetadataProvider
&& providers.equals(((ChainedRelMetadataProvider) obj).providers);
}
@Override public int hashCode() {
return providers.hashCode();
}
@Deprecated // to be removed before 2.0
@Override public <@Nullable M extends @Nullable Metadata> @Nullable UnboundMetadata apply(
Class extends RelNode> relClass,
final Class extends M> 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)));
};
}
}
@Deprecated // to be removed before 2.0
@Override public Multimap> handlers(
MetadataDef def) {
final ImmutableMultimap.Builder> builder =
ImmutableMultimap.builder();
for (RelMetadataProvider provider : providers.reverse()) {
builder.putAll(provider.handlers(def));
}
return builder.build();
}
@Override public List> handlers(
Class extends MetadataHandler>> handlerClass) {
final ImmutableList.Builder> builder =
ImmutableList.builder();
for (RelMetadataProvider provider : providers) {
builder.addAll(provider.handlers(handlerClass));
}
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);
}
@Override public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args)
throws Throwable {
for (Metadata metadata : metadataList) {
try {
final Object o = method.invoke(metadata, args);
if (o != null) {
return o;
}
} catch (InvocationTargetException e) {
throw Util.throwAsRuntime(Util.causeOrSelf(e));
}
}
return null;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy