Skip to content

Commit

Permalink
Generalize AbstractPropertyManager
Browse files Browse the repository at this point in the history
Allow using different key than catalog name for storing properites
metadata. This is preparatory work for introducing table procedures,
where each table procedure, even from single catalog, may allow
different set of properties.
  • Loading branch information
losipiuk committed Oct 25, 2021
1 parent 30c25e1 commit c558ba6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 io.trino.metadata;

import io.trino.Session;
import io.trino.connector.CatalogName;
import io.trino.security.AccessControl;
import io.trino.spi.ErrorCodeSupplier;
import io.trino.spi.session.PropertyMetadata;
import io.trino.sql.tree.Expression;
import io.trino.sql.tree.NodeRef;
import io.trino.sql.tree.Parameter;

import java.util.List;
import java.util.Map;

abstract class AbstractCatalogPropertyManager
extends AbstractPropertyManager<CatalogName>
{
protected AbstractCatalogPropertyManager(String propertyType, ErrorCodeSupplier propertyError)
{
super(propertyType, propertyError);
}

public void addProperties(CatalogName catalogName, List<PropertyMetadata<?>> properties)
{
doAddProperties(catalogName, properties);
}

public void removeProperties(CatalogName catalogName)
{
doRemoveProperties(catalogName);
}

public Map<String, Object> getProperties(
CatalogName catalog,
String catalogNameForDiagnostics,
Map<String, Expression> sqlPropertyValues,
Session session,
Metadata metadata,
AccessControl accessControl,
Map<NodeRef<Parameter>, Expression> parameters,
boolean setDefaultProperties)
{
return doGetProperties(
catalog,
catalogNameForDiagnostics,
sqlPropertyValues,
session,
metadata,
accessControl,
parameters,
setDefaultProperties);
}

public Map<CatalogName, Map<String, PropertyMetadata<?>>> getAllProperties()
{
return doGetAllProperties();
}

@Override
protected String formatPropertiesKeyForMessage(String catalogName, CatalogName ignored)
{
return "Catalog '" + catalogName + "'";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import io.trino.Session;
import io.trino.connector.CatalogName;
import io.trino.security.AccessControl;
import io.trino.spi.ErrorCodeSupplier;
import io.trino.spi.TrinoException;
Expand All @@ -42,9 +41,9 @@
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

abstract class AbstractPropertyManager
abstract class AbstractPropertyManager<K>
{
private final ConcurrentMap<CatalogName, Map<String, PropertyMetadata<?>>> connectorProperties = new ConcurrentHashMap<>();
protected final ConcurrentMap<K, Map<String, PropertyMetadata<?>>> connectorProperties = new ConcurrentHashMap<>();
private final String propertyType;
private final ErrorCodeSupplier propertyError;

Expand All @@ -55,23 +54,23 @@ protected AbstractPropertyManager(String propertyType, ErrorCodeSupplier propert
this.propertyError = requireNonNull(propertyError, "propertyError is null");
}

public final void addProperties(CatalogName catalogName, List<PropertyMetadata<?>> properties)
protected final void doAddProperties(K propertiesKey, List<PropertyMetadata<?>> properties)
{
requireNonNull(catalogName, "catalogName is null");
requireNonNull(propertiesKey, "propertiesKey is null");
requireNonNull(properties, "properties is null");

Map<String, PropertyMetadata<?>> propertiesByName = Maps.uniqueIndex(properties, PropertyMetadata::getName);

checkState(connectorProperties.putIfAbsent(catalogName, propertiesByName) == null, "Properties for connector '%s' are already registered", catalogName);
checkState(connectorProperties.putIfAbsent(propertiesKey, propertiesByName) == null, "Properties for key %s are already registered", propertiesKey);
}

public final void removeProperties(CatalogName catalogName)
protected final void doRemoveProperties(K propertiesKey)
{
connectorProperties.remove(catalogName);
connectorProperties.remove(propertiesKey);
}

public final Map<String, Object> getProperties(
CatalogName catalogName,
protected final Map<String, Object> doGetProperties(
K propertiesKey,
String catalogNameForDiagnostics,
Map<String, Expression> sqlPropertyValues,
Session session,
Expand All @@ -80,9 +79,9 @@ public final Map<String, Object> getProperties(
Map<NodeRef<Parameter>, Expression> parameters,
boolean setDefaultProperties)
{
Map<String, PropertyMetadata<?>> supportedProperties = connectorProperties.get(catalogName);
Map<String, PropertyMetadata<?>> supportedProperties = connectorProperties.get(propertiesKey);
if (supportedProperties == null) {
throw new TrinoException(NOT_FOUND, "Catalog not found: " + catalogNameForDiagnostics);
throw new TrinoException(NOT_FOUND, formatPropertiesKeyForMessage(catalogNameForDiagnostics, propertiesKey) + " not found");
}

ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
Expand All @@ -94,8 +93,8 @@ public final Map<String, Object> getProperties(
if (property == null) {
throw new TrinoException(
propertyError,
format("Catalog '%s' does not support %s property '%s'",
catalogNameForDiagnostics,
format("%s does not support %s property '%s'",
formatPropertiesKeyForMessage(catalogNameForDiagnostics, propertiesKey),
propertyType,
propertyName));
}
Expand Down Expand Up @@ -150,7 +149,7 @@ public final Map<String, Object> getProperties(
return properties.build();
}

public Map<CatalogName, Map<String, PropertyMetadata<?>>> getAllProperties()
protected final Map<K, Map<String, PropertyMetadata<?>>> doGetAllProperties()
{
return ImmutableMap.copyOf(connectorProperties);
}
Expand All @@ -176,4 +175,6 @@ private Object evaluatePropertyValue(
}
return objectValue;
}

protected abstract String formatPropertiesKeyForMessage(String catalogName, K propertiesKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static io.trino.spi.StandardErrorCode.INVALID_ANALYZE_PROPERTY;

public class AnalyzePropertyManager
extends AbstractPropertyManager
extends AbstractCatalogPropertyManager
{
public AnalyzePropertyManager()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static io.trino.spi.StandardErrorCode.INVALID_COLUMN_PROPERTY;

public class ColumnPropertyManager
extends AbstractPropertyManager
extends AbstractCatalogPropertyManager
{
public ColumnPropertyManager()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static io.trino.spi.StandardErrorCode.INVALID_MATERIALIZED_VIEW_PROPERTY;

public class MaterializedViewPropertyManager
extends AbstractPropertyManager
extends AbstractCatalogPropertyManager
{
public MaterializedViewPropertyManager()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static io.trino.spi.StandardErrorCode.INVALID_SCHEMA_PROPERTY;

public class SchemaPropertyManager
extends AbstractPropertyManager
extends AbstractCatalogPropertyManager
{
public SchemaPropertyManager()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static io.trino.spi.StandardErrorCode.INVALID_TABLE_PROPERTY;

public class TablePropertyManager
extends AbstractPropertyManager
extends AbstractCatalogPropertyManager
{
public TablePropertyManager()
{
Expand Down

0 comments on commit c558ba6

Please sign in to comment.