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.metadata;
 
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.metadata.MetadataUtil.checkCatalogName;
 import static com.facebook.presto.metadata.QualifiedTableName.convertFromSchemaTableName;
 import static com.facebook.presto.metadata.TableLayout.fromConnectorLayout;
 import static com.facebook.presto.metadata.ViewDefinition.ViewColumn;
 import static com.facebook.presto.spi.StandardErrorCode.INVALID_VIEW;
 import static com.facebook.presto.spi.StandardErrorCode.NOT_FOUND;
 import static com.facebook.presto.spi.StandardErrorCode.SYNTAX_ERROR;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.Iterables.transform;
 import static java.lang.String.format;
 
 public class MetadataManager
         implements Metadata
 {
     private static final String INFORMATION_SCHEMA_NAME = "information_schema";
 
     private final ConcurrentMap<StringConnectorMetadataconnectorsById = new ConcurrentHashMap<>();
     private final FunctionRegistry functions;
     private final TypeManager typeManager;
     private final JsonCodec<ViewDefinitionviewCodec;
     private final SplitManager splitManager;
     private final BlockEncodingSerde blockEncodingSerde;
 
     public MetadataManager(FeaturesConfig featuresConfigTypeManager typeManagerSplitManager splitManagerBlockEncodingSerde blockEncodingSerde)
     {
        this(featuresConfigtypeManagercreateTestingViewCodec(), splitManagerblockEncodingSerde);
    }
    @Inject
    public MetadataManager(FeaturesConfig featuresConfigTypeManager typeManagerJsonCodec<ViewDefinitionviewCodecSplitManager splitManagerBlockEncodingSerde blockEncodingSerde)
    {
         = new FunctionRegistry(typeManagerblockEncodingSerdefeaturesConfig.isExperimentalSyntaxEnabled());
        this. = checkNotNull(typeManager"types is null");
        this. = checkNotNull(viewCodec"viewCodec is null");
        this. = checkNotNull(splitManager"splitManager is null");
        this. = checkNotNull(blockEncodingSerde"blockEncodingSerde is null");
    }
    {
        FeaturesConfig featuresConfig = new FeaturesConfig();
        TypeManager typeManager = new TypeRegistry();
        SplitManager splitManager = new SplitManager();
        BlockEncodingSerde blockEncodingSerde = new BlockEncodingManager(typeManager);
        return new MetadataManager(featuresConfigtypeManagersplitManagerblockEncodingSerde);
    }
    public synchronized void addConnectorMetadata(String connectorIdString catalogNameConnectorMetadata connectorMetadata)
    {
        checkMetadataArguments(connectorIdcatalogNameconnectorMetadata);
        checkArgument(!.containsKey(catalogName), "Catalog '%s' is already registered"catalogName);
        .put(connectorIdconnectorMetadata);
        .put(catalogNamenew ConnectorMetadataEntry(connectorIdconnectorMetadata));
    }
    public synchronized void addInformationSchemaMetadata(String connectorIdString catalogNameInformationSchemaMetadata metadata)
    {
        checkMetadataArguments(connectorIdcatalogNamemetadata);
        checkArgument(!.containsKey(catalogName), "Information schema for catalog '%s' is already registered"catalogName);
        .put(connectorIdmetadata);
        .put(catalogNamenew ConnectorMetadataEntry(connectorIdmetadata));
    }
    public synchronized void addSystemTablesMetadata(String connectorIdString catalogNameConnectorMetadata metadata)
    {
        checkMetadataArguments(connectorIdcatalogNamemetadata);
        checkArgument(!.containsKey(catalogName), "System tables for catalog '%s' are already registered"catalogName);
        .put(connectorIdmetadata);
        .put(catalogNamenew ConnectorMetadataEntry(connectorIdmetadata));
    }
    private void checkMetadataArguments(String connectorIdString catalogNameConnectorMetadata metadata)
    {
        checkNotNull(connectorId"connectorId is null");
        checkNotNull(catalogName"catalogName is null");
        checkNotNull(metadata"metadata is null");
        checkArgument(!.containsKey(connectorId), "Connector '%s' is already registered"connectorId);
    }
    @Override
    public Type getType(TypeSignature signature)
    {
        return .getType(signature);
    }
    @Override
    public FunctionInfo resolveFunction(QualifiedName nameList<TypeSignatureparameterTypesboolean approximate)
    {
        return .resolveFunction(nameparameterTypesapproximate);
    }
    @Override
    public FunctionInfo getExactFunction(Signature handle)
    {
        return .getExactFunction(handle);
    }
    @Override
    public boolean isAggregationFunction(QualifiedName name)
    {
        return .isAggregationFunction(name);
    }
    @Override
    {
        return .list();
    }
    @Override
    public void addFunctions(List<? extends ParametricFunctionfunctionInfos)
    {
        .addFunctions(functionInfos);
    }
    @Override
    public FunctionInfo resolveOperator(OperatorType operatorTypeList<? extends TypeargumentTypes)
            throws OperatorNotFoundException
    {
        return .resolveOperator(operatorTypeargumentTypes);
    }
    @Override
    public List<StringlistSchemaNames(Session sessionString catalogName)
    {
        checkCatalogName(catalogName);
        ImmutableSet.Builder<StringschemaNames = ImmutableSet.builder();
        for (ConnectorMetadataEntry entry : allConnectorsFor(catalogName)) {
            schemaNames.addAll(entry.getMetadata().listSchemaNames(session.toConnectorSession(entry.getCatalog())));
        }
        return ImmutableList.copyOf(schemaNames.build());
    }
    @Override
    public Optional<TableHandlegetTableHandle(Session sessionQualifiedTableName table)
    {
        checkNotNull(table"table is null");
        ConnectorMetadataEntry entry = getConnectorFor(table);
        if (entry != null) {
            ConnectorMetadata metadata = entry.getMetadata();
            ConnectorTableHandle tableHandle = metadata.getTableHandle(session.toConnectorSession(entry.getCatalog()), table.asSchemaTableName());
            if (tableHandle != null) {
                return Optional.of(new TableHandle(entry.getConnectorId(), tableHandle));
            }
        }
        return Optional.empty();
    }
    @Override
    public List<TableLayoutResultgetLayouts(TableHandle tableConstraint<ColumnHandleconstraintOptional<Set<ColumnHandle>> desiredColumns)
    {
        if (constraint.getSummary().isNone()) {
            return ImmutableList.of();
        }
        TupleDomain<ColumnHandlesummary = constraint.getSummary();
        String connectorId = table.getConnectorId();
        ConnectorTableHandle connectorTable = table.getConnectorHandle();
        Predicate<Map<ColumnHandle, ?>> predicate = constraint.predicate();
        List<ConnectorTableLayoutResultlayouts;
        try {
            ConnectorMetadata metadata = getConnectorMetadata(connectorId);
            layouts = metadata.getTableLayouts(connectorTablenew Constraint<>(summarypredicate::test), desiredColumns);
        }
        catch (UnsupportedOperationException e) {
            ConnectorSplitManager connectorSplitManager = .getConnectorSplitManager(connectorId);
            ConnectorPartitionResult result = connectorSplitManager.getPartitions(connectorTablesummary);
            List<ConnectorPartitionpartitions = result.getPartitions().stream()
                    .filter(partition -> predicate.test(partition.getTupleDomain().extractFixedValues()))
                    .collect(toImmutableList());
            List<TupleDomain<ColumnHandle>> partitionDomains = partitions.stream()
                    .map(ConnectorPartition::getTupleDomain)
                    .collect(toImmutableList());
            TupleDomain<ColumnHandleeffectivePredicate = TupleDomain.none();
            if (!partitionDomains.isEmpty()) {
                effectivePredicate = TupleDomain.columnWiseUnion(partitionDomains);
            }
            ConnectorTableLayout layout = new ConnectorTableLayout(new LegacyTableLayoutHandle(connectorTablepartitions), Optional.empty(), effectivePredicate, Optional.empty(), Optional.of(partitionDomains), ImmutableList.of());
            layouts = ImmutableList.of(new ConnectorTableLayoutResult(layoutresult.getUndeterminedTupleDomain()));
        }
        return layouts.stream()
                .map(entry -> new TableLayoutResult(fromConnectorLayout(connectorIdentry.getTableLayout()), entry.getUnenforcedConstraint()))
                .collect(toImmutableList());
    }
    public TableLayout getLayout(TableLayoutHandle handle)
    {
        if (handle.getConnectorHandle() instanceof LegacyTableLayoutHandle) {
            LegacyTableLayoutHandle legacyHandle = (LegacyTableLayoutHandlehandle.getConnectorHandle();
            List<TupleDomain<ColumnHandle>> partitionDomains = legacyHandle.getPartitions().stream()
                    .map(ConnectorPartition::getTupleDomain)
                    .collect(toImmutableList());
            TupleDomain<ColumnHandlepredicate = TupleDomain.none();
            if (!partitionDomains.isEmpty()) {
                predicate = TupleDomain.columnWiseUnion(partitionDomains);
            }
            return new TableLayout(handlenew ConnectorTableLayout(legacyHandle, Optional.empty(), predicate, Optional.empty(), Optional.of(partitionDomains), ImmutableList.of()));
        }
        String connectorId = handle.getConnectorId();
        ConnectorMetadata metadata = getConnectorMetadata(connectorId);
        return fromConnectorLayout(connectorIdmetadata.getTableLayout(handle.getConnectorHandle()));
    }
    @Override
    public TableMetadata getTableMetadata(TableHandle tableHandle)
    {
        ConnectorTableMetadata tableMetadata = lookupConnectorFor(tableHandle).getTableMetadata(tableHandle.getConnectorHandle());
        return new TableMetadata(tableHandle.getConnectorId(), tableMetadata);
    }
    @Override
    public Map<StringColumnHandlegetColumnHandles(TableHandle tableHandle)
    {
        return lookupConnectorFor(tableHandle).getColumnHandles(tableHandle.getConnectorHandle());
    }
    @Override
    public ColumnMetadata getColumnMetadata(TableHandle tableHandleColumnHandle columnHandle)
    {
        checkNotNull(tableHandle"tableHandle is null");
        checkNotNull(columnHandle"columnHandle is null");
        return lookupConnectorFor(tableHandle).getColumnMetadata(tableHandle.getConnectorHandle(), columnHandle);
    }
    @Override
    public List<QualifiedTableNamelistTables(Session sessionQualifiedTablePrefix prefix)
    {
        checkNotNull(prefix"prefix is null");
        String schemaNameOrNull = prefix.getSchemaName().orElse(null);
        Set<QualifiedTableNametables = new LinkedHashSet<>();
        for (ConnectorMetadataEntry entry : allConnectorsFor(prefix.getCatalogName())) {
            ConnectorSession connectorSession = session.toConnectorSession(entry.getCatalog());
            for (QualifiedTableName tableName : transform(entry.getMetadata().listTables(connectorSessionschemaNameOrNull), convertFromSchemaTableName(prefix.getCatalogName()))) {
                tables.add(tableName);
            }
        }
        return ImmutableList.copyOf(tables);
    }
    @Override
    {
        checkNotNull(tableHandle"tableHandle is null");
        ColumnHandle handle = lookupConnectorFor(tableHandle).getSampleWeightColumnHandle(tableHandle.getConnectorHandle());
        return Optional.ofNullable(handle);
    }
    @Override
    public boolean canCreateSampledTables(Session sessionString catalogName)
    {
        ConnectorMetadataEntry connectorMetadata = .get(catalogName);
        checkArgument(connectorMetadata != null"Catalog %s does not exist"catalogName);
        return connectorMetadata.getMetadata().canCreateSampledTables(session.toConnectorSession(connectorMetadata.getCatalog()));
    }
    @Override
    {
        checkNotNull(prefix"prefix is null");
        SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix();
        Map<QualifiedTableNameList<ColumnMetadata>> tableColumns = new HashMap<>();
        for (ConnectorMetadataEntry connectorMetadata : allConnectorsFor(prefix.getCatalogName())) {
            ConnectorMetadata metadata = connectorMetadata.getMetadata();
            ConnectorSession connectorSession = session.toConnectorSession(connectorMetadata.getCatalog());
            for (Entry<SchemaTableNameList<ColumnMetadata>> entry : metadata.listTableColumns(connectorSessiontablePrefix).entrySet()) {
                QualifiedTableName tableName = new QualifiedTableName(
                        prefix.getCatalogName(),
                        entry.getKey().getSchemaName(),
                        entry.getKey().getTableName());
                tableColumns.put(tableNameentry.getValue());
            }
            // if table and view names overlap, the view wins
            for (Entry<SchemaTableNameStringentry : metadata.getViews(connectorSessiontablePrefix).entrySet()) {
                QualifiedTableName tableName = new QualifiedTableName(
                        prefix.getCatalogName(),
                        entry.getKey().getSchemaName(),
                        entry.getKey().getTableName());
                int ordinalPosition = 0;
                ImmutableList.Builder<ColumnMetadatacolumns = ImmutableList.builder();
                for (ViewColumn column : deserializeView(entry.getValue()).getColumns()) {
                    columns.add(new ColumnMetadata(column.getName(), column.getType(), ordinalPositionfalse));
                    ordinalPosition++;
                }
                tableColumns.put(tableNamecolumns.build());
            }
        }
        return ImmutableMap.copyOf(tableColumns);
    }
    @Override
    public void createTable(Session sessionString catalogNameTableMetadata tableMetadata)
    {
        ConnectorMetadataEntry connectorMetadata = .get(catalogName);
        checkArgument(connectorMetadata != null"Catalog %s does not exist"catalogName);
        connectorMetadata.getMetadata().createTable(session.toConnectorSession(connectorMetadata.getCatalog()), tableMetadata.getMetadata());
    }
    @Override
    public void renameTable(TableHandle tableHandleQualifiedTableName newTableName)
    {
        String catalogName = newTableName.getCatalogName();
        ConnectorMetadataEntry target = .get(catalogName);
        if (target == null) {
            throw new PrestoException(format("Target catalog '%s' does not exist"catalogName));
        }
        if (!tableHandle.getConnectorId().equals(target.getConnectorId())) {
            throw new PrestoException("Cannot rename tables across catalogs");
        }
        lookupConnectorFor(tableHandle).renameTable(tableHandle.getConnectorHandle(), newTableName.asSchemaTableName());
    }
    @Override
    public void dropTable(TableHandle tableHandle)
    {
        lookupConnectorFor(tableHandle).dropTable(tableHandle.getConnectorHandle());
    }
    @Override
    public OutputTableHandle beginCreateTable(Session sessionString catalogNameTableMetadata tableMetadata)
    {
        ConnectorMetadataEntry connectorMetadata = .get(catalogName);
        checkArgument(connectorMetadata != null"Catalog %s does not exist"catalogName);
        ConnectorSession connectorSession = session.toConnectorSession(connectorMetadata.getCatalog());
        ConnectorOutputTableHandle handle = connectorMetadata.getMetadata().beginCreateTable(connectorSessiontableMetadata.getMetadata());
        return new OutputTableHandle(connectorMetadata.getConnectorId(), handle);
    }
    @Override
    public void commitCreateTable(OutputTableHandle tableHandleCollection<Slicefragments)
    {
        lookupConnectorFor(tableHandle).commitCreateTable(tableHandle.getConnectorHandle(), fragments);
    }
    @Override
    public void rollbackCreateTable(OutputTableHandle tableHandle)
    {
        lookupConnectorFor(tableHandle).rollbackCreateTable(tableHandle.getConnectorHandle());
    }
    @Override
    public InsertTableHandle beginInsert(Session sessionTableHandle tableHandle)
    {
        // assume connectorId and catalog are the same
        ConnectorSession connectorSession = session.toConnectorSession(tableHandle.getConnectorId());
        ConnectorInsertTableHandle handle = lookupConnectorFor(tableHandle).beginInsert(connectorSessiontableHandle.getConnectorHandle());
        return new InsertTableHandle(tableHandle.getConnectorId(), handle);
    }
    @Override
    public void commitInsert(InsertTableHandle tableHandleCollection<Slicefragments)
    {
        lookupConnectorFor(tableHandle).commitInsert(tableHandle.getConnectorHandle(), fragments);
    }
    @Override
    public void rollbackInsert(InsertTableHandle tableHandle)
    {
        lookupConnectorFor(tableHandle).rollbackInsert(tableHandle.getConnectorHandle());
    }
    @Override
    public Map<StringStringgetCatalogNames()
    {
        ImmutableMap.Builder<StringStringcatalogsMap = ImmutableMap.builder();
        for (Map.Entry<StringConnectorMetadataEntryentry : .entrySet()) {
            catalogsMap.put(entry.getKey(), entry.getValue().getConnectorId());
        }
        return catalogsMap.build();
    }
    @Override
    public List<QualifiedTableNamelistViews(Session sessionQualifiedTablePrefix prefix)
    {
        checkNotNull(prefix"prefix is null");
        String schemaNameOrNull = prefix.getSchemaName().orElse(null);
        Set<QualifiedTableNameviews = new LinkedHashSet<>();
        for (ConnectorMetadataEntry entry : allConnectorsFor(prefix.getCatalogName())) {
            ConnectorSession connectorSession = session.toConnectorSession(entry.getCatalog());
            for (QualifiedTableName tableName : transform(entry.getMetadata().listViews(connectorSessionschemaNameOrNull), convertFromSchemaTableName(prefix.getCatalogName()))) {
                views.add(tableName);
            }
        }
        return ImmutableList.copyOf(views);
    }
    @Override
    {
        checkNotNull(prefix"prefix is null");
        SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix();
        Map<QualifiedTableNameViewDefinitionviews = new LinkedHashMap<>();
        for (ConnectorMetadataEntry metadata : allConnectorsFor(prefix.getCatalogName())) {
            ConnectorSession connectorSession = session.toConnectorSession(metadata.getCatalog());
            for (Entry<SchemaTableNameStringentry : metadata.getMetadata().getViews(connectorSessiontablePrefix).entrySet()) {
                QualifiedTableName viewName = new QualifiedTableName(
                        prefix.getCatalogName(),
                        entry.getKey().getSchemaName(),
                        entry.getKey().getTableName());
                views.put(viewNamedeserializeView(entry.getValue()));
            }
        }
        return ImmutableMap.copyOf(views);
    }
    @Override
    public Optional<ViewDefinitiongetView(Session sessionQualifiedTableName viewName)
    {
        ConnectorMetadataEntry entry = getConnectorFor(viewName);
        if (entry != null) {
            SchemaTablePrefix prefix = viewName.asSchemaTableName().toSchemaTablePrefix();
            Map<SchemaTableNameStringviews = entry.getMetadata().getViews(session.toConnectorSession(entry.getCatalog()), prefix);
            String view = views.get(viewName.asSchemaTableName());
            if (view != null) {
                return Optional.of(deserializeView(view));
            }
        }
        return Optional.empty();
    }
    @Override
    public void createView(Session sessionQualifiedTableName viewNameString viewDataboolean replace)
    {
        ConnectorMetadataEntry connectorMetadata = .get(viewName.getCatalogName());
        checkArgument(connectorMetadata != null"Catalog %s does not exist"viewName.getCatalogName());
        connectorMetadata.getMetadata().createView(session.toConnectorSession(connectorMetadata.getCatalog()), viewName.asSchemaTableName(), viewDatareplace);
    }
    @Override
    public void dropView(Session sessionQualifiedTableName viewName)
    {
        ConnectorMetadataEntry connectorMetadata = .get(viewName.getCatalogName());
        checkArgument(connectorMetadata != null"Catalog %s does not exist"viewName.getCatalogName());
        connectorMetadata.getMetadata().dropView(session.toConnectorSession(connectorMetadata.getCatalog()), viewName.asSchemaTableName());
    }
    @Override
    {
        return ;
    }
    @Override
    public TypeManager getTypeManager()
    {
        return ;
    }
    @Override
    {
        return ;
    }
    private ViewDefinition deserializeView(String data)
    {
        try {
            return .fromJson(data);
        }
        catch (IllegalArgumentException e) {
            throw new PrestoException("Invalid view JSON: " + datae);
        }
    }
    private List<ConnectorMetadataEntryallConnectorsFor(String catalogName)
    {
        ImmutableList.Builder<ConnectorMetadataEntrybuilder = ImmutableList.builder();
        ConnectorMetadataEntry entry = .get(catalogName);
        if (entry != null) {
            builder.add(entry);
        }
        ConnectorMetadataEntry systemTables = .get(catalogName);
        if (systemTables != null) {
            builder.add(systemTables);
        }
        ConnectorMetadataEntry connector = .get(catalogName);
        if (connector != null) {
            builder.add(connector);
        }
        return builder.build();
    }
    {
        String catalog = name.getCatalogName();
        String schema = name.getSchemaName();
        if (schema.equals()) {
            return .get(catalog);
        }
        ConnectorMetadataEntry entry = .get(catalog);
        if ((entry != null) && (entry.getMetadata().getTableHandle(nullname.asSchemaTableName()) != null)) {
            return entry;
        }
        return .get(catalog);
    }
    private ConnectorMetadata lookupConnectorFor(TableHandle tableHandle)
    {
        return getConnectorMetadata(tableHandle.getConnectorId());
    }
    {
        return getConnectorMetadata(tableHandle.getConnectorId());
    }
    {
        return getConnectorMetadata(tableHandle.getConnectorId());
    }
    private ConnectorMetadata getConnectorMetadata(String connectorId)
    {
        ConnectorMetadata result = .get(connectorId);
        checkArgument(result != null"No connector for connector ID: %s"connectorId);
        return result;
    }
    private static class ConnectorMetadataEntry
    {
        private final String connectorId;
        private final ConnectorMetadata metadata;
        private ConnectorMetadataEntry(String connectorIdConnectorMetadata metadata)
        {
            this. = checkNotNull(connectorId"connectorId is null");
            this. = checkNotNull(metadata"metadata is null");
        }
        private String getConnectorId()
        {
            return ;
        }
        private String getCatalog()
        {
            // assume connectorId and catalog are the same
            return ;
        }
        private ConnectorMetadata getMetadata()
        {
            return ;
        }
    }
    {
        ObjectMapperProvider provider = new ObjectMapperProvider();
        provider.setJsonDeserializers(ImmutableMap.<Class<?>, JsonDeserializer<?>>of(Type.classnew TypeDeserializer(new TypeRegistry())));
        return new JsonCodecFactory(provider).jsonCodec(ViewDefinition.class);
    }
New to GrepCode? Check out our FAQ X