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

com.netflix.metacat.main.presto.metadata.HandleResolver Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/*
 * Copyright 2016 Netflix, Inc.
 *    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.
 */

/*
 * 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.netflix.metacat.main.presto.metadata;

import com.facebook.presto.metadata.LegacyTableLayoutHandle;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ConnectorHandleResolver;
import com.facebook.presto.spi.ConnectorIndexHandle;
import com.facebook.presto.spi.ConnectorInsertTableHandle;
import com.facebook.presto.spi.ConnectorOutputTableHandle;
import com.facebook.presto.spi.ConnectorSplit;
import com.facebook.presto.spi.ConnectorTableHandle;
import com.facebook.presto.spi.ConnectorTableLayoutHandle;

import javax.inject.Inject;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

public class HandleResolver
{
    private final ConcurrentMap handleIdResolvers = new ConcurrentHashMap<>();

    public HandleResolver()
    {
    }

    @Inject
    public HandleResolver(Map handleIdResolvers)
    {
        this.handleIdResolvers.putAll(handleIdResolvers);
    }

    public void addHandleResolver(String id, ConnectorHandleResolver connectorHandleResolver)
    {
        ConnectorHandleResolver existingResolver = handleIdResolvers.putIfAbsent(id, connectorHandleResolver);
        checkState(existingResolver == null, "Id %s is already assigned to resolver %s", id, existingResolver);
    }

    public String getId(ConnectorTableHandle tableHandle)
    {
        for (Entry entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(tableHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for table handle: " + tableHandle);
    }

    public String getId(ConnectorTableLayoutHandle handle)
    {
        if (handle instanceof LegacyTableLayoutHandle) {
            LegacyTableLayoutHandle legacyHandle = (LegacyTableLayoutHandle) handle;
            for (Entry entry : handleIdResolvers.entrySet()) {
                if (entry.getValue().canHandle(legacyHandle.getTable())) {
                    return entry.getKey();
                }
            }
        }
        else {
            for (Entry entry : handleIdResolvers.entrySet()) {
                if (entry.getValue().canHandle(handle)) {
                    return entry.getKey();
                }
            }
        }
        throw new IllegalArgumentException("No connector for table handle: " + handle);
    }

    public String getId(ColumnHandle columnHandle)
    {
        for (Entry entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(columnHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for column handle: " + columnHandle);
    }

    public String getId(ConnectorSplit split)
    {
        for (Entry entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(split)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for split: " + split);
    }

    public String getId(ConnectorIndexHandle indexHandle)
    {
        for (Entry entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(indexHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for index handle: " + indexHandle);
    }

    public String getId(ConnectorOutputTableHandle outputHandle)
    {
        for (Entry entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(outputHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for output table handle: " + outputHandle);
    }

    public String getId(ConnectorInsertTableHandle insertHandle)
    {
        for (Entry entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(insertHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for insert table handle: " + insertHandle);
    }

    public Class getTableHandleClass(String id)
    {
        return resolverFor(id).getTableHandleClass();
    }

    public Class getTableLayoutHandleClass(String id)
    {
        try {
            return resolverFor(id).getTableLayoutHandleClass();
        }
        catch (UnsupportedOperationException e) {
            return LegacyTableLayoutHandle.class;
        }
    }

    public Class getColumnHandleClass(String id)
    {
        return resolverFor(id).getColumnHandleClass();
    }

    public Class getSplitClass(String id)
    {
        return resolverFor(id).getSplitClass();
    }

    public Class getIndexHandleClass(String id)
    {
        return resolverFor(id).getIndexHandleClass();
    }

    public Class getOutputTableHandleClass(String id)
    {
        return resolverFor(id).getOutputTableHandleClass();
    }

    public Class getInsertTableHandleClass(String id)
    {
        return resolverFor(id).getInsertTableHandleClass();
    }

    public ConnectorHandleResolver resolverFor(String id)
    {
        ConnectorHandleResolver resolver = handleIdResolvers.get(id);
        checkArgument(resolver != null, "No handle resolver for %s", id);
        return resolver;
    }

    // *********************
    //
    // NETFLIX addition
    //
    // **********************

    public synchronized void flush(String catalogName){
        handleIdResolvers.remove(catalogName);
    }

    public synchronized void flushAll(){
        handleIdResolvers.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy